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
_NOTE: This section applies to FixedPointDecimals v0.5+._
51
+
52
+
By default, all arithmetic operations on FixedDecimals, except division, **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
58
+
FixedDecimal{Int8,2}(-1.28)
59
+
60
+
julia>abs(FixedDecimal{Int8,2}(-1.28)) # negative typemin wraps to typemin again
61
+
FixedDecimal{Int8,2}(-1.28)
62
+
```
63
+
64
+
*Note that **division** on FixedDecimals will throw OverflowErrors on overflow, and will not wrap. This decision may be reevaluated in a future breaking version change release of FixedDecimals. Please keep this in mind.*
65
+
66
+
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}
76
+
```
77
+
78
+
**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_rdiv`. However, as noted above, the default division arithmetic operators will throw on overflow anyway.
79
+
80
+
Here are all the checked arithmetic operations supported by `FixedDecimal`s:
81
+
-`Base.checked_add(x,y)`
82
+
-`Base.checked_sub(x,y)`
83
+
-`Base.checked_mul(x,y)`
84
+
-`Base.checked_div(x,y)`
85
+
-`FixedPointDecimals.checked_rdiv(x,y)`
86
+
-`Base.checked_cld(x,y)`
87
+
-`Base.checked_fld(x,y)`
88
+
-`Base.checked_rem(x,y)`
89
+
-`Base.checked_mod(x,y)`
90
+
-`Base.checked_neg(x)`
91
+
-`Base.checked_abs(x)`
92
+
93
+
### Conversions, Promotions, and Inexact Errors.
94
+
95
+
Note that arithmetic operations will _promote_ all arguments to the same FixedDecimal type
96
+
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:
97
+
```julia
98
+
julia>FixedDecimal{Int8,2}(2) # 200 doesn't fit in Int8
0 commit comments