1.53452352352 to 1.53 ??

Started by
9 comments, last by Rasmadrak 20 years, 1 month ago
Hello everybody! I''m just wondering if someone knew how to do as the topic says... I''ve got a regular float at 1.5435435345345345342... or whatever.. how do I limit the float to only use the first, say, 4 digits? any help is appreciated! /Robert "Game Maker For Life, probably never professional thou." =)
"Game Maker For Life, probably never professional thou." =)
Advertisement
quote:Original post by Rasmadrak
I''ve got a regular float at 1.5435435345345345342... or whatever..
how do I limit the float to only use the first, say, 4 digits?

hmm...
float a = 1.5435435345345345342f;
int b = (int)(1000.0f*a);
float c = ((float) b) / 1000.0f;

not the fastest way and doesn''t round correctly (just truncates), but should do what you wanted
and to use more or less digits just change 1000.f (both of em) to an appropriate coefficient (100.0f for 3 digits, 1000000.0f for 7 etc)
quote:how do I limit the float to only use the first, say, 4 digits?


On paper? Just stop writing after 4 digits. On a calculator? Just punch in 4 digits. In a programming language? What language? In C++ you can use setprecision. See examples
here. But, since you really didn''t give any info, I don''t know if that will help.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
If you just need this for printf() or similar, you can pass the precision in the format string, i.e.:
float n = 1.5435435345345345342f;printf("%.4f", n); 

Will print:
1.5435
The .4 in the string specifies 4 decimal points of precision.

"The ability to speak does not make you intelligent" - Qui-Gon Jinn
[ DGDev - The Delphi Games Development Community ] [ Help GameDev.net fight cancer ] [ Shareaza - The Ultimate P2P Client ]
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
In any case, floats are not stored in decimal, so you cannot really limit them to being stored with a specific number of decimal digits.
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
He already has his answers. Unless he gives more info no one can really help him.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
Thanks all!

I guess I''ll post what language I meant, C++, just to please Herr Gestapo above...

The problem I''m having is that I cant really use "floata == floatb" because after ~5-6 digits it´s not the same number anymore... are doubles more precise?



"Game Maker For Life, probably never professional thou." =)
"Game Maker For Life, probably never professional thou." =)
You never compare floating point numbers (yes, doubles are floating point numbers too) for equality. Instead you should check if the difference is smaller than a certain value.
Ok.. =)


How would I check if a float is close to another float's value?
ie. comparing two floats...
if (floata > 0.2 && floata < 0.3)or:if (floata > floatb-0.2 && floata < floatb+0.3)

having a fixed value b is easy, but what about two variable floats... is there a premade function for this, or should I make my own? =)


[edited by - Rasmadrak on March 26, 2004 6:38:07 AM]
"Game Maker For Life, probably never professional thou." =)

This topic is closed to new replies.

Advertisement