#### Archived

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

# Reducing floating point precision !

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

## Recommended Posts

I''m working with a good camera engine , so i want to decrease the precision of a floating point variable , so that 0.00000001 appears as 0.0 only , so how to do it ? thanks for your help ...Xeee

##### Share on other sites
ooops.

[edited by - jenova on April 17, 2002 4:47:40 PM]

##### Share on other sites
I doubt you''re doing anything that''s so precise that it won''t be happy with 0.00000001 instead of 0. If you really do need it consider a threshold instead of 0. E.g. if you''re doing something like if (value == 0) then change that to if (value < 0.001) so everything smaller than 0.001 will be considered, and if value == 0.00000001 then it will be treated as 0 in your program flow.

------------
- outRider -

##### Share on other sites
i may have mis-explained my problem , you idea(outrider) is good , but i really need to set this value to zero .
the problem in more details is the when i convert 90 degrees to radians by multiplying by PI/180 , where pi is defined by an 8 digits value , and i give the resulting value to cos(); the answer is not 0 , it is -4.4896592167739e-011 , it is an extremely small value , which is later checked for being zero and multiplied by something , so this is my problem .
if i can get a more precise value for PI (something like 16 or more digits) so that cos(toradians(90))== 0 , that may solve my problem coz as far as i know that PI has as infinite number of digits.
thanks for paying attention .

...Xeee

[edited by - xeee on April 17, 2002 7:55:17 PM]

##### Share on other sites
if (variable < 0.001)
variable = 0.0f;

-me

##### Share on other sites
palidine : i thought about this but i think it''s costy , cos that''s done 3 times every frame , or may b i''m under-estimating the speed of the CPU .

...Xeee

##### Share on other sites
quote:
which is later checked for being zero and multiplied by something

well then, where you check the above check like someone above suggested:

if (variable < 0.001)....

if you want it to be exactly zero you''re gona have to conver it somewhere. also i don''t think is a big load on the processor, maybe 3-5 cycles....dunno...

-me

##### Share on other sites
ok thanks palidine , i think that''s the only solution , but i tried to keep my game as fast as possible so i thought there''d b another solution , thanks anyway .

gotta continue my game .....

...Xeee

##### Share on other sites
you have a significant design flaw if your program is converting degrees to radian, well unless you are reading data from a file. Get rid of the degrees and work completely in radians. As for pi you can get a good value using trig. I don''t remember, maybe 4*atan(.5) or something like that. Ideally it would be a constant in math.h, that''s what java does.

##### Share on other sites
do you mean as in displaying the number on the screen or in a text file?

if so, if you are using something like sprintf, try this

%.3f

this is 3 decimal places for example

others are used for significant figures instead. Look in the help file.

If you actually mean the engine, then only thing I can say is why bother, but you can ignore that comment.

Beer - the love catalyst
good ol'' homepage

##### Share on other sites
This:
if (variable < 0.001)....
is MUCH cheaper than a cos(). I.e., you will hardly notice a slowdown.

Forever trusting who we are
And nothing else matters
- Metallica

##### Share on other sites
You really should use radians. But if must convert to degrees, you should first make your PI/180 a constant and make it as precise as you want ie: 0.174f, 0.17444444f, etc.

##### Share on other sites
quote:
Original post by Anonymous Poster
4*atan(.5) or something like that

4*atan(1), actually. Remember the unit circle?

##### Share on other sites
since cos returns values between -1 and 1, you''ve got to change the code to this:

inline float ABS( float value ) { if( value < 0 ) return -value; return value; }if( ABS(value) < 0.001f ){    // blah, blah, blah...}

Compared to the cos function itself, this is lightening fast!

(PS That ABS method should probably be a templated function, but I just did it like that for simplicity)

codeka.com - Just click it.