|
| 1 | +package kosgo |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + kos_mobile "github.com/klever-io/kos-rs/packages/kos-go/kos_mobile" |
| 7 | +) |
| 8 | + |
| 9 | +type Sign = kos_mobile.Sign |
| 10 | + |
| 11 | +const ( |
| 12 | + Minus = kos_mobile.SignMinus |
| 13 | + NoSign = kos_mobile.SignNoSign |
| 14 | + Plus = kos_mobile.SignPlus |
| 15 | +) |
| 16 | + |
| 17 | +type BigNumber struct { |
| 18 | + inner kos_mobile.BigNumber |
| 19 | +} |
| 20 | + |
| 21 | +func NewBigNumber(value string) (*BigNumber, error) { |
| 22 | + bn, err := kos_mobile.BigNumberNew(value) |
| 23 | + if err != nil { |
| 24 | + return nil, fmt.Errorf("invalid number: %w", err) |
| 25 | + } |
| 26 | + return &BigNumber{inner: bn}, nil |
| 27 | +} |
| 28 | + |
| 29 | +func NewBigNumberZero() *BigNumber { |
| 30 | + return &BigNumber{inner: kos_mobile.BigNumberNewZero()} |
| 31 | +} |
| 32 | + |
| 33 | +func (b *BigNumber) String() string { |
| 34 | + if b == nil { |
| 35 | + return "" |
| 36 | + } |
| 37 | + return kos_mobile.BigNumberString(b.inner) |
| 38 | +} |
| 39 | + |
| 40 | +func Add(lhs, rhs *BigNumber) (*BigNumber, error) { |
| 41 | + if lhs == nil || rhs == nil { |
| 42 | + return nil, fmt.Errorf("nil BigNumber operand") |
| 43 | + } |
| 44 | + result, err := kos_mobile.BigNumberAdd(lhs.inner, rhs.inner) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + return &BigNumber{inner: result}, nil |
| 49 | +} |
| 50 | + |
| 51 | +func Sub(lhs, rhs *BigNumber) (*BigNumber, error) { |
| 52 | + if lhs == nil || rhs == nil { |
| 53 | + return nil, fmt.Errorf("nil BigNumber operand") |
| 54 | + } |
| 55 | + result, err := kos_mobile.BigNumberSubtract(lhs.inner, rhs.inner) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + return &BigNumber{inner: result}, nil |
| 60 | +} |
| 61 | + |
| 62 | +func Mul(lhs, rhs *BigNumber) (*BigNumber, error) { |
| 63 | + if lhs == nil || rhs == nil { |
| 64 | + return nil, fmt.Errorf("nil BigNumber operand") |
| 65 | + } |
| 66 | + result, err := kos_mobile.BigNumberMultiply(lhs.inner, rhs.inner) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + return &BigNumber{inner: result}, nil |
| 71 | +} |
| 72 | + |
| 73 | +func Div(lhs, rhs *BigNumber) (*BigNumber, error) { |
| 74 | + if lhs == nil || rhs == nil { |
| 75 | + return nil, fmt.Errorf("nil BigNumber operand") |
| 76 | + } |
| 77 | + result, err := kos_mobile.BigNumberDivide(lhs.inner, rhs.inner) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + return &BigNumber{inner: result}, nil |
| 82 | +} |
| 83 | + |
| 84 | +func Pow(base, exp *BigNumber) (*BigNumber, error) { |
| 85 | + if base == nil || exp == nil { |
| 86 | + return nil, fmt.Errorf("nil BigNumber operand") |
| 87 | + } |
| 88 | + result, err := kos_mobile.BigNumberPow(base.inner, exp.inner) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + return &BigNumber{inner: result}, nil |
| 93 | +} |
| 94 | + |
| 95 | +func IsEqual(lhs, rhs *BigNumber) bool { |
| 96 | + if lhs == nil || rhs == nil { |
| 97 | + return false |
| 98 | + } |
| 99 | + return kos_mobile.BigNumberIsEqual(lhs.inner, rhs.inner) |
| 100 | +} |
| 101 | + |
| 102 | +func IsGt(lhs, rhs *BigNumber) bool { |
| 103 | + if lhs == nil || rhs == nil { |
| 104 | + return false |
| 105 | + } |
| 106 | + return kos_mobile.BigNumberIsGt(lhs.inner, rhs.inner) |
| 107 | +} |
| 108 | + |
| 109 | +func IsGte(lhs, rhs *BigNumber) bool { |
| 110 | + if lhs == nil || rhs == nil { |
| 111 | + return false |
| 112 | + } |
| 113 | + return kos_mobile.BigNumberIsGte(lhs.inner, rhs.inner) |
| 114 | +} |
| 115 | + |
| 116 | +func IsLt(lhs, rhs *BigNumber) bool { |
| 117 | + if lhs == nil || rhs == nil { |
| 118 | + return false |
| 119 | + } |
| 120 | + return kos_mobile.BigNumberIsLt(lhs.inner, rhs.inner) |
| 121 | +} |
| 122 | + |
| 123 | +func IsLte(lhs, rhs *BigNumber) bool { |
| 124 | + if lhs == nil || rhs == nil { |
| 125 | + return false |
| 126 | + } |
| 127 | + return kos_mobile.BigNumberIsLte(lhs.inner, rhs.inner) |
| 128 | +} |
| 129 | + |
| 130 | +func Abs(b *BigNumber) (*BigNumber, error) { |
| 131 | + if b == nil { |
| 132 | + return nil, fmt.Errorf("nil BigNumber operand") |
| 133 | + } |
| 134 | + result, err := kos_mobile.BigNumberAbsolute(b.inner) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + return &BigNumber{inner: result}, nil |
| 139 | +} |
| 140 | + |
| 141 | +func IsZero(b *BigNumber) bool { |
| 142 | + if b == nil { |
| 143 | + return false |
| 144 | + } |
| 145 | + return kos_mobile.BigNumberIsZero(b.inner) |
| 146 | +} |
| 147 | + |
| 148 | +func Increment(b *BigNumber) (*BigNumber, error) { |
| 149 | + if b == nil { |
| 150 | + return nil, fmt.Errorf("nil BigNumber operand") |
| 151 | + } |
| 152 | + result, err := kos_mobile.BigNumberIncrement(b.inner) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + return &BigNumber{inner: result}, nil |
| 157 | +} |
| 158 | + |
| 159 | +func Decrement(b *BigNumber) (*BigNumber, error) { |
| 160 | + if b == nil { |
| 161 | + return nil, fmt.Errorf("nil BigNumber operand") |
| 162 | + } |
| 163 | + result, err := kos_mobile.BigNumberDecrement(b.inner) |
| 164 | + if err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + return &BigNumber{inner: result}, nil |
| 168 | +} |
| 169 | + |
| 170 | +func IsPositive(b *BigNumber) bool { |
| 171 | + if b == nil { |
| 172 | + return false |
| 173 | + } |
| 174 | + return kos_mobile.BigNumberIsPositive(b.inner) |
| 175 | +} |
| 176 | + |
| 177 | +func IsNegative(b *BigNumber) bool { |
| 178 | + if b == nil { |
| 179 | + return false |
| 180 | + } |
| 181 | + return kos_mobile.BigNumberIsNegative(b.inner) |
| 182 | +} |
0 commit comments