Java双打不擅长数学 [英] Java Doubles are not good at math
问题描述
我目前正在用java编写一个计算器程序。这是我的第一个java程序,我习惯了c ++。
I am currently writing a calculator program in java. It is my first java program, I am used to c++.
我注意到java中的双打不是在c ++中双打。
在java和c ++中试试
4.1 * 3
/ .1
I have noticed that doubles in java are not at all like doubles in c++.
try this in java and c++
4.1*3
that/.1
br>
12.3 then 123,而c ++给出这个结果,但java给出了
12.299999999999999和122.99999999999999
it should be
12.3 then 123, and c++ gives this result but java gives
12.299999999999999 and 122.99999999999999
我如何做数学c ++与double,我明白任何你会使用12.299999999999999在程序中所有将没有什么区别,相比12.3,但当一个用户读的数字,这是非常丑陋。我已经研究了BigDecimal类,但是我不能做对数和对数的类
How can I do math like in c++ with doubles, I understand that anything you would use 12.299999999999999 in a program at all would make no difference compared to 12.3, but when a user is reading the numbers this is very ugly. I have looked into the BigDecimal class but I cannot do trig and logarithms and whatnot with that class
推荐答案
数学是一样的;显示是不同的,因为大多数C / C ++显示格式舍入数字,以避免最不重要的位的不精确。
The math is the same; the display is different, as most C/C++ display formats round numbers to avoid the imprecision in the least significant bits.
这篇关于Java双打不擅长数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!