2

This math equation...

(4195835 / 3145727) * 3145727 - 4195835

is supposed to equate to 0. According to the book Software Testing (2nd Ed),

If you get anything else, you have an old Intel Pentium CPU with a floating-point division buga software bug burned into a computer chip and reproduced over and over in the manufacturing process.

Using Python 2.7, I get -1050108 in command line.

Using Node I get 0, just as I would in the calculator

Any deeper explanation for this? This was originally brought up due to a bug in the video game Disney's Lion King, 1994. I thought I would test the equation on a few things.

2
  • Note that just because a mathematical expression equals zero does not mean that the same computation done with floating-point has to equal zero, and that's not a bug, that's how standard floating-point computations work. You make it sound as if your book is not explaining this very clearly. Commented Sep 18, 2013 at 8:32
  • @PascalCuoq I believe my title had some info about the book that related to the question and someone modified this title. Either way the book was not trying to explain floating points or anything more than sharing "Infamous Software Error Case Studies". I just took the computation and tried myself, with at the time odd results until it was obviously rounding the division. So, no the book had no reason to explain these details or should have based on this chapter. A very good book and worth the read. Commented Sep 18, 2013 at 16:39

2 Answers 2

12

You did integer math and not floating point math.

>>> (4195835 / 3145727) * 3145727 - 4195835
-1050108
>>> (4195835. / 3145727.) * 3145727. - 4195835.
0.0

Note that you can get the behavior you want from integers using py3k or PEP238 division.

>>> from __future__ import division
>>> (4195835 / 3145727) * 3145727 - 4195835
0.0
Sign up to request clarification or add additional context in comments.

Comments

9

With integer math (which is what you're using), (4195835 / 3145727) will give you 1.33382... rounded down to the integer 1.

So you effectively end up with:

  (4195835 / 3145727) * 3145727 - 4195835
=          1          * 3145727 - 4195835
=                       3145727 - 4195835
=                           -1050108

which is why you get that negative number.

You can force it to use floating point just by making one of the values floating point:

>>> (4195835. / 3145727) * 3145727 - 4195835
0.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.