This question already has answers here:
C floating point precision [duplicate]
(6 answers)
Closed 8 years ago.
Below is my code and program output:
#include <stdio.h>
int main()
{
float salary;
printf("Please enter the salary : ");
scanf("%f", &salary);
printf("Your Salary is : %f\n", salary);
}
desktop:~$ ./a.out
Please enter the salary : 101.8889999
Your Salary is : 101.889000
desktop:~$
I tried many ways by entering random decimal values but sometime it printed correct values (as entered during scanf call) and sometimes it just prints something near to it. Why is it that the float variable values are not printing correct values as entered?
As per example above - if I enter 101.8889999 then the value saved in salary float variable is 101.889000?
I tried changing the last line as :
printf("Your Salary is : %10.9f\n", salary);
But still it gave below output:
Please enter the salary : 101.889000
Your Salary is : 101.888999939
Please enter the salary : 10.999999999
Your Salary is : 11.000000000
Which is the correct value to print float using printf and is it that float values may / may not be stored as exact values as entered in float variables?
In C and in many other languages, floating point numbers are stored with a fixed number of bits (usually 32 or 64) so there is a limit to how much precision they can have, and there are only a finite number of numbers that can be exactly represented with a float. For all other numbers, the float can only store an approximation. You can see how many bits are used by printing the value of 8 * sizeof(float).
Additionally, floating point operations generally have rounding errors which makes them ill-suited for dealing with money.
If you are dealing with money, you might consider storing the values as an integer that represents the number of pennies, or the hundredths of a penny, or something like that.
Related
This question already has answers here:
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
Closed 1 year ago.
I made a simple C program to get a float as input from the user using scanf() function and dispay it using printf() function. But when the input number is quite large, the output deviates slightly. Why might that happen?
For example: if I give input as 2345, out put will be as expected. But if I give the output as 1234567898, the output will be unexpected.
#include <stdio.h>
int main()
{
float number;
scanf("%f", &number);
printf("The number %f", number);
return 0;
}
Single precision floating point (float) is good for approximately 6 significant decimal digits. double allows 15 significant figures. 1234567898.0 has 10 significant figures. Only the most significant first 6 digits can be relied on.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I am trying to separate the numbers in a floating input (xx.yy) and extract them out.
Not so sure if this is the online compiler fault but for some reasons, there are times when I run my following code, the decimal places that I extract seemingly -1 in value.
I am using this online compiler (https://repl.it/languages/c).
#include <stdio.h>
int main()
{
int whole_num = 0;
int dec_num = 0;
float u_input = 0.0;
printf("Input in a float: ");
scanf("%f", &u_input);
whole_num = (int) u_input;
printf("1. Whole number is %d\n", whole_num);
dec_num = (u_input - whole_num)*100;
printf("2. Decimal number is %d\n", dec_num);
return 0;
}
For example, there are times when I input in 99.95, instead of expecting 95 for my dec_num, it gave me 94. Then again, if I used another input such as 123.95 it returns me 95 instead. And as a result, if my code is giving me the -1 result, it will fails in other parts of my code.Decimal
I can't find out if it is an error with the way I have written my code and/ or if it is an issue with the compiler (also tried it with other online compilers which seemingly giving me similar results)
Can anyone share any insights to me on this? Or if there is a proper method that I will get the decimal number I expected without the -1?
Floating point numbers are represented using binary notation (see https://en.wikipedia.org/wiki/IEEE_754).
As such, decimal numbers that do not have an exact binary representation are stored as an approximation to the desired values.
For example, a value like 1.25 has an exact binary representation (5*(2^-2)), while numbers like 1.3 do not and need to be apprximated.
This website offers a good visualization of this phenomenon.
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 6 years ago.
i started learning c. Today, while i am working on a program, i found an interesting thing and i made another small program (similar to my issue) to check it.
#include<stdio.h>
int main(void)
{
float num1=867.0;
float num2=.6921;
printf("sum = %.4f \n",num1+num2);
return 0;
}
if i run the above program, i am getting 867.6921 as the answer. but if i changed the .4%f to %f the answer is changing to sum = 867.692078. why's the change in the output? Also, is there any way that i can get the answer without using the .4%f?
but if i changed the .4%f to %f the answer is changing to sum = 867.692078
printf rounds the result to that many digits after the floating point you specified, the default is .6. While rounding, it uses the current floating point rounding mode, the default is round to the nearest.
The float type has about seven digits of precision (can be anywhere from 6-9 digits). One alternative is to use g instead of f. This fixes the number of precise digits in the number, not just those after the decimal.
printf("sum = %.7g \n",num1+num2);
This produces output
sum = 867.6921
and will always give seven digits of precision for any input. Number formats would be like :
0.000000E+07
0000000
000000.0
00000.00
0000.000
000.0000
...etc
This question already has answers here:
How to only accept a certain precision (so many decimals places) in scanf?
(4 answers)
Closed 7 years ago.
I am extremely beginner in programming language. so i am facing some problems. please help me out.
Is it possible to take input a floating or double number with 2 digits after the decimal point using 'scanf' in C ??
See here: How to only accept a certain precision (so many decimals places) in scanf?
float value;
scanf("%4f", &value);
It's not really do that, but reads 4 digit edit: 4 characters float number. You can set other number instead of 4.
If you really need only 2 decimal places you can read the number with scanf, and after that round it using roundf.
#include <math.h>
...
float value;
scanf("%f", &value);
value = roundf(value*100)/100
You can read floats with
float a;
scanf("%f", &a);
You can read doubles with
double a;
scanf("%lf", &a);
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
With working with floating point values I don't know why my C compiler or the System is behaving unexpectedly. Not making it so confusing. Here is a simple program to take float value input and print the same.
#include <stdio.h>
int main()
{
float num;
printf("Enter float value ");
scanf("%f", &num);
printf("Value = %f", num);
return 0;
}
On multiple run of the same code giving me different and weird outputs. Here are some sample runs.
Sample Run #1 Correct Output
Enter float value 12.0312
Value = 12.031200
Sample Run #2 Incorrect and unexpected output
Enter float value 324.123
Value = 324.122986
Sample Run #3 Incorrect and unexpected output
Enter float value 945.1234
Value = 945.123413
I don't know why the system is adding some garbage values at the end. I am using GNU GCC compiler in code::blocks.
Your float cannot represent every conceivable number exactly. It has limitations. The closest float to "324.123" is 324.122986...
If you need more precision, use double, but even that has its limits too.
A float will be correct (match similarly limited input) to at least FLT_DIG significant digits. To print a number to FLT_DIG significant digits use:
printf("%.*e", FLT_DIG - 1, num);
FLT_DIG is at least 6.
number of decimal digits, q, such that any floating-point number with q decimal digits can be rounded into a floating-point number with p radix b digits and back again without change to the q decimal digits, ... C11dr ยง5.2.4.2.2 11
printf("Value = %f", num);
It will add some value if like :
Enter float value 945.1234
Value = 945.123413
because float value always print a 6 digit after the decimal point
to avoid this you can use the statement like :
printf("Value = %.3f", num);
This Will Give You A desire output as you want
it will print only three number after the decimal point
you can set as per your requirement like
"%.2f" ->for 2 number after the decimal
"%.3f" ->for 3 number after the decimal
"%.4f" ->for 4 number after the decimal