Consider the below C++ program and predict the output.
The output of above program is false if the “IEEE754 32-bit single float type” is used by compiler. If we define:
We are basically trying to assign a 32-bit integer (signed or unsigned) to a 32-bit float. The compiler will first convert the integer 0xffffffff to a nearest 32-bit float, and the memory representation of the float f is not the same as the integer 0xffffffff. We can see the above values by printing f and x.
f = 4.29497e+09 x = 4294967295
Even if we copy the memory directly, for example, we have an integer (value equal to 0xffffffff), and we copy over the content (memory values). Since the 0xffffffff in IEEE754 is not a valid float number, so if you compare this invalid representation to itself, it is not equal.
This article is contributed by Rishav Raj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Comparison of a float with a value in C
- Explicitly assigning port number to client in Socket
- Assigning multiple characters in an int in C language
- Comparison of static keyword in C++ and Java
- Results of comparison operations in C and C++
- Comparison of boolean data type in C++ and Java
- Comparison of Exception Handling in C++ and Java
- How to check whether a number is in the range[low, high] using one comparison ?
- Ratio Manipulations in C++ | Set 2 (Comparison)
- Comparison of Java with other programming languages
- Comparison of Python with Other Programming Languages
- 3-way comparison operator (Space Ship Operator) in C++ 20
- Brief Overview & Comparison of Object-Oriented Programming from C to Java
- Function Overloading and float in C++
- Difference between float and double in C/C++
- C/C++ program to find the size of int, float, double and char
- gcvt() | Convert float value to string in C
- Modulus of two float or double numbers
- <cfloat> float.h in C/C++ with Examples
- Integer literal in C/C++ (Prefixes and Suffixes)