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