Search code examples
javafloating-pointdoublefloating-accuracy

double values closed under inversion


For which input does the following function reduce to the identity (modulo 1 ulp)?

public static double invertTwice(double value) {
  double inverse = 1.0 / value;
  return 1.0 / inverse;
}

Also, what is the largest interval [min, max] for which the function is the identity (modulo 1 ulp)?

How come values in the range [1E-320...1E-310] are mapped to +Infinity when inverted? (A subsequent inversion of which will result in 0.0!)

Motivation: the general relation

a / b == a * (1.0 / b)

does not hold for all double pairs, even when b != 0.0, for instance a = 0.0 and b = 4.9E-324.

This has to be taken into account when normalizing vectors, for instance

[0.0 b 0.0] * (1.0 / b) == [NaN Infinity NaN]

but

[0.0/b b/b 0.0/b] == [0.0 1.0 0.0]

Remark: My question is not about normalizing vectors, but about what I wrote above. Thank you!


Solution

  • using bisection search, I found the interval of double values that is invariant under 2x inversion

    value == 1.0 / (1.0 / value)
    

    to be

    [5.562684646268010E-309 ... 1.7976931348623151E308]