简体   繁体   English

为什么 Rust 不通过 f64 和 f32 的 Ord 特征实现全排序?

[英]Why does Rust not implement total ordering via the Ord trait for f64 and f32?

While all the integer types in Rust implement Ord which emphasizes total ordering, the floating point types only implement PartialOrd .虽然 Rust 中的所有整数类型都实现了强调全序的Ord ,但浮点类型只实现PartialOrd This means that there could be floating point values which cannot be compared.这意味着可能存在无法比较的浮点值。 This seems difficult to digest since floating point numbers can be thought of as approximations to real numbers which happen to be a totally ordered set.这似乎很难消化,因为浮点数可以被认为是对实数的近似,而实数恰好是一个完全有序的集合。 Even the addition of positive and negative infinity keeps the set of real numbers totally ordered.即使加上正无穷和负无穷,也能保持实数集完全有序。 Why this odd choice in Rust?为什么在 Rust 中有这个奇怪的选择?

This restriction means that a generic sort/search algorithm can only assume partial ordering on numbers.此限制意味着通用排序/搜索算法只能假设对数字进行部分排序。 The IEEE 754 standard seems to provide for a total ordering predicate . IEEE 754 标准似乎提供了一个总排序谓词

Are NaN's so much of a problem in generic code?泛型代码中的 NaN 有这么大的问题吗?

What is your question, precisely? 你的问题究竟是什么? Are you asking whether NaN exists, or whether it can be obtained as the result of accidental or voluntary computations? 您是在询问是否存在NaN,或者是否可以通过意外或自愿计算获得NaN? Yes, it does and it can. 是的,它确实可以。 The sort of data structure that requires a total order for keys breaks down completely when the provided order is not a total order. 当提供的订单不是总订单时,需要总订单的数据结构完全分解。 You do not want even one exceptional value be different from itself, because it would break invariants of the structure and mean that anything can happen henceforth. 你甚至不希望一个特殊的价值与它本身不同,因为它会打破结构的不变量,并意味着今后任何事情都可能发生。 NaN is not something that should be assumed to be innocuous as long as no problem has been shown, although that has been tried in other languages . 只要没有出现任何问题,NaN就不应该被认为是无害的,尽管已经在其他语言中尝试过

IEEE 754's definition of the ordinary comparison operators < , <= , … makes them very useful in general—if not when you need a total order. IEEE 754对普通比较运算符<<= ,...的定义使它们在一般情况下非常有用 - 如果不需要总订单的话。 In particular, it is easy to write conditions so that NaN inputs will be sent to the error branch: 特别是,很容易编写条件,以便将NaN输入发送到错误分支:

if (!(x <= MAX)) { // NaN makes this condition true
  error();
}

if (!(x >= MIN)) { // NaN makes this condition true
  error();
}

Because < and <= are so useful, they are the operations implemented as single, fast instructions in modern processors—the totalOrder predicate from IEEE 754 is typically not implemented in hardware. 因为<<=非常有用,所以它们是在现代处理器中作为单个快速指令实现的操作 - 来自IEEE 754的totalOrder谓词通常不在硬件中实现。 Programming languages map the fast instructions to constructs in the language and leave anyone who exceptionally needs totalOrder to pick it from a library or even to define it themselves. 编程语言将快速指令映射到语言中的构造,并让异常需要totalOrder的任何人从库中选择它,甚至自己定义它。

It cannot, because of Rust's core design mistake of making Ord a sub-type of PartialOrd .它不能,因为 Rust 的核心设计错误是让Ord成为PartialOrd的子类型。

This means that despite floating point values having a total order, there can only ever be one implementation inside that hierarchy;这意味着尽管浮点值具有总顺序,但该层次结构中只能有一个实现; and that implementation uses the comparison predicate (which is only a partial order), instead of the total-order predicate (which is a total order (and therefore a partial order as well)).并且该实现使用比较谓词(仅是部分顺序),而不是全顺序谓词(这是全顺序(因此也是部分顺序))。

If Ord and PartialOrd were unrelated, it would be trivial to implement both 5.10 and 5.11 from the IEEE754 spec – but because they aren't – Rust has to pick one, and it chose 5.11.如果OrdPartialOrd不相关,那么实现 IEEE754 规范中的 5.10 和 5.11 将是微不足道的——但因为它们不是——Rust 必须选择一个,它选择了 5.11。


It's easy to imagine a different design in which eg Sortable provided 5.10 and Comparable provided 5.11, with types implementing both as appropriate.很容易想象一种不同的设计,例如, Sortable提供 5.10, Comparable提供 5.11,并且类型在适当的时候实现了这两者。

Then, a user could write Sortable if she needed the total order, and Comparable if she needs the partial order.然后,如果用户需要全序,则可以写Sortable ,如果需要偏序,则可以写Comparable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM