Click here to Skip to main content
15,891,428 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include<stdio.h>
main()
{
float a,b;
printf("Enter two real number values for a and b :\n");
scanf("%f %f",&a,&b);
printf("a = %f and b = %f\n",a,b);
getch();
}

Input :
456987.2
456987.3
Output :
456987.187500
456987.312500

Why is my output abnormal ? I mean why am i getting only approximate values as output ?


I am using Dev-C++ 5.11 IDE.
My computer has 64-bit architecture.

What I have tried:

I have tried to guess the reason behind the approximation of the real number values to be stored, but i can't come up with an explanation.
Posted
Updated 12-Apr-16 22:58pm
v3
Comments
Sergey Alexandrovich Kryukov 12-Apr-16 12:08pm    
This is how floating-point numbers work, nothing abnormal. You can format them into string in some more suitable way, if you want.
Do I even need to explain that real numbers can only be represented approximately? (Floating-point numbers are designed to serve as an approximation of the concept of mathematical real numbers.) Do I have to explain why? Do you understand that a real number can contain more information that your whole computer can hold? Do you understand that most real numbers contains infinite volume of information each?
—SA
Shubham_Dubey 12-Apr-16 12:17pm    
Sir, would you please send me a link to some related documents/articles so i can read how floating point numbers are stored.
Sergey Alexandrovich Kryukov 12-Apr-16 12:37pm    
Oh, sure. Not that you need to directly use it, but it's important to understand, by many reasons.
Please see Solution 4.
—SA
Shubham_Dubey 12-Apr-16 12:44pm    
Thanks a lot :)
Sergey Alexandrovich Kryukov 12-Apr-16 12:55pm    
You are very welcome.
Good luck, call again.
—SA

This is documented all over the place, thousands of times. What you're looking for is "IEEE floating point number representation[^]".
 
Share this answer
 
When you declare some variable as float or double, you tell the computer that it's floating-point data, approximate representation of mathematical real numbers. You call them "real" yourself, but then you need to understand the mathematical concept of real numbers. Please see my comment to the question.

They are approximate by definition; and they cannot be precise in principle.

The compile cannot "know" what you may assign to a floating-point value. On next step, you can calculate acos(−1). Even though −1 can be considered as exact value, the result is transcendental number π. Did you ever heard that it can be proven that any given finite sequence of digits can be found in the infinite representation of this number?

Maybe you need fractional but precise number? They are not hard to implement. It could be a record (struct) of two integer numbers: a mantissa and an exponent (say, a power of 10). Roughly, you define an integer number and, separately, the information on where to put a decimal point. And then you can define all operations on these number. Of course, they are nothing like real. You cannot even divide 1 / 3. Or you can, if you make it more advanced: add information on the period, which you can calculate and represent: 1/3 = 0.(3). But if you make this step, you can better implement the concept of rational numbers which is a pair of integers: numerator and denominator.

With floating-points, you also may want to learn numeric data formatting.

—SA
 
Share this answer
 
456987.187500 is the closest number that can be encoded as a single precision floating point number to 456987.2. Changing the least significant bit gives you a value that has a greater difference to 456987.2.

A float on most systems has (from memory) 24 significant bits, which is about 7 or 8 significant decimal digits. As soon as you want to try and represent a number with a greater number of significant bits it'll all go horribly wrong.

If you need more bits of precision try using a double or long double. If that's not enough you might need to look at rolling your own floating point representation or borrowing someone else's.
 
Share this answer
 
Shubham_Dubey asked:

Sir, would you please send me a link to some related documents/articles so i can read how floating point numbers are stored.
Oh, sure. This is fully standardized by IEEE 754: IEEE floating point — Wikipedia, the free encyclopedia[^].

See also:
Floating point — Wikipedia, the free encyclopedia[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900