C Data Types: Are int x=3 & float y=3.0 Equal?

  • Thread starter Thread starter fraz ali
  • Start date Start date
  • Tags Tags
    Data
AI Thread Summary
In C, when comparing different data types, such as an integer and a float, the integer is implicitly converted to a float for the comparison. For example, in the statement `if (x == y)` where `int x = 3` and `float y = 3.0`, both values are treated as equal due to this conversion. However, caution is advised when testing floating-point values for strict equality due to potential precision issues inherent in floating-point representation. It is recommended to use a tolerance approach, checking if the absolute difference between two floats is less than a small threshold. This is particularly important in programming contexts where precision is critical, such as financial applications. The discussion also highlights that floating-point specifications have evolved, but many implementations may not fully adhere to the latest standards, which can affect equality comparisons. Overall, while the comparison may return "equal" under certain conditions, the nuances of floating-point arithmetic necessitate careful handling to avoid unexpected results.
fraz ali
Messages
4
Reaction score
2
Hello everyone!
In C language,does two different variables with different data types but equal values equate each other?
For example,are these two statements equal:
C:
int x=3;
float y=3.0;
?
thanks in advance..
 
Last edited by a moderator:
Technology news on Phys.org
When you compare an int x with a float y (a tricky thing to do), as in
Code:
if (x==y)
the int is implicitly converted to float before the comparison is done.
 
  • Like
Likes fraz ali and Dave Ritche
Samy_A said:
the int is implicitly converted to float before the comparison is done.

One should be very careful about testing two floats for strict equality. If a and b are equal, a/5 and b*0.2 might not be.
 
  • Like
Likes Samy_A
Vanadium 50 said:
One should be very careful about testing two floats for strict equality. If a and b are equal, a/5 and b*0.2 might not be.
Absolutely.
Sometimes I had to test for a==b, and it only worked if I tested for abs(a-b) < some sufficiently small number. Don't know if there is a more elegant way to solve this.
 
But will they be equal?
Code:
#include<conio.h>
#include<stdio.h>
int x=3;
float y=3.0;
if (x==y)
{
printf("X is equal to Y");
}
else
{
printf("not equal");
}
getch();
}
What will this program return?
 
Last edited by a moderator:
Samy_A said:
When you compare an int x with a float y (a tricky thing to do), as in
Code:
if (x==y)
the int is implicitly converted to float before the comparison is done.
thanks
 
Last edited by a moderator:
  • Like
Likes Dave Ritche
Equality of floating point datatypes (IEEE-754) is a pain in the butt. Most folks handle this incorrectly when they start to program.

And yes, what you are talking about is called a 'promotion' - FWIW I do not know why - it looks more like a 'demotion' to me. Doubles and floats are promoted to integers, so as explained above, you run the risk of getting incorrect answers.

You need to investigate the concept of an ULP and probably the idea behind the fenv.h "functions": fesetround and fegetround. Promoting implictly rounds values.
This is a technical paper on the subject:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
A less mathematically daunting article:
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

This whole subject is one the main reasons COBOL is still prominent in financial code - you lose precision with floating point. This gives auditors nightmares.

Floating point specs were updated in IEEE-754-2008, but the changes not seem to be available on most commodity x86 implementations. Fujitsu SPARC 10 cpus have it. Some graphics cards( GPUS ) support it through add-ons like CUDA.
 
Dave Ritche said:
But will they be equal?
<code>
#include<conio.h>
#include<stdio.h>
int x=3;
float y=3.0;
if (x==y)
{
printf("X is equal to Y");
}
else
{
printf("not equal");
}
getch();
}
</code>
What will this program return?
My C++ compiler returns "X is equal to Y".
I don't know whether this is compiler dependent.
 
fraz ali said:
Hello everyone!
In C language,does two different variables with different data types but equal values equate each other?
For example,are these two statements equal:
C:
int x=3;
float y=3.0;
The values stored in x and y do not have equal values, based on the bit patterns that are stored in the two different memory locations.
jim mcnamara said:
And yes, what you are talking about is called a 'promotion' - FWIW I do not know why - it looks more like a 'demotion' to me. Doubles and floats are promoted to integers, so as explained above, you run the risk of getting incorrect answers.
Jim, this is not true. Assuming you are talking about the comparison if (x == y), since the value in y is float, the the int value of x is promoted to a float as well, and then the comparison can proceed. doubles and floats are never "promoted" to ints, which would likely cause loss of precision, but in circumstances such as the one below, floating point values can be demoted to integral values.
C:
int num = 3.6;
In the declaration/definition above, the double value 3.6 is truncated to 3 to initialize the variable, num.
 
  • #10
@Mark44 Yup - you are correct - I have it backwards. Thanks for the correction.
 
  • #11
Dave Ritche said:
What will this program return?

It's implementation dependent. My understanding is that if your implementation follows the IEEE-754 standards, it will return "equal". If you replace float y = 3.0 with float y = 3, it should always return equal: it is comparing "the float that 3 gets promoted to" with "the float that 3 gets promoted to", and such should always be true. That number may or may not be the same as the float 3.0. (IEEE says it is)

Many profs will say "never test floats for strict equality". I think a better way to think about it is "testing floats for strict equality is almost never what you want to do". If you test two floats for equality, you are asking if the two numbers are close enough to each other as to have the same binary representation. So it's not equality, it's a range - and if it's a range, shouldn't the programmer decide what that range should be, and not leave it to the compiler?
 
  • Like
Likes Ibix
Back
Top