diff --git a/lttoolbox/compression.cc b/lttoolbox/compression.cc index 9efed809..7fb557c2 100644 --- a/lttoolbox/compression.cc +++ b/lttoolbox/compression.cc @@ -289,6 +289,16 @@ Compression::long_multibyte_write(const double& value, FILE *output) unsigned int mantissa = static_cast(static_cast(0x40000000 * frexp(value, &exp))); unsigned int exponent = static_cast(static_cast(exp)); + if (std::isinf(value)) { + mantissa = std::numeric_limits::max(); + if (value < 0) { + exponent = std::numeric_limits::max() - 1; + } + else { + exponent = std::numeric_limits::max(); + } + } + if(mantissa < 0x04000000) { multibyte_write(mantissa, output); @@ -326,6 +336,16 @@ Compression::long_multibyte_write(const double& value, std::ostream &output) unsigned int mantissa = static_cast(static_cast(0x40000000 * frexp(value, &exp))); unsigned int exponent = static_cast(static_cast(exp)); + if (std::isinf(value)) { + mantissa = std::numeric_limits::max(); + if (value < 0) { + exponent = std::numeric_limits::max() - 1; + } + else { + exponent = std::numeric_limits::max(); + } + } + if(mantissa < 0x04000000) { multibyte_write(mantissa, output); @@ -393,7 +413,17 @@ Compression::long_multibyte_read(FILE *input) } double value = static_cast(static_cast(mantissa)) / 0x40000000; - result = ldexp(value, static_cast(exponent)); + if (mantissa == std::numeric_limits::max() && exponent >= std::numeric_limits::max() - 1) { + if (exponent == std::numeric_limits::max() - 1) { + result = -1.0*std::numeric_limits::infinity(); + } + else { + result = std::numeric_limits::infinity(); + } + } + else { + result = ldexp(value, static_cast(exponent)); + } return result; } @@ -436,7 +466,17 @@ Compression::long_multibyte_read(std::istream &input) } double value = static_cast(static_cast(mantissa)) / 0x40000000; - result = ldexp(value, static_cast(exponent)); + if (mantissa == std::numeric_limits::max() && exponent >= std::numeric_limits::max() - 1) { + if (exponent == std::numeric_limits::max() - 1) { + result = -1.0*std::numeric_limits::infinity(); + } + else { + result = std::numeric_limits::infinity(); + } + } + else { + result = ldexp(value, static_cast(exponent)); + } return result; } diff --git a/lttoolbox/string_utils.cc b/lttoolbox/string_utils.cc index d51213a6..59b30a0e 100644 --- a/lttoolbox/string_utils.cc +++ b/lttoolbox/string_utils.cc @@ -4,6 +4,7 @@ #include #include #include +#include UString StringUtils::trim(const UString& str) @@ -132,6 +133,14 @@ StringUtils::stod(const UString& str) { double ret; int c = u_sscanf(str.c_str(), "%lf", &ret); + if (str.size() == 3 && str[0] == 'i' && str[1] == 'n' && str[2] == 'f') { + ret = std::numeric_limits::infinity(); + c = 1; + } + if (str.size() == 4 && str[0] == '-' && str[1] == 'i' && str[2] == 'n' && str[3] == 'f') { + ret = -1*std::numeric_limits::infinity(); + c = 1; + } if (c != 1) { throw std::invalid_argument("unable to parse float"); }