android ndk - C: Passing floating point to function -
given following...
void test(){ float = 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 missing last one?
you need declare fark
before using it. specified in section 6.5.2.2, paragraph 6:
if expression denotes called function has type not include prototype, integer promotions performed on each argument, and arguments have type
float
promoteddouble
. these called default argument promotions.
(bold emphasis added me).
note defining fark
type incompatible implicitly assumed type constraint violation , requires compiler emit diagnostic message if call , definition in same translation unit. gcc warns then, clang rejects code [error: conflicting types 'fark'
]. if call , definition in different translation units, compiler can of course not diagnose mistake, calling function through expression incompatible type invokes undefined behaviour in case.
when value 0.7f
converted double
(i'm assuming float
using ieee754 32-bit representation, , double
ieee754 64-bit one), value of
0.699999988079071
whose bit-pattern (in hexadecimal notation) is
0x3fe6666660000000
(the biased exponent 1022, corresponding effective exponent of -1
, significand 1.6666660000000
).
that passed - on stack or in register - fark
.
when fark
reads 32 bits representation - since expects float
per definition -, depending on how double
passed, may read higher-order 32 bits or lower-order 32.
it did read in case lower-order 32 bits, resulting in float
value bit-pattern
0x60000000
which has biased exponent of 0xc0 = 192
, corresponding unbiased exponent 192 - 127 = 65
, significand 1.000000
. in other words, float
representation of
2^65 = 36893488147419103232
which value printed.
Comments
Post a Comment