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

  • Thread starter fraz ali
  • Start date
  • Tags
    Data
In summary, floating point values can be demoted to integral values, which can cause loss of precision.
  • #1
fraz ali
4
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
  • #2
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
  • #3
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
  • #4
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.
 
  • #5
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:
  • #6
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
  • #7
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.
 
  • #8
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.
 
  • #9
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

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

1. Are int and float data types interchangeable?

No, int and float data types are not interchangeable. They are two distinct data types with different properties and uses. Integers (int) are whole numbers with no decimal places, while floating-point numbers (float) can have decimal places.

2. Is it possible to convert an int variable to a float variable?

Yes, it is possible to convert an int variable to a float variable. This process is known as typecasting and can be done by using the float() function in C. However, it is important to note that the converted float value may not be exactly the same as the original integer value due to the differences in precision between the two data types.

3. Can an int variable store a float value?

No, an int variable cannot store a float value. Integers do not have the capacity to store decimal numbers, so any float value assigned to an int variable will be rounded down to the nearest whole number. This can result in loss of precision and inaccurate calculations.

4. What is the difference between int and float in terms of memory usage?

Integers typically use less memory than floating-point numbers. In C, an int variable uses 4 bytes of memory while a float variable uses 8 bytes. This is because floating-point numbers require more bits to represent decimal values compared to integers which only need bits to represent whole numbers.

5. Which data type is more suitable for storing numeric values with decimal places?

Floating-point numbers (float) are more suitable for storing numeric values with decimal places. Integers (int) can only store whole numbers, so any decimal values assigned to an int variable will be rounded down. Float variables, on the other hand, have the capacity to store decimal values with a higher level of precision.

Similar threads

Replies
10
Views
2K
Replies
5
Views
3K
Replies
30
Views
4K
Replies
4
Views
6K
Replies
10
Views
4K
Replies
7
Views
1K
Replies
19
Views
3K
Replies
5
Views
2K
Back
Top