Substitute for floatint point

S

Sathyaish

In C, what do you use to store numbers larger than the floating point
range? For e.g, consider the code below:


int main(void)
{

float a = 0.0;
double b = 0.0;
double f = 0.0;

a = 123456789.0;
printf("%f\n\n", a); //output: 123456792.000000

b = 123456789;
printf("%f\n\n", b); //output: 123456789.000000


printf("Enter a number: ");
scanf("%f", &f); //input: 123456789
printf("You entered: %f\n\n", f); //output: 0.000000

return 0;
}

What datatype should be used to negate the rounding error?
 
V

Vladimir Oka

Sathyaish said:
In C, what do you use to store numbers larger than the floating point
range? For e.g, consider the code below:


int main(void)
{

float a = 0.0;
double b = 0.0;
double f = 0.0;

a = 123456789.0;
printf("%f\n\n", a); //output: 123456792.000000

b = 123456789;
printf("%f\n\n", b); //output: 123456789.000000


printf("Enter a number: ");
scanf("%f", &f); //input: 123456789
printf("You entered: %f\n\n", f); //output: 0.000000

return 0;
}

What datatype should be used to negate the rounding error?

The only two standard types you have available are `float` and
`double`, but you know that already. If you need better precision,
you'll have to use some appropriate library providing the functionality
and data types you need. There are libraries out there that support
"infinite" precision. Consider GMP (http://www.swox.com/gmp/).

However, it is often possible to get around some of the floating point
limitations by clever choice of algorithms.
 
K

Keith Thompson

Sathyaish said:
In C, what do you use to store numbers larger than the floating point
range? For e.g, consider the code below:


int main(void)
{

float a = 0.0;
double b = 0.0;
double f = 0.0;

a = 123456789.0;
printf("%f\n\n", a); //output: 123456792.000000

b = 123456789;
printf("%f\n\n", b); //output: 123456789.000000


printf("Enter a number: ");
scanf("%f", &f); //input: 123456789
printf("You entered: %f\n\n", f); //output: 0.000000

return 0;
}

What datatype should be used to negate the rounding error?

This isn't a rounding error. The format to read a double using scanf
is "%lf", not "%f".

Also, you're missing the required "#include <stdio.h>".
 
K

Keith Thompson

Vladimir Oka said:
The only two standard types you have available are `float` and
`double`, but you know that already. If you need better precision,
you'll have to use some appropriate library providing the functionality
and data types you need. There are libraries out there that support
"infinite" precision. Consider GMP (http://www.swox.com/gmp/).

There's also long double, which may or may not have more precision
than double.
 
D

Dik T. Winter

This is not about range, but about precision.

a has 23 bits of precision, so rounding has to take place.

b has 53 bits of precision, so no rounding has to take place.

Does the conversion specifier in the call to scanf match with the argument?
 
J

Joe Wright

Sathyaish said:
In C, what do you use to store numbers larger than the floating point
range? For e.g, consider the code below:


int main(void)
{

float a = 0.0;
double b = 0.0;
double f = 0.0;

a = 123456789.0;
printf("%f\n\n", a); //output: 123456792.000000

b = 123456789;
printf("%f\n\n", b); //output: 123456789.000000


printf("Enter a number: ");
scanf("%f", &f); //input: 123456789
printf("You entered: %f\n\n", f); //output: 0.000000

return 0;
}

What datatype should be used to negate the rounding error?
Not a rounding error. Your scanf() stuffs a float value into a double
object. This 'scanf("%lf", &f);' works better. You must also include the
stdio.h header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top