Floating point inaccuracy
BlitzMax Forums/BlitzMax Programming/Floating point inaccuracy
| ||
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! :-) |
| ||
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 |
| ||
a:Float = 1.3 Print a Yeah, it equals 1.29999995......kinda scary. |
| ||
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)) |
| ||
Be nice if BlitzMax had a round function where you could specify the number of digits. |
| ||
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. |
| ||
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. |
| ||
Can't you just do this?...test:Double = 1.4! Print Int(test) + "." + Int(test * 10) Mod 10?? |
| ||
without having to write a hole bunch of silly code |
| ||
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)) |
| ||
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 ;-) |
| ||
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] |
| ||
Unfortunately my GreyAliensSuperFantabulousBitmapPrintingFunction cannot accept input parameters in that form which I why I need the separate parts :-) |
| ||
finicky I say...Fiiiniiicky! ;o) ...Yes, I'm bored. :o) |
| ||
a:Float = 1.3 Print a * 2 + .40 Out: 3.00000000 It's accurate, the problem seems to be output. |
| ||
yeah it's weird as 1.4 (1.39999) + 0.1 is 1.5 and not 1.49999 |
| ||
Could you just use Doubles? Do they work or do they show the same problem? |
| ||
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) |
| ||
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. |
| ||
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 |
| ||
Whoops, you're right. Should have paid more attention. :) |