Floating point number is rounding off in C [duplicate] - c

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

Related

I made a simple C program to receive a float as input from user and display it in screen. But the value changes by a small amount while printing [duplicate]

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.

Deriving decimal numbers in float input sometimes minus the value by 1 [duplicate]

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.

Float value input error [duplicate]

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

Float variable comparison fails [duplicate]

This question already has an answer here:
Why does this program gives no output for float and double datatypes?
(1 answer)
Closed 7 years ago.
The below program seems to look like it will run for one time but when i run in Turbo C , the output is nothing.
Can any one explain this ?
#include<stdio.h>
int main()
{
float x=1.1;
while(x==1.1)
{
printf("%f \n",x);
x=x-0.1;
}
return 0;
}
By default, floating point numbers are stored as type 'double'. So, a comparison on float and double value is done.
I think,
if(x==1.1f)
it should solve the problem.
And also FLT_EPSILON is the smallest difference between two floating point numbers for them to be same.
if( abs(x-1.1f) <= FLT_EPSILON)
should work
You can re-write your loop condition as follows-
while((1.0009<x)&&(x<1.10001))
As because x=1.1 in this x is never exact 1.1. At higher decimal places it can have different value.
You can see here at higher decimal places what are values and working example for your code -https://ideone.com/IgrLAY

Why does printing a float having value 2.5367 using `%3.3f` output 2.537 instead of 2.536? [duplicate]

This question already has answers here:
Why is printf round floating point numbers up?
(3 answers)
Closed 6 years ago.
#include<stdio.h>
void main()
{
float f = 2.5367;
printf("%3.3f",f);
}
Output is : 2.537
But how?
I think output should be 2.536 but the output is 2.537.
Because 2.5367 is closer to 2.537. Delta from 2.536 is 0.007 while delta from 2.537 is just 0.003.
What you want to do is just deleting last character of its string representation and is wrong in math.
In addition to existing answers, note that many C compilers try to follow IEEE 754 for floating-point matters. IEEE 754 recommends rounding according to the current rounding mode for conversions from binary floating-point to decimal. The default rounding mode is “round to nearest and ties to even”. Some compilation platforms do not take the rounding mode into account and always round according to the default nearest-even mode in conversions from floating-point to decimal.
since float f = 2.5367 needs to rounding up and there should be 3 digit after decimal, so the value will be 2.*** , means 2.537.
The documentation says:
The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.
That said, please use another book or resource for learning, whoever taught you that it's ok to use void main() is plain wrong and should not teach C.

Resources