Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the string representation of floating point numbers #214

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/StringFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "StringFunctions.h"

#include <cassert>
#include <locale>

namespace e57
Expand All @@ -14,35 +15,37 @@ namespace e57
ss << std::scientific << std::setprecision( precision ) << value;

// Try to remove trailing zeroes and decimal point
// E.g. 1.23456000000000000e+005 ==> 1.23456e+005
// E.g. 2.00000000000000000e+005 ==> 2e+005
// e.g. 1.23456000000000000e+005 ==> 1.23456e+005
// e.g. 2.00000000000000000e+005 ==> 2e+005

std::string s = ss.str();
const size_t len = s.length();

// Split into mantissa and exponent
// E.g. 1.23456000000000000e+005 ==> "1.23456000000000000" + "e+005"
std::string mantissa = s.substr( 0, len - 5 );
std::string exponent = s.substr( len - 5, 5 );
// e.g. 1.23456000000000000e+005 ==> "1.23456000000000000" + "e+005"
auto index = s.find_last_of( 'e' );
assert( index != std::string::npos ); // should not be possible

std::string mantissa = s.substr( 0, index );
const std::string exponent = s.substr( index );

// Double check that we understand the formatting
if ( exponent[0] == 'e' )
{
// Trim of any trailing zeros in mantissa
while ( mantissa[mantissa.length() - 1] == '0' )
// Trim trailing zeros from mantissa
while ( mantissa.back() == '0' )
{
mantissa = mantissa.substr( 0, mantissa.length() - 1 );
mantissa.pop_back();
}

// Make one attempt to trim off trailing decimal point
if ( mantissa[mantissa.length() - 1] == '.' )
// Trim trailing decimal point if possible
if ( mantissa.back() == '.' )
{
mantissa = mantissa.substr( 0, mantissa.length() - 1 );
mantissa.pop_back();
}

// Reassemble whole floating point number
// Check if can drop exponent.
if ( exponent == "e+000" )
if ( ( exponent == "e+00" ) || ( exponent == "e+000" ) )
{
s = mantissa;
}
Expand Down
24 changes: 20 additions & 4 deletions test/src/test_StringFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,37 @@ TEST( StringFunctions, FloatToStrConversion )
{
const auto converted = e57::floatingPointToStr<float>( 3.14159265f, 7 );

ASSERT_EQ( converted, "3.1415927e+00" );
ASSERT_EQ( converted, "3.1415927" );
}

// https://github.com/asmaloney/libE57Format/issues/147
TEST( StringFunctions, FloatToStrTrimDecimal )
{
const auto converted = e57::floatingPointToStr<float>( 42.0f, 5 );

ASSERT_EQ( converted, "4.2e+01" );
}

// https://github.com/asmaloney/libE57Format/issues/147
TEST( StringFunctions, FloatToStrTrimExponent )
{
const auto converted = e57::floatingPointToStr<float>( 4.0f, 5 );

ASSERT_EQ( converted, "4" );
}

TEST( StringFunctions, DoubleToStrConversion )
{
const auto converted = e57::floatingPointToStr<double>( 3.141592653589793238, 17 );

ASSERT_EQ( converted, "3.14159265358979312e+00" );
ASSERT_EQ( converted, "3.14159265358979312" );
}

TEST( StringFunctions, FloatToStrConversion2 )
{
const auto converted = e57::floatingPointToStr<float>( 123456.0f, 7 );

ASSERT_EQ( converted, "1.2345600e+05" );
ASSERT_EQ( converted, "1.23456e+05" );
}

// Related to https://github.com/asmaloney/libE57Format/issues/172
Expand All @@ -45,7 +61,7 @@ TEST( StringFunctions, FloatToStrLocale )

const auto converted = e57::floatingPointToStr<float>( 123456.0f, 7 );

ASSERT_EQ( converted, "1.2345600e+05" );
ASSERT_EQ( converted, "1.23456e+05" );

std::locale::global( std::locale::classic() );
}