-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#ifndef NJOY_TOOLS_DISCO_SCIENTIFIC | ||
#define NJOY_TOOLS_DISCO_SCIENTIFIC | ||
|
||
// system includes | ||
#include <sstream> | ||
#include <iomanip> | ||
|
||
// other includes | ||
#include "fast_float/fast_float.h" | ||
#include "tools/disco/Real.hpp" | ||
|
||
namespace njoy { | ||
namespace tools { | ||
namespace disco { | ||
|
||
/** | ||
* @brief A class for reading and writing fixed width data fields containing floating | ||
* point values | ||
*/ | ||
template < unsigned int Width, unsigned int Precision > | ||
class FixedPoint : public Real< Width > { | ||
|
||
/* fields */ | ||
|
||
public: | ||
|
||
using Real< Width >::read; | ||
|
||
template< typename Representation, typename Iterator > | ||
static void write( const Representation& value, Iterator& iter ) { | ||
|
||
const double absValue = std::abs( value ); | ||
|
||
std::ostringstream buffer; | ||
buffer << std::right << std::setw( Width ); | ||
|
||
if ( absValue == std::numeric_limits< Representation >::max() ) { | ||
|
||
if ( value < 0 ) { | ||
|
||
buffer << "-Inf"; | ||
} | ||
else { | ||
|
||
buffer << "Inf"; | ||
} | ||
} | ||
else { | ||
|
||
unsigned int precision = Width - 1; | ||
if ( value < 0 ) { | ||
|
||
precision -= 1; | ||
} | ||
precision = std::min( precision, Precision ); | ||
|
||
buffer << std::fixed | ||
<< std::uppercase | ||
<< std::setprecision( precision ) | ||
<< value; | ||
} | ||
|
||
for ( auto b : buffer.str() ) { | ||
|
||
*iter++ = b; | ||
} | ||
} | ||
}; | ||
|
||
} // disco namespace | ||
} // tools namespace | ||
} // njoy namespace | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_cpp_test( disco.FixedPoint FixedPoint.test.cpp ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// include Catch2 | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
// what we are testing | ||
#include "tools/disco/FixedPoint.hpp" | ||
|
||
// other includes | ||
|
||
// convenience typedefs | ||
using namespace njoy::tools::disco; | ||
|
||
SCENARIO( "FixedPoint" ) { | ||
|
||
double value; | ||
std::string buffer; | ||
auto iter = std::back_inserter( buffer ); | ||
|
||
value = 0; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " 0.00000" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = 2; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " 2.00000" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = 10; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " 10.00000" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = 3.14159265359; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " 3.14159" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = 0.69314718056; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " 0.69315" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
// value = 1e-100; | ||
// buffer = ""; | ||
// iter = std::back_inserter( buffer ); | ||
// FixedPoint< 12, 5 >::write( value, iter ); | ||
// CHECK( "1.00000E-100" == buffer ); | ||
// CHECK( 12 == buffer.size() ); | ||
// | ||
// value = 1e+100; | ||
// buffer = ""; | ||
// iter = std::back_inserter( buffer ); | ||
// FixedPoint< 12, 5 >::write( value, iter ); | ||
// CHECK( "1.00000E+100" == buffer ); | ||
// CHECK( 12 == buffer.size() ); | ||
|
||
value = -2; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " -2.00000" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = -10; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " -10.00000" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = -3.14159265359; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " -3.14159" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = -0.69314718056; | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " -0.69315" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = std::numeric_limits< double >::max(); | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " Inf" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
|
||
value = -std::numeric_limits< double >::max(); | ||
buffer = ""; | ||
iter = std::back_inserter( buffer ); | ||
FixedPoint< 12, 5 >::write( value, iter ); | ||
CHECK( " -Inf" == buffer ); | ||
CHECK( 12 == buffer.size() ); | ||
} // SCENARIO |