乘法错误 [英] multiplication error

查看:124
本文介绍了乘法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我乘12×25.4我得到304.7,而我期望304.8。我看到使用单数x 25.4我得到正确的答案,但使用偶数似乎总是由0.1处于关机状态。我写这主要是基于公式,使任何见解将是有益的科学应用。

 如果(((m1_sqs1_spinner.getSelectedItem()。的toString()。等于(IN))))    {//开始平方英寸
         双m1_sqs1_eng =新的双(m1_sqs1.getText()的toString());         双square_effective_dia_inch = m1_sqs1_eng;         双square_effective_dia_mm = square_effective_dia_inch * 25.4;
         m1_ed_mm.setText(Double.toString(square_effective_dia_mm));    } //在结束英寸正方形


解决方案

在Java的浮点数类型为近似值。试着增加1 + 2 + 3 + 4为浮动键入,你可能最终与10.00000003。

要确保精确的数学,请尝试使用的java.math.BigDecimal 键入。这是一个内存猪,但准确的。

If I multiply 12 x 25.4 i get 304.7 whereas I expect 304.8. I see that using odd numbers x 25.4 I get the correct answers but using even numbers always seem to be off by 0.1. I am writing a scientific app that is heavily based on formulas so any insight would be helpful.

if (((m1_sqs1_spinner.getSelectedItem().toString().equals("in"))))  

    { // start square in inches
         double m1_sqs1_eng = new Double(m1_sqs1.getText().toString()); 

         double square_effective_dia_inch = m1_sqs1_eng;

         double square_effective_dia_mm = square_effective_dia_inch * 25.4;
         m1_ed_mm.setText(Double.toString(square_effective_dia_mm));

    } // end square in inches

解决方案

Floating point number types in Java are approximations. Try adding 1 + 2 + 3 + 4 as float type and you might end up with 10.00000003.

To ensure accurate math, try using the java.math.BigDecimal type. It is a memory hog, but is accurate.

这篇关于乘法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆