You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, all arithmetic operations on FixedDecimals **will silently overflow**, following the standard behavior for bit integer types in Julia. For example:
julia>-FixedDecimal{Int8,2}(-1.28) # negative typemin wraps to typemin again
56
+
FixedDecimal{Int8,2}(-1.28)
57
+
58
+
julia>abs(FixedDecimal{Int8,2}(-1.28)) # negative typemin wraps to typemin again
59
+
FixedDecimal{Int8,2}(-1.28)
60
+
```
61
+
62
+
In most applications dealing with `FixedDecimals`, you will likely want to use the **checked arithmetic** operations instead. These operations will _throw an OverflowError_ on overflow or underflow, rather than silently wrapping. For example:
ERROR: OverflowError:1.00÷0.50 overflowed for type FixedDecimal{Int8, 2}
72
+
```
73
+
74
+
**Checked division:** Note that `checked_div` performs truncating, integer division. Julia Base does not provide a function to perform checked decimal division, so we provide one in this package, `FixedPointDecimals.checked_decimal_division`.
75
+
76
+
Here are all the checked arithmetic operations supported by `FixedDecimal`s:
77
+
-`Base.checked_add`
78
+
-`Base.checked_sub`
79
+
-`Base.checked_mul`
80
+
-`Base.checked_div`
81
+
-`FixedPointDecimals.checked_decimal_division`
82
+
-`Base.checked_cld`
83
+
-`Base.checked_fld`
84
+
-`Base.checked_rem`
85
+
-`Base.checked_mod`
86
+
-`Base.checked_neg`
87
+
-`Base.checked_abs`
88
+
89
+
### Conversions, Promotions, and Inexact Errors.
90
+
91
+
Note that arithmetic operations will _promote_ all arguments to the same FixedDecimal type
92
+
before performing the operation. If you are promoting a non-FixedDecimal _number_ to a FixedDecimal, there is always a chance that the Number will not fit in the FD type. In that case, the conversion will throw an exception. Here are some examples:
93
+
```julia
94
+
julia>FixedDecimal{Int8,2}(2) # 200 doesn't fit in Int8
0 commit comments