Skip to content

Commit c465e6f

Browse files
committed
Add README sections around overflow, conversions and inexact errors
1 parent ea46f72 commit c465e6f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,60 @@ julia> 0.1 + 0.2
4444
julia> FixedDecimal{Int,1}(0.1) + FixedDecimal{Int,1}(0.2)
4545
FixedDecimal{Int64,1}(0.3)
4646
```
47+
48+
### Arithmetic details: Overflow and checked math
49+
50+
By default, all arithmetic operations on FixedDecimals **will silently overflow**, following the standard behavior for bit integer types in Julia. For example:
51+
```julia
52+
julia> FixedDecimal{Int8,2}(1.0) + FixedDecimal{Int8,2}(1.0)
53+
FixedDecimal{Int8,2}(-0.56)
54+
55+
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:
63+
```julia
64+
julia> Base.checked_mul(FixedDecimal{Int8,2}(1.2), FixedDecimal{Int8,2}(1.2))
65+
ERROR: OverflowError: 1.20 * 1.20 overflowed for type FixedDecimal{Int8, 2}
66+
67+
julia> Base.checked_add(FixedDecimal{Int8,2}(1.2), 1)
68+
ERROR: OverflowError: 1.20 + 1.00 overflowed for type FixedDecimal{Int8, 2}
69+
70+
julia> Base.checked_div(Int8(1), FixedDecimal{Int8,2}(0.5))
71+
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
95+
ERROR: InexactError: convert(FixedDecimal{Int8, 2}, 2)
96+
97+
julia> FixedDecimal{Int8,2}(1) + 2 # Same here: 2 is promoted to FD{Int8,2}(2)
98+
ERROR: InexactError: convert(FixedDecimal{Int8, 2}, 2)
99+
100+
julia> FixedDecimal{Int8,2}(1) + FixedDecimal{Int8,1}(2) # Promote to the higher-precision type again throws.
101+
ERROR: InexactError: convert(FixedDecimal{Int8, 2}, 2.0)
102+
```
103+

0 commit comments

Comments
 (0)