1. 2

Hi Folks,

A colleague of mine ran into the following bit of code (in a java program, but its pseudo code here) he was analyzing and was wondering what it was for.

a = 0.8 b = 1 - a if (a + b != 1) throw exception

What could this be for? I was guessing its to test some kind of compiler bug, or floating point precision capability of the platform.


  2. 3

    Here is a formatted, more Java-like rewrite of your pseudo-code, as far as I understand it:

    double a = 0.8;
    double b = 1 - a;
    if (a + b != 1)
        throw new SomeException();

    Indent code by 4 spaces to preserve its formatting like this.

    I don’t see any purpose for this code except testing floating point precison capability or some compiler bug, as you mentioned. I don’t know what particular bug the author was worried about, though.

    Are you sure the code was actually necessary? Is it possible that a bad coder put it in and didn’t realize it will never throw an exception, or that someone put it in as test code while learning Java and forgot to delete it?

    1. 2

      Thank you for the formatting. I was in a hurry and did not check to see I had forgotten code tags. A pity we can’t edit the text, but your code is perfect.

      Yes, it’s possible this is relic code, where at some point b was set separately from a and this was a constraint check, but them some one decided to hard code the values and did not realize the check was now redundant.


    2. 2

      Can you use git blame or equivalent to find who wrote it, and what the commit message was? If the message is not useful, perhaps the person who wrote it is still around and might remember?