3 ms·
#include <stdio.h> int main() { float a = 1.0f; float b = 2.0f; auto c = a + b; printf("%lu\n", sizeof(c)); return
by pcwalton 13y ago
#include <stdio.h>
int main() {
float a = 1.0f;
float b = 2.0f;
auto c = a + b;
printf("%lu\n", sizeof(c));
return 0;
}
$ clang++ test.cpp
$ ./a.out
4
This is a perfect example of how C++'s floating point conversion rules are so complex nobody knows them!
- acqq 13y agoHave you tried float a = 1, b = 7; auto c = a / b? It should give you 8 (I don't have clang here). Note that in the example you constructed you have practically a constant which doesn't lose precision by staying as 32 bits? Have you also checked compiler options that affect the computations? The proper numerics is sometimes (often!) not default, but C and C++ compilers are not "one size fits all."
- pcwalton 13y agoTried that, also in -std=c++11 mode, still 4… Edit: By default clang++ has FLT_EVAL_METHOD set to 0 (i.e. Rust's behavior). When I tried with -mno-sse FLT_EVAL_METHOD was set to 2 (long double promotion), but the sizeof was still 4.
- dllthomas 13y agog++ seems to duplicate your clang results, for the record (as more or less expected).
- acqq 13y agoNow you know the pain of the people who do numerics. C99 should use 8 bytes for partial results at least the real ones (not the constants computable by the compiler). You should see that in the debugger too. C++ is unfortunately fully another topic. I also admit that I don't know if clang implemented numerics properly, I've never used it. This looks suspicious and at least conceptually wrong: it loses the precision "just because." After something like: fld dword ptr a fdiv dword ptr b there is a full 64-bit result in the FPU waiting to be stored even if the recent compiler implementers think "32-bits should be enough for everybody." That's that "bridges-can-fall-once dangerous" thing I've mentioned. I've also learned something: "auto" seems to be more dangerous than I expected! Wow.