+ 4

Union

#include <stdio.h> int main() { union f { int a; double fl; }; union f U; U.a = 5; printf("%f", U.fl); return 0; } Why does it output 0.00000?

28th Dec 2018, 10:35 AM
ShortCode
+ 8
Why didn't nobody mention that `int` and `float` are incompatible types and their bitfield get interpreted differently and that's why the output became zero? 32-bit FP field +#+########+####################+ | S | Exponent | Fraction | +#+########+####################+ 32-bit Integer field +#+#############################+ | S | Magnitude | +#+#############################+ _____ https://en.wikipedia.org/wiki/Single-precision_floating-point_format https://www.dummies.com/programming/c/the-real-difference-between-integers-and-floating-point-values/
28th Dec 2018, 11:35 AM
Babak
+ 7
"they will still be 0 because of the interpretation" It produces different `representation` in different implementations. For example, setting the U.fl = 154543.5005; in VC++, and `printf("%d", U.a);` yields 17179869 Which is the decimal representation of the binary value of `154543.5005` Âš Conversly, setting the U.a = 1128; and printf("%f", U.fl); yields -92559592117444873187641221127133740829952104046278057318678528.000000 Which is the FP representation of the binary value of `1128` Â˛ (The above actual value might be 0.000000000000000000000000001) _____ Âš Verification: https://rextester.com/DTTXK12756 Â˛ Verification: https://rextester.com/YECDT81643
28th Dec 2018, 12:27 PM
Babak
+ 6
Ok Ketan Lalcheta. Understand it now.
28th Dec 2018, 10:55 AM
ShortCode
+ 5
A union allows to store different data types in the same memory location. It is like a structure because it has members. However, a union variable uses the same memory location for all its member's and only one member at a time can occupy the memory location. When assignment is performed, the union memory location will be used for that member until another member assignment is performed. Trying to access a member that isn't occupying the memory location gives unexpected results. https://www.sololearn.com/learn/C/2944/
28th Dec 2018, 10:46 AM
Sekiro
+ 5
ShortCode in addition to what Sekiro has said, just try below: printf("%d",U.fl); and it would print assigned value of a i.e. 5. FL and a refers same location and fl is having value same as a until we overwrite value... just change %d instead of %f
28th Dec 2018, 10:53 AM
Ketan Lalcheta
+ 4
Yup! As I said, it has an atomic value like 0.0000000000000000001 and the increment from the above value to the next value ( 0.0000000000000000002) cannot be achieved by unit step increment. FP numbers spectrum -Inf |~~~~~|~~~~|~~~|~~|~||~|~~|~~~|~~~~|~~~~~| +Inf Integer's -Inf |~~~~~|~~~~~|~~~~~||~~~~~|~~~~~|~~~~~| +Inf _______ http://mirror.informatimago.com/next/developer.apple.com/documentation/mac/PPCNumerics/PPCNumerics-18.html
28th Dec 2018, 1:15 PM
Babak
+ 4
Glad to see you again Gordie Last night I was messing with intrinsics again! I just wanted to know if it's possible to accumulate 2 int64_t in the lower and higher part of an __m128i and then add them up as one __m128i, and finally retrieve the big integer byte-by-byte to the output?
29th Dec 2018, 5:28 AM
Babak
+ 3
What's the problems with float? int -> char (able) int -> float (0.0000)
28th Dec 2018, 10:51 AM
ShortCode
+ 2
So C++ Soldier (Babak), even if they have binary form, they will still be 0 because of the interpretation?
28th Dec 2018, 11:49 AM
ShortCode
+ 2
So, C++ Soldier (Babak) its not 0? https://code.sololearn.com/csBvoJdHLcpC/?ref=app I see all the fp higher than 0 except 0.
28th Dec 2018, 12:46 PM
ShortCode