]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/f64.rs
Auto merge of #29995 - DanielJCampbell:Expanded-Span-Printing, r=nrc
[rust.git] / src / libstd / num / f64.rs
1 // Copyright 2012-2015 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 //! The 64-bit floating point type.
12 //!
13 //! *[See also the `f64` primitive type](../primitive.f64.html).*
14
15 #![stable(feature = "rust1", since = "1.0.0")]
16 #![allow(missing_docs)]
17
18 use core::num;
19 use intrinsics;
20 use libc::c_int;
21 use num::{FpCategory, ParseFloatError};
22
23 #[stable(feature = "rust1", since = "1.0.0")]
24 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
25 #[stable(feature = "rust1", since = "1.0.0")]
26 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
27 #[stable(feature = "rust1", since = "1.0.0")]
28 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
29 #[stable(feature = "rust1", since = "1.0.0")]
30 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub use core::f64::consts;
33
34 #[allow(dead_code)]
35 mod cmath {
36     use libc::{c_double, c_int};
37
38     #[link_name = "m"]
39     extern {
40         pub fn acos(n: c_double) -> c_double;
41         pub fn asin(n: c_double) -> c_double;
42         pub fn atan(n: c_double) -> c_double;
43         pub fn atan2(a: c_double, b: c_double) -> c_double;
44         pub fn cbrt(n: c_double) -> c_double;
45         pub fn cosh(n: c_double) -> c_double;
46         pub fn erf(n: c_double) -> c_double;
47         pub fn erfc(n: c_double) -> c_double;
48         pub fn expm1(n: c_double) -> c_double;
49         pub fn fdim(a: c_double, b: c_double) -> c_double;
50         pub fn fmax(a: c_double, b: c_double) -> c_double;
51         pub fn fmin(a: c_double, b: c_double) -> c_double;
52         pub fn fmod(a: c_double, b: c_double) -> c_double;
53         pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
54         pub fn ilogb(n: c_double) -> c_int;
55         pub fn ldexp(x: c_double, n: c_int) -> c_double;
56         pub fn logb(n: c_double) -> c_double;
57         pub fn log1p(n: c_double) -> c_double;
58         pub fn nextafter(x: c_double, y: c_double) -> c_double;
59         pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
60         pub fn sinh(n: c_double) -> c_double;
61         pub fn tan(n: c_double) -> c_double;
62         pub fn tanh(n: c_double) -> c_double;
63         pub fn tgamma(n: c_double) -> c_double;
64
65         // These are commonly only available for doubles
66
67         pub fn j0(n: c_double) -> c_double;
68         pub fn j1(n: c_double) -> c_double;
69         pub fn jn(i: c_int, n: c_double) -> c_double;
70
71         pub fn y0(n: c_double) -> c_double;
72         pub fn y1(n: c_double) -> c_double;
73         pub fn yn(i: c_int, n: c_double) -> c_double;
74
75         #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
76         pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
77
78         #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
79         pub fn hypot(x: c_double, y: c_double) -> c_double;
80     }
81 }
82
83 #[cfg(not(test))]
84 #[lang = "f64"]
85 impl f64 {
86     /// Parses a float as with a given radix
87     #[unstable(feature = "float_from_str_radix", reason = "recently moved API",
88                issue = "27736")]
89     #[rustc_deprecated(since = "1.4.0",
90                  reason = "unclear how useful or correct this is")]
91     #[allow(deprecated)]
92     pub fn from_str_radix(s: &str, radix: u32) -> Result<f64, ParseFloatError> {
93         num::Float::from_str_radix(s, radix)
94     }
95
96     /// Returns `true` if this value is `NaN` and false otherwise.
97     ///
98     /// ```
99     /// use std::f64;
100     ///
101     /// let nan = f64::NAN;
102     /// let f = 7.0_f64;
103     ///
104     /// assert!(nan.is_nan());
105     /// assert!(!f.is_nan());
106     /// ```
107     #[stable(feature = "rust1", since = "1.0.0")]
108     #[inline]
109     pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
110
111     /// Returns `true` if this value is positive infinity or negative infinity and
112     /// false otherwise.
113     ///
114     /// ```
115     /// use std::f64;
116     ///
117     /// let f = 7.0f64;
118     /// let inf = f64::INFINITY;
119     /// let neg_inf = f64::NEG_INFINITY;
120     /// let nan = f64::NAN;
121     ///
122     /// assert!(!f.is_infinite());
123     /// assert!(!nan.is_infinite());
124     ///
125     /// assert!(inf.is_infinite());
126     /// assert!(neg_inf.is_infinite());
127     /// ```
128     #[stable(feature = "rust1", since = "1.0.0")]
129     #[inline]
130     pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
131
132     /// Returns `true` if this number is neither infinite nor `NaN`.
133     ///
134     /// ```
135     /// use std::f64;
136     ///
137     /// let f = 7.0f64;
138     /// let inf: f64 = f64::INFINITY;
139     /// let neg_inf: f64 = f64::NEG_INFINITY;
140     /// let nan: f64 = f64::NAN;
141     ///
142     /// assert!(f.is_finite());
143     ///
144     /// assert!(!nan.is_finite());
145     /// assert!(!inf.is_finite());
146     /// assert!(!neg_inf.is_finite());
147     /// ```
148     #[stable(feature = "rust1", since = "1.0.0")]
149     #[inline]
150     pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
151
152     /// Returns `true` if the number is neither zero, infinite,
153     /// [subnormal][subnormal], or `NaN`.
154     ///
155     /// ```
156     /// use std::f32;
157     ///
158     /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f64
159     /// let max = f32::MAX;
160     /// let lower_than_min = 1.0e-40_f32;
161     /// let zero = 0.0f32;
162     ///
163     /// assert!(min.is_normal());
164     /// assert!(max.is_normal());
165     ///
166     /// assert!(!zero.is_normal());
167     /// assert!(!f32::NAN.is_normal());
168     /// assert!(!f32::INFINITY.is_normal());
169     /// // Values between `0` and `min` are Subnormal.
170     /// assert!(!lower_than_min.is_normal());
171     /// ```
172     /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
173     #[stable(feature = "rust1", since = "1.0.0")]
174     #[inline]
175     pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
176
177     /// Returns the floating point category of the number. If only one property
178     /// is going to be tested, it is generally faster to use the specific
179     /// predicate instead.
180     ///
181     /// ```
182     /// use std::num::FpCategory;
183     /// use std::f64;
184     ///
185     /// let num = 12.4_f64;
186     /// let inf = f64::INFINITY;
187     ///
188     /// assert_eq!(num.classify(), FpCategory::Normal);
189     /// assert_eq!(inf.classify(), FpCategory::Infinite);
190     /// ```
191     #[stable(feature = "rust1", since = "1.0.0")]
192     #[inline]
193     pub fn classify(self) -> FpCategory { num::Float::classify(self) }
194
195     /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
196     /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
197     /// The floating point encoding is documented in the [Reference][floating-point].
198     ///
199     /// ```
200     /// #![feature(float_extras)]
201     ///
202     /// let num = 2.0f64;
203     ///
204     /// // (8388608, -22, 1)
205     /// let (mantissa, exponent, sign) = num.integer_decode();
206     /// let sign_f = sign as f64;
207     /// let mantissa_f = mantissa as f64;
208     /// let exponent_f = num.powf(exponent as f64);
209     ///
210     /// // 1 * 8388608 * 2^(-22) == 2
211     /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
212     ///
213     /// assert!(abs_difference < 1e-10);
214     /// ```
215     /// [floating-point]: ../../../../../reference.html#machine-types
216     #[unstable(feature = "float_extras", reason = "signature is undecided",
217                issue = "27752")]
218     #[inline]
219     pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
220
221     /// Returns the largest integer less than or equal to a number.
222     ///
223     /// ```
224     /// let f = 3.99_f64;
225     /// let g = 3.0_f64;
226     ///
227     /// assert_eq!(f.floor(), 3.0);
228     /// assert_eq!(g.floor(), 3.0);
229     /// ```
230     #[stable(feature = "rust1", since = "1.0.0")]
231     #[inline]
232     pub fn floor(self) -> f64 {
233         unsafe { intrinsics::floorf64(self) }
234     }
235
236     /// Returns the smallest integer greater than or equal to a number.
237     ///
238     /// ```
239     /// let f = 3.01_f64;
240     /// let g = 4.0_f64;
241     ///
242     /// assert_eq!(f.ceil(), 4.0);
243     /// assert_eq!(g.ceil(), 4.0);
244     /// ```
245     #[stable(feature = "rust1", since = "1.0.0")]
246     #[inline]
247     pub fn ceil(self) -> f64 {
248         unsafe { intrinsics::ceilf64(self) }
249     }
250
251     /// Returns the nearest integer to a number. Round half-way cases away from
252     /// `0.0`.
253     ///
254     /// ```
255     /// let f = 3.3_f64;
256     /// let g = -3.3_f64;
257     ///
258     /// assert_eq!(f.round(), 3.0);
259     /// assert_eq!(g.round(), -3.0);
260     /// ```
261     #[stable(feature = "rust1", since = "1.0.0")]
262     #[inline]
263     pub fn round(self) -> f64 {
264         unsafe { intrinsics::roundf64(self) }
265     }
266
267     /// Returns the integer part of a number.
268     ///
269     /// ```
270     /// let f = 3.3_f64;
271     /// let g = -3.7_f64;
272     ///
273     /// assert_eq!(f.trunc(), 3.0);
274     /// assert_eq!(g.trunc(), -3.0);
275     /// ```
276     #[stable(feature = "rust1", since = "1.0.0")]
277     #[inline]
278     pub fn trunc(self) -> f64 {
279         unsafe { intrinsics::truncf64(self) }
280     }
281
282     /// Returns the fractional part of a number.
283     ///
284     /// ```
285     /// let x = 3.5_f64;
286     /// let y = -3.5_f64;
287     /// let abs_difference_x = (x.fract() - 0.5).abs();
288     /// let abs_difference_y = (y.fract() - (-0.5)).abs();
289     ///
290     /// assert!(abs_difference_x < 1e-10);
291     /// assert!(abs_difference_y < 1e-10);
292     /// ```
293     #[stable(feature = "rust1", since = "1.0.0")]
294     #[inline]
295     pub fn fract(self) -> f64 { self - self.trunc() }
296
297     /// Computes the absolute value of `self`. Returns `NAN` if the
298     /// number is `NAN`.
299     ///
300     /// ```
301     /// use std::f64;
302     ///
303     /// let x = 3.5_f64;
304     /// let y = -3.5_f64;
305     ///
306     /// let abs_difference_x = (x.abs() - x).abs();
307     /// let abs_difference_y = (y.abs() - (-y)).abs();
308     ///
309     /// assert!(abs_difference_x < 1e-10);
310     /// assert!(abs_difference_y < 1e-10);
311     ///
312     /// assert!(f64::NAN.abs().is_nan());
313     /// ```
314     #[stable(feature = "rust1", since = "1.0.0")]
315     #[inline]
316     pub fn abs(self) -> f64 { num::Float::abs(self) }
317
318     /// Returns a number that represents the sign of `self`.
319     ///
320     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
321     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
322     /// - `NAN` if the number is `NAN`
323     ///
324     /// ```
325     /// use std::f64;
326     ///
327     /// let f = 3.5_f64;
328     ///
329     /// assert_eq!(f.signum(), 1.0);
330     /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
331     ///
332     /// assert!(f64::NAN.signum().is_nan());
333     /// ```
334     #[stable(feature = "rust1", since = "1.0.0")]
335     #[inline]
336     pub fn signum(self) -> f64 { num::Float::signum(self) }
337
338     /// Returns `true` if `self`'s sign bit is positive, including
339     /// `+0.0` and `INFINITY`.
340     ///
341     /// ```
342     /// use std::f64;
343     ///
344     /// let nan: f64 = f64::NAN;
345     ///
346     /// let f = 7.0_f64;
347     /// let g = -7.0_f64;
348     ///
349     /// assert!(f.is_sign_positive());
350     /// assert!(!g.is_sign_positive());
351     /// // Requires both tests to determine if is `NaN`
352     /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
353     /// ```
354     #[stable(feature = "rust1", since = "1.0.0")]
355     #[inline]
356     pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
357
358     #[stable(feature = "rust1", since = "1.0.0")]
359     #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
360     #[inline]
361     pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
362
363     /// Returns `true` if `self`'s sign is negative, including `-0.0`
364     /// and `NEG_INFINITY`.
365     ///
366     /// ```
367     /// use std::f64;
368     ///
369     /// let nan = f64::NAN;
370     ///
371     /// let f = 7.0_f64;
372     /// let g = -7.0_f64;
373     ///
374     /// assert!(!f.is_sign_negative());
375     /// assert!(g.is_sign_negative());
376     /// // Requires both tests to determine if is `NaN`.
377     /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
378     /// ```
379     #[stable(feature = "rust1", since = "1.0.0")]
380     #[inline]
381     pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
382
383     #[stable(feature = "rust1", since = "1.0.0")]
384     #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
385     #[inline]
386     pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) }
387
388     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
389     /// error. This produces a more accurate result with better performance than
390     /// a separate multiplication operation followed by an add.
391     ///
392     /// ```
393     /// let m = 10.0_f64;
394     /// let x = 4.0_f64;
395     /// let b = 60.0_f64;
396     ///
397     /// // 100.0
398     /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
399     ///
400     /// assert!(abs_difference < 1e-10);
401     /// ```
402     #[stable(feature = "rust1", since = "1.0.0")]
403     #[inline]
404     pub fn mul_add(self, a: f64, b: f64) -> f64 {
405         unsafe { intrinsics::fmaf64(self, a, b) }
406     }
407
408     /// Takes the reciprocal (inverse) of a number, `1/x`.
409     ///
410     /// ```
411     /// let x = 2.0_f64;
412     /// let abs_difference = (x.recip() - (1.0/x)).abs();
413     ///
414     /// assert!(abs_difference < 1e-10);
415     /// ```
416     #[stable(feature = "rust1", since = "1.0.0")]
417     #[inline]
418     pub fn recip(self) -> f64 { num::Float::recip(self) }
419
420     /// Raises a number to an integer power.
421     ///
422     /// Using this function is generally faster than using `powf`
423     ///
424     /// ```
425     /// let x = 2.0_f64;
426     /// let abs_difference = (x.powi(2) - x*x).abs();
427     ///
428     /// assert!(abs_difference < 1e-10);
429     /// ```
430     #[stable(feature = "rust1", since = "1.0.0")]
431     #[inline]
432     pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
433
434     /// Raises a number to a floating point power.
435     ///
436     /// ```
437     /// let x = 2.0_f64;
438     /// let abs_difference = (x.powf(2.0) - x*x).abs();
439     ///
440     /// assert!(abs_difference < 1e-10);
441     /// ```
442     #[stable(feature = "rust1", since = "1.0.0")]
443     #[inline]
444     pub fn powf(self, n: f64) -> f64 {
445         unsafe { intrinsics::powf64(self, n) }
446     }
447
448     /// Takes the square root of a number.
449     ///
450     /// Returns NaN if `self` is a negative number.
451     ///
452     /// ```
453     /// let positive = 4.0_f64;
454     /// let negative = -4.0_f64;
455     ///
456     /// let abs_difference = (positive.sqrt() - 2.0).abs();
457     ///
458     /// assert!(abs_difference < 1e-10);
459     /// assert!(negative.sqrt().is_nan());
460     /// ```
461     #[stable(feature = "rust1", since = "1.0.0")]
462     #[inline]
463     pub fn sqrt(self) -> f64 {
464         if self < 0.0 {
465             NAN
466         } else {
467             unsafe { intrinsics::sqrtf64(self) }
468         }
469     }
470
471     /// Returns `e^(self)`, (the exponential function).
472     ///
473     /// ```
474     /// let one = 1.0_f64;
475     /// // e^1
476     /// let e = one.exp();
477     ///
478     /// // ln(e) - 1 == 0
479     /// let abs_difference = (e.ln() - 1.0).abs();
480     ///
481     /// assert!(abs_difference < 1e-10);
482     /// ```
483     #[stable(feature = "rust1", since = "1.0.0")]
484     #[inline]
485     pub fn exp(self) -> f64 {
486         unsafe { intrinsics::expf64(self) }
487     }
488
489     /// Returns `2^(self)`.
490     ///
491     /// ```
492     /// let f = 2.0_f64;
493     ///
494     /// // 2^2 - 4 == 0
495     /// let abs_difference = (f.exp2() - 4.0).abs();
496     ///
497     /// assert!(abs_difference < 1e-10);
498     /// ```
499     #[stable(feature = "rust1", since = "1.0.0")]
500     #[inline]
501     pub fn exp2(self) -> f64 {
502         unsafe { intrinsics::exp2f64(self) }
503     }
504
505     /// Returns the natural logarithm of the number.
506     ///
507     /// ```
508     /// let one = 1.0_f64;
509     /// // e^1
510     /// let e = one.exp();
511     ///
512     /// // ln(e) - 1 == 0
513     /// let abs_difference = (e.ln() - 1.0).abs();
514     ///
515     /// assert!(abs_difference < 1e-10);
516     /// ```
517     #[stable(feature = "rust1", since = "1.0.0")]
518     #[inline]
519     pub fn ln(self) -> f64 {
520         unsafe { intrinsics::logf64(self) }
521     }
522
523     /// Returns the logarithm of the number with respect to an arbitrary base.
524     ///
525     /// ```
526     /// let ten = 10.0_f64;
527     /// let two = 2.0_f64;
528     ///
529     /// // log10(10) - 1 == 0
530     /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
531     ///
532     /// // log2(2) - 1 == 0
533     /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
534     ///
535     /// assert!(abs_difference_10 < 1e-10);
536     /// assert!(abs_difference_2 < 1e-10);
537     /// ```
538     #[stable(feature = "rust1", since = "1.0.0")]
539     #[inline]
540     pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
541
542     /// Returns the base 2 logarithm of the number.
543     ///
544     /// ```
545     /// let two = 2.0_f64;
546     ///
547     /// // log2(2) - 1 == 0
548     /// let abs_difference = (two.log2() - 1.0).abs();
549     ///
550     /// assert!(abs_difference < 1e-10);
551     /// ```
552     #[stable(feature = "rust1", since = "1.0.0")]
553     #[inline]
554     pub fn log2(self) -> f64 {
555         unsafe { intrinsics::log2f64(self) }
556     }
557
558     /// Returns the base 10 logarithm of the number.
559     ///
560     /// ```
561     /// let ten = 10.0_f64;
562     ///
563     /// // log10(10) - 1 == 0
564     /// let abs_difference = (ten.log10() - 1.0).abs();
565     ///
566     /// assert!(abs_difference < 1e-10);
567     /// ```
568     #[stable(feature = "rust1", since = "1.0.0")]
569     #[inline]
570     pub fn log10(self) -> f64 {
571         unsafe { intrinsics::log10f64(self) }
572     }
573
574     /// Converts radians to degrees.
575     ///
576     /// ```
577     /// use std::f64::consts;
578     ///
579     /// let angle = consts::PI;
580     ///
581     /// let abs_difference = (angle.to_degrees() - 180.0).abs();
582     ///
583     /// assert!(abs_difference < 1e-10);
584     /// ```
585     #[stable(feature = "rust1", since = "1.0.0")]
586     #[inline]
587     pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
588
589     /// Converts degrees to radians.
590     ///
591     /// ```
592     /// use std::f64::consts;
593     ///
594     /// let angle = 180.0_f64;
595     ///
596     /// let abs_difference = (angle.to_radians() - consts::PI).abs();
597     ///
598     /// assert!(abs_difference < 1e-10);
599     /// ```
600     #[stable(feature = "rust1", since = "1.0.0")]
601     #[inline]
602     pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
603
604     /// Constructs a floating point number of `x*2^exp`.
605     ///
606     /// ```
607     /// #![feature(float_extras)]
608     ///
609     /// // 3*2^2 - 12 == 0
610     /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs();
611     ///
612     /// assert!(abs_difference < 1e-10);
613     /// ```
614     #[unstable(feature = "float_extras",
615                reason = "pending integer conventions",
616                issue = "27752")]
617     #[inline]
618     pub fn ldexp(x: f64, exp: isize) -> f64 {
619         unsafe { cmath::ldexp(x, exp as c_int) }
620     }
621
622     /// Breaks the number into a normalized fraction and a base-2 exponent,
623     /// satisfying:
624     ///
625     ///  * `self = x * 2^exp`
626     ///  * `0.5 <= abs(x) < 1.0`
627     ///
628     /// ```
629     /// #![feature(float_extras)]
630     ///
631     /// let x = 4.0_f64;
632     ///
633     /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
634     /// let f = x.frexp();
635     /// let abs_difference_0 = (f.0 - 0.5).abs();
636     /// let abs_difference_1 = (f.1 as f64 - 3.0).abs();
637     ///
638     /// assert!(abs_difference_0 < 1e-10);
639     /// assert!(abs_difference_1 < 1e-10);
640     /// ```
641     #[unstable(feature = "float_extras",
642                reason = "pending integer conventions",
643                issue = "27752")]
644     #[inline]
645     pub fn frexp(self) -> (f64, isize) {
646         unsafe {
647             let mut exp = 0;
648             let x = cmath::frexp(self, &mut exp);
649             (x, exp as isize)
650         }
651     }
652
653     /// Returns the next representable floating-point value in the direction of
654     /// `other`.
655     ///
656     /// ```
657     /// #![feature(float_extras)]
658     ///
659     /// let x = 1.0f32;
660     ///
661     /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
662     ///
663     /// assert!(abs_diff < 1e-10);
664     /// ```
665     #[unstable(feature = "float_extras",
666                reason = "unsure about its place in the world",
667                issue = "27752")]
668     #[inline]
669     pub fn next_after(self, other: f64) -> f64 {
670         unsafe { cmath::nextafter(self, other) }
671     }
672
673     /// Returns the maximum of the two numbers.
674     ///
675     /// ```
676     /// let x = 1.0_f64;
677     /// let y = 2.0_f64;
678     ///
679     /// assert_eq!(x.max(y), y);
680     /// ```
681     ///
682     /// If one of the arguments is NaN, then the other argument is returned.
683     #[stable(feature = "rust1", since = "1.0.0")]
684     #[inline]
685     pub fn max(self, other: f64) -> f64 {
686         unsafe { cmath::fmax(self, other) }
687     }
688
689     /// Returns the minimum of the two numbers.
690     ///
691     /// ```
692     /// let x = 1.0_f64;
693     /// let y = 2.0_f64;
694     ///
695     /// assert_eq!(x.min(y), x);
696     /// ```
697     ///
698     /// If one of the arguments is NaN, then the other argument is returned.
699     #[stable(feature = "rust1", since = "1.0.0")]
700     #[inline]
701     pub fn min(self, other: f64) -> f64 {
702         unsafe { cmath::fmin(self, other) }
703     }
704
705     /// The positive difference of two numbers.
706     ///
707     /// * If `self <= other`: `0:0`
708     /// * Else: `self - other`
709     ///
710     /// ```
711     /// let x = 3.0_f64;
712     /// let y = -3.0_f64;
713     ///
714     /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
715     /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
716     ///
717     /// assert!(abs_difference_x < 1e-10);
718     /// assert!(abs_difference_y < 1e-10);
719     /// ```
720     #[stable(feature = "rust1", since = "1.0.0")]
721     #[inline]
722     pub fn abs_sub(self, other: f64) -> f64 {
723         unsafe { cmath::fdim(self, other) }
724     }
725
726     /// Takes the cubic root of a number.
727     ///
728     /// ```
729     /// let x = 8.0_f64;
730     ///
731     /// // x^(1/3) - 2 == 0
732     /// let abs_difference = (x.cbrt() - 2.0).abs();
733     ///
734     /// assert!(abs_difference < 1e-10);
735     /// ```
736     #[stable(feature = "rust1", since = "1.0.0")]
737     #[inline]
738     pub fn cbrt(self) -> f64 {
739         unsafe { cmath::cbrt(self) }
740     }
741
742     /// Calculates the length of the hypotenuse of a right-angle triangle given
743     /// legs of length `x` and `y`.
744     ///
745     /// ```
746     /// let x = 2.0_f64;
747     /// let y = 3.0_f64;
748     ///
749     /// // sqrt(x^2 + y^2)
750     /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
751     ///
752     /// assert!(abs_difference < 1e-10);
753     /// ```
754     #[stable(feature = "rust1", since = "1.0.0")]
755     #[inline]
756     pub fn hypot(self, other: f64) -> f64 {
757         unsafe { cmath::hypot(self, other) }
758     }
759
760     /// Computes the sine of a number (in radians).
761     ///
762     /// ```
763     /// use std::f64;
764     ///
765     /// let x = f64::consts::PI/2.0;
766     ///
767     /// let abs_difference = (x.sin() - 1.0).abs();
768     ///
769     /// assert!(abs_difference < 1e-10);
770     /// ```
771     #[stable(feature = "rust1", since = "1.0.0")]
772     #[inline]
773     pub fn sin(self) -> f64 {
774         unsafe { intrinsics::sinf64(self) }
775     }
776
777     /// Computes the cosine of a number (in radians).
778     ///
779     /// ```
780     /// use std::f64;
781     ///
782     /// let x = 2.0*f64::consts::PI;
783     ///
784     /// let abs_difference = (x.cos() - 1.0).abs();
785     ///
786     /// assert!(abs_difference < 1e-10);
787     /// ```
788     #[stable(feature = "rust1", since = "1.0.0")]
789     #[inline]
790     pub fn cos(self) -> f64 {
791         unsafe { intrinsics::cosf64(self) }
792     }
793
794     /// Computes the tangent of a number (in radians).
795     ///
796     /// ```
797     /// use std::f64;
798     ///
799     /// let x = f64::consts::PI/4.0;
800     /// let abs_difference = (x.tan() - 1.0).abs();
801     ///
802     /// assert!(abs_difference < 1e-14);
803     /// ```
804     #[stable(feature = "rust1", since = "1.0.0")]
805     #[inline]
806     pub fn tan(self) -> f64 {
807         unsafe { cmath::tan(self) }
808     }
809
810     /// Computes the arcsine of a number. Return value is in radians in
811     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
812     /// [-1, 1].
813     ///
814     /// ```
815     /// use std::f64;
816     ///
817     /// let f = f64::consts::PI / 2.0;
818     ///
819     /// // asin(sin(pi/2))
820     /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
821     ///
822     /// assert!(abs_difference < 1e-10);
823     /// ```
824     #[stable(feature = "rust1", since = "1.0.0")]
825     #[inline]
826     pub fn asin(self) -> f64 {
827         unsafe { cmath::asin(self) }
828     }
829
830     /// Computes the arccosine of a number. Return value is in radians in
831     /// the range [0, pi] or NaN if the number is outside the range
832     /// [-1, 1].
833     ///
834     /// ```
835     /// use std::f64;
836     ///
837     /// let f = f64::consts::PI / 4.0;
838     ///
839     /// // acos(cos(pi/4))
840     /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
841     ///
842     /// assert!(abs_difference < 1e-10);
843     /// ```
844     #[stable(feature = "rust1", since = "1.0.0")]
845     #[inline]
846     pub fn acos(self) -> f64 {
847         unsafe { cmath::acos(self) }
848     }
849
850     /// Computes the arctangent of a number. Return value is in radians in the
851     /// range [-pi/2, pi/2];
852     ///
853     /// ```
854     /// let f = 1.0_f64;
855     ///
856     /// // atan(tan(1))
857     /// let abs_difference = (f.tan().atan() - 1.0).abs();
858     ///
859     /// assert!(abs_difference < 1e-10);
860     /// ```
861     #[stable(feature = "rust1", since = "1.0.0")]
862     #[inline]
863     pub fn atan(self) -> f64 {
864         unsafe { cmath::atan(self) }
865     }
866
867     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
868     ///
869     /// * `x = 0`, `y = 0`: `0`
870     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
871     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
872     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
873     ///
874     /// ```
875     /// use std::f64;
876     ///
877     /// let pi = f64::consts::PI;
878     /// // All angles from horizontal right (+x)
879     /// // 45 deg counter-clockwise
880     /// let x1 = 3.0_f64;
881     /// let y1 = -3.0_f64;
882     ///
883     /// // 135 deg clockwise
884     /// let x2 = -3.0_f64;
885     /// let y2 = 3.0_f64;
886     ///
887     /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
888     /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
889     ///
890     /// assert!(abs_difference_1 < 1e-10);
891     /// assert!(abs_difference_2 < 1e-10);
892     /// ```
893     #[stable(feature = "rust1", since = "1.0.0")]
894     #[inline]
895     pub fn atan2(self, other: f64) -> f64 {
896         unsafe { cmath::atan2(self, other) }
897     }
898
899     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
900     /// `(sin(x), cos(x))`.
901     ///
902     /// ```
903     /// use std::f64;
904     ///
905     /// let x = f64::consts::PI/4.0;
906     /// let f = x.sin_cos();
907     ///
908     /// let abs_difference_0 = (f.0 - x.sin()).abs();
909     /// let abs_difference_1 = (f.1 - x.cos()).abs();
910     ///
911     /// assert!(abs_difference_0 < 1e-10);
912     /// assert!(abs_difference_0 < 1e-10);
913     /// ```
914     #[stable(feature = "rust1", since = "1.0.0")]
915     #[inline]
916     pub fn sin_cos(self) -> (f64, f64) {
917         (self.sin(), self.cos())
918     }
919
920     /// Returns `e^(self) - 1` in a way that is accurate even if the
921     /// number is close to zero.
922     ///
923     /// ```
924     /// let x = 7.0_f64;
925     ///
926     /// // e^(ln(7)) - 1
927     /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
928     ///
929     /// assert!(abs_difference < 1e-10);
930     /// ```
931     #[stable(feature = "rust1", since = "1.0.0")]
932     #[inline]
933     pub fn exp_m1(self) -> f64 {
934         unsafe { cmath::expm1(self) }
935     }
936
937     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
938     /// the operations were performed separately.
939     ///
940     /// ```
941     /// use std::f64;
942     ///
943     /// let x = f64::consts::E - 1.0;
944     ///
945     /// // ln(1 + (e - 1)) == ln(e) == 1
946     /// let abs_difference = (x.ln_1p() - 1.0).abs();
947     ///
948     /// assert!(abs_difference < 1e-10);
949     /// ```
950     #[stable(feature = "rust1", since = "1.0.0")]
951     #[inline]
952     pub fn ln_1p(self) -> f64 {
953         unsafe { cmath::log1p(self) }
954     }
955
956     /// Hyperbolic sine function.
957     ///
958     /// ```
959     /// use std::f64;
960     ///
961     /// let e = f64::consts::E;
962     /// let x = 1.0_f64;
963     ///
964     /// let f = x.sinh();
965     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
966     /// let g = (e*e - 1.0)/(2.0*e);
967     /// let abs_difference = (f - g).abs();
968     ///
969     /// assert!(abs_difference < 1e-10);
970     /// ```
971     #[stable(feature = "rust1", since = "1.0.0")]
972     #[inline]
973     pub fn sinh(self) -> f64 {
974         unsafe { cmath::sinh(self) }
975     }
976
977     /// Hyperbolic cosine function.
978     ///
979     /// ```
980     /// use std::f64;
981     ///
982     /// let e = f64::consts::E;
983     /// let x = 1.0_f64;
984     /// let f = x.cosh();
985     /// // Solving cosh() at 1 gives this result
986     /// let g = (e*e + 1.0)/(2.0*e);
987     /// let abs_difference = (f - g).abs();
988     ///
989     /// // Same result
990     /// assert!(abs_difference < 1.0e-10);
991     /// ```
992     #[stable(feature = "rust1", since = "1.0.0")]
993     #[inline]
994     pub fn cosh(self) -> f64 {
995         unsafe { cmath::cosh(self) }
996     }
997
998     /// Hyperbolic tangent function.
999     ///
1000     /// ```
1001     /// use std::f64;
1002     ///
1003     /// let e = f64::consts::E;
1004     /// let x = 1.0_f64;
1005     ///
1006     /// let f = x.tanh();
1007     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1008     /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1009     /// let abs_difference = (f - g).abs();
1010     ///
1011     /// assert!(abs_difference < 1.0e-10);
1012     /// ```
1013     #[stable(feature = "rust1", since = "1.0.0")]
1014     #[inline]
1015     pub fn tanh(self) -> f64 {
1016         unsafe { cmath::tanh(self) }
1017     }
1018
1019     /// Inverse hyperbolic sine function.
1020     ///
1021     /// ```
1022     /// let x = 1.0_f64;
1023     /// let f = x.sinh().asinh();
1024     ///
1025     /// let abs_difference = (f - x).abs();
1026     ///
1027     /// assert!(abs_difference < 1.0e-10);
1028     /// ```
1029     #[stable(feature = "rust1", since = "1.0.0")]
1030     #[inline]
1031     pub fn asinh(self) -> f64 {
1032         match self {
1033             NEG_INFINITY => NEG_INFINITY,
1034             x => (x + ((x * x) + 1.0).sqrt()).ln(),
1035         }
1036     }
1037
1038     /// Inverse hyperbolic cosine function.
1039     ///
1040     /// ```
1041     /// let x = 1.0_f64;
1042     /// let f = x.cosh().acosh();
1043     ///
1044     /// let abs_difference = (f - x).abs();
1045     ///
1046     /// assert!(abs_difference < 1.0e-10);
1047     /// ```
1048     #[stable(feature = "rust1", since = "1.0.0")]
1049     #[inline]
1050     pub fn acosh(self) -> f64 {
1051         match self {
1052             x if x < 1.0 => NAN,
1053             x => (x + ((x * x) - 1.0).sqrt()).ln(),
1054         }
1055     }
1056
1057     /// Inverse hyperbolic tangent function.
1058     ///
1059     /// ```
1060     /// use std::f64;
1061     ///
1062     /// let e = f64::consts::E;
1063     /// let f = e.tanh().atanh();
1064     ///
1065     /// let abs_difference = (f - e).abs();
1066     ///
1067     /// assert!(abs_difference < 1.0e-10);
1068     /// ```
1069     #[stable(feature = "rust1", since = "1.0.0")]
1070     #[inline]
1071     pub fn atanh(self) -> f64 {
1072         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1073     }
1074 }
1075
1076 #[cfg(test)]
1077 mod tests {
1078     use f64;
1079     use f64::*;
1080     use num::*;
1081     use num::FpCategory as Fp;
1082
1083     #[test]
1084     fn test_num_f64() {
1085         test_num(10f64, 2f64);
1086     }
1087
1088     #[test]
1089     fn test_min_nan() {
1090         assert_eq!(NAN.min(2.0), 2.0);
1091         assert_eq!(2.0f64.min(NAN), 2.0);
1092     }
1093
1094     #[test]
1095     fn test_max_nan() {
1096         assert_eq!(NAN.max(2.0), 2.0);
1097         assert_eq!(2.0f64.max(NAN), 2.0);
1098     }
1099
1100     #[test]
1101     fn test_nan() {
1102         let nan: f64 = NAN;
1103         assert!(nan.is_nan());
1104         assert!(!nan.is_infinite());
1105         assert!(!nan.is_finite());
1106         assert!(!nan.is_normal());
1107         assert!(!nan.is_sign_positive());
1108         assert!(!nan.is_sign_negative());
1109         assert_eq!(Fp::Nan, nan.classify());
1110     }
1111
1112     #[test]
1113     fn test_infinity() {
1114         let inf: f64 = INFINITY;
1115         assert!(inf.is_infinite());
1116         assert!(!inf.is_finite());
1117         assert!(inf.is_sign_positive());
1118         assert!(!inf.is_sign_negative());
1119         assert!(!inf.is_nan());
1120         assert!(!inf.is_normal());
1121         assert_eq!(Fp::Infinite, inf.classify());
1122     }
1123
1124     #[test]
1125     fn test_neg_infinity() {
1126         let neg_inf: f64 = NEG_INFINITY;
1127         assert!(neg_inf.is_infinite());
1128         assert!(!neg_inf.is_finite());
1129         assert!(!neg_inf.is_sign_positive());
1130         assert!(neg_inf.is_sign_negative());
1131         assert!(!neg_inf.is_nan());
1132         assert!(!neg_inf.is_normal());
1133         assert_eq!(Fp::Infinite, neg_inf.classify());
1134     }
1135
1136     #[test]
1137     fn test_zero() {
1138         let zero: f64 = 0.0f64;
1139         assert_eq!(0.0, zero);
1140         assert!(!zero.is_infinite());
1141         assert!(zero.is_finite());
1142         assert!(zero.is_sign_positive());
1143         assert!(!zero.is_sign_negative());
1144         assert!(!zero.is_nan());
1145         assert!(!zero.is_normal());
1146         assert_eq!(Fp::Zero, zero.classify());
1147     }
1148
1149     #[test]
1150     fn test_neg_zero() {
1151         let neg_zero: f64 = -0.0;
1152         assert_eq!(0.0, neg_zero);
1153         assert!(!neg_zero.is_infinite());
1154         assert!(neg_zero.is_finite());
1155         assert!(!neg_zero.is_sign_positive());
1156         assert!(neg_zero.is_sign_negative());
1157         assert!(!neg_zero.is_nan());
1158         assert!(!neg_zero.is_normal());
1159         assert_eq!(Fp::Zero, neg_zero.classify());
1160     }
1161
1162     #[test]
1163     fn test_one() {
1164         let one: f64 = 1.0f64;
1165         assert_eq!(1.0, one);
1166         assert!(!one.is_infinite());
1167         assert!(one.is_finite());
1168         assert!(one.is_sign_positive());
1169         assert!(!one.is_sign_negative());
1170         assert!(!one.is_nan());
1171         assert!(one.is_normal());
1172         assert_eq!(Fp::Normal, one.classify());
1173     }
1174
1175     #[test]
1176     fn test_is_nan() {
1177         let nan: f64 = NAN;
1178         let inf: f64 = INFINITY;
1179         let neg_inf: f64 = NEG_INFINITY;
1180         assert!(nan.is_nan());
1181         assert!(!0.0f64.is_nan());
1182         assert!(!5.3f64.is_nan());
1183         assert!(!(-10.732f64).is_nan());
1184         assert!(!inf.is_nan());
1185         assert!(!neg_inf.is_nan());
1186     }
1187
1188     #[test]
1189     fn test_is_infinite() {
1190         let nan: f64 = NAN;
1191         let inf: f64 = INFINITY;
1192         let neg_inf: f64 = NEG_INFINITY;
1193         assert!(!nan.is_infinite());
1194         assert!(inf.is_infinite());
1195         assert!(neg_inf.is_infinite());
1196         assert!(!0.0f64.is_infinite());
1197         assert!(!42.8f64.is_infinite());
1198         assert!(!(-109.2f64).is_infinite());
1199     }
1200
1201     #[test]
1202     fn test_is_finite() {
1203         let nan: f64 = NAN;
1204         let inf: f64 = INFINITY;
1205         let neg_inf: f64 = NEG_INFINITY;
1206         assert!(!nan.is_finite());
1207         assert!(!inf.is_finite());
1208         assert!(!neg_inf.is_finite());
1209         assert!(0.0f64.is_finite());
1210         assert!(42.8f64.is_finite());
1211         assert!((-109.2f64).is_finite());
1212     }
1213
1214     #[test]
1215     fn test_is_normal() {
1216         let nan: f64 = NAN;
1217         let inf: f64 = INFINITY;
1218         let neg_inf: f64 = NEG_INFINITY;
1219         let zero: f64 = 0.0f64;
1220         let neg_zero: f64 = -0.0;
1221         assert!(!nan.is_normal());
1222         assert!(!inf.is_normal());
1223         assert!(!neg_inf.is_normal());
1224         assert!(!zero.is_normal());
1225         assert!(!neg_zero.is_normal());
1226         assert!(1f64.is_normal());
1227         assert!(1e-307f64.is_normal());
1228         assert!(!1e-308f64.is_normal());
1229     }
1230
1231     #[test]
1232     fn test_classify() {
1233         let nan: f64 = NAN;
1234         let inf: f64 = INFINITY;
1235         let neg_inf: f64 = NEG_INFINITY;
1236         let zero: f64 = 0.0f64;
1237         let neg_zero: f64 = -0.0;
1238         assert_eq!(nan.classify(), Fp::Nan);
1239         assert_eq!(inf.classify(), Fp::Infinite);
1240         assert_eq!(neg_inf.classify(), Fp::Infinite);
1241         assert_eq!(zero.classify(), Fp::Zero);
1242         assert_eq!(neg_zero.classify(), Fp::Zero);
1243         assert_eq!(1e-307f64.classify(), Fp::Normal);
1244         assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1245     }
1246
1247     #[test]
1248     fn test_integer_decode() {
1249         assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
1250         assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
1251         assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
1252         assert_eq!(0f64.integer_decode(), (0, -1075, 1));
1253         assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
1254         assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1));
1255         assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
1256         assert_eq!(NAN.integer_decode(), (6755399441055744, 972, 1));
1257     }
1258
1259     #[test]
1260     fn test_floor() {
1261         assert_approx_eq!(1.0f64.floor(), 1.0f64);
1262         assert_approx_eq!(1.3f64.floor(), 1.0f64);
1263         assert_approx_eq!(1.5f64.floor(), 1.0f64);
1264         assert_approx_eq!(1.7f64.floor(), 1.0f64);
1265         assert_approx_eq!(0.0f64.floor(), 0.0f64);
1266         assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1267         assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1268         assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1269         assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1270         assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1271     }
1272
1273     #[test]
1274     fn test_ceil() {
1275         assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1276         assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1277         assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1278         assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1279         assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1280         assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1281         assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1282         assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1283         assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1284         assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1285     }
1286
1287     #[test]
1288     fn test_round() {
1289         assert_approx_eq!(1.0f64.round(), 1.0f64);
1290         assert_approx_eq!(1.3f64.round(), 1.0f64);
1291         assert_approx_eq!(1.5f64.round(), 2.0f64);
1292         assert_approx_eq!(1.7f64.round(), 2.0f64);
1293         assert_approx_eq!(0.0f64.round(), 0.0f64);
1294         assert_approx_eq!((-0.0f64).round(), -0.0f64);
1295         assert_approx_eq!((-1.0f64).round(), -1.0f64);
1296         assert_approx_eq!((-1.3f64).round(), -1.0f64);
1297         assert_approx_eq!((-1.5f64).round(), -2.0f64);
1298         assert_approx_eq!((-1.7f64).round(), -2.0f64);
1299     }
1300
1301     #[test]
1302     fn test_trunc() {
1303         assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1304         assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1305         assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1306         assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1307         assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1308         assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1309         assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1310         assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1311         assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1312         assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1313     }
1314
1315     #[test]
1316     fn test_fract() {
1317         assert_approx_eq!(1.0f64.fract(), 0.0f64);
1318         assert_approx_eq!(1.3f64.fract(), 0.3f64);
1319         assert_approx_eq!(1.5f64.fract(), 0.5f64);
1320         assert_approx_eq!(1.7f64.fract(), 0.7f64);
1321         assert_approx_eq!(0.0f64.fract(), 0.0f64);
1322         assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1323         assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1324         assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1325         assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1326         assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1327     }
1328
1329     #[test]
1330     fn test_abs() {
1331         assert_eq!(INFINITY.abs(), INFINITY);
1332         assert_eq!(1f64.abs(), 1f64);
1333         assert_eq!(0f64.abs(), 0f64);
1334         assert_eq!((-0f64).abs(), 0f64);
1335         assert_eq!((-1f64).abs(), 1f64);
1336         assert_eq!(NEG_INFINITY.abs(), INFINITY);
1337         assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1338         assert!(NAN.abs().is_nan());
1339     }
1340
1341     #[test]
1342     fn test_signum() {
1343         assert_eq!(INFINITY.signum(), 1f64);
1344         assert_eq!(1f64.signum(), 1f64);
1345         assert_eq!(0f64.signum(), 1f64);
1346         assert_eq!((-0f64).signum(), -1f64);
1347         assert_eq!((-1f64).signum(), -1f64);
1348         assert_eq!(NEG_INFINITY.signum(), -1f64);
1349         assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1350         assert!(NAN.signum().is_nan());
1351     }
1352
1353     #[test]
1354     fn test_is_sign_positive() {
1355         assert!(INFINITY.is_sign_positive());
1356         assert!(1f64.is_sign_positive());
1357         assert!(0f64.is_sign_positive());
1358         assert!(!(-0f64).is_sign_positive());
1359         assert!(!(-1f64).is_sign_positive());
1360         assert!(!NEG_INFINITY.is_sign_positive());
1361         assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1362         assert!(!NAN.is_sign_positive());
1363     }
1364
1365     #[test]
1366     fn test_is_sign_negative() {
1367         assert!(!INFINITY.is_sign_negative());
1368         assert!(!1f64.is_sign_negative());
1369         assert!(!0f64.is_sign_negative());
1370         assert!((-0f64).is_sign_negative());
1371         assert!((-1f64).is_sign_negative());
1372         assert!(NEG_INFINITY.is_sign_negative());
1373         assert!((1f64/NEG_INFINITY).is_sign_negative());
1374         assert!(!NAN.is_sign_negative());
1375     }
1376
1377     #[test]
1378     fn test_mul_add() {
1379         let nan: f64 = NAN;
1380         let inf: f64 = INFINITY;
1381         let neg_inf: f64 = NEG_INFINITY;
1382         assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1383         assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1384         assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1385         assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1386         assert!(nan.mul_add(7.8, 9.0).is_nan());
1387         assert_eq!(inf.mul_add(7.8, 9.0), inf);
1388         assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1389         assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1390         assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1391     }
1392
1393     #[test]
1394     fn test_recip() {
1395         let nan: f64 = NAN;
1396         let inf: f64 = INFINITY;
1397         let neg_inf: f64 = NEG_INFINITY;
1398         assert_eq!(1.0f64.recip(), 1.0);
1399         assert_eq!(2.0f64.recip(), 0.5);
1400         assert_eq!((-0.4f64).recip(), -2.5);
1401         assert_eq!(0.0f64.recip(), inf);
1402         assert!(nan.recip().is_nan());
1403         assert_eq!(inf.recip(), 0.0);
1404         assert_eq!(neg_inf.recip(), 0.0);
1405     }
1406
1407     #[test]
1408     fn test_powi() {
1409         let nan: f64 = NAN;
1410         let inf: f64 = INFINITY;
1411         let neg_inf: f64 = NEG_INFINITY;
1412         assert_eq!(1.0f64.powi(1), 1.0);
1413         assert_approx_eq!((-3.1f64).powi(2), 9.61);
1414         assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1415         assert_eq!(8.3f64.powi(0), 1.0);
1416         assert!(nan.powi(2).is_nan());
1417         assert_eq!(inf.powi(3), inf);
1418         assert_eq!(neg_inf.powi(2), inf);
1419     }
1420
1421     #[test]
1422     fn test_powf() {
1423         let nan: f64 = NAN;
1424         let inf: f64 = INFINITY;
1425         let neg_inf: f64 = NEG_INFINITY;
1426         assert_eq!(1.0f64.powf(1.0), 1.0);
1427         assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1428         assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1429         assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1430         assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1431         assert_eq!(8.3f64.powf(0.0), 1.0);
1432         assert!(nan.powf(2.0).is_nan());
1433         assert_eq!(inf.powf(2.0), inf);
1434         assert_eq!(neg_inf.powf(3.0), neg_inf);
1435     }
1436
1437     #[test]
1438     fn test_sqrt_domain() {
1439         assert!(NAN.sqrt().is_nan());
1440         assert!(NEG_INFINITY.sqrt().is_nan());
1441         assert!((-1.0f64).sqrt().is_nan());
1442         assert_eq!((-0.0f64).sqrt(), -0.0);
1443         assert_eq!(0.0f64.sqrt(), 0.0);
1444         assert_eq!(1.0f64.sqrt(), 1.0);
1445         assert_eq!(INFINITY.sqrt(), INFINITY);
1446     }
1447
1448     #[test]
1449     fn test_exp() {
1450         assert_eq!(1.0, 0.0f64.exp());
1451         assert_approx_eq!(2.718282, 1.0f64.exp());
1452         assert_approx_eq!(148.413159, 5.0f64.exp());
1453
1454         let inf: f64 = INFINITY;
1455         let neg_inf: f64 = NEG_INFINITY;
1456         let nan: f64 = NAN;
1457         assert_eq!(inf, inf.exp());
1458         assert_eq!(0.0, neg_inf.exp());
1459         assert!(nan.exp().is_nan());
1460     }
1461
1462     #[test]
1463     fn test_exp2() {
1464         assert_eq!(32.0, 5.0f64.exp2());
1465         assert_eq!(1.0, 0.0f64.exp2());
1466
1467         let inf: f64 = INFINITY;
1468         let neg_inf: f64 = NEG_INFINITY;
1469         let nan: f64 = NAN;
1470         assert_eq!(inf, inf.exp2());
1471         assert_eq!(0.0, neg_inf.exp2());
1472         assert!(nan.exp2().is_nan());
1473     }
1474
1475     #[test]
1476     fn test_ln() {
1477         let nan: f64 = NAN;
1478         let inf: f64 = INFINITY;
1479         let neg_inf: f64 = NEG_INFINITY;
1480         assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1481         assert!(nan.ln().is_nan());
1482         assert_eq!(inf.ln(), inf);
1483         assert!(neg_inf.ln().is_nan());
1484         assert!((-2.3f64).ln().is_nan());
1485         assert_eq!((-0.0f64).ln(), neg_inf);
1486         assert_eq!(0.0f64.ln(), neg_inf);
1487         assert_approx_eq!(4.0f64.ln(), 1.386294);
1488     }
1489
1490     #[test]
1491     fn test_log() {
1492         let nan: f64 = NAN;
1493         let inf: f64 = INFINITY;
1494         let neg_inf: f64 = NEG_INFINITY;
1495         assert_eq!(10.0f64.log(10.0), 1.0);
1496         assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1497         assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1498         assert!(1.0f64.log(1.0).is_nan());
1499         assert!(1.0f64.log(-13.9).is_nan());
1500         assert!(nan.log(2.3).is_nan());
1501         assert_eq!(inf.log(10.0), inf);
1502         assert!(neg_inf.log(8.8).is_nan());
1503         assert!((-2.3f64).log(0.1).is_nan());
1504         assert_eq!((-0.0f64).log(2.0), neg_inf);
1505         assert_eq!(0.0f64.log(7.0), neg_inf);
1506     }
1507
1508     #[test]
1509     fn test_log2() {
1510         let nan: f64 = NAN;
1511         let inf: f64 = INFINITY;
1512         let neg_inf: f64 = NEG_INFINITY;
1513         assert_approx_eq!(10.0f64.log2(), 3.321928);
1514         assert_approx_eq!(2.3f64.log2(), 1.201634);
1515         assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1516         assert!(nan.log2().is_nan());
1517         assert_eq!(inf.log2(), inf);
1518         assert!(neg_inf.log2().is_nan());
1519         assert!((-2.3f64).log2().is_nan());
1520         assert_eq!((-0.0f64).log2(), neg_inf);
1521         assert_eq!(0.0f64.log2(), neg_inf);
1522     }
1523
1524     #[test]
1525     fn test_log10() {
1526         let nan: f64 = NAN;
1527         let inf: f64 = INFINITY;
1528         let neg_inf: f64 = NEG_INFINITY;
1529         assert_eq!(10.0f64.log10(), 1.0);
1530         assert_approx_eq!(2.3f64.log10(), 0.361728);
1531         assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1532         assert_eq!(1.0f64.log10(), 0.0);
1533         assert!(nan.log10().is_nan());
1534         assert_eq!(inf.log10(), inf);
1535         assert!(neg_inf.log10().is_nan());
1536         assert!((-2.3f64).log10().is_nan());
1537         assert_eq!((-0.0f64).log10(), neg_inf);
1538         assert_eq!(0.0f64.log10(), neg_inf);
1539     }
1540
1541     #[test]
1542     fn test_to_degrees() {
1543         let pi: f64 = consts::PI;
1544         let nan: f64 = NAN;
1545         let inf: f64 = INFINITY;
1546         let neg_inf: f64 = NEG_INFINITY;
1547         assert_eq!(0.0f64.to_degrees(), 0.0);
1548         assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1549         assert_eq!(pi.to_degrees(), 180.0);
1550         assert!(nan.to_degrees().is_nan());
1551         assert_eq!(inf.to_degrees(), inf);
1552         assert_eq!(neg_inf.to_degrees(), neg_inf);
1553     }
1554
1555     #[test]
1556     fn test_to_radians() {
1557         let pi: f64 = consts::PI;
1558         let nan: f64 = NAN;
1559         let inf: f64 = INFINITY;
1560         let neg_inf: f64 = NEG_INFINITY;
1561         assert_eq!(0.0f64.to_radians(), 0.0);
1562         assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1563         assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1564         assert_eq!(180.0f64.to_radians(), pi);
1565         assert!(nan.to_radians().is_nan());
1566         assert_eq!(inf.to_radians(), inf);
1567         assert_eq!(neg_inf.to_radians(), neg_inf);
1568     }
1569
1570     #[test]
1571     fn test_ldexp() {
1572         // We have to use from_str until base-2 exponents
1573         // are supported in floating-point literals
1574         let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap();
1575         let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap();
1576         let f3: f64 = f64::from_str_radix("1.Cp-12", 16).unwrap();
1577         assert_eq!(f64::ldexp(1f64, -123), f1);
1578         assert_eq!(f64::ldexp(1f64, -111), f2);
1579         assert_eq!(f64::ldexp(1.75f64, -12), f3);
1580
1581         assert_eq!(f64::ldexp(0f64, -123), 0f64);
1582         assert_eq!(f64::ldexp(-0f64, -123), -0f64);
1583
1584         let inf: f64 = INFINITY;
1585         let neg_inf: f64 = NEG_INFINITY;
1586         let nan: f64 = NAN;
1587         assert_eq!(f64::ldexp(inf, -123), inf);
1588         assert_eq!(f64::ldexp(neg_inf, -123), neg_inf);
1589         assert!(f64::ldexp(nan, -123).is_nan());
1590     }
1591
1592     #[test]
1593     fn test_frexp() {
1594         // We have to use from_str until base-2 exponents
1595         // are supported in floating-point literals
1596         let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap();
1597         let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap();
1598         let f3: f64 = f64::from_str_radix("1.Cp-123", 16).unwrap();
1599         let (x1, exp1) = f1.frexp();
1600         let (x2, exp2) = f2.frexp();
1601         let (x3, exp3) = f3.frexp();
1602         assert_eq!((x1, exp1), (0.5f64, -122));
1603         assert_eq!((x2, exp2), (0.5f64, -110));
1604         assert_eq!((x3, exp3), (0.875f64, -122));
1605         assert_eq!(f64::ldexp(x1, exp1), f1);
1606         assert_eq!(f64::ldexp(x2, exp2), f2);
1607         assert_eq!(f64::ldexp(x3, exp3), f3);
1608
1609         assert_eq!(0f64.frexp(), (0f64, 0));
1610         assert_eq!((-0f64).frexp(), (-0f64, 0));
1611     }
1612
1613     #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1614     fn test_frexp_nowin() {
1615         let inf: f64 = INFINITY;
1616         let neg_inf: f64 = NEG_INFINITY;
1617         let nan: f64 = NAN;
1618         assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1619         assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1620         assert!(match nan.frexp() { (x, _) => x.is_nan() })
1621     }
1622
1623     #[test]
1624     fn test_abs_sub() {
1625         assert_eq!((-1f64).abs_sub(1f64), 0f64);
1626         assert_eq!(1f64.abs_sub(1f64), 0f64);
1627         assert_eq!(1f64.abs_sub(0f64), 1f64);
1628         assert_eq!(1f64.abs_sub(-1f64), 2f64);
1629         assert_eq!(NEG_INFINITY.abs_sub(0f64), 0f64);
1630         assert_eq!(INFINITY.abs_sub(1f64), INFINITY);
1631         assert_eq!(0f64.abs_sub(NEG_INFINITY), INFINITY);
1632         assert_eq!(0f64.abs_sub(INFINITY), 0f64);
1633     }
1634
1635     #[test]
1636     fn test_abs_sub_nowin() {
1637         assert!(NAN.abs_sub(-1f64).is_nan());
1638         assert!(1f64.abs_sub(NAN).is_nan());
1639     }
1640
1641     #[test]
1642     fn test_asinh() {
1643         assert_eq!(0.0f64.asinh(), 0.0f64);
1644         assert_eq!((-0.0f64).asinh(), -0.0f64);
1645
1646         let inf: f64 = INFINITY;
1647         let neg_inf: f64 = NEG_INFINITY;
1648         let nan: f64 = NAN;
1649         assert_eq!(inf.asinh(), inf);
1650         assert_eq!(neg_inf.asinh(), neg_inf);
1651         assert!(nan.asinh().is_nan());
1652         assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1653         assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1654     }
1655
1656     #[test]
1657     fn test_acosh() {
1658         assert_eq!(1.0f64.acosh(), 0.0f64);
1659         assert!(0.999f64.acosh().is_nan());
1660
1661         let inf: f64 = INFINITY;
1662         let neg_inf: f64 = NEG_INFINITY;
1663         let nan: f64 = NAN;
1664         assert_eq!(inf.acosh(), inf);
1665         assert!(neg_inf.acosh().is_nan());
1666         assert!(nan.acosh().is_nan());
1667         assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1668         assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1669     }
1670
1671     #[test]
1672     fn test_atanh() {
1673         assert_eq!(0.0f64.atanh(), 0.0f64);
1674         assert_eq!((-0.0f64).atanh(), -0.0f64);
1675
1676         let inf: f64 = INFINITY;
1677         let neg_inf: f64 = NEG_INFINITY;
1678         let nan: f64 = NAN;
1679         assert_eq!(1.0f64.atanh(), inf);
1680         assert_eq!((-1.0f64).atanh(), neg_inf);
1681         assert!(2f64.atanh().atanh().is_nan());
1682         assert!((-2f64).atanh().atanh().is_nan());
1683         assert!(inf.atanh().is_nan());
1684         assert!(neg_inf.atanh().is_nan());
1685         assert!(nan.atanh().is_nan());
1686         assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1687         assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1688     }
1689
1690     #[test]
1691     fn test_real_consts() {
1692         use super::consts;
1693         let pi: f64 = consts::PI;
1694         let frac_pi_2: f64 = consts::FRAC_PI_2;
1695         let frac_pi_3: f64 = consts::FRAC_PI_3;
1696         let frac_pi_4: f64 = consts::FRAC_PI_4;
1697         let frac_pi_6: f64 = consts::FRAC_PI_6;
1698         let frac_pi_8: f64 = consts::FRAC_PI_8;
1699         let frac_1_pi: f64 = consts::FRAC_1_PI;
1700         let frac_2_pi: f64 = consts::FRAC_2_PI;
1701         let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1702         let sqrt2: f64 = consts::SQRT_2;
1703         let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1704         let e: f64 = consts::E;
1705         let log2_e: f64 = consts::LOG2_E;
1706         let log10_e: f64 = consts::LOG10_E;
1707         let ln_2: f64 = consts::LN_2;
1708         let ln_10: f64 = consts::LN_10;
1709
1710         assert_approx_eq!(frac_pi_2, pi / 2f64);
1711         assert_approx_eq!(frac_pi_3, pi / 3f64);
1712         assert_approx_eq!(frac_pi_4, pi / 4f64);
1713         assert_approx_eq!(frac_pi_6, pi / 6f64);
1714         assert_approx_eq!(frac_pi_8, pi / 8f64);
1715         assert_approx_eq!(frac_1_pi, 1f64 / pi);
1716         assert_approx_eq!(frac_2_pi, 2f64 / pi);
1717         assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1718         assert_approx_eq!(sqrt2, 2f64.sqrt());
1719         assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1720         assert_approx_eq!(log2_e, e.log2());
1721         assert_approx_eq!(log10_e, e.log10());
1722         assert_approx_eq!(ln_2, 2f64.ln());
1723         assert_approx_eq!(ln_10, 10f64.ln());
1724     }
1725 }