Skip to content

Commit b345e8a

Browse files
committed
Added comparison operators and a discussion of rounding.
1 parent e91c744 commit b345e8a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

text/0041-fixed-point.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ The following operations are defined on it:
5656
- If `value` is an `int` or `float`, it'll be cast to a `fixed.Const` first.
5757
- If `value` is a `fixed.Value`, the precision will be extended or rounded as required.
5858
- `.round(f_width=0)`: Return a new `fixed.Value` with precision changed to `f_width`, rounding as required.
59-
- `.__add__(other)`, `.__radd__(other)`, `.__sub__(other)`, `.__rsub__(other)`, `.__mul__(other)`, `.__rmul__(other)`: Binary arithmetic operations.
59+
- `.__add__(other)`, `.__radd__(other)`, `.__sub__(other)`, `.__rsub__(other)`, `.__mul__(other)`, `.__rmul__(other)`: Binary arithmetic operators.
6060
- If `other` is a `Value`, it'll be cast to a `fixed.Value` first.
6161
- If `other` is an `int`, it'll be cast to a `fixed.Const` first.
6262
- If `other` is a `float`: TBD
6363
- The result will be a new `fixed.Value` with enough precision to hold any resulting value without rounding or overflowing.
64-
- `.__lshift__(other)`, `.__rshift__(other)`: Bit shift operations.
65-
- `.__neg__()`, `.__pos__()`, `.__abs__()`: Unary arithmetic operations.
64+
- `.__lshift__(other)`, `.__rshift__(other)`: Bit shift operators.
65+
- `.__neg__()`, `.__pos__()`, `.__abs__()`: Unary arithmetic operators.
66+
- `.__lt__(other)`, `.__le__(other)`, `.__eq__(other)`, `.__ne__(other)`, `.__gt__(other)`, `.__ge__(other)`: Comparison operators.
6667

6768
`fixed.Const` is a `fixed.Value` subclass.
6869
The following additional operations are defined on it:
@@ -110,6 +111,13 @@ TBD
110111
- We could use the same width for `other` as for `self`, adjusted to the appropriate exponent for the value.
111112
- We could outright reject it, requiring the user to explicitly specify precision like e.g. `value * Q(15).const(1 / 3)`.
112113

114+
- How should we handle rounding?
115+
- Truncating and adding the most significant truncated bit is cheap and is effectively round to nearest with ties rounded towards positive infinity.
116+
- Simply truncating is free, rounds towards negative infinity.
117+
- IEEE 754 defaults to round to nearest, ties to even, which is more expensive to implement.
118+
- Should we make it user selectable?
119+
- We still need a default mode used when a higher precision number is passed to `.eq()`.
120+
113121
- Are there any other operations that would be good to have?
114122

115123
- Are there any operations that would be good to *not* have?
@@ -127,6 +135,7 @@ TBD
127135
- I feel like the `i_width` and `f_width` names are difficult enough to read that it's of more importance than bikeshedding to come up with something more readable. (@whitequark)
128136
- `.int_bits`, `.frac_bits`?
129137
- cursed option: `int, frac = x.width`?
138+
- `.round()` is a bit awkwardly named when it's used both to increase and decrease precision.
130139

131140
## Future possibilities
132141
[future-possibilities]: #future-possibilities

0 commit comments

Comments
 (0)