【问题标题】:How do I get Tan function to show undefined?如何让 Tan 函数显示未定义?
【发布时间】: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


【解决方案1】:

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

TextView txtDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtDisplay = (TextView) findViewById(R.id.txtDisplay);
    findViewById(R.id.btnTan).setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnTan) {
        double value = Double.parseDouble(txtDisplay.getText().toString());
        if (value % 180 != 0 && value % 90 == 0) 
            txtDisplay.setText("undefined");
        else {
            value = Math.round(Math.tan(Math.toRadians(value)));
            txtDisplay.setText(String.valueOf(value));
        }
    }
}

【讨论】:

  • @VipanchithReddy 再试一次,用上面的代码你可以少做错,虽然一开始很难做错。哦,好吧。
  • @VipanchithReddy 使用上面的代码,但是total 的目的是什么?我认为您不必使用该值。
  • @VipanchithReddy 我的方法完全相同,但更具可读性。您不必初始化总计。您不必将 textview 设置为空。您将新文本设置为值加上一个空字符串。一点都不需要。使用上面的代码,它更容易理解和阅读。此外,总计不是实际总计,因此变量名称具有误导性。同样,使用上面的代码,它的工作方式与您的完全相同,但开销更少,因此更容易理解。
【解决方案2】:

非常感谢。

通过改变来修改代码

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

【讨论】:

    【解决方案3】:

    只需检查该值是 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);
    }
    

    【讨论】:

    • 那么 -90 和 -270 以及 tan 无限的其他无数个值呢?
    • @Henry 我不是数学家,我只是在不了解背景的情况下提出一个技术解决方案。这个问题在我看来是不完整的。这个答案符合问题!
    • 在 if 子句中,使用这个:(i % 180 != 0 && i % 90 == 0)(其中ivalue)。也适用于底片。
    • 您的代码实际上是在检查角度是 90 弧度还是 270 弧度;这根本无法解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多