Floating point inaccuracy

BlitzMax Forums/BlitzMax Programming/Floating point inaccuracy

Grey Alien(Posted 2007) [#1]
Yes I know floating point numbers can't hold numbers as accurately as you like but I can't seem to hold 1.4 !!!

Check it out:

test:Double = 1.3
test:+0.1!

Print test

test2:Double = 1.4
Print test2


Gives output:

1.3999999523162843
1.3999999761581421


Sigh. Any idea how I can round this to one decimal place in blitz so that it can be used to display 1.4 without having to write a hole bunch of silly code. Thanks! :-)


CS_TBL(Posted 2007) [#2]
hm.. perhaps silly, but if you're only going to use one decimal, why not *10 and use ints instead, and display the result as /10 ? :P


Chroma(Posted 2007) [#3]
a:Float = 1.3
Print a


Yeah, it equals 1.29999995......kinda scary.


Grey Alien(Posted 2007) [#4]
yeah and these sort of numbers will be flying man to Mars...

CS_TBL: YEah I did something like that:

		Local temp$=String(multiplierghost)
		Local MultiplierText$ = ccFirstStringToSubChop(temp,".") 'This returns the number before the decimal place and updates temp to be the number after the decimal place.
		MultiplierText:+"."+(Int(Float("0."+temp)*10))



Grey Alien(Posted 2007) [#5]
Be nice if BlitzMax had a round function where you could specify the number of digits.


Brucey(Posted 2007) [#6]
Interestingly printf chops rather than rounds numbers... I think I might add a flag to my string Formatter module to enable rounding of floats/decimals for the given precision.


tonyg(Posted 2007) [#7]
I've moaned about this since BlitzMax was released. Each time it started off with people saying "yep, can't be right" and then the boffins come in with their 'floating point precision' explanations.
When I said BB showed the numbers OK I was deafened by 'BB was less accurate' replies.


Yan(Posted 2007) [#8]
Can't you just do this?...
test:Double = 1.4!
Print Int(test) + "." + Int(test * 10) Mod 10
??


Damien Sturdy(Posted 2007) [#9]

without having to write a hole bunch of silly code




Yan(Posted 2007) [#10]
Exactly...
yeah and these sort of numbers will be flying man to Mars...

CS_TBL: YEah I did something like that:

Local temp$=String(multiplierghost)
Local MultiplierText$ = ccFirstStringToSubChop(temp,".") 'This returns the number before the decimal place and updates temp to be the number after the decimal place.
MultiplierText:+"."+(Int(Float("0."+temp)*10))



Grey Alien(Posted 2007) [#11]
Yeah but as per usual there's code missing. I'm not just printing it I'm using a bitmap font and also making some decisions based on some of the values. Thanks for the tip though I might change to Mod ;-)


Yan(Posted 2007) [#12]
Blimey...
GreyAliensSuperFantabulousBitmapPrintingFunction  Int(test) + "." + Int(test * 10) Mod 10
...Happy now. ;op

What's with everyone being soooo finicky today?

Is it silly season already?

:o)

[edit]
...WOT!?...
[/edit]


Grey Alien(Posted 2007) [#13]
Unfortunately my GreyAliensSuperFantabulousBitmapPrintingFunction cannot accept input parameters in that form which I why I need the separate parts :-)


Yan(Posted 2007) [#14]
finicky I say...Fiiiniiicky! ;o)


...Yes, I'm bored. :o)


Blueapples(Posted 2007) [#15]
a:Float = 1.3
Print a * 2 + .40

Out:
3.00000000

It's accurate, the problem seems to be output.


Grey Alien(Posted 2007) [#16]
yeah it's weird as 1.4 (1.39999) + 0.1 is 1.5 and not 1.49999


TaskMaster(Posted 2007) [#17]
Could you just use Doubles? Do they work or do they show the same problem?


Dreamora(Posted 2007) [#18]
If you need more precise float, download the current mingw and recompile all modules beside maxgui

this will remove this crappy problems with float (not only for this case but float ^float scenario as well)


Grey Alien(Posted 2007) [#19]
TaskMaster: It's already using doubles if you look at the code.

Dreamora: REALLY!? So this was always just a mingw issue then? weird. Well I've written a workaround now.


Dreamora(Posted 2007) [#20]
It seems so. The ^ thread where I posted showed that I get 8.0000 with 2^3 in any of the potential combinations where all others get 7.999... with float ^ float


TaskMaster(Posted 2007) [#21]
Whoops, you're right. Should have paid more attention. :)