fix(rxNum/rxInt/rxDouble): Delegate all non mutable operations to the original objects
Showing
2 changed files
with
594 additions
and
78 deletions
| @@ -3,6 +3,8 @@ import 'dart:collection'; | @@ -3,6 +3,8 @@ import 'dart:collection'; | ||
| 3 | 3 | ||
| 4 | import '../rx_core/rx_interface.dart'; | 4 | import '../rx_core/rx_interface.dart'; | 
| 5 | 5 | ||
| 6 | +part 'rx_num.dart'; | ||
| 7 | + | ||
| 6 | /// global object that registers against `GetX` and `Obx`, and allows the | 8 | /// global object that registers against `GetX` and `Obx`, and allows the | 
| 7 | /// reactivity | 9 | /// reactivity | 
| 8 | /// of those `Widgets` and Rx values. | 10 | /// of those `Widgets` and Rx values. | 
| @@ -15,22 +17,6 @@ class _RxImpl<T> implements RxInterface<T> { | @@ -15,22 +17,6 @@ class _RxImpl<T> implements RxInterface<T> { | ||
| 15 | 17 | ||
| 16 | T _value; | 18 | T _value; | 
| 17 | 19 | ||
| 18 | - /// Common to all Types [T], this operator overloading is using for | ||
| 19 | - /// assignment, same as rx.value | ||
| 20 | - /// | ||
| 21 | - /// Example: | ||
| 22 | - /// ``` | ||
| 23 | - /// var counter = 0.obs ; | ||
| 24 | - /// counter <<= 3; // same as counter.value=3; | ||
| 25 | - /// print(counter); // calls .toString() now | ||
| 26 | - /// ``` | ||
| 27 | - /// | ||
| 28 | - /// WARNING: still WIP, needs testing! | ||
| 29 | - _RxImpl<T> operator <<(T val) { | ||
| 30 | - subject.add(_value = val); | ||
| 31 | - return this; | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | bool get canUpdate => _subscriptions.isNotEmpty; | 20 | bool get canUpdate => _subscriptions.isNotEmpty; | 
| 35 | 21 | ||
| 36 | /// Makes this Rx looks like a function so you can update a new | 22 | /// Makes this Rx looks like a function so you can update a new | 
| @@ -217,68 +203,6 @@ class RxBool extends _RxImpl<bool> { | @@ -217,68 +203,6 @@ class RxBool extends _RxImpl<bool> { | ||
| 217 | } | 203 | } | 
| 218 | } | 204 | } | 
| 219 | 205 | ||
| 220 | -/// Rx class for `num` Types (`double` and `int`) shared comparison operations | ||
| 221 | -abstract class _RxNumComparators<T extends num> extends _RxImpl<T> { | ||
| 222 | - bool operator <=(T other) => _value <= other; | ||
| 223 | - | ||
| 224 | - bool operator >=(T other) => _value >= other; | ||
| 225 | - | ||
| 226 | - bool operator <(T other) => _value < other; | ||
| 227 | - | ||
| 228 | - bool operator >(T other) => _value > other; | ||
| 229 | -} | ||
| 230 | - | ||
| 231 | -/// Rx class for `double` Type. | ||
| 232 | -class RxDouble extends _RxNumComparators<double> { | ||
| 233 | - RxDouble([double initial]) { | ||
| 234 | - _value = initial; | ||
| 235 | - } | ||
| 236 | - | ||
| 237 | - double operator *(double val) => _value * val; | ||
| 238 | - | ||
| 239 | - double operator -(double val) => _value - val; | ||
| 240 | - | ||
| 241 | - double operator +(double val) => _value + val; | ||
| 242 | - | ||
| 243 | - double operator /(double val) => _value / val; | ||
| 244 | - | ||
| 245 | - double operator %(double val) => _value % val; | ||
| 246 | -} | ||
| 247 | - | ||
| 248 | -/// Rx class for `num` Type. | ||
| 249 | -class RxNum extends _RxNumComparators<num> { | ||
| 250 | - RxNum([num initial]) { | ||
| 251 | - _value = initial; | ||
| 252 | - } | ||
| 253 | - | ||
| 254 | - num operator *(num val) => _value * val; | ||
| 255 | - | ||
| 256 | - num operator -(num val) => _value - val; | ||
| 257 | - | ||
| 258 | - num operator +(num val) => _value + val; | ||
| 259 | - | ||
| 260 | - num operator /(num val) => _value / val; | ||
| 261 | - | ||
| 262 | - num operator %(num val) => _value % val; | ||
| 263 | -} | ||
| 264 | - | ||
| 265 | -/// Rx class for `int` Type. | ||
| 266 | -class RxInt extends _RxNumComparators<int> { | ||
| 267 | - RxInt([int initial]) { | ||
| 268 | - _value = initial; | ||
| 269 | - } | ||
| 270 | - | ||
| 271 | - int operator %(int val) => _value % val; | ||
| 272 | - | ||
| 273 | - int operator *(int val) => _value * val; | ||
| 274 | - | ||
| 275 | - int operator -(int val) => _value - val; | ||
| 276 | - | ||
| 277 | - int operator +(int val) => _value + val; | ||
| 278 | - | ||
| 279 | - double operator /(int val) => _value / val; | ||
| 280 | -} | ||
| 281 | - | ||
| 282 | /// Rx class for `String` Type. | 206 | /// Rx class for `String` Type. | 
| 283 | class RxString extends _RxImpl<String> { | 207 | class RxString extends _RxImpl<String> { | 
| 284 | RxString([String initial]) { | 208 | RxString([String initial]) { | 
lib/src/state_manager/rx/rx_core/rx_num.dart
0 → 100644
| 1 | +part of 'rx_impl.dart'; | ||
| 2 | + | ||
| 3 | +/// Base Rx class for all num Rx's. | ||
| 4 | +class _BaseRxNum<T extends num> extends _RxImpl<T> { | ||
| 5 | + /// Addition operator. */ | ||
| 6 | + num operator +(num other) => _value + other; | ||
| 7 | + | ||
| 8 | + /// Subtraction operator. | ||
| 9 | + num operator -(num other) => _value - other; | ||
| 10 | + | ||
| 11 | + /// Multiplication operator. | ||
| 12 | + num operator *(num other) => _value * other; | ||
| 13 | + | ||
| 14 | + /// Euclidean modulo operator. | ||
| 15 | + /// | ||
| 16 | + /// Returns the remainder of the Euclidean division. The Euclidean division of | ||
| 17 | + /// two integers `a` and `b` yields two integers `q` and `r` such that | ||
| 18 | + /// `a == b * q + r` and `0 <= r < b.abs()`. | ||
| 19 | + /// | ||
| 20 | + /// The Euclidean division is only defined for integers, but can be easily | ||
| 21 | + /// extended to work with doubles. In that case `r` may have a non-integer | ||
| 22 | + /// value, but it still verifies `0 <= r < |b|`. | ||
| 23 | + /// | ||
| 24 | + /// The sign of the returned value `r` is always positive. | ||
| 25 | + /// | ||
| 26 | + /// See [remainder] for the remainder of the truncating division. | ||
| 27 | + num operator %(num other) => _value % other; | ||
| 28 | + | ||
| 29 | + /// Division operator. | ||
| 30 | + double operator /(num other) => _value / other; | ||
| 31 | + | ||
| 32 | + /// Truncating division operator. | ||
| 33 | + /// | ||
| 34 | + /// If either operand is a [double] then the result of the truncating division | ||
| 35 | + /// `a ~/ b` is equivalent to `(a / b).truncate().toInt()`. | ||
| 36 | + /// | ||
| 37 | + /// If both operands are [int]s then `a ~/ b` performs the truncating | ||
| 38 | + /// integer division. | ||
| 39 | + int operator ~/(num other) => _value ~/ other; | ||
| 40 | + | ||
| 41 | + /// Negate operator. | ||
| 42 | + num operator -() => -_value; | ||
| 43 | + | ||
| 44 | + /// Returns the remainder of the truncating division of `this` by [other]. | ||
| 45 | + /// | ||
| 46 | + /// The result `r` of this operation satisfies: | ||
| 47 | + /// `this == (this ~/ other) * other + r`. | ||
| 48 | + /// As a consequence the remainder `r` has the same sign as the divider | ||
| 49 | + /// `this`. | ||
| 50 | + num remainder(num other) => _value.remainder(other); | ||
| 51 | + | ||
| 52 | + /// Relational less than operator. | ||
| 53 | + bool operator <(num other) => _value < other; | ||
| 54 | + | ||
| 55 | + /// Relational less than or equal operator. | ||
| 56 | + bool operator <=(num other) => _value <= other; | ||
| 57 | + | ||
| 58 | + /// Relational greater than operator. | ||
| 59 | + bool operator >(num other) => _value > other; | ||
| 60 | + | ||
| 61 | + /// Relational greater than or equal operator. | ||
| 62 | + bool operator >=(num other) => _value >= other; | ||
| 63 | + | ||
| 64 | + /// True if the number is the double Not-a-Number value; otherwise, false. | ||
| 65 | + bool get isNaN => _value.isNaN; | ||
| 66 | + | ||
| 67 | + /// True if the number is negative; otherwise, false. | ||
| 68 | + /// | ||
| 69 | + /// Negative numbers are those less than zero, and the double `-0.0`. | ||
| 70 | + bool get isNegative => _value.isNegative; | ||
| 71 | + | ||
| 72 | + /// True if the number is positive infinity or negative infinity; otherwise, | ||
| 73 | + /// false. | ||
| 74 | + bool get isInfinite => _value.isInfinite; | ||
| 75 | + | ||
| 76 | + /// True if the number is finite; otherwise, false. | ||
| 77 | + /// | ||
| 78 | + /// The only non-finite numbers are NaN, positive infinity, and | ||
| 79 | + /// negative infinity. | ||
| 80 | + bool get isFinite => _value.isFinite; | ||
| 81 | + | ||
| 82 | + /// Returns the absolute value of this [num]. | ||
| 83 | + num abs() => _value.abs(); | ||
| 84 | + | ||
| 85 | + /// Returns minus one, zero or plus one depending on the sign and | ||
| 86 | + /// numerical value of the number. | ||
| 87 | + /// | ||
| 88 | + /// Returns minus one if the number is less than zero, | ||
| 89 | + /// plus one if the number is greater than zero, | ||
| 90 | + /// and zero if the number is equal to zero. | ||
| 91 | + /// | ||
| 92 | + /// Returns NaN if the number is the double NaN value. | ||
| 93 | + /// | ||
| 94 | + /// Returns a number of the same type as this number. | ||
| 95 | + /// For doubles, `-0.0.sign == -0.0`. | ||
| 96 | + /// The result satisfies: | ||
| 97 | + /// | ||
| 98 | + /// n == n.sign * n.abs() | ||
| 99 | + /// | ||
| 100 | + /// for all numbers `n` (except NaN, because NaN isn't `==` to itself). | ||
| 101 | + num get sign => _value.sign; | ||
| 102 | + | ||
| 103 | + /// Returns the integer closest to `this`. | ||
| 104 | + /// | ||
| 105 | + /// Rounds away from zero when there is no closest integer: | ||
| 106 | + /// `(3.5).round() == 4` and `(-3.5).round() == -4`. | ||
| 107 | + /// | ||
| 108 | + /// If `this` is not finite (`NaN` or infinity), throws an [UnsupportedError]. | ||
| 109 | + int round() => _value.round(); | ||
| 110 | + | ||
| 111 | + /// Returns the greatest integer no greater than `this`. | ||
| 112 | + /// | ||
| 113 | + /// If `this` is not finite (`NaN` or infinity), throws an [UnsupportedError]. | ||
| 114 | + int floor() => _value.floor(); | ||
| 115 | + | ||
| 116 | + /// Returns the least integer no smaller than `this`. | ||
| 117 | + /// | ||
| 118 | + /// If `this` is not finite (`NaN` or infinity), throws an [UnsupportedError]. | ||
| 119 | + int ceil() => _value.ceil(); | ||
| 120 | + | ||
| 121 | + /// Returns the integer obtained by discarding any fractional | ||
| 122 | + /// digits from `this`. | ||
| 123 | + /// | ||
| 124 | + /// If `this` is not finite (`NaN` or infinity), throws an [UnsupportedError]. | ||
| 125 | + int truncate() => _value.truncate(); | ||
| 126 | + | ||
| 127 | + /// Returns the double integer value closest to `this`. | ||
| 128 | + /// | ||
| 129 | + /// Rounds away from zero when there is no closest integer: | ||
| 130 | + /// `(3.5).roundToDouble() == 4` and `(-3.5).roundToDouble() == -4`. | ||
| 131 | + /// | ||
| 132 | + /// If this is already an integer valued double, including `-0.0`, or it is a | ||
| 133 | + /// non-finite double value, the value is returned unmodified. | ||
| 134 | + /// | ||
| 135 | + /// For the purpose of rounding, `-0.0` is considered to be below `0.0`, | ||
| 136 | + /// and `-0.0` is therefore considered closer to negative numbers than `0.0`. | ||
| 137 | + /// This means that for a value, `d` in the range `-0.5 < d < 0.0`, | ||
| 138 | + /// the result is `-0.0`. | ||
| 139 | + /// | ||
| 140 | + /// The result is always a double. | ||
| 141 | + /// If this is a numerically large integer, the result may be an infinite | ||
| 142 | + /// double. | ||
| 143 | + double roundToDouble() => _value.roundToDouble(); | ||
| 144 | + | ||
| 145 | + /// Returns the greatest double integer value no greater than `this`. | ||
| 146 | + /// | ||
| 147 | + /// If this is already an integer valued double, including `-0.0`, or it is a | ||
| 148 | + /// non-finite double value, the value is returned unmodified. | ||
| 149 | + /// | ||
| 150 | + /// For the purpose of rounding, `-0.0` is considered to be below `0.0`. | ||
| 151 | + /// A number `d` in the range `0.0 < d < 1.0` will return `0.0`. | ||
| 152 | + /// | ||
| 153 | + /// The result is always a double. | ||
| 154 | + /// If this is a numerically large integer, the result may be an infinite | ||
| 155 | + /// double. | ||
| 156 | + double floorToDouble() => _value.floorToDouble(); | ||
| 157 | + | ||
| 158 | + /// Returns the least double integer value no smaller than `this`. | ||
| 159 | + /// | ||
| 160 | + /// If this is already an integer valued double, including `-0.0`, or it is a | ||
| 161 | + /// non-finite double value, the value is returned unmodified. | ||
| 162 | + /// | ||
| 163 | + /// For the purpose of rounding, `-0.0` is considered to be below `0.0`. | ||
| 164 | + /// A number `d` in the range `-1.0 < d < 0.0` will return `-0.0`. | ||
| 165 | + /// | ||
| 166 | + /// The result is always a double. | ||
| 167 | + /// If this is a numerically large integer, the result may be an infinite | ||
| 168 | + /// double. | ||
| 169 | + double ceilToDouble() => _value.ceilToDouble(); | ||
| 170 | + | ||
| 171 | + /// Returns the double integer value obtained by discarding any fractional | ||
| 172 | + /// digits from the double value of `this`. | ||
| 173 | + /// | ||
| 174 | + /// If this is already an integer valued double, including `-0.0`, or it is a | ||
| 175 | + /// non-finite double value, the value is returned unmodified. | ||
| 176 | + /// | ||
| 177 | + /// For the purpose of rounding, `-0.0` is considered to be below `0.0`. | ||
| 178 | + /// A number `d` in the range `-1.0 < d < 0.0` will return `-0.0`, and | ||
| 179 | + /// in the range `0.0 < d < 1.0` it will return 0.0. | ||
| 180 | + /// | ||
| 181 | + /// The result is always a double. | ||
| 182 | + /// If this is a numerically large integer, the result may be an infinite | ||
| 183 | + /// double. | ||
| 184 | + double truncateToDouble() => _value.truncateToDouble(); | ||
| 185 | + | ||
| 186 | + /// Returns this [num] clamped to be in the range [lowerLimit]-[upperLimit]. | ||
| 187 | + /// | ||
| 188 | + /// The comparison is done using [compareTo] and therefore takes `-0.0` into | ||
| 189 | + /// account. This also implies that [double.nan] is treated as the maximal | ||
| 190 | + /// double value. | ||
| 191 | + /// | ||
| 192 | + /// The arguments [lowerLimit] and [upperLimit] must form a valid range where | ||
| 193 | + /// `lowerLimit.compareTo(upperLimit) <= 0`. | ||
| 194 | + num clamp(num lowerLimit, num upperLimit) => | ||
| 195 | + _value.clamp(lowerLimit, upperLimit); | ||
| 196 | + | ||
| 197 | + /// Truncates this [num] to an integer and returns the result as an [int]. */ | ||
| 198 | + int toInt() => _value.toInt(); | ||
| 199 | + | ||
| 200 | + /// Return this [num] as a [double]. | ||
| 201 | + /// | ||
| 202 | + /// If the number is not representable as a [double], an | ||
| 203 | + /// approximation is returned. For numerically large integers, the | ||
| 204 | + /// approximation may be infinite. | ||
| 205 | + double toDouble() => _value.toDouble(); | ||
| 206 | + | ||
| 207 | + /// Returns a decimal-point string-representation of `this`. | ||
| 208 | + /// | ||
| 209 | + /// Converts `this` to a [double] before computing the string representation. | ||
| 210 | + /// | ||
| 211 | + /// If the absolute value of `this` is greater or equal to `10^21` then this | ||
| 212 | + /// methods returns an exponential representation computed by | ||
| 213 | + /// `this.toStringAsExponential()`. Otherwise the result | ||
| 214 | + /// is the closest string representation with exactly [fractionDigits] digits | ||
| 215 | + /// after the decimal point. If [fractionDigits] equals 0 then the decimal | ||
| 216 | + /// point is omitted. | ||
| 217 | + /// | ||
| 218 | + /// The parameter [fractionDigits] must be an integer satisfying: | ||
| 219 | + /// `0 <= fractionDigits <= 20`. | ||
| 220 | + /// | ||
| 221 | + /// Examples: | ||
| 222 | + /// | ||
| 223 | + /// 1.toStringAsFixed(3); // 1.000 | ||
| 224 | + /// (4321.12345678).toStringAsFixed(3); // 4321.123 | ||
| 225 | + /// (4321.12345678).toStringAsFixed(5); // 4321.12346 | ||
| 226 | + /// 123456789012345.toStringAsFixed(3); // 123456789012345.000 | ||
| 227 | + /// 10000000000000000.toStringAsFixed(4); // 10000000000000000.0000 | ||
| 228 | + /// 5.25.toStringAsFixed(0); // 5 | ||
| 229 | + String toStringAsFixed(int fractionDigits) => | ||
| 230 | + _value.toStringAsFixed(fractionDigits); | ||
| 231 | + | ||
| 232 | + /// Returns an exponential string-representation of `this`. | ||
| 233 | + /// | ||
| 234 | + /// Converts `this` to a [double] before computing the string representation. | ||
| 235 | + /// | ||
| 236 | + /// If [fractionDigits] is given then it must be an integer satisfying: | ||
| 237 | + /// `0 <= fractionDigits <= 20`. In this case the string contains exactly | ||
| 238 | + /// [fractionDigits] after the decimal point. Otherwise, without the | ||
| 239 | + /// parameter, the returned string uses the shortest number of digits that | ||
| 240 | + /// accurately represent [this]. | ||
| 241 | + /// | ||
| 242 | + /// If [fractionDigits] equals 0 then the decimal point is omitted. | ||
| 243 | + /// Examples: | ||
| 244 | + /// | ||
| 245 | + /// 1.toStringAsExponential(); // 1e+0 | ||
| 246 | + /// 1.toStringAsExponential(3); // 1.000e+0 | ||
| 247 | + /// 123456.toStringAsExponential(); // 1.23456e+5 | ||
| 248 | + /// 123456.toStringAsExponential(3); // 1.235e+5 | ||
| 249 | + /// 123.toStringAsExponential(0); // 1e+2 | ||
| 250 | + String toStringAsExponential([int fractionDigits]) => | ||
| 251 | + _value.toStringAsExponential(fractionDigits); | ||
| 252 | + | ||
| 253 | + /// Converts `this` to a double and returns a string representation with | ||
| 254 | + /// exactly [precision] significant digits. | ||
| 255 | + /// | ||
| 256 | + /// The parameter [precision] must be an integer satisfying: | ||
| 257 | + /// `1 <= precision <= 21`. | ||
| 258 | + /// | ||
| 259 | + /// Examples: | ||
| 260 | + /// | ||
| 261 | + /// 1.toStringAsPrecision(2); // 1.0 | ||
| 262 | + /// 1e15.toStringAsPrecision(3); // 1.00e+15 | ||
| 263 | + /// 1234567.toStringAsPrecision(3); // 1.23e+6 | ||
| 264 | + /// 1234567.toStringAsPrecision(9); // 1234567.00 | ||
| 265 | + /// 12345678901234567890.toStringAsPrecision(20); // 12345678901234567168 | ||
| 266 | + /// 12345678901234567890.toStringAsPrecision(14); // 1.2345678901235e+19 | ||
| 267 | + /// 0.00000012345.toStringAsPrecision(15); // 1.23450000000000e-7 | ||
| 268 | + /// 0.0000012345.toStringAsPrecision(15); // 0.00000123450000000000 | ||
| 269 | + String toStringAsPrecision(int precision) => | ||
| 270 | + _value.toStringAsPrecision(precision); | ||
| 271 | +} | ||
| 272 | + | ||
| 273 | +class RxNum extends _BaseRxNum<num> {} | ||
| 274 | + | ||
| 275 | +class RxDouble extends _BaseRxNum<double> { | ||
| 276 | + RxDouble([double initial]) { | ||
| 277 | + _value = initial; | ||
| 278 | + } | ||
| 279 | + | ||
| 280 | + /// Addition operator. | ||
| 281 | + double operator +(num other) => _value + other; | ||
| 282 | + | ||
| 283 | + /// Subtraction operator. | ||
| 284 | + double operator -(num other) => _value - other; | ||
| 285 | + | ||
| 286 | + /// Multiplication operator. | ||
| 287 | + double operator *(num other) => _value * other; | ||
| 288 | + | ||
| 289 | + double operator %(num other) => _value % other; | ||
| 290 | + | ||
| 291 | + /// Division operator. | ||
| 292 | + double operator /(num other) => _value / other; | ||
| 293 | + | ||
| 294 | + /// Truncating division operator. | ||
| 295 | + /// | ||
| 296 | + /// The result of the truncating division `a ~/ b` is equivalent to | ||
| 297 | + /// `(a / b).truncate()`. | ||
| 298 | + int operator ~/(num other) => _value ~/ other; | ||
| 299 | + | ||
| 300 | + /// Negate operator. */ | ||
| 301 | + double operator -() => -_value; | ||
| 302 | + | ||
| 303 | + /// Returns the absolute value of this [double]. | ||
| 304 | + double abs() => _value.abs(); | ||
| 305 | + | ||
| 306 | + /// Returns the sign of the double's numerical value. | ||
| 307 | + /// | ||
| 308 | + /// Returns -1.0 if the value is less than zero, | ||
| 309 | + /// +1.0 if the value is greater than zero, | ||
| 310 | + /// and the value itself if it is -0.0, 0.0 or NaN. | ||
| 311 | + double get sign => _value.sign; | ||
| 312 | + | ||
| 313 | + /// Returns the integer closest to `this`. | ||
| 314 | + /// | ||
| 315 | + /// Rounds away from zero when there is no closest integer: | ||
| 316 | + /// `(3.5).round() == 4` and `(-3.5).round() == -4`. | ||
| 317 | + /// | ||
| 318 | + /// If `this` is not finite (`NaN` or infinity), throws an [UnsupportedError]. | ||
| 319 | + int round() => _value.round(); | ||
| 320 | + | ||
| 321 | + /// Returns the greatest integer no greater than `this`. | ||
| 322 | + /// | ||
| 323 | + /// If `this` is not finite (`NaN` or infinity), throws an [UnsupportedError]. | ||
| 324 | + int floor() => _value.floor(); | ||
| 325 | + | ||
| 326 | + /// Returns the least integer no smaller than `this`. | ||
| 327 | + /// | ||
| 328 | + /// If `this` is not finite (`NaN` or infinity), throws an [UnsupportedError]. | ||
| 329 | + int ceil() => _value.ceil(); | ||
| 330 | + | ||
| 331 | + /// Returns the integer obtained by discarding any fractional | ||
| 332 | + /// digits from `this`. | ||
| 333 | + /// | ||
| 334 | + /// If `this` is not finite (`NaN` or infinity), throws an [UnsupportedError]. | ||
| 335 | + int truncate() => _value.truncate(); | ||
| 336 | + | ||
| 337 | + /// Returns the integer double value closest to `this`. | ||
| 338 | + /// | ||
| 339 | + /// Rounds away from zero when there is no closest integer: | ||
| 340 | + /// `(3.5).roundToDouble() == 4` and `(-3.5).roundToDouble() == -4`. | ||
| 341 | + /// | ||
| 342 | + /// If this is already an integer valued double, including `-0.0`, or it is | ||
| 343 | + /// not a finite value, the value is returned unmodified. | ||
| 344 | + /// | ||
| 345 | + /// For the purpose of rounding, `-0.0` is considered to be below `0.0`, | ||
| 346 | + /// and `-0.0` is therefore considered closer to negative numbers than `0.0`. | ||
| 347 | + /// This means that for a value, `d` in the range `-0.5 < d < 0.0`, | ||
| 348 | + /// the result is `-0.0`. | ||
| 349 | + double roundToDouble() => _value.roundToDouble(); | ||
| 350 | + | ||
| 351 | + /// Returns the greatest integer double value no greater than `this`. | ||
| 352 | + /// | ||
| 353 | + /// If this is already an integer valued double, including `-0.0`, or it is | ||
| 354 | + /// not a finite value, the value is returned unmodified. | ||
| 355 | + /// | ||
| 356 | + /// For the purpose of rounding, `-0.0` is considered to be below `0.0`. | ||
| 357 | + /// A number `d` in the range `0.0 < d < 1.0` will return `0.0`. | ||
| 358 | + double floorToDouble() => _value.floorToDouble(); | ||
| 359 | + | ||
| 360 | + /// Returns the least integer double value no smaller than `this`. | ||
| 361 | + /// | ||
| 362 | + /// If this is already an integer valued double, including `-0.0`, or it is | ||
| 363 | + /// not a finite value, the value is returned unmodified. | ||
| 364 | + /// | ||
| 365 | + /// For the purpose of rounding, `-0.0` is considered to be below `0.0`. | ||
| 366 | + /// A number `d` in the range `-1.0 < d < 0.0` will return `-0.0`. | ||
| 367 | + double ceilToDouble() => _value.ceilToDouble(); | ||
| 368 | + | ||
| 369 | + /// Returns the integer double value obtained by discarding any fractional | ||
| 370 | + /// digits from `this`. | ||
| 371 | + /// | ||
| 372 | + /// If this is already an integer valued double, including `-0.0`, or it is | ||
| 373 | + /// not a finite value, the value is returned unmodified. | ||
| 374 | + /// | ||
| 375 | + /// For the purpose of rounding, `-0.0` is considered to be below `0.0`. | ||
| 376 | + /// A number `d` in the range `-1.0 < d < 0.0` will return `-0.0`, and | ||
| 377 | + /// in the range `0.0 < d < 1.0` it will return 0.0. | ||
| 378 | + double truncateToDouble() => _value.truncateToDouble(); | ||
| 379 | +} | ||
| 380 | + | ||
| 381 | +class RxInt extends _BaseRxNum<int> { | ||
| 382 | + RxInt([int initial]) { | ||
| 383 | + _value = initial; | ||
| 384 | + } | ||
| 385 | + | ||
| 386 | + /// Bit-wise and operator. | ||
| 387 | + /// | ||
| 388 | + /// Treating both `this` and [other] as sufficiently large two's component | ||
| 389 | + /// integers, the result is a number with only the bits set that are set in | ||
| 390 | + /// both `this` and [other] | ||
| 391 | + /// | ||
| 392 | + /// If both operands are negative, the result is negative, otherwise | ||
| 393 | + /// the result is non-negative. | ||
| 394 | + int operator &(int other) => _value & other; | ||
| 395 | + | ||
| 396 | + /// Bit-wise or operator. | ||
| 397 | + /// | ||
| 398 | + /// Treating both `this` and [other] as sufficiently large two's component | ||
| 399 | + /// integers, the result is a number with the bits set that are set in either | ||
| 400 | + /// of `this` and [other] | ||
| 401 | + /// | ||
| 402 | + /// If both operands are non-negative, the result is non-negative, | ||
| 403 | + /// otherwise the result is negative. | ||
| 404 | + int operator |(int other) => _value | other; | ||
| 405 | + | ||
| 406 | + /// Bit-wise exclusive-or operator. | ||
| 407 | + /// | ||
| 408 | + /// Treating both `this` and [other] as sufficiently large two's component | ||
| 409 | + /// integers, the result is a number with the bits set that are set in one, | ||
| 410 | + /// but not both, of `this` and [other] | ||
| 411 | + /// | ||
| 412 | + /// If the operands have the same sign, the result is non-negative, | ||
| 413 | + /// otherwise the result is negative. | ||
| 414 | + int operator ^(int other) => _value ^ other; | ||
| 415 | + | ||
| 416 | + /// The bit-wise negate operator. | ||
| 417 | + /// | ||
| 418 | + /// Treating `this` as a sufficiently large two's component integer, | ||
| 419 | + /// the result is a number with the opposite bits set. | ||
| 420 | + /// | ||
| 421 | + /// This maps any integer `x` to `-x - 1`. | ||
| 422 | + int operator ~() => ~_value; | ||
| 423 | + | ||
| 424 | + /// Shift the bits of this integer to the left by [shiftAmount]. | ||
| 425 | + /// | ||
| 426 | + /// Shifting to the left makes the number larger, effectively multiplying | ||
| 427 | + /// the number by `pow(2, shiftIndex)`. | ||
| 428 | + /// | ||
| 429 | + /// There is no limit on the size of the result. It may be relevant to | ||
| 430 | + /// limit intermediate values by using the "and" operator with a suitable | ||
| 431 | + /// mask. | ||
| 432 | + /// | ||
| 433 | + /// It is an error if [shiftAmount] is negative. | ||
| 434 | + int operator <<(int shiftAmount) => _value << shiftAmount; | ||
| 435 | + | ||
| 436 | + /// Shift the bits of this integer to the right by [shiftAmount]. | ||
| 437 | + /// | ||
| 438 | + /// Shifting to the right makes the number smaller and drops the least | ||
| 439 | + /// significant bits, effectively doing an integer division by | ||
| 440 | + ///`pow(2, shiftIndex)`. | ||
| 441 | + /// | ||
| 442 | + /// It is an error if [shiftAmount] is negative. | ||
| 443 | + int operator >>(int shiftAmount) => _value >> shiftAmount; | ||
| 444 | + | ||
| 445 | + /// Returns this integer to the power of [exponent] modulo [modulus]. | ||
| 446 | + /// | ||
| 447 | + /// The [exponent] must be non-negative and [modulus] must be | ||
| 448 | + /// positive. | ||
| 449 | + int modPow(int exponent, int modulus) => _value.modPow(exponent, modulus); | ||
| 450 | + | ||
| 451 | + /// Returns the modular multiplicative inverse of this integer | ||
| 452 | + /// modulo [modulus]. | ||
| 453 | + /// | ||
| 454 | + /// The [modulus] must be positive. | ||
| 455 | + /// | ||
| 456 | + /// It is an error if no modular inverse exists. | ||
| 457 | + int modInverse(int modulus) => _value.modInverse(modulus); | ||
| 458 | + | ||
| 459 | + /// Returns the greatest common divisor of this integer and [other]. | ||
| 460 | + /// | ||
| 461 | + /// If either number is non-zero, the result is the numerically greatest | ||
| 462 | + /// integer dividing both `this` and `other`. | ||
| 463 | + /// | ||
| 464 | + /// The greatest common divisor is independent of the order, | ||
| 465 | + /// so `x.gcd(y)` is always the same as `y.gcd(x)`. | ||
| 466 | + /// | ||
| 467 | + /// For any integer `x`, `x.gcd(x)` is `x.abs()`. | ||
| 468 | + /// | ||
| 469 | + /// If both `this` and `other` is zero, the result is also zero. | ||
| 470 | + int gcd(int other) => _value.gcd(other); | ||
| 471 | + | ||
| 472 | + /// Returns true if and only if this integer is even. | ||
| 473 | + bool get isEven => _value.isEven; | ||
| 474 | + | ||
| 475 | + /// Returns true if and only if this integer is odd. | ||
| 476 | + bool get isOdd => _value.isOdd; | ||
| 477 | + | ||
| 478 | + /// Returns the minimum number of bits required to store this integer. | ||
| 479 | + /// | ||
| 480 | + /// The number of bits excludes the sign bit, which gives the natural length | ||
| 481 | + /// for non-negative (unsigned) values. Negative values are complemented to | ||
| 482 | + /// return the bit position of the first bit that differs from the sign bit. | ||
| 483 | + /// | ||
| 484 | + /// To find the number of bits needed to store the value as a signed value, | ||
| 485 | + /// add one, i.e. use `x.bitLength + 1`. | ||
| 486 | + /// ``` | ||
| 487 | + /// x.bitLength == (-x-1).bitLength | ||
| 488 | + /// | ||
| 489 | + /// 3.bitLength == 2; // 00000011 | ||
| 490 | + /// 2.bitLength == 2; // 00000010 | ||
| 491 | + /// 1.bitLength == 1; // 00000001 | ||
| 492 | + /// 0.bitLength == 0; // 00000000 | ||
| 493 | + /// (-1).bitLength == 0; // 11111111 | ||
| 494 | + /// (-2).bitLength == 1; // 11111110 | ||
| 495 | + /// (-3).bitLength == 2; // 11111101 | ||
| 496 | + /// (-4).bitLength == 2; // 11111100 | ||
| 497 | + /// ``` | ||
| 498 | + int get bitLength => _value.bitLength; | ||
| 499 | + | ||
| 500 | + /// Returns the least significant [width] bits of this integer as a | ||
| 501 | + /// non-negative number (i.e. unsigned representation). The returned value | ||
| 502 | + /// has zeros in all bit positions higher than [width]. | ||
| 503 | + /// ``` | ||
| 504 | + /// (-1).toUnsigned(5) == 31 // 11111111 -> 00011111 | ||
| 505 | + /// ``` | ||
| 506 | + /// This operation can be used to simulate arithmetic from low level | ||
| 507 | + /// languages. | ||
| 508 | + /// For example, to increment an 8 bit quantity: | ||
| 509 | + /// ``` | ||
| 510 | + /// q = (q + 1).toUnsigned(8); | ||
| 511 | + /// ``` | ||
| 512 | + /// `q` will count from `0` up to `255` and then wrap around to `0`. | ||
| 513 | + /// | ||
| 514 | + /// If the input fits in [width] bits without truncation, the result is the | ||
| 515 | + /// same as the input. The minimum width needed to avoid truncation of `x` is | ||
| 516 | + /// given by `x.bitLength`, i.e. | ||
| 517 | + /// ``` | ||
| 518 | + /// x == x.toUnsigned(x.bitLength); | ||
| 519 | + /// ``` | ||
| 520 | + int toUnsigned(int width) => _value.toUnsigned(width); | ||
| 521 | + | ||
| 522 | + /// Returns the least significant [width] bits of this integer, extending the | ||
| 523 | + /// highest retained bit to the sign. This is the same as truncating the | ||
| 524 | + /// value to fit in [width] bits using an signed 2-s complement | ||
| 525 | + /// representation. | ||
| 526 | + /// The returned value has the same bit value in all positions higher than | ||
| 527 | + /// [width]. | ||
| 528 | + /// | ||
| 529 | + /// ``` | ||
| 530 | + /// V--sign bit-V | ||
| 531 | + /// 16.toSigned(5) == -16 // 00010000 -> 11110000 | ||
| 532 | + /// 239.toSigned(5) == 15 // 11101111 -> 00001111 | ||
| 533 | + /// ^ ^ | ||
| 534 | + /// ``` | ||
| 535 | + /// This operation can be used to simulate arithmetic from low level | ||
| 536 | + /// languages. | ||
| 537 | + /// For example, to increment an 8 bit signed quantity: | ||
| 538 | + /// ``` | ||
| 539 | + /// q = (q + 1).toSigned(8); | ||
| 540 | + /// ``` | ||
| 541 | + /// `q` will count from `0` up to `127`, wrap to `-128` and count back up to | ||
| 542 | + /// `127`. | ||
| 543 | + /// | ||
| 544 | + /// If the input value fits in [width] bits without truncation, the result is | ||
| 545 | + /// the same as the input. The minimum width needed to avoid truncation | ||
| 546 | + /// of `x` is `x.bitLength + 1`, i.e. | ||
| 547 | + /// ``` | ||
| 548 | + /// x == x.toSigned(x.bitLength + 1); | ||
| 549 | + /// ``` | ||
| 550 | + int toSigned(int width) => _value.toSigned(width); | ||
| 551 | + | ||
| 552 | + /// Return the negative value of this integer. | ||
| 553 | + /// | ||
| 554 | + /// The result of negating an integer always has the opposite sign, except | ||
| 555 | + /// for zero, which is its own negation. | ||
| 556 | + int operator -() => -_value; | ||
| 557 | + | ||
| 558 | + /// Returns the absolute value of this integer. | ||
| 559 | + /// | ||
| 560 | + /// For any integer `x`, the result is the same as `x < 0 ? -x : x`. | ||
| 561 | + int abs() => _value.abs(); | ||
| 562 | + | ||
| 563 | + /// Returns the sign of this integer. | ||
| 564 | + /// | ||
| 565 | + /// Returns 0 for zero, -1 for values less than zero and | ||
| 566 | + /// +1 for values greater than zero. | ||
| 567 | + int get sign => _value.sign; | ||
| 568 | + | ||
| 569 | + /// Returns `this`. | ||
| 570 | + int round() => _value.round(); | ||
| 571 | + | ||
| 572 | + /// Returns `this`. | ||
| 573 | + int floor() => _value.floor(); | ||
| 574 | + | ||
| 575 | + /// Returns `this`. | ||
| 576 | + int ceil() => _value.ceil(); | ||
| 577 | + | ||
| 578 | + /// Returns `this`. | ||
| 579 | + int truncate() => _value.truncate(); | ||
| 580 | + | ||
| 581 | + /// Returns `this.toDouble()`. | ||
| 582 | + double roundToDouble() => _value.roundToDouble(); | ||
| 583 | + | ||
| 584 | + /// Returns `this.toDouble()`. | ||
| 585 | + double floorToDouble() => _value.floorToDouble(); | ||
| 586 | + | ||
| 587 | + /// Returns `this.toDouble()`. | ||
| 588 | + double ceilToDouble() => _value.ceilToDouble(); | ||
| 589 | + | ||
| 590 | + /// Returns `this.toDouble()`. | ||
| 591 | + double truncateToDouble() => _value.truncateToDouble(); | ||
| 592 | +} | 
- 
Please register or login to post a comment