Java 如何使Tan函数显示未定义的?

Java 如何使Tan函数显示未定义的?,java,android,calculator,trigonometry,Java,Android,Calculator,Trigonometry,我正在开发一个android计算器应用程序,它可以计算三角函数。我发现我的计算器显示: Tan 90° = 1.633123935319537E16 instead of Tan 90° = undefined Tan 270° = 5.443746451065124E15 instead of Tan 270° = undefined 我知道这些值实际上意味着未定义,但这些计算可能会让用户感到有点困惑。我正在使用Eclipse制作这个应用程序 我的问题是如何让我的计算器显示Tan为90°、2

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

Tan 90° = 1.633123935319537E16 instead of Tan 90° = undefined
Tan 270° = 5.443746451065124E15 instead of Tan 270° = undefined
我知道这些值实际上意味着未定义,但这些计算可能会让用户感到有点困惑。我正在使用Eclipse制作这个应用程序

我的问题是如何让我的计算器显示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);
    }
}

只需检查该值是90°还是270°:

double value = Math.toRadians(Double.parseDouble(txtDisplay.getText().toString()));
if (value == Math.abs(90) || value == Math.abs(270)) {
   txtDisplay.setText("undefined");
} else {
   total = Math.round(Math.tan(value));
   txtDisplay.setText(txtDisplay.getText().toString() + total);
}

只需检查度值是否为270、90或切线为NaN的任何其他值

多谢各位

通过更改

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);
}

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);
}
解决了我的问题

因此,我的新代码是:

    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) {
            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-statement。在计算前检查90或270。如果为90或270,则不会执行计算。您可以看到舍入误差的影响。当转换成弧度时,90度不再是90度。其他值是什么?你能描述一下吗?关于-90和-270,以及tan为无穷大的无穷多的其他值呢?@Henry我不是数学家,我只是在不知道背景的情况下提出了一个技术解决方案。这个问题在我看来是不完整的。这个答案符合这个问题!在if子句中,使用以下命令:i%180!=0&&i%90==0,其中i是值。也适用于负片。你的代码实际上是在检查角度是90弧度还是270弧度;这对解决问题毫无帮助。@VipanchithReddy请再试一次,使用上面的代码,您可以少犯错误,尽管一开始很难出错。哦。@VipanchithReddy使用上面的代码,但是total的目的是什么?我认为,您不必使用该值。@VipanchithReddy我的方法与此完全相同,但更具可读性。您可以在不必初始化的情况下初始化total。您可以将textview设置为空,而不必这样做。将新文本设置为值加上空字符串。没有必要。使用上面的代码,它更容易理解和阅读。此外,total不是实际的total,因此变量名具有误导性。同样,使用上面的代码,它的工作原理与您的完全相同,但开销较小,因此更容易理解。
    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) {
            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);
            }
        }