Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获得Cos和Tan函数的用户友好值?_Java_Android_Calculator_Trigonometry - Fatal编程技术网

Java 如何获得Cos和Tan函数的用户友好值?

Java 如何获得Cos和Tan函数的用户友好值?,java,android,calculator,trigonometry,Java,Android,Calculator,Trigonometry,我正在开发一个android计算器应用程序,它可以计算三角函数。我发现我的计算器显示: Cos 90° = 6.123233995736766E-17 instead of Cos 90° = 0 Cos 270° = -1.8369701987210297E-16 instead of Cos 270° = 0 Tan 180° = -1.2246467991473532E-16 instead of Tan 180° = 0 Tan 360° = -2.4492935982947064E

我正在开发一个android计算器应用程序,它可以计算三角函数。我发现我的计算器显示:

Cos 90° = 6.123233995736766E-17 instead of  Cos 90° = 0
Cos 270° = -1.8369701987210297E-16 instead of Cos 270° = 0

Tan 180° = -1.2246467991473532E-16 instead of Tan 180° = 0
Tan 360° = -2.4492935982947064E-16 instead of Tan 360° = 0
我知道这些值实际上意味着
0
,但这些计算可能会让用户感到有点困惑。我还测试了0°、15°、18°、22.5°、30°、36°、45°、60°和72°的Cos和Tan值。他们似乎工作得很好。我正在使用Eclipse制作这个应用程序

我的问题是“如何让我的计算器显示Cos和Tan函数的用户友好值?”

这是我的Cos和Tan函数代码:

ImageButton btnCos;
ImageButton btnTan;
TextView txtDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnCos = (ImageButton) findViewById(R.id.btnCos);
    btnTan = (ImageButton) findViewById(R.id.btnTan);
    txtDisplay = (TextView) findViewById(R.id.txtDisplay);

    btnCos.setOnClickListener(this);
    btnTan.setOnClickListener(this);

}

Double total = 0.0;

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

if (v.getId() == R.id.btnCos) {
    if (txtDisplay.getText().equals("")) {
        txtDisplay.setText("");
    }
    else {
        total = Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
        txtDisplay.setText("");
        txtDisplay.setText(txtDisplay.getText().toString() + total);
    }
}
else if (v.getId() == R.id.btnTan) {
    if (txtDisplay.getText().equals("")) {
        txtDisplay.setText("");
    }
    else {
        total = Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
        txtDisplay.setText("");
        txtDisplay.setText(txtDisplay.getText().toString() + total);
    }
}

Java数学触发器函数使用弧度。有一个效用函数,(你可能需要-,所以我相信你想要这样的东西

System.out.println(Math.round(Math.cos(Math.toRadians(90))));
输出为

0

Java Math trig函数使用弧度。有一个实用函数,(你可能需要-所以我相信你想要这样的函数

System.out.println(Math.round(Math.cos(Math.toRadians(90))));
输出为

0
cos(90°)不是~6.123,而是~6.123E-17。这是一个非常小的数字

这是由于浮点舍入。SO和其他站点上有很多资源;我建议首先使用floating-point-gui.de。基本上,您不能将90°精确表示为有限精度浮点数,因此您实际上并没有要求cos(90°).你问的是一个非常接近90°的数字的cos,因此答案非常接近于0

类似的推理也适用于其他结果。例如,tan(90°)是~1.633E16——一个非常大的数字——因为你要的是一个非常接近90°的数字的tan,但不完全是这个数字。

cos(90°)不是~6.123,而是~6.123E-17。这是一个非常小的数字

这是由于浮点舍入。SO和其他站点上有很多资源;我建议首先使用floating-point-gui.de。基本上,您不能将90°精确表示为有限精度浮点数,因此您实际上并没有要求cos(90°).你问的是一个非常接近90°的数字的cos,因此答案非常接近于0


类似的推理也适用于其他结果。例如,tan(90°)是~1.633E16——一个非常大的数字——因为你要的是一个非常接近90°的数字的tan,但并不完全如此。你可能在小数点后看得不够远:6.123233995736766E-17=6.10-17,几乎为零

由于浮点运算有这些错误,通常情况下只能通过格式化输出来隐藏它们

String.format("%0.3f", x);

另一方面,有些地区使用十进制逗号(事实上这是ISO标准)。因此,您可能更喜欢使用DecimalFormat来解析和格式化数字。

您可能在小数点后看得不够远:6.123233995736766E-17=6.10-17,几乎为零

由于浮点运算有这些错误,通常情况下只能通过格式化输出来隐藏它们

String.format("%0.3f", x);
另外,有些地区使用十进制逗号(事实上这是ISO标准)。因此您可能更喜欢使用十进制格式来解析和格式化数字。

非常感谢

通过更改

if (txtDisplay.getText().equals("")) {
    txtDisplay.setText("");
}
else {
    total = Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
    txtDisplay.setText("");
    txtDisplay.setText(txtDisplay.getText().toString() + total);
}

改变

if (txtDisplay.getText().equals("")) {
    txtDisplay.setText("");
}
else {
    total = Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
    txtDisplay.setText("");
    txtDisplay.setText(txtDisplay.getText().toString() + total);
}

解决了我的问题

因此,我的新代码是:

ImageButton btnCos;
ImageButton btnTan;
TextView txtDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnCos = (ImageButton) findViewById(R.id.btnCos);
    btnTan = (ImageButton) findViewById(R.id.btnTan);
    txtDisplay = (TextView) findViewById(R.id.txtDisplay);

    btnCos.setOnClickListener(this);
    btnTan.setOnClickListener(this);

}

Double total = 0.0;

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if (v.getId() == R.id.btnCos) {
        double value = Double.parseDouble(txtDisplay.getText().toString());
        if (value % 90 == 0) {
            double total = 0.0;
            total = Math.round(Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString()))));
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
        else if (value % 60 == 0) {
            total = Math.floor(Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())))) + 0.5;
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
        else {
            total = Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
    }
    else (v.getId() == R.id.btnTan) {
        double value = Double.parseDouble(txtDisplay.getText().toString());
        if (value % 180 != 0 && value % 90 == 0) {
            txtDisplay.setText("undefined");
        }
        else if (value % 45 == 0) {
            double total = 0.0;
            total = Math.round(Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString()))));
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
        else {
            total = Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
    }
多谢各位

通过更改

if (txtDisplay.getText().equals("")) {
    txtDisplay.setText("");
}
else {
    total = Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
    txtDisplay.setText("");
    txtDisplay.setText(txtDisplay.getText().toString() + total);
}

改变

if (txtDisplay.getText().equals("")) {
    txtDisplay.setText("");
}
else {
    total = Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
    txtDisplay.setText("");
    txtDisplay.setText(txtDisplay.getText().toString() + total);
}

解决了我的问题

因此,我的新代码是:

ImageButton btnCos;
ImageButton btnTan;
TextView txtDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnCos = (ImageButton) findViewById(R.id.btnCos);
    btnTan = (ImageButton) findViewById(R.id.btnTan);
    txtDisplay = (TextView) findViewById(R.id.txtDisplay);

    btnCos.setOnClickListener(this);
    btnTan.setOnClickListener(this);

}

Double total = 0.0;

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if (v.getId() == R.id.btnCos) {
        double value = Double.parseDouble(txtDisplay.getText().toString());
        if (value % 90 == 0) {
            double total = 0.0;
            total = Math.round(Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString()))));
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
        else if (value % 60 == 0) {
            total = Math.floor(Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())))) + 0.5;
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
        else {
            total = Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
    }
    else (v.getId() == R.id.btnTan) {
        double value = Double.parseDouble(txtDisplay.getText().toString());
        if (value % 180 != 0 && value % 90 == 0) {
            txtDisplay.setText("undefined");
        }
        else if (value % 45 == 0) {
            double total = 0.0;
            total = Math.round(Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString()))));
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
        else {
            total = Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
            txtDisplay.setText("");
            txtDisplay.setText(txtDisplay.getText().toString() + total);
        }
    }

不过,OP已经使用了这种方法。他们的问题不是通过了学位,而是他们没有注意到结果上的E-17。@VipanchithReddy我建议你添加
Math.round()
@VipanchithReddy
total=Math.round(Math.cos(Math.toRadians(Double.parseDouble)(txtDisplay.getText().toString()))
total=Math.round(Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
不过OP已经使用了这种方法。他们的问题不是他们通过了学位,而是他们没有注意到结果中的E-17。@vipanchhreddy我建议你添加
Math.round()
@VipanchithReddy
total=Math.round(Math.cos(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));
total=Math.round(Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString())));