为什么 Rust 不通过 f64 和 f32 的 Ord 特征实现总排序? [英] Why does Rust not implement total ordering via the Ord trait for f64 and f32?

查看:29
本文介绍了为什么 Rust 不通过 f64 和 f32 的 Ord 特征实现总排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然 Rust 中的所有整数类型都实现了 Ord 强调全序,浮点类型只实现 PartialOrd.这意味着可能存在无法比较的浮点值.这似乎难以理解,因为浮点数可以被认为是对恰好是全序集的实数的近似.即使正无穷大和负无穷大相加也能保持实数集完全有序.为什么在 Rust 中有这个奇怪的选择?

While all the integer types in Rust implement Ord which emphasizes total ordering, the floating point types only implement 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?

此限制意味着通用排序/搜索算法只能假设数字的部分排序.IEEE 754 标准似乎提供了一个总排序谓词.

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.

在泛型代码中,NaN 是一个大问题吗?

Are NaN's so much of a problem in generic code?

推荐答案

您的问题是什么?你是问 NaN 是否存在,或者它是否可以作为偶然或自愿计算的结果获得?是的,它确实可以.当提供的顺序不是总顺序时,需要键的总顺序的那种数据结构完全分解.您不希望任何异常值与其自身不同,因为它会破坏结构的不变量并意味着此后任何事情都可能发生.只要没有显示出问题,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? 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.

IEEE 754 对普通比较运算符的定义 <<=... 使它们在一般情况下非常有用 - 如果不是在您需要全序时.特别是,很容易编写条件,以便将 NaN 输入发送到错误分支:

IEEE 754's definition of the ordinary comparison operators <, <=, … makes them very useful in general—if not when you need a total order. In particular, it is easy to write conditions so that NaN inputs will be sent to the error branch:

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

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

因为 <<= 非常有用,所以它们是在现代处理器中作为单个快速指令实现的操作——IEEE 754 的 totalOrder 谓词通常是未在硬件中实现.编程语言将快速指令映射到语言中的构造,让任何特别需要 totalOrder 的人从库中选择它,甚至自己定义它.

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. 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.

这篇关于为什么 Rust 不通过 f64 和 f32 的 Ord 特征实现总排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆