0

Hi All,
Today I came across a peculiar piece of code.

int main(int argc, char* argv[])
{
         printf("%lf\n",(0.99-0.90-0.09));

}

According to me the output for this code should be 0, but to my surprise when I ran this code the answer that I got was -0.000.
I cannot understand the reason for the minus sign

Edited by abhimanipal: n/a

4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by abhimanipal
0

Try this:

int main(int argc, char* argv[])
{
         printf("%lf\n",(0.99F - 0.90F - 0.09F));

}

Edited by gerard4143: n/a

2

You are dealing with floating point numbers. .99 probably is stored as a close approximation of .99, very close. But not close enough. Same with all the other values. Therefore, you have a little wiggle in the values and probably end up with something similar to -0.000000001 as an answer.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.