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