Archived

This topic is now archived and is closed to further replies.

1.53452352352 to 1.53 ??

This topic is 5009 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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." =)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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." =)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites