Fun and games with Python vs C

I’m using Python to test the code generated by a C compiler.  Many of the tests are along the lines of:

a = 5
b = 10
result = run_c_code_for_add_in_emulator(a, b)
assert result == a + b

This works fine except when dividing integers with rounding. Under GCC on x86, -100/30 is -3, but in Python -100//30 is -4.  Hmm.

This has the interesting side effect that in Python 2.5 -a/b != -(a/b).

The work-around seems to be to do it explicitly as int(float(a) / float(b)) is -3.

Avatar
Michael Hope
Software Engineer