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

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

问题描述

虽然Rust中的所有整数类型都实现了 Ord 强调总排序,浮点类型只实现 PartialOrd 。这意味着可能存在无法比较的浮点值。这似乎很难消化,因为浮点数可以被认为是实数的近似,而实数恰好是一个完全有序的集合。即使增加了正面和负面的无限可以保持一组实际的数字完全有序。为什么在Rust这个奇怪的选择?

这个限制意味着一个通用的排序/搜索算法只能对数字进行部分排序。 IEEE 754标准似乎提供了总排序谓词



NaN在泛型代码中有那么多问题?

解决方案

您的问题恰恰是什么?你问NaN是否存在,或者是否可以由于意外或自愿计算而获得?是的,它确实可以。当提供的订单不是全部订单时,需要全部订单的数据结构完全崩溃。你甚至不希望即使有一个例外的价值与它本身不同,因为它会打破结构的不变性,并意味着今后会发生任何事情。只要没有显示出任何问题,NaN不应该被认为是无害的,虽然

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

  if(!(x < = MAX)){// NaN使这个条件成立
error();


if(!(x> = MIN)){// NaN使这个条件成立
error();因为< >



>和 <= 非常有用,它们是在现代处理器中作为单个快速指令实现的操作 - 来自IEEE 754的totalOrder谓词通常不以硬件实现。编程语言将快速指令映射为语言结构,并让任何异常需要totalOrder的人从库中选择它,甚至自己定义它。


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?

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.

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

解决方案

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'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();
}

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并没有通过Ord特性为f64和f32实现总排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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