Math Isnt Working

BlitzMax Forums/BlitzMax Programming/Math Isnt Working

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

For num = 0 To 10
	Print 2^num
Next
The number 511.99999999999994 becomes 511 when when assigned to an integer variable. BlitzMax converts floating point values to integer by truncation, simply throwing away everything after the decimal point. This kind of 'off by one' error is bound to happen if you depend on floating point results to have exact integer values.

The other question is why there may be different results when constants are used rather than variables. BlitzMax originally computed x^y using the pow() function in the C++ math library. This generated a lot of complaints because the MingW pow() was very slow. So a native BlitzMax pow() was written. An expression like 2^9 is evaluated by the compiler before the program runs and still uses the old pow(). 2^nine, where nine is a variable, uses the new pow().

The new pow() does not exactly match the old one. The original pow() has code to detect special cases where the result can be computed exactly.

The important lesson here is that you should not depend on floating point arithmetic to give exact integer results.