Search code examples
floating-pointieee-754

When is x(1/x) = 1 in floating point arithmetic?


I know that in floating point arithmetic, x(1/x) might not be exactly 1 due to rounding error. The smallest positive integer for which this happens should be x=49.

I thought x(1/x) would be exactly 1 if 1/x can be expressed exactly in floating point arithmetic, but my professor said it is actually more nuanced than that and pointed out 5 as an example. I just cannot figure out what other reasons could there be. Can someone give me a hint? Thanks in advance!


Solution

  • I thought x(1/x) would be exactly 1 if 1/x can be expressed exactly in floating point arithmetic,

    True, when "1/x can be expressed exactly" as the mathematical equivalent.

    but my professor said it is actually more nuanced than that and pointed out 5 as an example.

    5 does not meet the criteria of "if 1/x can be expressed exactly" as 1.0/5.0 is not exactly 0.2 with common double. Try:

    printf("%.*g\n", DBL_DECIMAL_DIG, 1.0/5.0);
    

    might not be exactly 1 due to rounding error.

    True. There also exist range concerns as 1.0/DBL_TRUE_MIN is usually infinity.
    0.0, Infinity, NAN are the other usually suspects.

    ... what other reasons ...

    "There is a rounding error (possibly zero) when 1/x is computed. And there is another rounding error when the result of that is multiplied by x. Sometimes the roundings happen to be in opposite directions and they cancel each other out. Sometimes they do not." @Eric Postpischil