Given the following...
void test(){
float a = 0.7f;
LOGD("Width %.1f",0.7f);
LOGD("Width %.1f",a);
fark(a);
}
void fark(float test){
LOGD("Width %.1f",test);
}
this outputs....
05-18 22:35:25.215: D/Native(8241): Width 0.7
05-18 22:35:25.215: D/Native(8241): Width 0.7
05-18 22:35:25.215: D/Native(8241): Width 36893488147419103232.0
what am I missing about the last one?
You need to declare fark before using it. As specified in section 6.5.2.2, paragraph 6:
If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type
floatare promoted todouble. These are called the default argument promotions.
(bold emphasis added by me).
Note that defining fark with a type incompatible with the implicitly assumed type is a constraint violation and requires the compiler to emit a diagnostic message if the call and definition are in the same translation unit. gcc only warns then, but clang rejects the code [error: conflicting types for 'fark']. If call and definition are in different translation units, the compiler can of course not diagnose the mistake, but calling a function through an expression with incompatible type invokes undefined behaviour in any case.
When the value 0.7f is converted to double (I'm assuming float using the IEEE754 32-bit representation, and double the IEEE754 64-bit one), you get a value of
0.699999988079071
whose bit-pattern (in hexadecimal notation) is
0x3FE6666660000000
(the biased exponent is 1022, corresponding to an effective exponent of -1, the significand is 1.6666660000000).
That is passed - on the stack or in a register - to fark.
When fark reads 32 bits from that representation - since it expects a float per its definition -, depending on how the double is passed, it may read the higher-order 32 bits or the lower-order 32.
It did read in this case the lower-order 32 bits, resulting in the float value with the bit-pattern
0x60000000
which has a biased exponent of 0xC0 = 192, corresponding to the unbiased exponent 192 - 127 = 65 and significand 1.000000. In other words, that is the float representation of
2^65 = 36893488147419103232
which is the value printed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With