Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
int digit = 1;
float result=0.0;
double temp = 200000;
float tick = 0.00100000005;

result = digit/1000000.0;
long long phase = temp*result*1000/tick*1000


result will be equal to 9.99999997e-07. If manually calculate it should be 0.000001 How can I make the exponential num to be 0.000001? Thanks.

if result = 9.99999997e-07 phase calculated will be 199999,however if result = 0.000001 phase calculated will be 200000. I want to get the result 200000 but how can I round the exponential num to 0.000001?
Posted
Comments
[no name] 11-Apr-14 21:01pm    
This problem has nothing to do with rounding.

Perhaps this[^] article will help.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Apr-14 15:37pm    
Sounds like a good idea, a 5.
—SA
jeron1 8-Apr-14 18:17pm    
Thanks, there are some gems here, you just have to look.
Floating points are imprecise. 9.99999997e-07 is the closest number that can be represented as a float. Using a double would give much more precision... often enough for computation like that.

You should also write your expression in a way to avoid as much imprecision as possible. As floating point can represent exactly multiple of 1 up to about 15 digits but cannot represent exactly fractionnal number (except for fraction like 0.25, 0.5, 0.75 and other where the numerator would be an integer and the denominator a power of 2), it might be preferable to keep each value serate and do the final computation:
C++
long long phase = (digit * temp * 1000 * 1000) / (ticks * 1000000.0);

It should be noted that tick should be a double too for improved precision. Otherwise, you loose all your precision there.

Finally, in some cases, you might also want to round the resulting number. This is particulary the case where converting back to integer. If the result of phase computation is something like 199999.998, you probably want a result of 200000. Without rounding, the value would be truncated to 199999.
C++
long long phase = floor((digit * temp * 1000 * 1000) / (ticks * 1000000.0) + 0.5);

When you do computation like that, it is useful to know the language specification and learn how floating points works. Ideally, this is the kind of things, one would learn in scholl but if you are Learning by yourself, you should be able to find a lot of information on Google.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900