2015-02-09 23:55:58 +00:00
|
|
|
#pragma once
|
2018-03-13 20:13:45 +00:00
|
|
|
#include <fc/uint128.hpp>
|
2014-10-15 21:00:49 +00:00
|
|
|
|
2015-02-03 16:31:20 +00:00
|
|
|
#define FC_REAL128_PRECISION (uint64_t(1000000) * uint64_t(1000000) * uint64_t(1000000))
|
|
|
|
|
|
2014-10-15 21:00:49 +00:00
|
|
|
namespace fc {
|
|
|
|
|
class variant;
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-07 18:47:13 +00:00
|
|
|
* Provides fixed point math operations based on decimal fractions
|
|
|
|
|
* with 18 places.
|
|
|
|
|
* Delegates to fc::bigint for multiplication and division.
|
2014-10-15 21:00:49 +00:00
|
|
|
*/
|
|
|
|
|
class real128
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-10-16 00:46:24 +00:00
|
|
|
real128( uint64_t integer = 0);
|
|
|
|
|
explicit real128( const std::string& str );
|
2014-10-15 21:00:49 +00:00
|
|
|
operator std::string()const;
|
|
|
|
|
|
|
|
|
|
friend real128 operator * ( real128 a, const real128& b ) { a *= b; return a; }
|
|
|
|
|
friend real128 operator / ( real128 a, const real128& b ) { a /= b; return a; }
|
|
|
|
|
friend real128 operator + ( real128 a, const real128& b ) { a += b; return a; }
|
|
|
|
|
friend real128 operator - ( real128 a, const real128& b ) { a -= b; return a; }
|
|
|
|
|
|
|
|
|
|
real128& operator += ( const real128& o );
|
|
|
|
|
real128& operator -= ( const real128& o );
|
|
|
|
|
real128& operator /= ( const real128& o );
|
|
|
|
|
real128& operator *= ( const real128& o );
|
2018-03-13 20:13:45 +00:00
|
|
|
|
2015-02-03 16:22:02 +00:00
|
|
|
static real128 from_fixed( const uint128& fixed );
|
2014-10-15 21:00:49 +00:00
|
|
|
|
2014-10-16 00:46:24 +00:00
|
|
|
uint64_t to_uint64()const;
|
2014-10-15 21:00:49 +00:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint128 fixed;
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-19 16:35:57 +00:00
|
|
|
void to_variant( const real128& var, variant& vo, uint32_t max_depth = 1 );
|
|
|
|
|
void from_variant( const variant& var, real128& vo, uint32_t max_depth = 1 );
|
2014-10-15 21:00:49 +00:00
|
|
|
|
2018-03-13 20:13:45 +00:00
|
|
|
namespace raw
|
2015-02-09 23:55:58 +00:00
|
|
|
{
|
|
|
|
|
template<typename Stream>
|
2018-03-13 20:13:45 +00:00
|
|
|
inline void pack( Stream& s, const real128& value_to_pack, uint32_t _max_depth=FC_PACK_MAX_DEPTH )
|
|
|
|
|
{ s.write( (char*)&value_to_pack, sizeof(value_to_pack) ); }
|
|
|
|
|
|
2015-02-09 23:55:58 +00:00
|
|
|
template<typename Stream>
|
2018-03-13 20:13:45 +00:00
|
|
|
inline void unpack( Stream& s, real128& value_to_unpack, uint32_t _max_depth=FC_PACK_MAX_DEPTH )
|
|
|
|
|
{ s.read( (char*)&value_to_unpack, sizeof(value_to_unpack) ); }
|
2015-02-09 23:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-15 21:00:49 +00:00
|
|
|
} // namespace fc
|