Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::real128 Class Reference

#include <real128.hpp>

Public Member Functions

 real128 (uint64_t integer=0)
 
 real128 (const std::string &str)
 
 operator std::string () const
 
real128operator+= (const real128 &o)
 
real128operator-= (const real128 &o)
 
real128operator/= (const real128 &o)
 
real128operator*= (const real128 &o)
 
uint64_t to_uint64 () const
 

Static Public Member Functions

static real128 from_fixed (const uint128 &fixed)
 

Friends

real128 operator* (real128 a, const real128 &b)
 
real128 operator/ (real128 a, const real128 &b)
 
real128 operator+ (real128 a, const real128 &b)
 
real128 operator- (real128 a, const real128 &b)
 

Detailed Description

Provides fixed point math operations based on decimal fractions with 18 places. Delegates to fc::bigint for multiplication and division.

Definition at line 14 of file real128.hpp.

Constructor & Destructor Documentation

◆ real128() [1/2]

fc::real128::real128 ( uint64_t integer = 0)

Definition at line 14 of file real128.cpp.

15 {
16 fixed = uint128(integer) * FC_REAL128_PRECISION;
17 }
#define FC_REAL128_PRECISION
Definition real128.hpp:4
Here is the caller graph for this function:

◆ real128() [2/2]

fc::real128::real128 ( const std::string & str)
explicit

Definition at line 53 of file real128.cpp.

54 {
55 const char* c = ratio_str.c_str();
56 int digit = *c - '0';
57 if (digit >= 0 && digit <= 9)
58 {
59 uint64_t int_part = digit;
60 ++c;
61 digit = *c - '0';
62 while (digit >= 0 && digit <= 9)
63 {
64 int_part = int_part * 10 + digit;
65 ++c;
66 digit = *c - '0';
67 }
68 *this = real128(int_part);
69 }
70 else
71 {
72 // if the string doesn't look like "123.45" or ".45", this code isn't designed to parse it correctly
73 // in particular, we don't try to handle leading whitespace or '+'/'-' indicators at the beginning
74 FC_ASSERT(*c == '.');
75 fixed = fc::uint128();
76 }
77
78
79 if (*c == '.')
80 {
81 c++;
82 digit = *c - '0';
83 if (digit >= 0 && digit <= 9)
84 {
85 int64_t frac_part = digit;
86 int64_t frac_magnitude = 10;
87 ++c;
88 digit = *c - '0';
89 while (digit >= 0 && digit <= 9)
90 {
91 frac_part = frac_part * 10 + digit;
92 frac_magnitude *= 10;
93 ++c;
94 digit = *c - '0';
95 }
96 *this += real128( frac_part ) / real128( frac_magnitude );
97 }
98 }
99 }
real128(uint64_t integer=0)
Definition real128.cpp:14
an implementation of 128 bit unsigned integer
Definition uint128.hpp:22
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
signed __int64 int64_t
Definition stdint.h:135
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the call graph for this function:

Member Function Documentation

◆ from_fixed()

real128 fc::real128::from_fixed ( const uint128 & fixed)
static

Definition at line 114 of file real128.cpp.

115 {
116 real128 result;
117 result.fixed = fixed;
118 return result;
119 }

◆ operator std::string()

fc::real128::operator std::string ( ) const

Definition at line 100 of file real128.cpp.

101 {
102 std::stringstream ss;
103 ss << std::string(fixed / FC_REAL128_PRECISION);
104 ss << '.';
105 auto frac = (fixed % FC_REAL128_PRECISION) + FC_REAL128_PRECISION;
106 ss << std::string( frac ).substr(1);
107
108 auto number = ss.str();
109 while( number.back() == '0' ) number.pop_back();
110
111 return number;
112 }
static const Segment ss(Segment::ss)

◆ operator*=()

real128 & fc::real128::operator*= ( const real128 & o)

Definition at line 42 of file real128.cpp.

43 { try {
44 fc::bigint self(fixed);
45 fc::bigint other(o.fixed);
46 self *= other;
48 fixed = self;
49 return *this;
50 } FC_CAPTURE_AND_RETHROW( (*this)(o) ) }
#define FC_CAPTURE_AND_RETHROW(...)
@ self
the connection is to itself
Definition protocol.hpp:48

◆ operator+=()

real128 & fc::real128::operator+= ( const real128 & o)

Definition at line 18 of file real128.cpp.

19 {
20 fixed += o.fixed;
21 return *this;
22 }

◆ operator-=()

real128 & fc::real128::operator-= ( const real128 & o)

Definition at line 23 of file real128.cpp.

24 {
25 fixed -= o.fixed;
26 return *this;
27 }

◆ operator/=()

real128 & fc::real128::operator/= ( const real128 & o)

Definition at line 29 of file real128.cpp.

30 { try {
31 FC_ASSERT( o.fixed > uint128(0), "Divide by Zero" );
32
33 fc::bigint self(fixed);
34 fc::bigint other(o.fixed);
36 self /= other;
37 fixed = self;
38
39 return *this;
40 } FC_CAPTURE_AND_RETHROW( (*this)(o) ) }

◆ to_uint64()

uint64_t fc::real128::to_uint64 ( ) const

Definition at line 9 of file real128.cpp.

10 {
11 return (fixed/ FC_REAL128_PRECISION).to_uint64();
12 }

Friends And Related Symbol Documentation

◆ operator*

real128 operator* ( real128 a,
const real128 & b )
friend

Definition at line 21 of file real128.hpp.

21{ a *= b; return a; }
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181

◆ operator+

real128 operator+ ( real128 a,
const real128 & b )
friend

Definition at line 23 of file real128.hpp.

23{ a += b; return a; }

◆ operator-

real128 operator- ( real128 a,
const real128 & b )
friend

Definition at line 24 of file real128.hpp.

24{ a -= b; return a; }

◆ operator/

real128 operator/ ( real128 a,
const real128 & b )
friend

Definition at line 22 of file real128.hpp.

22{ a /= b; return a; }

The documentation for this class was generated from the following files: