By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,433 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,433 IT Pros & Developers. It's quick & easy.

Confusion in Data type!

P: 2
Hi Everybody,

help me to clear the doubt.

[HTML]main()
{
double x = 1125899973951488.0;
float y,z,r;
y = x+1;
z = x-1;
r = y-z;
printf("%f\n",r);
}[/HTML]

The actual result should be 2.0.But it compiles to give the result 134217728.000000. Whats happening there?

Thanks in advance,

Rama.
May 24 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
You are mixing double and float. float is smaller than double so you lose data due to rounding.

If you use double throughout, you will get your expected result.

In fact, let me say that unless you have a real technical reason for doing otherwise, a reason that you can write down, always use double.

float is a carryover from days of yore when machines had very small memories.
May 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.