mercredi 3 août 2016

C++11 numeric_limits

When I compiled this C++ code I didn't expect to see this output

#include <iostream>
#include <iomanip>
#include <limits>

int main() {
    const long double ldMinFloat = std::numeric_limits<float>::lowest();
    std::cout << std::left << std::setw(20) << "ldMinFloat" << "= " << std::fixed << ldMinFloat << std::endl;
    std::cout << std::left << std::setw(20) << "(ldMinFloat - 10)" << "= " << std::fixed << (ldMinFloat - 10) << std::endl;
return 0;
    return 0;
}

Here is the output

ldMinFloat          = -340282346638528859811704183484516925440.000000
(ldMinFloat - 10)   = -340282346638528859811704183484516925440.000000

Can someone be kind enough to explain why the subtraction is not -340282346638528859811704183484516925450.000000???

Based on this link long double max value is +/- 1.797,693,134,862,315,7*10^308 and I don't really understand why the mantis would explain this behaviour in basic integer arithmetic? or is it the implicit conversion from float to long double? or it's the operator << of std::cout?

Any idea to help me feel less stupid before going to sleep?

Aucun commentaire:

Enregistrer un commentaire