number hold by double container is greater than float even if values are equal [duplicate] - c

This question already has answers here:
Floating point comparison [duplicate]
(5 answers)
Closed 9 years ago.
main()
{
float f=0.7;
if(0.7>f)
printf("Hi");
else
printf("Hello"):
}
When I compile this program output comes out to be Hi.
Can someone please explain the scenerio how 0.7>0.7 is true? Is this due to fact that 0.7 used in if statement is a double and f is a float?
Even if it is double still its value is 0.7 difference created is just that in case of double it is stored in 8 byte while in case of float it is stored in 4 byte. But I think it does not matters how big is your container when the value stored in container is equal. So 0.7 can never be greater than f. So according to me it should result in Hello. then why Hi is the output?

The container does makes the difference here. The idea is how these values are stored and then retrieved. If you use a number that can be converted back to exact decimal value. You may see the difference.

Use this and see the output:
float f=0.7f;
If you define float f = 0.7, it is stored as manttisa and exponent. It depends on the precision. So in this case .7 will be less than .7. If you define 0.7f it will be equal to 0.7.

Related

Two floating point addition is not changing result [duplicate]

This question already has answers here:
Why does adding a small float to a large float just drop the small one?
(4 answers)
Integers and float precision
(9 answers)
epsilon for various float values
(1 answer)
Closed 5 years ago.
#include<stdio.h>
int main()
{
int loopCounter = 0;
float data1,data2;
data1 = 2000.0f;
data2 = 0.0001f;
while(1)
{
data1 = data1 + data2;
printf("Loop Counter %d , Data %f\n",loopCounter,data1);
loopCounter++;
}
return 0;
}
I am running this code on Linux machine using the GCC compiler but if the addition of the 2 float reaches the value 2048.0 it does not change anymore.
Does anyone have an idea why this is happening?
For any floating point value, in combination with a (much) smaller adding delta, there is a value from which adding the delta results in a value which is so "similar" to the previous one, that the representation in float is identical.
Your code is practically tailor-made to find that value.
So, while at first adding delta to 2000 and a few following values results in visible changes, your code will sooner or later (quite soon actually) reach the special value. Which I assume in your case is 2048.
2048.0 and 2048.0001 have no different representation in float. Hence the adding has no effect on the variable.
Anyone has idea why this is happening?
Because of floating point precision.
Read Ranges of floating point datatype in C?
Notice that:
2000.0001
already exceeds the limit a float can store (~7 digits for IEEE-754 single precision).
What happens in your case is that the precision is limited, which results in the "halting" issue you report. In other words, after some point, the adition doesn't have any actual effect (which is reflected to the user).
By that I mean that the new sum is so close to the previous sum, that their float reprecentation is the same, causing data1 to store the same value.
In your machine, this value is 2048 and all the rest additions by a small δ do not affect the float representation of data1.

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

Not getting expected value when running a program [duplicate]

This question already has answers here:
What decides what datatype that will be used to store the temporary value in?
(3 answers)
Closed 7 years ago.
int a=10;
float b;
printf("the no\n");
b=((10-1)/12)*50;
printf("b value is %f",b);
return 0;
But when I calculate the b value in scientific calc we get b=40. And my question is why it shows b=0 when I run my code
In the calculation, the expression ((10-1)/12) is a division of an integer by an integer. The first integer evaluates to 9, and since 9/12 is less than 1, 9/12 evaluates to 0. This 0 is then multiplied by 50 to give b = 0.
To make the division act like a float, make one of the constants in the division portion a float. You can do this several ways the shortest of which is to add "f" as a suffix to one of the numbers, e.g. use 10F instead of 10, to get ((10F-1)/12). This makes the subtraction a floating point operation, which make the division and then multiplication of your original expression floating point operations as well. This should then give you the expected (float) result.
There will be a loss of data when you try to perform integer division. As already explained,
b=((10-1)/12)*50;
will yield 0 because :
((10-1)/12)*50; = (9/12)*50; = (0)*50; = 0;
To prevent this data loss, you can do the following:
((10-1)/12.0)*50;
Basically, by 12.0, it will be read as a float, so appropriately, floating point division will be performed.

Why does this C computation involving exponents produce the wrong answer? [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 9 years ago.
I am trying to implement the below formula on C
Here is my code:
int function(int x){
return pow(10, (((x-1)/(253/3))-1));
}
int main(void){
int z = function(252);
printf("z: %d\n",z);
return 0;
}
it outputs 10. However a calculator outputs 94.6.
could anyone explain me what I am doing wrong?
Note that in this line
(((x-1)/(253/3))-1))
You are dividing the integer value x - 1 by an integer value 253 / 3. This will truncate the value to an int, meaning that you'll be raising an integer power to an integer power.
To fix this, try changing this expression to
(((x-1)/(253.0 / 3.0))-1))
This now will use doubles in the expression, giving you the value you want.
Hope this helps!
Adding to that, integers do not give you the decimal part of numbers, so a number like 3.5 will get cut down to 3 using integer. To fix this double is the way to go. In other words 3 is different than 3.0

Float and double output [duplicate]

This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Closed 9 years ago.
Please explain the output for different cases
#include<stdio.h>
int main()
{
float a=5.9; //a=0.9
if (a==5.9)
printf("Equal");
else if (a<5.9)
printf("Less than");
else
printf("Greater than");
return 0;
}
When a is 5.9 the output is "Greater than", when a is 0.9 the output is "Less than". Why?
In C, the literal “5.9” has type double and is converted by common compilers to 5.9000000000000003552713678800500929355621337890625, because they use binary IEEE-754 floating point, and that number is the value representable as a double that is closest to 5.9. Your code initializes a float with that value. That requires converting a double to a float, and the result is 5.900000095367431640625, because that is the float value that is closest to the double value. Obviously, the latter is greater than the former, which is why your output is “Greater than”.
With .9, the literal “.9” is converted to the double 0.90000000000000002220446049250313080847263336181640625. Then that double is converted to the float 0.89999997615814208984375, which is obviously less than the double.
Your variable is a float but the constant is a double. Since that value can't be stored precisely ti will be stored differently as a float and as a double. If you use the same data types you'll get the desired results
http://codepad.org/1q5mwFGd
http://codepad.org/Q4lOQnG8
Floating point numbers are inherently imprecise. For a quick introduction, you can read up a bit here. http://floating-point-gui.de/errors/comparison/
For some suggestions on effective comparison strategies, see this post. What is the most effective way for float and double comparison?
This is because internally, the floating point numbers are stored in binary form and 5.9 can't be represented exactly as 5.9. Have a look at the following similar question and answer: Why does a C floating-point type modify the actual input of 125.1 to 125.099998 on output?
When comparing floating-point numbers it is best to avoid '=='.
This because some values cannot be correctly stored without some loss of precision.
So it is better to say:
if( fabs(a-5.9) < .0001 ) {
// 'EQUAL' or at least near enough!
}
I know this takes more to compute, but it will behave the way you expect.
Due to decimal binary conversion, 0.9 represented as a summation of powers of 2^-1:
0.9 - (0.5 + 0.25 + 0.125) = 0.025
But 0.025 is not an exact power of 2^-1. So you need to represent both numbers in the comparison with the same precision. Since float and double have different precision they compare not equal.

Resources