Search code examples
javafloating-pointfloating-accuracynumerical-analysisstrictfp

Unit of least precision without strictfp


Looking at Math class, the ulp method:

public static float ulp(float f)

If the method is applied like: Math.ulp(Float.MAX_VALUE), then this rule from the Javadoc documentation comes up:

If the argument is ±Float.MAX_VALUE, then the result is equal to 2104.

Can it be ensured from machine to machine that this number (2104) will always be that one even if the float is not strictfp?


Solution

  • Lack of strictfp doesn't magically make your numbers do weird things.

    It just allows platforms with more precision to execute calculations with more precision than the result actually holds (i.e. if you do calculations on float then non-strictfp code might actually use a 40-bit floating point register for intermediary steps).

    This doesn't change the fact that your inputs and outputs that you see in your code will still always be float.

    In other words: strictfp is not a property of a float variable or number. It's a property of code that operates on float or double.

    In short: yes, if the spec says that this number will be returned, then it will be returned.