如何检查四舍五入的错误?

时间:2021-11-12 15:05:48

This may be a dumb question to ask: While using float there is some rounding off done for decimal places, how to check for rounding off errors preferably in java or C? Any suggestions will be greatly appreciated.

这可能是一个愚蠢的问题:使用float时,对于小数位有一些舍入,如何检查舍入错误,最好是在java或C?任何建议将不胜感激。

1 个解决方案

#1


0  

Well, you could do the operation in something with NO rounding, and compare the results. For example, in java you could use Big Decimal to do an operation after you did it with float... and see if you end up with the same number.

好吧,你可以在没有舍入的情况下进行操作,并比较结果。例如,在java中,您可以使用Big Decimal在使用float执行操作后执行操作...并查看是否最终使用相同的数字。

A better question, is why do you want to do this? Float will often do weird rounds, you don't want to use it anywhere you need precision (i.e. with a banking transaction). In that case, use Big Decimal. But if you just want to represent a 3D point on the screen, 1.5 rounding to 1.499997 won't change anything...

一个更好的问题,你为什么要这样做? Float通常会做奇怪的回合,你不想在任何需要精确度的地方使用它(即通过银行交易)。在这种情况下,请使用Big Decimal。但如果您只想在屏幕上表示3D点,1.5舍入到1.499997将不会改变任何东西......

#1


0  

Well, you could do the operation in something with NO rounding, and compare the results. For example, in java you could use Big Decimal to do an operation after you did it with float... and see if you end up with the same number.

好吧,你可以在没有舍入的情况下进行操作,并比较结果。例如,在java中,您可以使用Big Decimal在使用float执行操作后执行操作...并查看是否最终使用相同的数字。

A better question, is why do you want to do this? Float will often do weird rounds, you don't want to use it anywhere you need precision (i.e. with a banking transaction). In that case, use Big Decimal. But if you just want to represent a 3D point on the screen, 1.5 rounding to 1.499997 won't change anything...

一个更好的问题,你为什么要这样做? Float通常会做奇怪的回合,你不想在任何需要精确度的地方使用它(即通过银行交易)。在这种情况下,请使用Big Decimal。但如果您只想在屏幕上表示3D点,1.5舍入到1.499997将不会改变任何东西......