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