root/software/fixed/fixed_util.cc @ 42048db0
| c7e0510c | David Sorber | #include <cmath>
|
||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include "fixed.h"
|
||||
#include "int128.h"
|
||||
| ef18eb33 | David Sorber | #define USE_INT128 1
|
||
| c7e0510c | David Sorber | // g++ fixed_test.cc -o fixed_test
|
||
int main(int argc, char** argv)
|
||||
{
|
||||
std::cout << "Fixed Type Test Util\n" << std::endl;
|
||||
| 30849ea7 | David Sorber | |||
| ef18eb33 | David Sorber | fixed_128_128 value;
|
||
const char* input = "-0.31415926535897932384626433832795028841";
|
||||
value.parse(input);
|
||||
std::cout << "[0] Value: " << value << std::endl;
|
||||
value = value * 0;
|
||||
std::cout << "[1] Value: " << value << std::endl;
|
||||
#if 0
|
||||
| 30849ea7 | David Sorber | fixed_64_64 val;
|
||
fixed_64_64 val2;
|
||||
val.parse("0.314159263538979");
|
||||
std::cout << "[1] Val: " << val << std::endl;
|
||||
val2 = val * 10;
|
||||
std::cout << "[2] Val: " << val2 << std::endl;
|
||||
val2 = val * 86;
|
||||
std::cout << "[3] Val: " << val2 << std::endl;
|
||||
| ef18eb33 | David Sorber | |||
std::cout << "\n--------------------------------------------------------------------\n" << std::endl;
|
||||
fixed_128_128 val3;
|
||||
fixed_128_128 val4;
|
||||
val3.parse("0.314159263538979");
|
||||
std::cout << "[1] Val: " << val3 << std::endl;
|
||||
val4 = val3 * 10;
|
||||
std::cout << "[2] Val: " << val4 << std::endl;
|
||||
val4 = val3 * 86;
|
||||
std::cout << "[3] Val: " << val4 << std::endl;
|
||||
#endif
|
||||
| c7e0510c | David Sorber | |||
| 30849ea7 | David Sorber | #if 0
|
||
| 852d0468 | David Sorber | //-------------------------------------------------------------------------
|
||
| c7e0510c | David Sorber | fixed_64_64 foobar;
|
||
std::cout << "Type size: " << sizeof(fixed_64_64) << std::endl;
|
||||
std::cout << "Integer decimal digits: " << fixed_64_64::integer_decimal_digits << std::endl;
|
||||
std::cout << "Fractional decimal digits: " << fixed_64_64::fractional_decimal_digits << std::endl;
|
||||
std::cout << "Max integer value: " << fixed_64_64::MAX_INTEGER_VALUE << std::endl;
|
||||
std::cout << "Max fractional value: " << fixed_64_64::MAX_FRACTIONAL_VALUE << std::endl;
|
||||
std::cout << std::endl;
|
||||
| 852d0468 | David Sorber | std::cout << "Value: " << foobar << "\n\n" << std::endl;
|
||
//-------------------------------------------------------------------------
|
||||
#ifdef USE_INT128
|
||||
fixed_128_128 barfoo;
|
||||
std::cout << "Type size: " << sizeof(barfoo) << std::endl;
|
||||
std::cout << "Integer decimal digits: " << fixed_128_128::integer_decimal_digits << std::endl;
|
||||
std::cout << "Fractional decimal digits: " << fixed_128_128::fractional_decimal_digits << std::endl;
|
||||
std::cout << "Max integer value: " << fixed_128_128::MAX_INTEGER_VALUE << std::endl;
|
||||
std::cout << "Max fractional value: " << fixed_128_128::MAX_FRACTIONAL_VALUE << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "Value: " << barfoo << "\n\n" << std::endl;
|
||||
#endif
|
||||
//-------------------------------------------------------------------------
|
||||
| c7e0510c | David Sorber | |||
fixed_64_64 quxbar(3, 1415926353);
|
||||
std::cout << "Value: " << quxbar << std::endl;
|
||||
fixed_64_64 intMax(999999999999999999ULL);
|
||||
std::cout << "Value: " << intMax << std::endl;
|
||||
try
|
||||
{
|
||||
fixed_64_64 intOverflow(1999999999999999999ULL);
|
||||
std::cout << "Value: " << intMax << std::endl;
|
||||
}
|
||||
catch (const std::out_of_range& excep)
|
||||
{
|
||||
std::cout << "Integer overflow caught" << std::endl;
|
||||
}
|
||||
fixed_64_64 fracMax(0, 9999999999999999999ULL);
|
||||
std::cout << "Value: " << fracMax << std::endl;
|
||||
try
|
||||
{
|
||||
fixed_64_64 fracOverflow(0, 10000000000000000001ULL);
|
||||
}
|
||||
catch (const std::out_of_range& excep)
|
||||
{
|
||||
std::cout << "Fractional overflow caught" << std::endl;
|
||||
}
|
||||
fixed_64_64 value1;
|
||||
fixed_64_64 value2;
|
||||
const char* strVal = "100.1";
|
||||
value1.parse(strVal);
|
||||
std::cout << "String value: " << strVal << std::endl;
|
||||
std::cout << "Parsed value: " << value1 << std::endl;
|
||||
strVal = "100";
|
||||
value2.parse(strVal);
|
||||
std::cout << "String value: " << strVal << std::endl;
|
||||
std::cout << "Parsed value: " << value2 << std::endl;
|
||||
std::cout << value1 << " == " << value2 << " --> " << (value1 == value2) << std::endl;
|
||||
std::cout << value1 << " != " << value2 << " --> " << (value1 != value2) << std::endl;
|
||||
std::cout << value1 << " > " << value2 << " --> " << (value1 > value2) << std::endl;
|
||||
std::cout << value1 << " < " << value2 << " --> " << (value1 < value2) << std::endl;
|
||||
std::cout << value1 << " >= " << value2 << " --> " << (value1 >= value2) << std::endl;
|
||||
std::cout << value1 << " <= " << value2 << " --> " << (value1 <= value2) << std::endl;
|
||||
//~ double foo = std::pow(10, 18);
|
||||
//~ std::cout << "Foo: " << std::fixed << std::setprecision(0) << foo << std::endl;
|
||||
//~ std::cout << "Foo: " << std::fixed << std::setprecision(0) << (static_cast<uint64_t>(foo) - 1) << std::endl;
|
||||
uint128_t big = 1098298217598174ULL;
|
||||
std::cout << "BIG INT: " << big << std::endl;
|
||||
big *= 1098982694765;
|
||||
std::cout << "BIGGER INT: " << big << std::endl;
|
||||
std::cout << "INT128T MAX: " << std::numeric_limits<int128_t>::max() << std::endl;
|
||||
std::cout << "UINT128T MAX: " << std::numeric_limits<uint128_t>::max() << std::endl;
|
||||
strVal = "-1207010734831637608463359110";
|
||||
int128_t s128Val = strtoll_128(strVal, nullptr, 0);
|
||||
std::cout << "String value: " << strVal << std::endl;
|
||||
std::cout << "Parsed value: " << s128Val << std::endl;
|
||||
//~ strVal = "991207010734831637608463359110888";
|
||||
strVal = "340282366920938463463374607431768211456";
|
||||
uint128_t u128Val = strtoull_128(strVal, nullptr, 0);
|
||||
std::cout << "String value: " << strVal << std::endl;
|
||||
std::cout << "Parsed value: " << u128Val << std::endl;
|
||||
| 30849ea7 | David Sorber | #endif
|
||
| c7e0510c | David Sorber | |||
return 0;
|
||||
}
|