0

如何避免使用 Colt 矩阵库执行的财务计算中的浮点错误?

4

3 回答 3

3

Some essential reading about the nastiness of floating point types: What Every Computer Scientist Should Know About Floating Point Arithmetic

And something more Java flavoured: How Java’s Floating-Point Hurts Everyone Everywhere

于 2009-02-13T14:31:57.813 回答
0

一种常见的技术是使用整数并自己跟踪小数。例如,您可以决定您所有的货币金额都将以小数点后 3 位的精度表示。所以不要写:

double dollars = 10.05d;

你写

int dollars = 10050;

当您要打印金额时:

System.out.println( dollars/100d );
于 2009-02-13T15:37:32.867 回答
0

对于财务来说,使用 BigDecimal 和相关类更为常见。float 和 doubles 在任何情况下都非常容易出现舍入错误。

如果您决定尝试 BigDecimal,您可能需要查看Apache Commons Math 。

于 2009-02-13T17:49:56.793 回答