【发布时间】:2014-08-19 12:21:39
【问题描述】:
我正在开发一个可以计算三角函数的安卓计算器应用程序。我发现我的计算器显示:
Tan 90° = 1.633123935319537E16 instead of Tan 90° = undefined
Tan 270° = 5.443746451065124E15 instead of Tan 270° = undefined
我知道这些值实际上是指undefined,但这些计算可能会让用户有些困惑。我正在使用 Eclipse 来制作这个应用程序。
我的问题是“我如何让我的计算器显示消息 undefined 对于 Tan 90°、270° 和其他 Tan 为无穷大的值?”。
这是我的 Tan 函数代码:
ImageButton btnTan;
TextView txtDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTan = (ImageButton) findViewById(R.id.btnTan);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
btnTan.setOnClickListener(this);
}
Double total = 0.0;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btnTan) {
if (txtDisplay.getText().equals("")) {
txtDisplay.setText("");
}
else {
double total = 0.0;
total = Math.round(Math.tan(Math.toRadians(Double.parseDouble(txtDisplay.getText().toString()))));
txtDisplay.setText("");
txtDisplay.setText(txtDisplay.getText().toString() + total);
}
}
【问题讨论】:
-
使用
if-statement。 -
在计算前检查 90 或 270。如果是 90 或 270,则不会执行计算。
-
您会看到舍入误差的影响。 90 度,当转换为弧度时,不再是 90 度。
-
那些“其他值”是什么?你能描述一下吗?
标签: java android calculator trigonometry