> void main ()
> {
> float x = 1073741855;
>
> printf ("%f\n", x);
> cout << x;
> }
>
> and the out put on running is as follows
>
> 1073741824.000000
> 1.07374e+09
>
> What i want to know is that why am i not
> getting
> the o/p as 1073741855 .There is a difference
> of 31.What am i doing wrong?
The reason why u are not getting 1073741855, but 1073741824 is generally
attributed to floating point rounding off error. If u study the way floating
point numbers are stored internally, u will find that the representation is
not very accurate for integers. Here u have 10 significant digits, which is
a lot more than what you will require for most of the floating point
operations. If you need greater accuracy, then you can try out "double" &
"long double" datatypes.
PS: If i compile the above program under MS VC++ 6.0 sp5, the compiler gives
the following warning, which can give u a further explaination of the
problem!
warning C4305: 'initializing' : truncation from 'const int' to 'float'
changing the code to the following:
#include <iostream> // This is typically preffered over iostream.h
#include <stdio.h>
int main(int argc, char* argv[])
{
double x = 1073741855.0; // Note that i have put a .0 at the end
/* In fact changing the above line to the following will also work w/o
warnings under vc++ 6.0 sp5
double x = 1073741855;
*/
printf ("%lf\n", x); // Note the use of %lf in place of %f
std::cout << x; // since i am using iostream, i have to use std::
return 0;
}
gives this output under vc++ (as desired):
1073741855.000000
1.07374e+009
================================================
To subscribe, send email to [EMAIL PROTECTED] with subscribe in subject header
To unsubscribe, send email to [EMAIL PROTECTED] with unsubscribe in subject header
Archives are available at http://www.mail-archive.com/ilugd%40wpaa.org
=================================================