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

Try this:

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

}

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.

Got it ... Thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.