a very weird result

A

ai lian

The code is as the following:

{
double limit= 0.02*33.00;
double limit1= 0.06*11.00;
double limit2= 0.03*22.00;
double limit3= 0.01*66.00;

limit=(floor(limit*100))/100;
limit1=(floor(limit1*100))/100;
limit2=(floor(limit2*100))/100;
limit3=(floor(limit3*100))/100;

printf("limit= %.2f\n",limit);
printf("limit1 = %.2f\n",limit1);
printf("limit2 = %.2f\n",limit2);
printf("limit3 = %.2f\n",limit3);
}

What's weird is the output:

limit= 0.66
limit1 = 0.65
limit2 = 0.65
limit3 = 0.66

Why are the result for limit1 and limit2 is 0.65 instead of 0.66?

Thanks in advance.
 
K

Karl Heinz Buchegger

ai said:
The code is as the following:

{
double limit= 0.02*33.00;
double limit1= 0.06*11.00;
double limit2= 0.03*22.00;
double limit3= 0.01*66.00;

limit=(floor(limit*100))/100;
limit1=(floor(limit1*100))/100;
limit2=(floor(limit2*100))/100;
limit3=(floor(limit3*100))/100;

printf("limit= %.2f\n",limit);
printf("limit1 = %.2f\n",limit1);
printf("limit2 = %.2f\n",limit2);
printf("limit3 = %.2f\n",limit3);
}

What's weird is the output:

limit= 0.66
limit1 = 0.65
limit2 = 0.65
limit3 = 0.66

Why are the result for limit1 and limit2 is 0.65 instead of 0.66?

Thanks in advance.

See eg.
http://www.petebecker.com/js200006.html
 
J

John Harrison

The code is as the following:

{
double limit= 0.02*33.00;
double limit1= 0.06*11.00;
double limit2= 0.03*22.00;
double limit3= 0.01*66.00;

limit=(floor(limit*100))/100;
limit1=(floor(limit1*100))/100;
limit2=(floor(limit2*100))/100;
limit3=(floor(limit3*100))/100;

printf("limit= %.2f\n",limit);
printf("limit1 = %.2f\n",limit1);
printf("limit2 = %.2f\n",limit2);
printf("limit3 = %.2f\n",limit3);
}

What's weird is the output:

limit= 0.66
limit1 = 0.65
limit2 = 0.65
limit3 = 0.66

Why are the result for limit1 and limit2 is 0.65 instead of 0.66?

Thanks in advance.

Rounding errors.

0.06*11.00*100 does not equal 66, obviously it equals a number very
slightly less than 66, floor then rounds that number down to 65 and so you
get 0.65.

Never assume that floating point arithmetic will give you exact results,
in particular your problem is that 0.06 is not *exactly* 6 one hundredths,
and the rounding errors start there.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top