Skip to content

Commit c1eaafe

Browse files
committed
auto merge of #16425 : nham/rust/fix_nan_format, r=alexcrichton
Currently, this: println!("{}", std::f64::NAN); prints "-NaN". This commit is an attempt to change that to "NaN" instead.
2 parents e8204a8 + 04233a1 commit c1eaafe

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/libcore/fmt/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use collections::Collection;
1919
use iter::{Iterator, range};
2020
use kinds::Copy;
2121
use mem;
22+
use num::Float;
2223
use option::{Option, Some, None};
2324
use ops::Deref;
2425
use result::{Ok, Err};
@@ -584,7 +585,7 @@ macro_rules! floating(($ty:ident) => {
584585
float::ExpNone,
585586
false,
586587
|bytes| {
587-
fmt.pad_integral(*self >= 0.0, "", bytes)
588+
fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
588589
})
589590
}
590591
}
@@ -605,7 +606,7 @@ macro_rules! floating(($ty:ident) => {
605606
float::ExpDec,
606607
false,
607608
|bytes| {
608-
fmt.pad_integral(*self >= 0.0, "", bytes)
609+
fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
609610
})
610611
}
611612
}
@@ -626,7 +627,7 @@ macro_rules! floating(($ty:ident) => {
626627
float::ExpDec,
627628
true,
628629
|bytes| {
629-
fmt.pad_integral(*self >= 0.0, "", bytes)
630+
fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
630631
})
631632
}
632633
}

src/test/run-pass/format-nan.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main() {
12+
use std::f64;
13+
let x = "NaN".to_string();
14+
assert_eq!(format!("{}", f64::NAN), x);
15+
assert_eq!(format!("{:e}", f64::NAN), x);
16+
assert_eq!(format!("{:E}", f64::NAN), x);
17+
}
18+

0 commit comments

Comments
 (0)