Skip to content

Commit 42fc32f

Browse files
committed
auto merge of #12869 : thestinger/rust/cmp, r=brson
The `Float` trait provides correct `min` and `max` methods on floating point types, providing a consistent result regardless of the order the parameters are passed. These generic functions do not take the necessary performance hit to correctly support a partial order, so the true requirement should be given as a type bound. Closes #12712
2 parents e99d523 + 4e1c215 commit 42fc32f

File tree

4 files changed

+7
-9
lines changed

4 files changed

+7
-9
lines changed

src/doc/guide-tasks.md

-2
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,6 @@ fn pnorm(nums: &~[f64], p: uint) -> f64 {
351351
352352
fn main() {
353353
let numbers = vec::from_fn(1000000, |_| rand::random::<f64>());
354-
println!("Inf-norm = {}", *numbers.iter().max().unwrap());
355-
356354
let numbers_arc = Arc::new(numbers);
357355
358356
for num in range(1u, 10) {

src/libstd/cmp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ pub trait Equiv<T> {
184184
}
185185

186186
#[inline]
187-
pub fn min<T:Ord>(v1: T, v2: T) -> T {
187+
pub fn min<T: TotalOrd>(v1: T, v2: T) -> T {
188188
if v1 < v2 { v1 } else { v2 }
189189
}
190190

191191
#[inline]
192-
pub fn max<T:Ord>(v1: T, v2: T) -> T {
192+
pub fn max<T: TotalOrd>(v1: T, v2: T) -> T {
193193
if v1 > v2 { v1 } else { v2 }
194194
}
195195

src/libstd/iter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use cmp;
6868
use num::{Zero, One, CheckedAdd, CheckedSub, Saturating, ToPrimitive, Int};
6969
use option::{Option, Some, None};
7070
use ops::{Add, Mul, Sub};
71-
use cmp::{Eq, Ord};
71+
use cmp::{Eq, Ord, TotalOrd};
7272
use clone::Clone;
7373
use uint;
7474
use mem;
@@ -626,7 +626,7 @@ pub trait Iterator<A> {
626626
/// assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10);
627627
/// ```
628628
#[inline]
629-
fn max_by<B: Ord>(&mut self, f: |&A| -> B) -> Option<A> {
629+
fn max_by<B: TotalOrd>(&mut self, f: |&A| -> B) -> Option<A> {
630630
self.fold(None, |max: Option<(A, B)>, x| {
631631
let x_val = f(&x);
632632
match max {
@@ -650,7 +650,7 @@ pub trait Iterator<A> {
650650
/// assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
651651
/// ```
652652
#[inline]
653-
fn min_by<B: Ord>(&mut self, f: |&A| -> B) -> Option<A> {
653+
fn min_by<B: TotalOrd>(&mut self, f: |&A| -> B) -> Option<A> {
654654
self.fold(None, |min: Option<(A, B)>, x| {
655655
let x_val = f(&x);
656656
match min {
@@ -917,7 +917,7 @@ pub trait OrdIterator<A> {
917917
fn min_max(&mut self) -> MinMaxResult<A>;
918918
}
919919

920-
impl<A: Ord, T: Iterator<A>> OrdIterator<A> for T {
920+
impl<A: TotalOrd, T: Iterator<A>> OrdIterator<A> for T {
921921
#[inline]
922922
fn max(&mut self) -> Option<A> {
923923
self.fold(None, |max, x| {

src/libtest/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ impl MetricMap {
10701070
if delta.abs() <= noise {
10711071
LikelyNoise
10721072
} else {
1073-
let pct = delta.abs() / cmp::max(vold.value, f64::EPSILON) * 100.0;
1073+
let pct = delta.abs() / vold.value.max(f64::EPSILON) * 100.0;
10741074
if vold.noise < 0.0 {
10751075
// When 'noise' is negative, it means we want
10761076
// to see deltas that go up over time, and can

0 commit comments

Comments
 (0)