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:
operation defined in IEEE 754-2008
As I understand positive side of IEEE 754 is to increase precision of floating point arithmetics so if I'll use double
or float
in Java would presision of computations be same as in BigDecimal
? And if not than what's the point of using IEEE 754 standard in Math
class?
Java uses a subset of the IEEE 754 binary floating point standard to represent floating point numbers and define the results of arithmetic operations. Virtually all modern computers conform to this standard. A float is represented using 32 bits, and each possible combination of bits represents one real number.
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE).
The IEEE 754 standard specifies two precisions for floating-point numbers. Single precision numbers have 32 bits − 1 for the sign, 8 for the exponent, and 23 for the significand. The significand also includes an implied 1 to the left of its radix point.
IEEE 754 numbers are divided into two based on the above three components: single precision and double precision.
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:
BigDecimal
JavaDocIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With