ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

a strange float point error
Posted by tjq(killer of zju) 21 Feb 2001 13:00
in the 1070 local time, I find some error when converting
double to int,

int main()
{
double f=0.12;
int t;
t=int(f)*60+(f-int(f))*100;
cout<<t<<endl;
return 0;
}

I expect to get 12, but the result is 11, many compilers
were tested, can anyone tell me how to avoid this?
Re: a strange float point error
Posted by Marat Bakirov 21 Feb 2001 14:13
I am afraid you van do nothing except smth like round
(0.12*100)

You see, because double values are really stored in binary
format, numbers that can't be presented with  a degree  2
denominator (2,8,128,etc) are presented by approximation.
I think 0.12 is really something like  0.1199999????.

Anmd his is the reason