0
votes

The language is Java and the machine is a normal PC with an Intel processor. The type is double.

Floating point numbers can't represent exactly every fractional number, the result of the operations like division or subtraction are somehow rounded to some near floating point representation.

I am asking whether the used rounding strategy is:

  1. rounding away from zero
  2. rounding towards zero
  3. rounding towards negative infinity
  4. rounding towards positive infinity
  5. rounding to nearest (half up)
  6. rounding to nearest (half down)
  7. rounding to nearest (half even)
1
Often, this is configurable. Not all platforms offer all of the above, AFAIK, but certainly more than one strategy. Default for many platforms is probably 7, half even or 5, half up.Rudy Velthuis
I'm guessing the IEEE standard (which most platforms use) specifies, this, although I've not looked: en.wikipedia.org/wiki/IEEE_floating_point#Expression_evaluationThe Archetypal Paul
The IEEE standard does indeed specify such matters BUT the Java VM specification specifies subtly different floating-point arithmetic. You could start reading at docs.oracle.com/javase/specs/jvms/se8/html/index.htmlHigh Performance Mark
For Java it is basically #7, round to nearest with ties round to even.Patricia Shanahan

1 Answers

3
votes

Java Virtual Machine Floating-Point Arithmetic and IEEE 754

...

  • The rounding operations of the Java Virtual Machine always use IEEE 754 round to nearest mode. Inexact results are rounded to the nearest representable value, with ties going to the value with a zero least-significant bit. This is the IEEE 754 default mode. But Java Virtual Machine instructions that convert values of floating-point types to values of integral types round toward zero. The Java Virtual Machine does not give any means to change the floating-point rounding mode.

Thus, as Patricia Shanahan already answered in the comments, JVM's rounding mode is fixed to #7 rounding to nearest (half even) from your list. However this only applies to float-to-float rounding. Float-to-integer rounding uses #2 rounding towards zero