I\'m interested if Java is using IEEE 754 standard for implementing its floating point arithmetic. Here I saw this kind of thing in documentation:
op
I'm interested if Java is using IEEE 754 standard for implementing it's floating point arithmetic.
IEEE-754 defines standards for multiple floating-point types. For many years, they were all binary floating point; that's what Java's float
and double
are: float
is a 32-bit IEEE-754 binary floating point value (what the standard calls binary32). double
is a 64-bit one (what the standard calls binary64). These binary floating point numbers are very efficient for computers to calculate, but because they work in binary and we work in decimal, there are some expectation mismatches; for instance, 0.1
cannot be stored precisely in a double
, and you get oddities like 0.1 + 0.2
turning out to be 0.30000000000000004
. See Is floating point math broken? for details. They're not a good choice for financial calculations, for instance.
BigDecimal
is a Java class that implements decimal fractional numbers with arbitrary precision. It's much slower than using double
, but the results fit with our decimal-minded expectations (for instance, that 0.1 + 0.2
will be 0.3
).
The 2008 edition of IEEE-754 adds significant new formats, in particular decimal32, decimal64, and decimal128. These are decimal floating point, and so they work the same way that we do. 0.1
can be accurately stored in a decimal64
. 0.1 + 0.2
is 0.3
in decimal64
. However, as far as I can tell, they aren't really relevant to your question.
Since BigDecimal
predates IEEE-754 2008 (by some margin), it defines its own semantics.
And if not than what's the point of using IEEE 754 standard in Math class?
JDK9 adds new operations to Math
that do things defined by the IEEE-754 2008 spec (such as fma, which does a fused multiply-add), and so it defines those operations with reference to the IEEE-754 2008 spec, for clarity.
More reading: