4 ms·
These examples all seem to be using double precision floating point, which might be misleading. For example, the C++ snippet is adding two double literals, not
by unfamiliar 10y ago
These examples all seem to be using double precision floating point, which might be misleading. For example, the C++ snippet is adding two double literals, not floating point literals. The result is even more inaccurate using single precision arithmetic:
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setprecision(17) << 0.1 + 0.2 << '\n'; // 0.30000000000000004
std::cout << std::setprecision(17) << 0.1f + 0.2f << '\n'; // 0.30000001192092896
}