如何在数学函数中获得真正的价值

时间:2017-04-13 04:09:37

标签: javascript java

大家好,我有这个代码在数学中生成函数但是使用这个函数结果是错误的o.O

x = 0的

  

“ - 3 * X ^ 2-16 * X + 2”

代码:

public static void main(String[] args) throws Exception {

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        engine.put("X", 0);

        Object operation = engine.eval("-3*X^2-16*X+2");
        //Object operation2 = engine.eval("(X+3)");

        System.out.println("Evaluado operacion 1: " + operation);
        //System.out.println("Evaluado operacion 2: " + operation2);

    }

结果是2,但我得到4

  

评估操作1:4

我还有其他代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gustavo_santa;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JOptionPane;

/**
 *
 * @author osmarvirux
 */
public class SerieB {
    //xi and x4
    int xi;
    int x4;
    //f(x) function variables
    String positive_negative;
    int num_one;
    int elevation_one;
    String add_subtract_one;
    int num_two;
    int elevation_two;
    String add_subtract_two;
    int num_three;
    //results
    String xi_result;
    String x4_result;

    public SerieB(int xi, int x4, String positive_negative, int num_one, int elevation_one, String add_subtract_one, int num_two, int elevation_two, String add_subtract_two, int num_three) {
        this.xi = xi;
        this.x4 = x4;
        this.positive_negative = positive_negative;
        this.num_one = num_one;
        this.elevation_one = elevation_one;
        this.add_subtract_one = add_subtract_one;
        this.num_two = num_two;
        this.elevation_two = elevation_two;
        this.add_subtract_two = add_subtract_two;
        this.num_three = num_three;
    }

    public void Procedure_xi(){
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        if (positive_negative == "-"){

           try {
            xi_result=(num_one*(Math.pow(xi, elevation_one)))+add_subtract_one+(num_two*(Math.pow(xi, elevation_two)))
                    +add_subtract_two+num_three;
            Object result = engine.eval(xi_result);
            System.out.println(xi_result+" = "+result);
            } catch(ScriptException se) {
                se.printStackTrace();
            }           
        }else{
             try {
            xi_result=((-num_one*(Math.pow(xi, elevation_one)))+add_subtract_one+(num_two*(Math.pow(xi, elevation_two)))
            +add_subtract_two+num_three);
            Object result = engine.eval(xi_result);
            System.out.println(xi_result+" = "+result);
            } catch(ScriptException se) {
                se.printStackTrace();
            }       

        }
    }
    public void Procedure_x4(){
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        if (positive_negative == "-"){

           try {
            x4_result=(num_one*(Math.pow(x4, elevation_one)))+add_subtract_one+(num_two*(Math.pow(x4, elevation_two)))
                    +add_subtract_two+num_three;
            Object result = engine.eval(x4_result);
            System.out.println(x4_result+" = "+result);
            } catch(ScriptException se) {
                se.printStackTrace();
            }           
        }else{
             try {
            x4_result=((-num_one*(Math.pow(x4, elevation_one)))+add_subtract_one+(num_two*(Math.pow(x4, elevation_two)))
            +add_subtract_two+num_three);
            Object result = engine.eval(x4_result);
            System.out.println(x4_result+" = "+result);
            } catch(ScriptException se) {
                se.printStackTrace();
            }       

        }
    }
    public static void main(String[] args){
        //-3x^2-16x+2
        SerieB obj = new SerieB(0, 1, "+", -3, 2, "-", 16, 1, "+", 2);
        obj.Procedure_xi();
        obj.Procedure_x4();
    }


}

此代码的结果为2,但我想使用

  

ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngineManager manager = new ScriptEngineManager();

因为是一个库,我认为更精确,我不想使用我的代码,因为有很多行,我不知道是否100%有效。有人可以帮帮我吗?或者给我一个推荐来解决这个数学函数?非常感谢

3 个答案:

答案 0 :(得分:1)

您获得的结果是正确的。

混淆源于这样一个事实,即您假设成为权力运算符(^)实际上是JavaScript中的bitwise XOR运算符(您使用的是JavaScript脚本)发动机)。

因此,评估0 ^ 2会产生2,同时评估Math.pow(0, 2)会产生0,因此产生差异。

要获得您期望的结果,表达式必须为:

-3*Math.pow(X,2)-16*X+2

您可以预处理表达式,以使用Math.pow()的调用替换指数运算:



let X = 0;
let expression = "-3*X^2-16*X+2"
let processed = expression.replace(/(\w+)\^(\w+)/g, 'Math.pow($1,$2)');

console.log(processed); // prints "-3*Math.pow(X,2)-16*X+2"
console.log(eval(processed)); // prints "2"




使用脚本引擎,如下所示:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("X", 0);
engine.put("expression", "-3*X^2-16*X+2");
engine.put("processed", engine.eval("expression.replace(/(\\w+)\\^(\\w+)/g, 'Math.pow($1,$2)')"));

System.out.println(engine.eval("eval(processed)")); // 2.0

或者,如果您更喜欢在Java中进行正则表达式替换:

String expression = "-3*X^2-16*X+2";
String processed = expression.replaceAll("(\\w+)\\^(\\w+)", "Math.pow($1,$2)");

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("X", 0);

System.out.println(engine.eval(processed)); // 2.0

答案 1 :(得分:0)

我认为正在发生的事情是你的计划正在评估

(-3*0)^2 - 16*0 + 2 = 0^2 +2 = 2+2 =4

因为计算机科学中的运算符^表示按位排他,或者 这基本上意味着如果位是1或0然后将它们更改为0,否则1.和2由10表示,而0是0所以2^0 = 2

尝试将^2替换为*X 或者,你可以使用Math.pow(X,n),如果你需要求幂幂n,但是为了平方,最好把它写成X*X

以下内容因编辑问题而过时,但仍然有效:

你在问题​​的第二部分写了

Object operation = engine.eval("-3*(X^2)-16*X +2");
String processed = operation.replace(/(\w+)\^(\w+)/g, 'Math.pow($1,$2)');

基于其他用户的回答。你应该写的

String expr = "-3*(X^2)-16*X +2";
String processed = expr.replaceAll("(\\w+)(\\^)(\\w+)", 'Math.pow($1,$2'));
Object operation = engine.eval(processed);

答案 2 :(得分:0)

您的电源语法不正确。将-3*X^2-16*X+2替换为-3*Math.pow(X,2)-16*X+2。见Javascript, What does the ^ (caret) operator do?