Java 数字/数学抽象
我目前正在编写一个应用程序,其中包含大量数学计算。在某些情况下,这些计算需要快速完成,我们可以处理少量的精度损失,以尽快完成数学计算。另一方面,有时我们要求计算非常精确(并且中间有一些用例,我们将实现我们自己的乘法/除法/加法/减法/幂等方法,这比浮点数更精确* float (或 double * double,我知道 float 是一个糟糕的选择),但比 BigDecimal.multiply(BigDecimal) ... 甚至像 apfloat 而不是 BigDecimal 之类的东西更快。
是否有任何现有的库允许抽象此类数字格式,以便抽象出不同的数学方法,还是我必须创建自己的库?
我已经开始编写一些代码,似乎工作正常,但我认为拥有一个经过良好测试的库比重新发明轮子要好得多。
编辑以澄清以下评论中的内容:
问题是 BigDecimal 明显慢于 double * double 等。我们需要能够在精度和速度之间切换。其背后的原因是,我们需要能够运行快速测试来调试并与现实世界数据进行交叉检查(不需要完全准确),但最终的模拟(这通常需要几天到几周的时间:对于调试)将需要高精度。因此需要能够随意切换。
I'm currently in the process of writing an application that has quite a number of mathematically calculations in it. In some situations, these calculations need to be done quickly and we can deal with a small amount of precision loss to get the maths done as fast as possible. On the other hand, sometimes we require that the calculations be done very precisely (and there are use cases for in between, where we would implement our own way of doing multiplication/division/addition/subtraction/power etc that is more precise that float * float (or double * double, I am aware that float is a bad choice), but faster than BigDecimal.multiply(BigDecimal)... or even something like apfloat rather than BigDecimal).
Are there any existing libraries that allow an abstraction of such number formats, so that different ways of doing the maths are abstracted away, or do I have to create my own?
I've started coding up something, which seems to be working okay, but it would be far better to have a nicely tested library rather than reinventing the wheel I think.
EDITED to clarify the situation with content from a comment below:
The problem is that BigDecimal is significantly slower than double * double etc. We need to be able to switch between precision and speed. The reasoning behind this is that we need to be able to run fast tests for debugging and cross checks with real world data (which does not need to be perfectly accurate), but the final simulation (which will often take days to weeks: unacceptable for debugging) will require high precision. Thus the need to be able to switch at will.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
double
的精度是float
的两倍以上,并且通常慢不超过 10%。恕我直言,很少有充分的理由使用float
,使用double
或BigDecimal
通常更好。double
has more than twice the precision asfloat
and is typically no more than 10% slower. IMHO, there is a very rarely a good reason to usefloat
, usingdouble
orBigDecimal
is usually better.