Floatting numbers dont add right...

Blitz3D Forums/Blitz3D Programming/Floatting numbers dont add right...

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.

Here's an even more glaring example:
; Puzzle about floating point: Why is size = 5000 so inaccurate? 

; Short answer: Floats have 24-bit precision but 16777200 + 1 needs 25 bits.


size = 500   ; Works exactly, but try 5000.
sum# = 0


For i = 1 To size
	For j = 1 To size
		sum = sum + 1.0
	Next
Next

Print "  'One at a time' sum = " + sum
Print

sum = 0
For i = 1 To size
	block# = 0.0
	For j = 1 To size
		block = block + 1.0
	Next
	sum = sum + block
Next

Print "'Block at a time' sum = " + sum

WaitKey() : End

In this case adding 1.0 works exactly, up to 2^24.
Thereafter adding 1.0 does nothing at all because that would require 25-bit precision.

Single precision floats are 24-bit.