Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
real128.cpp
Go to the documentation of this file.
1#include <fc/real128.hpp>
4#include <sstream>
5#include <stdint.h>
6
7namespace fc
8{
10 {
11 return (fixed/ FC_REAL128_PRECISION).to_uint64();
12 }
13
15 {
16 fixed = uint128(integer) * FC_REAL128_PRECISION;
17 }
19 {
20 fixed += o.fixed;
21 return *this;
22 }
24 {
25 fixed -= o.fixed;
26 return *this;
27 }
28
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) ) }
41
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) ) }
51
52
53 real128::real128( const std::string& ratio_str )
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 }
100 real128::operator std::string()const
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 }
113
115 {
116 real128 result;
117 result.fixed = fixed;
118 return result;
119 }
120
121 void to_variant( const real128& var, variant& vo )
122 {
123 vo = std::string(var);
124 }
125 void from_variant( const variant& var, real128& vo )
126 {
127 vo = real128(var.as_string());
128 }
129
130} // namespace fc
real128(uint64_t integer=0)
Definition real128.cpp:14
real128 & operator*=(const real128 &o)
Definition real128.cpp:42
real128 & operator+=(const real128 &o)
Definition real128.cpp:18
real128 & operator-=(const real128 &o)
Definition real128.cpp:23
real128 & operator/=(const real128 &o)
Definition real128.cpp:29
static real128 from_fixed(const uint128 &fixed)
Definition real128.cpp:114
uint64_t to_uint64() const
Definition real128.cpp:9
an implementation of 128 bit unsigned integer
Definition uint128.hpp:22
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
string as_string() const
Definition variant.cpp:469
Defines exception's used by fc.
#define FC_CAPTURE_AND_RETHROW(...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
namespace sysio::chain
Definition authority.cpp:3
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
#define FC_REAL128_PRECISION
Definition real128.hpp:4
signed __int64 int64_t
Definition stdint.h:135
unsigned __int64 uint64_t
Definition stdint.h:136