+ 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?
10 Respostas
+ 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/
+ 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
+ 6
Ok Ketan Lalcheta. Understand it now.
+ 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/
+ 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
+ 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?
+ 3
What's the problems with float?
int -> char (able)
int -> float (0.0000)
+ 2
So C++ Soldier (Babak), even if they have binary form, they will still be 0 because of the interpretation?
+ 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.