Strange problem with values over 10,000.00

Blitz3D Forums/Blitz3D Programming/Strange problem with values over 10,000.00

Cancerian(Posted 2004) [#1]
I have a program that counts up a monetary cost tally in cents per second. The problem I'm having is that once the value reaches 10,000 Blitz won't accept anything past the first decimal point. So even something simple like:

10000 + .02

Will just return 10000.0, it drops everything past that first decimal point. If I have to I can probably create a work around for this but it would be a collosal pain in the butt compared to just having the math work properly.

Is this problem common knowledge? Am I doing something wrong? Any help is appreciated, thanks :)


fredborg(Posted 2004) [#2]
It's because floating point numbers become more and more inaccurate the bigger they get. It works like that in any program, as it's how the computer deals with the numbers.

Instead you could split your money variable into dollars and cents, along the lines of this:
Graphics 200,16,0,2
SetBuffer BackBuffer()

; Start amount
Global dollars = 10000
Global cents = 0

While Not KeyHit(1)
  ; Add 20 cents
  AddMoney(0.20)
  Cls
  Text 0,0,"You have: $"+dollars+","+cents
  Flip
Wend

Function AddMoney(amount#)
	
	add_cents   = amount*100
	cents = cents + add_cents
	If cents>99
		add_dollars = cents/100
		cents = cents - (add_dollars*100)
		dollars = dollars + add_dollars
	EndIf
	
End Function 



Cancerian(Posted 2004) [#3]
I figured I would probably have to come up with a workaround. Thanks for your help :)