Comments (3)
So according to what I could find about the definition of total order provided by IEEE 754, part of it would require that negative zero and positive zero compare differently from the default implementation. In other words, -0.0 < +0.0
returns false by default, but for total order it would have to be true. It also describes how different representations of the same floating point number would be ordered, and how NaNs with different payloads would compare with each other.
This looks like a big problem because Rust's documentation for Ord
and PartialOrd
explicitly say:
Implementations of
PartialEq
,PartialOrd
, andOrd
must agree with each other.
If IEEE-conformant total order is implemented using Ord
, then it would disagree with the implementation of PartialOrd
for f32
and f64
, likely violating assumptions made by the standard library. I'm not sure what the exact consequences of that would be, but they don't sound good.
The only way I can think of to avoid this would be to have a separate trait specifically for floating-point total order.
(Btw, this is all based on what I could find from the section I linked above. If I misunderstood something or got something wrong, feel free to correct me.)
from rust-ordered-float.
If IEEE-conformant total order is implemented using Ord, then it would disagree with the implementation of PartialOrd for f32 and f64
If we would implement Ord
for f32
and f64
in standard library then yes, this would be a problem and PartialOrd
should be compatible with Ord
. But this library instead provides wrappers for f32
and f64
, implementation of Ord
and PartialOrd
for these wrappers can agree with each other regardless of what standard library does for f32
and f64
.
This is definition of total order from the standard:
5.10 Details of totalOrder predicate
For each supported arithmetic format, an implementation shall provide the following predicate that defines an ordering among all operands in a particular format:
boolean totalOrder(source, source)
totalOrder(x, y) imposes a total ordering on canonical members of the format of x and y:
a) If x<y, totalOrder(x, y) is true.
b) If x>y, totalOrder(x, y) is false.
c) If x=y:
- totalOrder(−0, +0) is true.
- totalOrder(+0, −0) is false.
- If x and y represent the same floating-point datum:
i) If x and y have negative sign, totalOrder(x, y) is true if and only if the exponent of x≥ the exponent of y
ii) otherwise totalOrder(x, y) is true if and only if the exponent of x≤ the exponent of y.d) If x and y are unordered numerically because x or y is NaN:
- totalOrder(−NaN, y) is true where -NaN represents a NaN with negative sign bit and y is a floating-point number.
- totalOrder(x, +NaN) is true where +NaN represents a NaN with positive sign bit and x is a floating-point number.
- If x and y are both NaNs, then totalOrder reflects a total ordering based on:
i) negative sign orders below positive sign
ii) signaling orders below quiet for +NaN, reverse for -NaN
iii) lesser payload, when regarded as an integer, orders below greater payload for +NaN,reverse for -NaN.Neither signaling NaNs nor quiet NaNs signal an exception. For canonical x and y, totalOrder(x, y) and totalOrder(y, x) are both true if x and y are bitwise identical.
NOTE—totalOrder does not impose a total ordering on all encodings in a format. In particular, it does not distinguish among different encodings of the same floating-point representation, as when one or both encodings are non-canonical.
from rust-ordered-float.
@kryptan because people (myself included) still expect these wrappers to behave like a number. The aim is to be as close as possible to how ordering works for the native float types, whilst still upholding the guarantees of Ord. If negative zero compared differently to positive zero it would be useless for me and many other people.
It may make sense to have another type that implements the IEEE-defined ordering, but I suspect it will see less usage...
from rust-ordered-float.
Related Issues (20)
- Is it safe to call `NotNan::new_unchecked` with a non-float type? HOT 2
- `NotNan<f64>` should impl `From<i32>` and similar lossless conversions HOT 2
- Possible regression: Updating 2.8.0 to 2.9.0 causes "type annotations needed" for `BTreeSet::range` etc. HOT 5
- OrderedFloat should implement the trait Into. HOT 1
- Release Request: Please release v3.1.0 to include #110 HOT 2
- Optimize the representation of `NotNaN` HOT 2
- `rand` types missing traits
- Missing `FloatConst` trait HOT 1
- Missing `Pow` trait
- `OrderedFloat` and `NotNan` should implement `geo::HasKernel` (behind feature)
- Implement `AsPrimitive<NotNan<T>>` for integer types HOT 2
- Support checked ops.
- Implement `simba::scalar::SubsetOf` for `OrderedFloat`
- Build of v3.9.0 fails with `--feature=rkyv` HOT 3
- Changelog or a clear list of breaking changes between releases? HOT 1
- Invalid Hash implementation HOT 5
- OrderedFloat comparison with f32/f64 is not total HOT 1
- Missing binary op implementations for `OrderedFloat` HOT 2
- OrderedFloat does not implement Deserialize HOT 1
- Div by zero produces NotNan(inf) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-ordered-float.