# Thread: precision problem - Help pls

1. ## precision problem - Help pls

Hi,

I am doing some calculations with double precision 8. Here is the problem:

First I calculate 1/6 which gives me 0.16666667 and then to this result I multiply 3 which gives me 0.50000001, but the ideal solution should be 0.5.

If I try the same thing with a calculator I get the ideal solution. How can I also get something like calculator - shoe maximum precision where ever needed and show least whereever not needed.

Thanks a lot.  Reply With Quote

2. Intermediate user Join Date
Jan 2006
Location
La Spezia,Italy
Posts
77
Thanks
9
Thanked 5 Times in 5 Posts
Qt products Platforms  ## Re: precision problem - Help pls

I don't know how you can do what you want... But I can give you some counsel concerning what I know... First of all, it is better to avoid useless calculation (like division and then multiplication) , you should do that by hand (if possibile) and use the final expression. Another thing would be particular numerical methos that can spare you some calculation (and the consequent error). For and "easy" introduction (but the methos showed can be out of date and not so theoretically "clean") to numerical methos you can take a look at Numerical Recipes http://www.nr.com/.
I hope it could help.  Reply With Quote

3. ## Re: precision problem - Help pls

Not all values can be stored in floating point types and they get rounded to the nearest value (in this case 0.16666667).

1.0/6 = 0.16666667 (there is no way of storing 2/3 in a float or double)
0.16666667*3 = 0.(...)1 (because 7*3=21, so the result has to end with "1")

If you agree to loose one decimal place of the result, you can round the value and receive the proper result (0.5000000).

Another workaround is not to operate on floating point values as long as you can:

(1/6)*3 = 3*(1/6) = 3/6 = 0.5  Reply With Quote

4. ## Re: precision problem - Help pls

The example I gave was part of a big problem which the user gives. Therefore, I dont have much control over what user can enter. If give the same problem in windows calculator, it gives the answer perfectly i.e., 1/6 = 0.16666667 and then multiplying it with 3 gives 0.5.

Any idea on this ?

Thanks a lot.  Reply With Quote

5. Guru Join Date
Jan 2006
Location
Warsaw, Poland
Posts
5,372
Thanks
28
Thanked 975 Times in 912 Posts
Qt products  Platforms  ## Re: precision problem - Help pls

Suppose that your number can have more than 8 digits after the decimal place.

This way you have:
> x = 1 / 6
x = 0.16666666...67
> x = 3 * x
x = 0.50000000...01

What would happen if you were displaying those numbers only up to 8th decimal place (of course rounded and without meaningless zeroes)?  Reply With Quote

6. ## Re: precision problem - Help pls Originally Posted by wysota
Another workaround is not to operate on floating point values as long as you can:

(1/6)*3 = 3*(1/6) = 3/6 = 0.5
Sticking with the above advise. I would like to maintain 2 floating variables.
Qt Code:
`float numerator = 1, denominator = 1;`
To copy to clipboard, switch view to plain text mode

Whenever you have to multiply fractions, keep multiplying numerators to the numerator, as well as keep multiplying denominators to the denominator.

Qt Code:
`//multiply (1/6)numerator *= 1;denominator *=6; //multiply (3)numerator *= 3;denominator *=1;  // since 3 can also be written as (3/1)`
To copy to clipboard, switch view to plain text mode
In the end calculate
Qt Code:
`result = numerator/denominator`
To copy to clipboard, switch view to plain text mode  Reply With Quote

7. Novice Join Date
May 2006
Posts
32
Thanks
15
Thanked 2 Times in 2 Posts

## Re: precision problem - Help pls

I try this code snippet in VC6

Then I got 0.5--The same as Windows small caculator.

Qt Code:
`int main(int argc, char* argv[]){	double i;	/*              usr input stack of caculator.         */	i = 1* 1.0 / 6;	i * =3 * 1.0 ; 	return 0;}`
To copy to clipboard, switch view to plain text mode
Last edited by iGoo; 26th May 2006 at 02:54.  Reply With Quote

8. ## Re: precision problem - Help pls

The compiler probably optimised that during compilation.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.