Model OrdF64 as a non-NaN float#972
Conversation
03a3a63 to
fba0e68
Compare
|
We actually have a much stronger invariant available than "not Rather than making a ton of functions like Similarly we should change The only places that a NaN can actually occur are where we do division -- I believe these are all in
|
|
I would propose splitting this into several commits:
|
Update the type name to better reflect its domain invariants. The underlying floats are guaranteed to be both positive and finite, making `PositiveReal` a more accurate and descriptive name than `OrdF64`, which only highlighted the `Ord` trait implementation.
Add `Mul` and Add trait implementations for `PositiveReal` to enable arithmetic operations on `PositiveReal` values. Display impl is needed for `PositiveReal` to be used in `Policy`. Provide a `normalize` method on `PositiveReal` to normalize two values into a valid probability distribution. Implement the `ratio` method on `Threshold` to expose the threshold ratio (k / n) as a `PositiveReal`.
Introduce a `parse_probability` function to parse non-zero strings into PositiveReal values. This implementation wraps around the `parse_num_nonzero` function to safely parse string inputs, enforcing the non-zero invariant.
Change `Or` weights from usize to PositiveReal and all probability parameters from raw f64 to PositiveReal, making invalid states unrepresentable. Thread Threshold through the compiler instead of extracting k/n pairs.
`cargo +nightly rbmt lint` fails with useless use of `format!`, this patch fixes the culprit and uses `to_string` over the previously used `format!` macro.
|
@apoelstra Thank you so much for the detailed spec you provided, I believe this is ready for review, although you didn't mention, using |
PositiveRealnewtype for probability handlingSummary:
Replaces
f64andOrdF64withPositiveRealnewtype throughout the policy module, making invalid probability states unrepresentable at the type level.Changes:
OrdF64toPositiveReal.PositiveRealinto its own module.Add,Mul), display, andnormalizeforPositiveReal,ratioforThreshold.parse_probabilityhelper.PositiveRealin the policy module.closes #445