Java Number / Maths Abstraction

I am currently writing an application in which there are many mathematical calculations. In some situations, these calculations need to be done quickly, and we can cope with a small amount of accuracy loss to get the math done as quickly as possible. On the other hand, sometimes we require that the calculations be performed very accurately (and there are use cases between them, where we will implement our own way of doing multiplication / division / addition / subtraction / power, etc., more precisely, that float * float (or double * double, I know that float is a bad choice), but faster than BigDecimal.multiply (BigDecimal) ... or even something like apfloat, not BigDecimal).

Are there any existing libraries that allow us to abstract from such number formats, so that different ways of doing math are abstracted, or do I need to create my own?

I started coding something that seems to work fine, but it would be much better to have a well-tested library, rather than reinventing the wheel that I think.

EDITED to clarify the situation with the content from the comment below:

The problem is that BigDecimal is much slower than double * double, etc. We need to be able to switch between accuracy and speed. The reason for this is that we need to be able to run quick tests for debugging and cross-validation with real-world data (which does not have to be absolutely accurate), but the final simulation (which often takes from several days to several weeks: is not acceptable for debugging) will require high accuracy. Thus, it is necessary to be able to switch as desired.

+4
source share
1 answer

double more than twice the precision of the float and, as a rule, no more than 10% slower. IMHO, there is very rarely a reason to use float , using double or BigDecimal usually better.

+5
source

Source: https://habr.com/ru/post/1380724/


All Articles