3 ms·
OK, so we agree that this is serious, and we need to make sure that programmers understand whether 32-bit or 64-bit arithmetic is being performed, lest the brid
by pcwalton 13y ago
OK, so we agree that this is serious, and we need to make sure that programmers understand whether 32-bit or 64-bit arithmetic is being performed, lest the bridge collapses.
In Rust a programmer familiar with the rules would write it this way, making it clear what's happening:
bridge.d = (bridge.a as f64 * bridge.b as f64 + bridge.c as f64) as f32;
Taking advantage of the implicit conversions, a programmer familiar with C++'s rules might write:
bridge.d = bridge.a * bridge.b + bridge.c;
Now suppose that, years later, someone comes along and wants to refactor the code by adding a temporary. In Rust:
let tmp = bridge.a as f64 * bridge.b as f64;
bridge.d = (tmp + bridge.c as f64) as f32;
In C++:
auto tmp = bridge.a * bridge.b;
bridge.d = tmp + bridge.c;
And now they've changed the meaning of the code in a serious way, because adding a temporary silently changes the meaning of the code.
- acqq 13y agoIt seems you missed something: auto tmp = bridge.a * bridge.b; The type of the right hand expression is 64-bit FP by definition we discuss, so "auto" means "double" in your example. Only if you do simple assignment the type remains 32-bit, every "+" "-" "*" etc promotes it to 64. It can actually be implemented in your language too and the rest of the compiler will keep functioning.
- 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.