|
| 1 | +#ifndef OPENSMT_FLOAT_H |
| 2 | +#define OPENSMT_FLOAT_H |
| 3 | + |
| 4 | +//- !!! |
| 5 | +#include "InternalException.h" |
| 6 | + |
| 7 | +#include <string> |
| 8 | +#include <concepts> |
| 9 | +#include <cassert> |
| 10 | +#include <cmath> |
| 11 | + |
| 12 | +namespace opensmt { |
| 13 | + class Float { |
| 14 | + public: |
| 15 | + struct Hash; |
| 16 | + |
| 17 | + using Value = double; |
| 18 | + |
| 19 | + static_assert(std::floating_point<Value>); |
| 20 | + |
| 21 | + Float() = default; |
| 22 | + ~Float() = default; |
| 23 | + Float(Float const &) = default; |
| 24 | + Float(Float &&) = default; |
| 25 | + Float & operator =(Float const &) = default; |
| 26 | + Float & operator =(Float &&) = default; |
| 27 | + |
| 28 | + constexpr Float(Value) noexcept; |
| 29 | + constexpr Float(std::floating_point auto val) : Float(Value(val)) { } |
| 30 | + constexpr Float(std::integral auto val) : Float(Value(val)) { } |
| 31 | + explicit constexpr Float(std::integral auto den, std::integral auto num); |
| 32 | + explicit Float(char const *); |
| 33 | + |
| 34 | + Value value() const noexcept { return _value; } |
| 35 | + Value & value() noexcept { return _value; } |
| 36 | + |
| 37 | + int sign() const { |
| 38 | + auto const val = value(); |
| 39 | + if (val > 0) return 1; |
| 40 | + if (val < 0) return -1; |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + bool isZero() const { return value() == 0; } |
| 45 | + bool isOne() const { return value() == 1; } |
| 46 | + |
| 47 | + bool isInteger() const { |
| 48 | + auto const val = value(); |
| 49 | + return val == std::trunc(val); |
| 50 | + } |
| 51 | + |
| 52 | + bool operator ==(Float const & other) const { return value() == other.value(); } |
| 53 | + bool operator !=(Float const & other) const { return value() != other.value(); } |
| 54 | + bool operator < (Float const & other) const { return value() < other.value(); } |
| 55 | + bool operator > (Float const & other) const { return value() > other.value(); } |
| 56 | + bool operator <=(Float const & other) const { return value() <= other.value(); } |
| 57 | + bool operator >=(Float const & other) const { return value() >= other.value(); } |
| 58 | + |
| 59 | + int compare(Float const & other) const { |
| 60 | + if (*this > other) return 1; |
| 61 | + if (*this < other) return -1; |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + Float operator -() const { return -value(); } |
| 66 | + Float operator +() const { return value(); } |
| 67 | + Float operator +(Float const & other) const { return value() + other.value(); } |
| 68 | + Float operator -(Float const & other) const { return value() - other.value(); } |
| 69 | + Float operator *(Float const & other) const { return value() * other.value(); } |
| 70 | + Float operator /(Float const & other) const { return value() / other.value(); } |
| 71 | + Float & operator +=(Float const & other) { value() += other.value(); return *this; } |
| 72 | + Float & operator -=(Float const & other) { value() -= other.value(); return *this; } |
| 73 | + Float & operator *=(Float const & other) { value() *= other.value(); return *this; } |
| 74 | + Float & operator /=(Float const & other) { value() /= other.value(); return *this; } |
| 75 | + |
| 76 | + //- !!!! |
| 77 | + Float operator %(Float const & other) const { throw InternalException("forbidden"); } |
| 78 | + |
| 79 | + Float abs() const { |
| 80 | + return std::abs(value()); |
| 81 | + } |
| 82 | + |
| 83 | + Float inverse() const { |
| 84 | + return 1./value(); |
| 85 | + } |
| 86 | + |
| 87 | + Float ceil() const { |
| 88 | + return std::ceil(value()); |
| 89 | + } |
| 90 | + |
| 91 | + Float floor() const { |
| 92 | + return std::floor(value()); |
| 93 | + } |
| 94 | + |
| 95 | + void negate() & { |
| 96 | + if (isZero()) return; |
| 97 | + value() = -value(); |
| 98 | + assert(normalized(value())); |
| 99 | + } |
| 100 | + |
| 101 | + void reset() & noexcept { |
| 102 | + value() = 0; |
| 103 | + } |
| 104 | + |
| 105 | + double get_d() const { return value(); } |
| 106 | + |
| 107 | + std::string get_str() const; |
| 108 | + void print(std::ostream &) const; |
| 109 | + friend std::ostream & operator <<(std::ostream & os, Float const & x) { |
| 110 | + x.print(os); |
| 111 | + return os; |
| 112 | + } |
| 113 | + |
| 114 | + //- !!!!!!! |
| 115 | + Float get_den() const { throw InternalException("forbidden"); } |
| 116 | + protected: |
| 117 | + static constexpr bool normalized(Value) noexcept; |
| 118 | + static constexpr Value normalize(Value) noexcept; |
| 119 | + private: |
| 120 | + Value _value; |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +namespace opensmt { |
| 125 | + struct Float::Hash { |
| 126 | + std::size_t operator ()(Float const & x) const noexcept { |
| 127 | + return std::hash<Float::Value>{}(x.value()); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + constexpr Float::Float(Value val) noexcept : _value{normalize(val)} { } |
| 132 | + |
| 133 | + constexpr Float::Float(std::integral auto den, std::integral auto num) : Float(Value(den)/Value(num)) { |
| 134 | + assert(normalized(value())); |
| 135 | + } |
| 136 | + |
| 137 | + constexpr bool Float::normalized(Value val) noexcept { |
| 138 | + return !std::signbit(val) || val < 0; |
| 139 | + } |
| 140 | + |
| 141 | + constexpr Float::Value Float::normalize(Value val) noexcept { |
| 142 | + static_assert(0. == -0.); |
| 143 | + if (val == 0) { |
| 144 | + static_assert(normalized(0)); |
| 145 | + return 0; |
| 146 | + } |
| 147 | + assert(normalized(val)); |
| 148 | + return val; |
| 149 | + } |
| 150 | + |
| 151 | + inline Float abs(Float const & x) { return x.abs(); } |
| 152 | + |
| 153 | + inline int cmpabs(Float const & a, Float const & b) { |
| 154 | + return abs(a).compare(abs(b)); |
| 155 | + }; |
| 156 | + |
| 157 | + inline void addition (Float & dst, Float const & a, Float const & b) { dst = a + b; } |
| 158 | + inline void subtraction (Float & dst, Float const & a, Float const & b) { dst = a - b; } |
| 159 | + inline void multiplication(Float & dst, Float const & a, Float const & b) { dst = a * b; } |
| 160 | + inline void division (Float & dst, Float const & a, Float const & b) { dst = a / b; } |
| 161 | + inline void additionAssign (Float & a, Float const & b) { a += b; } |
| 162 | + inline void subtractionAssign (Float & a, Float const & b) { a -= b; } |
| 163 | + inline void multiplicationAssign(Float & a, Float const & b) { a *= b; } |
| 164 | + inline void divisionAssign (Float & a, Float const & b) { a /= b; } |
| 165 | + |
| 166 | + //- !!!!!!! |
| 167 | + inline Float gcd (Float const &, Float const &) { throw InternalException("forbidden"); } |
| 168 | + inline Float lcm (Float const &, Float const &) { throw InternalException("forbidden"); } |
| 169 | + inline Float fastrat_fdiv_q(Float const &, Float const &) { throw InternalException("forbidden"); } |
| 170 | +} |
| 171 | + |
| 172 | +namespace std { |
| 173 | + template <> |
| 174 | + struct hash<opensmt::Float> : opensmt::Float::Hash {}; |
| 175 | +} |
| 176 | + |
| 177 | +#endif //OPENSMT_FLOAT_H |
0 commit comments