Skip to content

Commit

Permalink
Support infinite weights in lt-comp (see #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
TinoDidriksen committed Jul 22, 2022
1 parent e32f92a commit b860ffa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
44 changes: 42 additions & 2 deletions lttoolbox/compression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ Compression::long_multibyte_write(const double& value, FILE *output)
unsigned int mantissa = static_cast<unsigned int>(static_cast<int>(0x40000000 * frexp(value, &exp)));
unsigned int exponent = static_cast<unsigned int>(static_cast<int>(exp));

if (std::isinf(value)) {
mantissa = std::numeric_limits<unsigned int>::max();
if (value < 0) {
exponent = std::numeric_limits<unsigned int>::max() - 1;
}
else {
exponent = std::numeric_limits<unsigned int>::max();
}
}

if(mantissa < 0x04000000)
{
multibyte_write(mantissa, output);
Expand Down Expand Up @@ -326,6 +336,16 @@ Compression::long_multibyte_write(const double& value, std::ostream &output)
unsigned int mantissa = static_cast<unsigned int>(static_cast<int>(0x40000000 * frexp(value, &exp)));
unsigned int exponent = static_cast<unsigned int>(static_cast<int>(exp));

if (std::isinf(value)) {
mantissa = std::numeric_limits<unsigned int>::max();
if (value < 0) {
exponent = std::numeric_limits<unsigned int>::max() - 1;
}
else {
exponent = std::numeric_limits<unsigned int>::max();
}
}

if(mantissa < 0x04000000)
{
multibyte_write(mantissa, output);
Expand Down Expand Up @@ -393,7 +413,17 @@ Compression::long_multibyte_read(FILE *input)
}

double value = static_cast<double>(static_cast<int>(mantissa)) / 0x40000000;
result = ldexp(value, static_cast<int>(exponent));
if (mantissa == std::numeric_limits<unsigned int>::max() && exponent >= std::numeric_limits<unsigned int>::max() - 1) {
if (exponent == std::numeric_limits<unsigned int>::max() - 1) {
result = -1.0*std::numeric_limits<double>::infinity();
}
else {
result = std::numeric_limits<double>::infinity();
}
}
else {
result = ldexp(value, static_cast<int>(exponent));
}

return result;
}
Expand Down Expand Up @@ -436,7 +466,17 @@ Compression::long_multibyte_read(std::istream &input)
}

double value = static_cast<double>(static_cast<int>(mantissa)) / 0x40000000;
result = ldexp(value, static_cast<int>(exponent));
if (mantissa == std::numeric_limits<unsigned int>::max() && exponent >= std::numeric_limits<unsigned int>::max() - 1) {
if (exponent == std::numeric_limits<unsigned int>::max() - 1) {
result = -1.0*std::numeric_limits<double>::infinity();
}
else {
result = std::numeric_limits<double>::infinity();
}
}
else {
result = ldexp(value, static_cast<int>(exponent));
}

return result;
}
9 changes: 9 additions & 0 deletions lttoolbox/string_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <unicode/uchar.h>
#include <unicode/ustring.h>
#include <iostream>
#include <limits>

UString
StringUtils::trim(const UString& str)
Expand Down Expand Up @@ -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<double>::infinity();
c = 1;
}
if (str.size() == 4 && str[0] == '-' && str[1] == 'i' && str[2] == 'n' && str[3] == 'f') {
ret = -1*std::numeric_limits<double>::infinity();
c = 1;
}
if (c != 1) {
throw std::invalid_argument("unable to parse float");
}
Expand Down

0 comments on commit b860ffa

Please sign in to comment.