Function opensn::ReadDecimalNumber

Function Documentation

bool opensn::ReadDecimalNumber(std::istream &stream, double &value)

Reads a decimal floating-point number from a stream, with the same result on every platform.

Reading a double with operator>> gives different results with different C++ standard libraries. libc++ (macOS) also takes hexadecimal digits, so “0.01abc” fails and “0x1p3” reads as 8, and it refuses subnormal values such as 1e-310. libstdc++ (Linux) reads “0.01abc” as 0.01 and “0x1p3” as 0, leaving the rest in the stream, and reads subnormal values. This function reads as libstdc++ does, on every platform.

It skips leading whitespace and takes the longest prefix of the form [+-] digits [. digits] [(e|E) [+-] digits], with an exponent only after a mantissa digit. The value is the correctly rounded conversion of that prefix in the C locale. A value that underflows reads as a subnormal value or zero. The function returns false and sets failbit when the prefix is not a number or the value overflows, and then leaves value unchanged. The prefix is consumed either way.