0.35=0.349999994

BlitzMax Forums/BlitzMax Beginners Area/0.35=0.349999994

Volker(Posted 2008) [#1]
I am sure this has been asked befor, but I have no clue
what to search for:

Local str:String = "0.35"
Print str.ToFloat()

How to round up floats in Blitzmax?
I want my 0.35 back..


GfK(Posted 2008) [#2]
Its just the way floats are represented - they're not accurate. Its not specific to Blitz, either.

If you need it to appear as "0.35" then you need to keep it as a string.

Alternatively, you can get a greater degree of accuracy by using a Double instead of a Float, but still don't count on it.


Brucey(Posted 2008) [#3]
0.35 can't be represented exactly as an IEEE float, hence the "noise" you see. That number is as close at it can store.

You can get closer with Double, but it's still not precise.

Your best bet, if you really needed exact figures all the time, would be to use some kind of arbitrary precision math library. They tend to store numbers "as is", and therefore should not lose precision in the same way.

Example : http://code.google.com/p/maxmods/wiki/MapmModule


Volker(Posted 2008) [#4]
Since the value represents dollars and cent I will use two Integer.
Much better then 0.34999 dollar.
Got this idea reading your answers.
Thanks.


TaskMaster(Posted 2008) [#5]
Just keep all your figures multiplied by 100 and only deal in integers.

Then convert to string yourself when displaying them.

In other words, if you want to keep 5.35 in a variable, multiply it by 100 and have 535 in the variable, when you want to print it, convert to string and insert decimal two places from the right.


Volker(Posted 2008) [#6]
Thats nice. Could be my idea :-)


ImaginaryHuman(Posted 2008) [#7]
This has been asked like 10 times now. ;-)


GfK(Posted 2008) [#8]
This has been asked like 10 times now
So, twice then. In binary.


Volker(Posted 2008) [#9]
This has been asked like 10 times now. ;-)

Just 10 times?
Every question has it' s answer nowadays somewhere in the outback of the internet.
But how to find and how much time does it take?

And I don't have the time.
I'm sure the world awaits my new Pong engine.
The ball is bump mapped, uses shaders and the background
is in realtime raytracing. Just waiting for Intels hexcore to get it working.


Gabriel(Posted 2008) [#10]
But how to find and how much time does it take? And I don't have the time.

So when you want to know something, your time is more important than the time of the people who answer you? That's probably not the attitude to have if you want them to help you again.


Ginger Tea(Posted 2008) [#11]
I'm sure the world awaits my new Pong engine.
The ball is bump mapped, uses shaders and the background
is in realtime raytracing. Just waiting for Intels hexcore to get it working.



Volker(Posted 2008) [#12]
Pong HD. Ouch. I'm to late with my engine :-)


Volker(Posted 2008) [#13]
@Gabriel
I believe you haven't seen my self-criticism in saying "And I don't have the time".
I understand that some regulars are getting bored beeing asked
the same questing hundred of times, but this is .. let's look .. yes,
it is Blitzmax Beginners Area.
And I wrote in my first postings that I don't know what to search for.
If you want to discuss this further, let's go to general discussions
and make a topic "Allowed and disallowed questions".
By the way, a FAQ would help to avoid repeating questions like mine.


ImaginaryHuman(Posted 2008) [#14]
Sorry to poke fun at you Volker, I didn't mean to offend you. It's just that this question actually has been asked several times, and quite recently too, so when you ask the same question it suggests you haven't used the search engine to find related threads, which is what most people do. When you then make a new thread about the same question it then requires the same answers to be written all over again, and usually by the same people who answered before. So it does get a bit tiresome. Yes you may be a beginner but the people answering aren't going to be beginners, or new to the question. I do recommend using the search feature, if you search for topics to do with floating point you'll probably find something along these same lines.


Volker(Posted 2008) [#15]
I do recommend using the search feature, if you search for topics to do with floating point you'll probably find something along these same lines.

I tried "float string", "float int", "float decimal". No helping matches.
"Floating points" or "rounding" would have helped, now I know better.
I am not offended.
The habit Gabriel was talking about is wide spread an can hurt
such a nice community like this one.