What is the matter with printing some float values? [duplicate] - c

This question already has answers here:
Is floating point math broken?
(31 answers)
Floating point inaccuracy examples
(7 answers)
Closed 6 years ago.
i would like to know what is my problem when i try to print some float values, for example, in this simple programme :
float n = 127.998 ;
printf("%f",n);
The execution gives : 127.998001.
So why i have the additional 1 back of this number ?

Some values cannot be accurately stored in a floating point data type. There is no guarantee that your float n = 127.998 will actually be stored as exactly 127.998 . For values that cannot be accurately represented in floating point types, the closest value to that is stored instead, which is what you got.

Related

Why 29/8*100 will return 300 in C [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 11 months ago.
The following is the code
float lc = lcount/argc*100;
return lc;
I tried to get 362.5 but the result is 300
because initially it will computer division(because at the left of equation and having same precedence as multiplication )
lcount/argc(29/8=3)
and then multiplied by 100.
And at the last the computed result will be stored in floating format.
Your problem might be you are thinking initially it will compute 29/8=3.625
which wouldn't.

Why is there no output for this program? [duplicate]

This question already has answers here:
Comparing float and double in C
(2 answers)
strange output in comparison of float with float literal
(8 answers)
Closed 1 year ago.
Can someone pls explain me why this is not printing hello?
int main(){
float i;
i=1.2;
while (i==1.2){
printf("hello");
}
Computers only store digital information. Integers can be represented accurately in binary, but floating point numbers are approximated.
It seems that in the approximations, additional tiny values are preventing your exact comparison from being true.
Now is a good time to google "what every programmer should know about floating point numbers" and read it. It will save you countless hours of future programming and debugging if you learn it early.
In addition, there are two floating point types "float" and "double". You are comparing a float to a double, so the approximations of the value are likely not the same approximation, creating more of a chance that the two values won't be equal.

the problem is from a algorithm problem I do recently,but I can't gain the right answer [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Is floating point math broken?
(31 answers)
How should I do floating point comparison?
(12 answers)
Closed 3 years ago.
Given the following snippet
int k = 12;
float a = 1.0/12;
if ( 1.0 / k == a )
printf("%d",0);
I would expect the condition to be true, but it prints nothing.
What's the reason? well, maybe I am not polite,I am feel very sorry,this is my first time to ask a question on this sites,I don't know there will be someone
answer me,so thank you for you to correct me,I will do better next time ,if you
have spare time ,would you please solve my problem
the following is my code,when it comes to the second example,it just print five equation
You don't see anything because the condition results to false. Your variable a is float, while 1.0/k in if clause is double. They have different precision and therefore are not equal. In general it is a bad idea to compare floats on equality.

How to properly round up doubles in C? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
For some reason, ceil(x) rounds up round numbers to x+1.
For example:
double og_grade = 50;
double fact_grade = ceil(og_grade*1.1);
og_grade*1.1 should be 55.0000, but ceil(og_grade*1.1) returns 56.0000
Note that og_grade is always a whole number.
I tried the ceil(x) function, but for some reason when x in already
round, it rounds it up to x+1
No that would mean that the implementation of ceil was defective. Not impossible but extremely unlikely.
It's likely that the x for which this effect is observed is in fact not integral, and the decimal portion is omitted from the formatting or debugger.
Assuming IEEE754, the closest double to 1.1 is slightly larger than that; this most likely accounts for your result.
In your case, given that op_grade is in fact a whole number, your best bet is to use an int for op_grade, and multiply by 11 instead; the subsequent rounding checks are then both trivial and exact.

How to round a float to particular number of digits after decimal point? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a function to round a float in C or do I need to write my own?
Rounding Number to 2 Decimal Places in C
I am looking for a function in c which could round my float variable to another float variable with two digits after the decimal point, help and demonstrate please.
thank you
Update:
I need this function for calculation purpose, not for printing.
you may try something like this:
float a = 1.234568;
float b = ((int) a*100.f) / 100.f;
instead of (int) you may use floor()/ceil() according to your requirements.

Resources