]> git.lizzy.rs Git - rust.git/blob - src/libstd/f64.rs
rustbuild: fix remap-debuginfo when building a release
[rust.git] / src / libstd / 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 //! This module provides constants which are specific to the implementation
12 //! of the `f64` floating point data type.
13 //!
14 //! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
15 //!
16 //! Mathematically significant numbers are provided in the `consts` sub-module.
17
18 #![stable(feature = "rust1", since = "1.0.0")]
19 #![allow(missing_docs)]
20
21 #[cfg(not(test))]
22 use intrinsics;
23 #[cfg(not(test))]
24 use sys::cmath;
25
26 #[stable(feature = "rust1", since = "1.0.0")]
27 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
28 #[stable(feature = "rust1", since = "1.0.0")]
29 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub use core::f64::consts;
36
37 #[cfg(not(test))]
38 #[lang = "f64_runtime"]
39 impl f64 {
40     /// Returns the largest integer less than or equal to a number.
41     ///
42     /// # Examples
43     ///
44     /// ```
45     /// let f = 3.99_f64;
46     /// let g = 3.0_f64;
47     ///
48     /// assert_eq!(f.floor(), 3.0);
49     /// assert_eq!(g.floor(), 3.0);
50     /// ```
51     #[stable(feature = "rust1", since = "1.0.0")]
52     #[inline]
53     pub fn floor(self) -> f64 {
54         unsafe { intrinsics::floorf64(self) }
55     }
56
57     /// Returns the smallest integer greater than or equal to a number.
58     ///
59     /// # Examples
60     ///
61     /// ```
62     /// let f = 3.01_f64;
63     /// let g = 4.0_f64;
64     ///
65     /// assert_eq!(f.ceil(), 4.0);
66     /// assert_eq!(g.ceil(), 4.0);
67     /// ```
68     #[stable(feature = "rust1", since = "1.0.0")]
69     #[inline]
70     pub fn ceil(self) -> f64 {
71         unsafe { intrinsics::ceilf64(self) }
72     }
73
74     /// Returns the nearest integer to a number. Round half-way cases away from
75     /// `0.0`.
76     ///
77     /// # Examples
78     ///
79     /// ```
80     /// let f = 3.3_f64;
81     /// let g = -3.3_f64;
82     ///
83     /// assert_eq!(f.round(), 3.0);
84     /// assert_eq!(g.round(), -3.0);
85     /// ```
86     #[stable(feature = "rust1", since = "1.0.0")]
87     #[inline]
88     pub fn round(self) -> f64 {
89         unsafe { intrinsics::roundf64(self) }
90     }
91
92     /// Returns the integer part of a number.
93     ///
94     /// # Examples
95     ///
96     /// ```
97     /// let f = 3.3_f64;
98     /// let g = -3.7_f64;
99     ///
100     /// assert_eq!(f.trunc(), 3.0);
101     /// assert_eq!(g.trunc(), -3.0);
102     /// ```
103     #[stable(feature = "rust1", since = "1.0.0")]
104     #[inline]
105     pub fn trunc(self) -> f64 {
106         unsafe { intrinsics::truncf64(self) }
107     }
108
109     /// Returns the fractional part of a number.
110     ///
111     /// # Examples
112     ///
113     /// ```
114     /// let x = 3.5_f64;
115     /// let y = -3.5_f64;
116     /// let abs_difference_x = (x.fract() - 0.5).abs();
117     /// let abs_difference_y = (y.fract() - (-0.5)).abs();
118     ///
119     /// assert!(abs_difference_x < 1e-10);
120     /// assert!(abs_difference_y < 1e-10);
121     /// ```
122     #[stable(feature = "rust1", since = "1.0.0")]
123     #[inline]
124     pub fn fract(self) -> f64 { self - self.trunc() }
125
126     /// Computes the absolute value of `self`. Returns `NAN` if the
127     /// number is `NAN`.
128     ///
129     /// # Examples
130     ///
131     /// ```
132     /// use std::f64;
133     ///
134     /// let x = 3.5_f64;
135     /// let y = -3.5_f64;
136     ///
137     /// let abs_difference_x = (x.abs() - x).abs();
138     /// let abs_difference_y = (y.abs() - (-y)).abs();
139     ///
140     /// assert!(abs_difference_x < 1e-10);
141     /// assert!(abs_difference_y < 1e-10);
142     ///
143     /// assert!(f64::NAN.abs().is_nan());
144     /// ```
145     #[stable(feature = "rust1", since = "1.0.0")]
146     #[inline]
147     pub fn abs(self) -> f64 {
148         unsafe { intrinsics::fabsf64(self) }
149     }
150
151     /// Returns a number that represents the sign of `self`.
152     ///
153     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
154     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
155     /// - `NAN` if the number is `NAN`
156     ///
157     /// # Examples
158     ///
159     /// ```
160     /// use std::f64;
161     ///
162     /// let f = 3.5_f64;
163     ///
164     /// assert_eq!(f.signum(), 1.0);
165     /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
166     ///
167     /// assert!(f64::NAN.signum().is_nan());
168     /// ```
169     #[stable(feature = "rust1", since = "1.0.0")]
170     #[inline]
171     pub fn signum(self) -> f64 {
172         if self.is_nan() {
173             NAN
174         } else {
175             unsafe { intrinsics::copysignf64(1.0, self) }
176         }
177     }
178
179     /// Returns a number composed of the magnitude of one number and the sign of
180     /// another.
181     ///
182     /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
183     /// equal to `-y`. If `self` is a `NAN`, then a `NAN` with the sign of `y`
184     /// is returned.
185     ///
186     /// # Examples
187     ///
188     /// ```
189     /// #![feature(copysign)]
190     /// use std::f64;
191     ///
192     /// let f = 3.5_f64;
193     ///
194     /// assert_eq!(f.copysign(0.42), 3.5_f64);
195     /// assert_eq!(f.copysign(-0.42), -3.5_f64);
196     /// assert_eq!((-f).copysign(0.42), 3.5_f64);
197     /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
198     ///
199     /// assert!(f64::NAN.copysign(1.0).is_nan());
200     /// ```
201     #[inline]
202     #[must_use]
203     #[unstable(feature="copysign", issue="55169")]
204     pub fn copysign(self, y: f64) -> f64 {
205         unsafe { intrinsics::copysignf64(self, y) }
206     }
207
208     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
209     /// error, yielding a more accurate result than an unfused multiply-add.
210     ///
211     /// Using `mul_add` can be more performant than an unfused multiply-add if
212     /// the target architecture has a dedicated `fma` CPU instruction.
213     ///
214     /// # Examples
215     ///
216     /// ```
217     /// let m = 10.0_f64;
218     /// let x = 4.0_f64;
219     /// let b = 60.0_f64;
220     ///
221     /// // 100.0
222     /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
223     ///
224     /// assert!(abs_difference < 1e-10);
225     /// ```
226     #[stable(feature = "rust1", since = "1.0.0")]
227     #[inline]
228     pub fn mul_add(self, a: f64, b: f64) -> f64 {
229         unsafe { intrinsics::fmaf64(self, a, b) }
230     }
231
232     /// Calculates Euclidean division, the matching method for `mod_euc`.
233     ///
234     /// This computes the integer `n` such that
235     /// `self = n * rhs + self.mod_euc(rhs)`.
236     /// In other words, the result is `self / rhs` rounded to the integer `n`
237     /// such that `self >= n * rhs`.
238     ///
239     /// # Examples
240     ///
241     /// ```
242     /// #![feature(euclidean_division)]
243     /// let a: f64 = 7.0;
244     /// let b = 4.0;
245     /// assert_eq!(a.div_euc(b), 1.0); // 7.0 > 4.0 * 1.0
246     /// assert_eq!((-a).div_euc(b), -2.0); // -7.0 >= 4.0 * -2.0
247     /// assert_eq!(a.div_euc(-b), -1.0); // 7.0 >= -4.0 * -1.0
248     /// assert_eq!((-a).div_euc(-b), 2.0); // -7.0 >= -4.0 * 2.0
249     /// ```
250     #[inline]
251     #[unstable(feature = "euclidean_division", issue = "49048")]
252     pub fn div_euc(self, rhs: f64) -> f64 {
253         let q = (self / rhs).trunc();
254         if self % rhs < 0.0 {
255             return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
256         }
257         q
258     }
259
260     /// Calculates the Euclidean modulo (self mod rhs), which is never negative.
261     ///
262     /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
263     /// most cases.  However, due to a floating point round-off error it can
264     /// result in `r == rhs.abs()`, violating the mathematical definition, if
265     /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
266     /// This result is not an element of the function's codomain, but it is the
267     /// closest floating point number in the real numbers and thus fulfills the
268     /// property `self == self.div_euc(rhs) * rhs + self.mod_euc(rhs)`
269     /// approximatively.
270     ///
271     /// # Examples
272     ///
273     /// ```
274     /// #![feature(euclidean_division)]
275     /// let a: f64 = 7.0;
276     /// let b = 4.0;
277     /// assert_eq!(a.mod_euc(b), 3.0);
278     /// assert_eq!((-a).mod_euc(b), 1.0);
279     /// assert_eq!(a.mod_euc(-b), 3.0);
280     /// assert_eq!((-a).mod_euc(-b), 1.0);
281     /// // limitation due to round-off error
282     /// assert!((-std::f64::EPSILON).mod_euc(3.0) != 0.0);
283     /// ```
284     #[inline]
285     #[unstable(feature = "euclidean_division", issue = "49048")]
286     pub fn mod_euc(self, rhs: f64) -> f64 {
287         let r = self % rhs;
288         if r < 0.0 {
289             r + rhs.abs()
290         } else {
291             r
292         }
293     }
294
295     /// Raises a number to an integer power.
296     ///
297     /// Using this function is generally faster than using `powf`
298     ///
299     /// # Examples
300     ///
301     /// ```
302     /// let x = 2.0_f64;
303     /// let abs_difference = (x.powi(2) - x*x).abs();
304     ///
305     /// assert!(abs_difference < 1e-10);
306     /// ```
307     #[stable(feature = "rust1", since = "1.0.0")]
308     #[inline]
309     pub fn powi(self, n: i32) -> f64 {
310         unsafe { intrinsics::powif64(self, n) }
311     }
312
313     /// Raises a number to a floating point power.
314     ///
315     /// # Examples
316     ///
317     /// ```
318     /// let x = 2.0_f64;
319     /// let abs_difference = (x.powf(2.0) - x*x).abs();
320     ///
321     /// assert!(abs_difference < 1e-10);
322     /// ```
323     #[stable(feature = "rust1", since = "1.0.0")]
324     #[inline]
325     pub fn powf(self, n: f64) -> f64 {
326         unsafe { intrinsics::powf64(self, n) }
327     }
328
329     /// Takes the square root of a number.
330     ///
331     /// Returns NaN if `self` is a negative number.
332     ///
333     /// # Examples
334     ///
335     /// ```
336     /// let positive = 4.0_f64;
337     /// let negative = -4.0_f64;
338     ///
339     /// let abs_difference = (positive.sqrt() - 2.0).abs();
340     ///
341     /// assert!(abs_difference < 1e-10);
342     /// assert!(negative.sqrt().is_nan());
343     /// ```
344     #[stable(feature = "rust1", since = "1.0.0")]
345     #[inline]
346     pub fn sqrt(self) -> f64 {
347         if self < 0.0 {
348             NAN
349         } else {
350             unsafe { intrinsics::sqrtf64(self) }
351         }
352     }
353
354     /// Returns `e^(self)`, (the exponential function).
355     ///
356     /// # Examples
357     ///
358     /// ```
359     /// let one = 1.0_f64;
360     /// // e^1
361     /// let e = one.exp();
362     ///
363     /// // ln(e) - 1 == 0
364     /// let abs_difference = (e.ln() - 1.0).abs();
365     ///
366     /// assert!(abs_difference < 1e-10);
367     /// ```
368     #[stable(feature = "rust1", since = "1.0.0")]
369     #[inline]
370     pub fn exp(self) -> f64 {
371         unsafe { intrinsics::expf64(self) }
372     }
373
374     /// Returns `2^(self)`.
375     ///
376     /// # Examples
377     ///
378     /// ```
379     /// let f = 2.0_f64;
380     ///
381     /// // 2^2 - 4 == 0
382     /// let abs_difference = (f.exp2() - 4.0).abs();
383     ///
384     /// assert!(abs_difference < 1e-10);
385     /// ```
386     #[stable(feature = "rust1", since = "1.0.0")]
387     #[inline]
388     pub fn exp2(self) -> f64 {
389         unsafe { intrinsics::exp2f64(self) }
390     }
391
392     /// Returns the natural logarithm of the number.
393     ///
394     /// # Examples
395     ///
396     /// ```
397     /// let one = 1.0_f64;
398     /// // e^1
399     /// let e = one.exp();
400     ///
401     /// // ln(e) - 1 == 0
402     /// let abs_difference = (e.ln() - 1.0).abs();
403     ///
404     /// assert!(abs_difference < 1e-10);
405     /// ```
406     #[stable(feature = "rust1", since = "1.0.0")]
407     #[inline]
408     pub fn ln(self) -> f64 {
409         self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
410     }
411
412     /// Returns the logarithm of the number with respect to an arbitrary base.
413     ///
414     /// The result may not be correctly rounded owing to implementation details;
415     /// `self.log2()` can produce more accurate results for base 2, and
416     /// `self.log10()` can produce more accurate results for base 10.
417     ///
418     /// # Examples
419     ///
420     /// ```
421     /// let five = 5.0_f64;
422     ///
423     /// // log5(5) - 1 == 0
424     /// let abs_difference = (five.log(5.0) - 1.0).abs();
425     ///
426     /// assert!(abs_difference < 1e-10);
427     /// ```
428     #[stable(feature = "rust1", since = "1.0.0")]
429     #[inline]
430     pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
431
432     /// Returns the base 2 logarithm of the number.
433     ///
434     /// # Examples
435     ///
436     /// ```
437     /// let two = 2.0_f64;
438     ///
439     /// // log2(2) - 1 == 0
440     /// let abs_difference = (two.log2() - 1.0).abs();
441     ///
442     /// assert!(abs_difference < 1e-10);
443     /// ```
444     #[stable(feature = "rust1", since = "1.0.0")]
445     #[inline]
446     pub fn log2(self) -> f64 {
447         self.log_wrapper(|n| {
448             #[cfg(target_os = "android")]
449             return ::sys::android::log2f64(n);
450             #[cfg(not(target_os = "android"))]
451             return unsafe { intrinsics::log2f64(n) };
452         })
453     }
454
455     /// Returns the base 10 logarithm of the number.
456     ///
457     /// # Examples
458     ///
459     /// ```
460     /// let ten = 10.0_f64;
461     ///
462     /// // log10(10) - 1 == 0
463     /// let abs_difference = (ten.log10() - 1.0).abs();
464     ///
465     /// assert!(abs_difference < 1e-10);
466     /// ```
467     #[stable(feature = "rust1", since = "1.0.0")]
468     #[inline]
469     pub fn log10(self) -> f64 {
470         self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
471     }
472
473     /// The positive difference of two numbers.
474     ///
475     /// * If `self <= other`: `0:0`
476     /// * Else: `self - other`
477     ///
478     /// # Examples
479     ///
480     /// ```
481     /// let x = 3.0_f64;
482     /// let y = -3.0_f64;
483     ///
484     /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
485     /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
486     ///
487     /// assert!(abs_difference_x < 1e-10);
488     /// assert!(abs_difference_y < 1e-10);
489     /// ```
490     #[stable(feature = "rust1", since = "1.0.0")]
491     #[inline]
492     #[rustc_deprecated(since = "1.10.0",
493                        reason = "you probably meant `(self - other).abs()`: \
494                                  this operation is `(self - other).max(0.0)` (also \
495                                  known as `fdim` in C). If you truly need the positive \
496                                  difference, consider using that expression or the C function \
497                                  `fdim`, depending on how you wish to handle NaN (please consider \
498                                  filing an issue describing your use-case too).")]
499      pub fn abs_sub(self, other: f64) -> f64 {
500          unsafe { cmath::fdim(self, other) }
501      }
502
503     /// Takes the cubic root of a number.
504     ///
505     /// # Examples
506     ///
507     /// ```
508     /// let x = 8.0_f64;
509     ///
510     /// // x^(1/3) - 2 == 0
511     /// let abs_difference = (x.cbrt() - 2.0).abs();
512     ///
513     /// assert!(abs_difference < 1e-10);
514     /// ```
515     #[stable(feature = "rust1", since = "1.0.0")]
516     #[inline]
517     pub fn cbrt(self) -> f64 {
518         unsafe { cmath::cbrt(self) }
519     }
520
521     /// Calculates the length of the hypotenuse of a right-angle triangle given
522     /// legs of length `x` and `y`.
523     ///
524     /// # Examples
525     ///
526     /// ```
527     /// let x = 2.0_f64;
528     /// let y = 3.0_f64;
529     ///
530     /// // sqrt(x^2 + y^2)
531     /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
532     ///
533     /// assert!(abs_difference < 1e-10);
534     /// ```
535     #[stable(feature = "rust1", since = "1.0.0")]
536     #[inline]
537     pub fn hypot(self, other: f64) -> f64 {
538         unsafe { cmath::hypot(self, other) }
539     }
540
541     /// Computes the sine of a number (in radians).
542     ///
543     /// # Examples
544     ///
545     /// ```
546     /// use std::f64;
547     ///
548     /// let x = f64::consts::PI/2.0;
549     ///
550     /// let abs_difference = (x.sin() - 1.0).abs();
551     ///
552     /// assert!(abs_difference < 1e-10);
553     /// ```
554     #[stable(feature = "rust1", since = "1.0.0")]
555     #[inline]
556     pub fn sin(self) -> f64 {
557         unsafe { intrinsics::sinf64(self) }
558     }
559
560     /// Computes the cosine of a number (in radians).
561     ///
562     /// # Examples
563     ///
564     /// ```
565     /// use std::f64;
566     ///
567     /// let x = 2.0*f64::consts::PI;
568     ///
569     /// let abs_difference = (x.cos() - 1.0).abs();
570     ///
571     /// assert!(abs_difference < 1e-10);
572     /// ```
573     #[stable(feature = "rust1", since = "1.0.0")]
574     #[inline]
575     pub fn cos(self) -> f64 {
576         unsafe { intrinsics::cosf64(self) }
577     }
578
579     /// Computes the tangent of a number (in radians).
580     ///
581     /// # Examples
582     ///
583     /// ```
584     /// use std::f64;
585     ///
586     /// let x = f64::consts::PI/4.0;
587     /// let abs_difference = (x.tan() - 1.0).abs();
588     ///
589     /// assert!(abs_difference < 1e-14);
590     /// ```
591     #[stable(feature = "rust1", since = "1.0.0")]
592     #[inline]
593     pub fn tan(self) -> f64 {
594         unsafe { cmath::tan(self) }
595     }
596
597     /// Computes the arcsine of a number. Return value is in radians in
598     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
599     /// [-1, 1].
600     ///
601     /// # Examples
602     ///
603     /// ```
604     /// use std::f64;
605     ///
606     /// let f = f64::consts::PI / 2.0;
607     ///
608     /// // asin(sin(pi/2))
609     /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
610     ///
611     /// assert!(abs_difference < 1e-10);
612     /// ```
613     #[stable(feature = "rust1", since = "1.0.0")]
614     #[inline]
615     pub fn asin(self) -> f64 {
616         unsafe { cmath::asin(self) }
617     }
618
619     /// Computes the arccosine of a number. Return value is in radians in
620     /// the range [0, pi] or NaN if the number is outside the range
621     /// [-1, 1].
622     ///
623     /// # Examples
624     ///
625     /// ```
626     /// use std::f64;
627     ///
628     /// let f = f64::consts::PI / 4.0;
629     ///
630     /// // acos(cos(pi/4))
631     /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
632     ///
633     /// assert!(abs_difference < 1e-10);
634     /// ```
635     #[stable(feature = "rust1", since = "1.0.0")]
636     #[inline]
637     pub fn acos(self) -> f64 {
638         unsafe { cmath::acos(self) }
639     }
640
641     /// Computes the arctangent of a number. Return value is in radians in the
642     /// range [-pi/2, pi/2];
643     ///
644     /// # Examples
645     ///
646     /// ```
647     /// let f = 1.0_f64;
648     ///
649     /// // atan(tan(1))
650     /// let abs_difference = (f.tan().atan() - 1.0).abs();
651     ///
652     /// assert!(abs_difference < 1e-10);
653     /// ```
654     #[stable(feature = "rust1", since = "1.0.0")]
655     #[inline]
656     pub fn atan(self) -> f64 {
657         unsafe { cmath::atan(self) }
658     }
659
660     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
661     ///
662     /// * `x = 0`, `y = 0`: `0`
663     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
664     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
665     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
666     ///
667     /// # Examples
668     ///
669     /// ```
670     /// use std::f64;
671     ///
672     /// let pi = f64::consts::PI;
673     /// // Positive angles measured counter-clockwise
674     /// // from positive x axis
675     /// // -pi/4 radians (45 deg clockwise)
676     /// let x1 = 3.0_f64;
677     /// let y1 = -3.0_f64;
678     ///
679     /// // 3pi/4 radians (135 deg counter-clockwise)
680     /// let x2 = -3.0_f64;
681     /// let y2 = 3.0_f64;
682     ///
683     /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
684     /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
685     ///
686     /// assert!(abs_difference_1 < 1e-10);
687     /// assert!(abs_difference_2 < 1e-10);
688     /// ```
689     #[stable(feature = "rust1", since = "1.0.0")]
690     #[inline]
691     pub fn atan2(self, other: f64) -> f64 {
692         unsafe { cmath::atan2(self, other) }
693     }
694
695     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
696     /// `(sin(x), cos(x))`.
697     ///
698     /// # Examples
699     ///
700     /// ```
701     /// use std::f64;
702     ///
703     /// let x = f64::consts::PI/4.0;
704     /// let f = x.sin_cos();
705     ///
706     /// let abs_difference_0 = (f.0 - x.sin()).abs();
707     /// let abs_difference_1 = (f.1 - x.cos()).abs();
708     ///
709     /// assert!(abs_difference_0 < 1e-10);
710     /// assert!(abs_difference_1 < 1e-10);
711     /// ```
712     #[stable(feature = "rust1", since = "1.0.0")]
713     #[inline]
714     pub fn sin_cos(self) -> (f64, f64) {
715         (self.sin(), self.cos())
716     }
717
718     /// Returns `e^(self) - 1` in a way that is accurate even if the
719     /// number is close to zero.
720     ///
721     /// # Examples
722     ///
723     /// ```
724     /// let x = 7.0_f64;
725     ///
726     /// // e^(ln(7)) - 1
727     /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
728     ///
729     /// assert!(abs_difference < 1e-10);
730     /// ```
731     #[stable(feature = "rust1", since = "1.0.0")]
732     #[inline]
733     pub fn exp_m1(self) -> f64 {
734         unsafe { cmath::expm1(self) }
735     }
736
737     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
738     /// the operations were performed separately.
739     ///
740     /// # Examples
741     ///
742     /// ```
743     /// use std::f64;
744     ///
745     /// let x = f64::consts::E - 1.0;
746     ///
747     /// // ln(1 + (e - 1)) == ln(e) == 1
748     /// let abs_difference = (x.ln_1p() - 1.0).abs();
749     ///
750     /// assert!(abs_difference < 1e-10);
751     /// ```
752     #[stable(feature = "rust1", since = "1.0.0")]
753     #[inline]
754     pub fn ln_1p(self) -> f64 {
755         unsafe { cmath::log1p(self) }
756     }
757
758     /// Hyperbolic sine function.
759     ///
760     /// # Examples
761     ///
762     /// ```
763     /// use std::f64;
764     ///
765     /// let e = f64::consts::E;
766     /// let x = 1.0_f64;
767     ///
768     /// let f = x.sinh();
769     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
770     /// let g = (e*e - 1.0)/(2.0*e);
771     /// let abs_difference = (f - g).abs();
772     ///
773     /// assert!(abs_difference < 1e-10);
774     /// ```
775     #[stable(feature = "rust1", since = "1.0.0")]
776     #[inline]
777     pub fn sinh(self) -> f64 {
778         unsafe { cmath::sinh(self) }
779     }
780
781     /// Hyperbolic cosine function.
782     ///
783     /// # Examples
784     ///
785     /// ```
786     /// use std::f64;
787     ///
788     /// let e = f64::consts::E;
789     /// let x = 1.0_f64;
790     /// let f = x.cosh();
791     /// // Solving cosh() at 1 gives this result
792     /// let g = (e*e + 1.0)/(2.0*e);
793     /// let abs_difference = (f - g).abs();
794     ///
795     /// // Same result
796     /// assert!(abs_difference < 1.0e-10);
797     /// ```
798     #[stable(feature = "rust1", since = "1.0.0")]
799     #[inline]
800     pub fn cosh(self) -> f64 {
801         unsafe { cmath::cosh(self) }
802     }
803
804     /// Hyperbolic tangent function.
805     ///
806     /// # Examples
807     ///
808     /// ```
809     /// use std::f64;
810     ///
811     /// let e = f64::consts::E;
812     /// let x = 1.0_f64;
813     ///
814     /// let f = x.tanh();
815     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
816     /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
817     /// let abs_difference = (f - g).abs();
818     ///
819     /// assert!(abs_difference < 1.0e-10);
820     /// ```
821     #[stable(feature = "rust1", since = "1.0.0")]
822     #[inline]
823     pub fn tanh(self) -> f64 {
824         unsafe { cmath::tanh(self) }
825     }
826
827     /// Inverse hyperbolic sine function.
828     ///
829     /// # Examples
830     ///
831     /// ```
832     /// let x = 1.0_f64;
833     /// let f = x.sinh().asinh();
834     ///
835     /// let abs_difference = (f - x).abs();
836     ///
837     /// assert!(abs_difference < 1.0e-10);
838     /// ```
839     #[stable(feature = "rust1", since = "1.0.0")]
840     #[inline]
841     pub fn asinh(self) -> f64 {
842         if self == NEG_INFINITY {
843             NEG_INFINITY
844         } else {
845             (self + ((self * self) + 1.0).sqrt()).ln()
846         }
847     }
848
849     /// Inverse hyperbolic cosine function.
850     ///
851     /// # Examples
852     ///
853     /// ```
854     /// let x = 1.0_f64;
855     /// let f = x.cosh().acosh();
856     ///
857     /// let abs_difference = (f - x).abs();
858     ///
859     /// assert!(abs_difference < 1.0e-10);
860     /// ```
861     #[stable(feature = "rust1", since = "1.0.0")]
862     #[inline]
863     pub fn acosh(self) -> f64 {
864         match self {
865             x if x < 1.0 => NAN,
866             x => (x + ((x * x) - 1.0).sqrt()).ln(),
867         }
868     }
869
870     /// Inverse hyperbolic tangent function.
871     ///
872     /// # Examples
873     ///
874     /// ```
875     /// use std::f64;
876     ///
877     /// let e = f64::consts::E;
878     /// let f = e.tanh().atanh();
879     ///
880     /// let abs_difference = (f - e).abs();
881     ///
882     /// assert!(abs_difference < 1.0e-10);
883     /// ```
884     #[stable(feature = "rust1", since = "1.0.0")]
885     #[inline]
886     pub fn atanh(self) -> f64 {
887         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
888     }
889
890     // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
891     // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
892     // of expected NaN).
893     fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
894         if !cfg!(target_os = "solaris") {
895             log_fn(self)
896         } else {
897             if self.is_finite() {
898                 if self > 0.0 {
899                     log_fn(self)
900                 } else if self == 0.0 {
901                     NEG_INFINITY // log(0) = -Inf
902                 } else {
903                     NAN // log(-n) = NaN
904                 }
905             } else if self.is_nan() {
906                 self // log(NaN) = NaN
907             } else if self > 0.0 {
908                 self // log(Inf) = Inf
909             } else {
910                 NAN // log(-Inf) = NaN
911             }
912         }
913     }
914 }
915
916 #[cfg(test)]
917 mod tests {
918     use f64;
919     use f64::*;
920     use num::*;
921     use num::FpCategory as Fp;
922
923     #[test]
924     fn test_num_f64() {
925         test_num(10f64, 2f64);
926     }
927
928     #[test]
929     fn test_min_nan() {
930         assert_eq!(NAN.min(2.0), 2.0);
931         assert_eq!(2.0f64.min(NAN), 2.0);
932     }
933
934     #[test]
935     fn test_max_nan() {
936         assert_eq!(NAN.max(2.0), 2.0);
937         assert_eq!(2.0f64.max(NAN), 2.0);
938     }
939
940     #[test]
941     fn test_nan() {
942         let nan: f64 = NAN;
943         assert!(nan.is_nan());
944         assert!(!nan.is_infinite());
945         assert!(!nan.is_finite());
946         assert!(!nan.is_normal());
947         assert!(nan.is_sign_positive());
948         assert!(!nan.is_sign_negative());
949         assert_eq!(Fp::Nan, nan.classify());
950     }
951
952     #[test]
953     fn test_infinity() {
954         let inf: f64 = INFINITY;
955         assert!(inf.is_infinite());
956         assert!(!inf.is_finite());
957         assert!(inf.is_sign_positive());
958         assert!(!inf.is_sign_negative());
959         assert!(!inf.is_nan());
960         assert!(!inf.is_normal());
961         assert_eq!(Fp::Infinite, inf.classify());
962     }
963
964     #[test]
965     fn test_neg_infinity() {
966         let neg_inf: f64 = NEG_INFINITY;
967         assert!(neg_inf.is_infinite());
968         assert!(!neg_inf.is_finite());
969         assert!(!neg_inf.is_sign_positive());
970         assert!(neg_inf.is_sign_negative());
971         assert!(!neg_inf.is_nan());
972         assert!(!neg_inf.is_normal());
973         assert_eq!(Fp::Infinite, neg_inf.classify());
974     }
975
976     #[test]
977     fn test_zero() {
978         let zero: f64 = 0.0f64;
979         assert_eq!(0.0, zero);
980         assert!(!zero.is_infinite());
981         assert!(zero.is_finite());
982         assert!(zero.is_sign_positive());
983         assert!(!zero.is_sign_negative());
984         assert!(!zero.is_nan());
985         assert!(!zero.is_normal());
986         assert_eq!(Fp::Zero, zero.classify());
987     }
988
989     #[test]
990     fn test_neg_zero() {
991         let neg_zero: f64 = -0.0;
992         assert_eq!(0.0, neg_zero);
993         assert!(!neg_zero.is_infinite());
994         assert!(neg_zero.is_finite());
995         assert!(!neg_zero.is_sign_positive());
996         assert!(neg_zero.is_sign_negative());
997         assert!(!neg_zero.is_nan());
998         assert!(!neg_zero.is_normal());
999         assert_eq!(Fp::Zero, neg_zero.classify());
1000     }
1001
1002     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1003     #[test]
1004     fn test_one() {
1005         let one: f64 = 1.0f64;
1006         assert_eq!(1.0, one);
1007         assert!(!one.is_infinite());
1008         assert!(one.is_finite());
1009         assert!(one.is_sign_positive());
1010         assert!(!one.is_sign_negative());
1011         assert!(!one.is_nan());
1012         assert!(one.is_normal());
1013         assert_eq!(Fp::Normal, one.classify());
1014     }
1015
1016     #[test]
1017     fn test_is_nan() {
1018         let nan: f64 = NAN;
1019         let inf: f64 = INFINITY;
1020         let neg_inf: f64 = NEG_INFINITY;
1021         assert!(nan.is_nan());
1022         assert!(!0.0f64.is_nan());
1023         assert!(!5.3f64.is_nan());
1024         assert!(!(-10.732f64).is_nan());
1025         assert!(!inf.is_nan());
1026         assert!(!neg_inf.is_nan());
1027     }
1028
1029     #[test]
1030     fn test_is_infinite() {
1031         let nan: f64 = NAN;
1032         let inf: f64 = INFINITY;
1033         let neg_inf: f64 = NEG_INFINITY;
1034         assert!(!nan.is_infinite());
1035         assert!(inf.is_infinite());
1036         assert!(neg_inf.is_infinite());
1037         assert!(!0.0f64.is_infinite());
1038         assert!(!42.8f64.is_infinite());
1039         assert!(!(-109.2f64).is_infinite());
1040     }
1041
1042     #[test]
1043     fn test_is_finite() {
1044         let nan: f64 = NAN;
1045         let inf: f64 = INFINITY;
1046         let neg_inf: f64 = NEG_INFINITY;
1047         assert!(!nan.is_finite());
1048         assert!(!inf.is_finite());
1049         assert!(!neg_inf.is_finite());
1050         assert!(0.0f64.is_finite());
1051         assert!(42.8f64.is_finite());
1052         assert!((-109.2f64).is_finite());
1053     }
1054
1055     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1056     #[test]
1057     fn test_is_normal() {
1058         let nan: f64 = NAN;
1059         let inf: f64 = INFINITY;
1060         let neg_inf: f64 = NEG_INFINITY;
1061         let zero: f64 = 0.0f64;
1062         let neg_zero: f64 = -0.0;
1063         assert!(!nan.is_normal());
1064         assert!(!inf.is_normal());
1065         assert!(!neg_inf.is_normal());
1066         assert!(!zero.is_normal());
1067         assert!(!neg_zero.is_normal());
1068         assert!(1f64.is_normal());
1069         assert!(1e-307f64.is_normal());
1070         assert!(!1e-308f64.is_normal());
1071     }
1072
1073     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1074     #[test]
1075     fn test_classify() {
1076         let nan: f64 = NAN;
1077         let inf: f64 = INFINITY;
1078         let neg_inf: f64 = NEG_INFINITY;
1079         let zero: f64 = 0.0f64;
1080         let neg_zero: f64 = -0.0;
1081         assert_eq!(nan.classify(), Fp::Nan);
1082         assert_eq!(inf.classify(), Fp::Infinite);
1083         assert_eq!(neg_inf.classify(), Fp::Infinite);
1084         assert_eq!(zero.classify(), Fp::Zero);
1085         assert_eq!(neg_zero.classify(), Fp::Zero);
1086         assert_eq!(1e-307f64.classify(), Fp::Normal);
1087         assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1088     }
1089
1090     #[test]
1091     fn test_floor() {
1092         assert_approx_eq!(1.0f64.floor(), 1.0f64);
1093         assert_approx_eq!(1.3f64.floor(), 1.0f64);
1094         assert_approx_eq!(1.5f64.floor(), 1.0f64);
1095         assert_approx_eq!(1.7f64.floor(), 1.0f64);
1096         assert_approx_eq!(0.0f64.floor(), 0.0f64);
1097         assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1098         assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1099         assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1100         assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1101         assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1102     }
1103
1104     #[test]
1105     fn test_ceil() {
1106         assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1107         assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1108         assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1109         assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1110         assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1111         assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1112         assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1113         assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1114         assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1115         assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1116     }
1117
1118     #[test]
1119     fn test_round() {
1120         assert_approx_eq!(1.0f64.round(), 1.0f64);
1121         assert_approx_eq!(1.3f64.round(), 1.0f64);
1122         assert_approx_eq!(1.5f64.round(), 2.0f64);
1123         assert_approx_eq!(1.7f64.round(), 2.0f64);
1124         assert_approx_eq!(0.0f64.round(), 0.0f64);
1125         assert_approx_eq!((-0.0f64).round(), -0.0f64);
1126         assert_approx_eq!((-1.0f64).round(), -1.0f64);
1127         assert_approx_eq!((-1.3f64).round(), -1.0f64);
1128         assert_approx_eq!((-1.5f64).round(), -2.0f64);
1129         assert_approx_eq!((-1.7f64).round(), -2.0f64);
1130     }
1131
1132     #[test]
1133     fn test_trunc() {
1134         assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1135         assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1136         assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1137         assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1138         assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1139         assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1140         assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1141         assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1142         assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1143         assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1144     }
1145
1146     #[test]
1147     fn test_fract() {
1148         assert_approx_eq!(1.0f64.fract(), 0.0f64);
1149         assert_approx_eq!(1.3f64.fract(), 0.3f64);
1150         assert_approx_eq!(1.5f64.fract(), 0.5f64);
1151         assert_approx_eq!(1.7f64.fract(), 0.7f64);
1152         assert_approx_eq!(0.0f64.fract(), 0.0f64);
1153         assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1154         assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1155         assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1156         assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1157         assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1158     }
1159
1160     #[test]
1161     fn test_abs() {
1162         assert_eq!(INFINITY.abs(), INFINITY);
1163         assert_eq!(1f64.abs(), 1f64);
1164         assert_eq!(0f64.abs(), 0f64);
1165         assert_eq!((-0f64).abs(), 0f64);
1166         assert_eq!((-1f64).abs(), 1f64);
1167         assert_eq!(NEG_INFINITY.abs(), INFINITY);
1168         assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1169         assert!(NAN.abs().is_nan());
1170     }
1171
1172     #[test]
1173     fn test_signum() {
1174         assert_eq!(INFINITY.signum(), 1f64);
1175         assert_eq!(1f64.signum(), 1f64);
1176         assert_eq!(0f64.signum(), 1f64);
1177         assert_eq!((-0f64).signum(), -1f64);
1178         assert_eq!((-1f64).signum(), -1f64);
1179         assert_eq!(NEG_INFINITY.signum(), -1f64);
1180         assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1181         assert!(NAN.signum().is_nan());
1182     }
1183
1184     #[test]
1185     fn test_is_sign_positive() {
1186         assert!(INFINITY.is_sign_positive());
1187         assert!(1f64.is_sign_positive());
1188         assert!(0f64.is_sign_positive());
1189         assert!(!(-0f64).is_sign_positive());
1190         assert!(!(-1f64).is_sign_positive());
1191         assert!(!NEG_INFINITY.is_sign_positive());
1192         assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1193         assert!(NAN.is_sign_positive());
1194         assert!(!(-NAN).is_sign_positive());
1195     }
1196
1197     #[test]
1198     fn test_is_sign_negative() {
1199         assert!(!INFINITY.is_sign_negative());
1200         assert!(!1f64.is_sign_negative());
1201         assert!(!0f64.is_sign_negative());
1202         assert!((-0f64).is_sign_negative());
1203         assert!((-1f64).is_sign_negative());
1204         assert!(NEG_INFINITY.is_sign_negative());
1205         assert!((1f64/NEG_INFINITY).is_sign_negative());
1206         assert!(!NAN.is_sign_negative());
1207         assert!((-NAN).is_sign_negative());
1208     }
1209
1210     #[test]
1211     fn test_mul_add() {
1212         let nan: f64 = NAN;
1213         let inf: f64 = INFINITY;
1214         let neg_inf: f64 = NEG_INFINITY;
1215         assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1216         assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1217         assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1218         assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1219         assert!(nan.mul_add(7.8, 9.0).is_nan());
1220         assert_eq!(inf.mul_add(7.8, 9.0), inf);
1221         assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1222         assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1223         assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1224     }
1225
1226     #[test]
1227     fn test_recip() {
1228         let nan: f64 = NAN;
1229         let inf: f64 = INFINITY;
1230         let neg_inf: f64 = NEG_INFINITY;
1231         assert_eq!(1.0f64.recip(), 1.0);
1232         assert_eq!(2.0f64.recip(), 0.5);
1233         assert_eq!((-0.4f64).recip(), -2.5);
1234         assert_eq!(0.0f64.recip(), inf);
1235         assert!(nan.recip().is_nan());
1236         assert_eq!(inf.recip(), 0.0);
1237         assert_eq!(neg_inf.recip(), 0.0);
1238     }
1239
1240     #[test]
1241     fn test_powi() {
1242         let nan: f64 = NAN;
1243         let inf: f64 = INFINITY;
1244         let neg_inf: f64 = NEG_INFINITY;
1245         assert_eq!(1.0f64.powi(1), 1.0);
1246         assert_approx_eq!((-3.1f64).powi(2), 9.61);
1247         assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1248         assert_eq!(8.3f64.powi(0), 1.0);
1249         assert!(nan.powi(2).is_nan());
1250         assert_eq!(inf.powi(3), inf);
1251         assert_eq!(neg_inf.powi(2), inf);
1252     }
1253
1254     #[test]
1255     fn test_powf() {
1256         let nan: f64 = NAN;
1257         let inf: f64 = INFINITY;
1258         let neg_inf: f64 = NEG_INFINITY;
1259         assert_eq!(1.0f64.powf(1.0), 1.0);
1260         assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1261         assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1262         assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1263         assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1264         assert_eq!(8.3f64.powf(0.0), 1.0);
1265         assert!(nan.powf(2.0).is_nan());
1266         assert_eq!(inf.powf(2.0), inf);
1267         assert_eq!(neg_inf.powf(3.0), neg_inf);
1268     }
1269
1270     #[test]
1271     fn test_sqrt_domain() {
1272         assert!(NAN.sqrt().is_nan());
1273         assert!(NEG_INFINITY.sqrt().is_nan());
1274         assert!((-1.0f64).sqrt().is_nan());
1275         assert_eq!((-0.0f64).sqrt(), -0.0);
1276         assert_eq!(0.0f64.sqrt(), 0.0);
1277         assert_eq!(1.0f64.sqrt(), 1.0);
1278         assert_eq!(INFINITY.sqrt(), INFINITY);
1279     }
1280
1281     #[test]
1282     fn test_exp() {
1283         assert_eq!(1.0, 0.0f64.exp());
1284         assert_approx_eq!(2.718282, 1.0f64.exp());
1285         assert_approx_eq!(148.413159, 5.0f64.exp());
1286
1287         let inf: f64 = INFINITY;
1288         let neg_inf: f64 = NEG_INFINITY;
1289         let nan: f64 = NAN;
1290         assert_eq!(inf, inf.exp());
1291         assert_eq!(0.0, neg_inf.exp());
1292         assert!(nan.exp().is_nan());
1293     }
1294
1295     #[test]
1296     fn test_exp2() {
1297         assert_eq!(32.0, 5.0f64.exp2());
1298         assert_eq!(1.0, 0.0f64.exp2());
1299
1300         let inf: f64 = INFINITY;
1301         let neg_inf: f64 = NEG_INFINITY;
1302         let nan: f64 = NAN;
1303         assert_eq!(inf, inf.exp2());
1304         assert_eq!(0.0, neg_inf.exp2());
1305         assert!(nan.exp2().is_nan());
1306     }
1307
1308     #[test]
1309     fn test_ln() {
1310         let nan: f64 = NAN;
1311         let inf: f64 = INFINITY;
1312         let neg_inf: f64 = NEG_INFINITY;
1313         assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1314         assert!(nan.ln().is_nan());
1315         assert_eq!(inf.ln(), inf);
1316         assert!(neg_inf.ln().is_nan());
1317         assert!((-2.3f64).ln().is_nan());
1318         assert_eq!((-0.0f64).ln(), neg_inf);
1319         assert_eq!(0.0f64.ln(), neg_inf);
1320         assert_approx_eq!(4.0f64.ln(), 1.386294);
1321     }
1322
1323     #[test]
1324     fn test_log() {
1325         let nan: f64 = NAN;
1326         let inf: f64 = INFINITY;
1327         let neg_inf: f64 = NEG_INFINITY;
1328         assert_eq!(10.0f64.log(10.0), 1.0);
1329         assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1330         assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1331         assert!(1.0f64.log(1.0).is_nan());
1332         assert!(1.0f64.log(-13.9).is_nan());
1333         assert!(nan.log(2.3).is_nan());
1334         assert_eq!(inf.log(10.0), inf);
1335         assert!(neg_inf.log(8.8).is_nan());
1336         assert!((-2.3f64).log(0.1).is_nan());
1337         assert_eq!((-0.0f64).log(2.0), neg_inf);
1338         assert_eq!(0.0f64.log(7.0), neg_inf);
1339     }
1340
1341     #[test]
1342     fn test_log2() {
1343         let nan: f64 = NAN;
1344         let inf: f64 = INFINITY;
1345         let neg_inf: f64 = NEG_INFINITY;
1346         assert_approx_eq!(10.0f64.log2(), 3.321928);
1347         assert_approx_eq!(2.3f64.log2(), 1.201634);
1348         assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1349         assert!(nan.log2().is_nan());
1350         assert_eq!(inf.log2(), inf);
1351         assert!(neg_inf.log2().is_nan());
1352         assert!((-2.3f64).log2().is_nan());
1353         assert_eq!((-0.0f64).log2(), neg_inf);
1354         assert_eq!(0.0f64.log2(), neg_inf);
1355     }
1356
1357     #[test]
1358     fn test_log10() {
1359         let nan: f64 = NAN;
1360         let inf: f64 = INFINITY;
1361         let neg_inf: f64 = NEG_INFINITY;
1362         assert_eq!(10.0f64.log10(), 1.0);
1363         assert_approx_eq!(2.3f64.log10(), 0.361728);
1364         assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1365         assert_eq!(1.0f64.log10(), 0.0);
1366         assert!(nan.log10().is_nan());
1367         assert_eq!(inf.log10(), inf);
1368         assert!(neg_inf.log10().is_nan());
1369         assert!((-2.3f64).log10().is_nan());
1370         assert_eq!((-0.0f64).log10(), neg_inf);
1371         assert_eq!(0.0f64.log10(), neg_inf);
1372     }
1373
1374     #[test]
1375     fn test_to_degrees() {
1376         let pi: f64 = consts::PI;
1377         let nan: f64 = NAN;
1378         let inf: f64 = INFINITY;
1379         let neg_inf: f64 = NEG_INFINITY;
1380         assert_eq!(0.0f64.to_degrees(), 0.0);
1381         assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1382         assert_eq!(pi.to_degrees(), 180.0);
1383         assert!(nan.to_degrees().is_nan());
1384         assert_eq!(inf.to_degrees(), inf);
1385         assert_eq!(neg_inf.to_degrees(), neg_inf);
1386     }
1387
1388     #[test]
1389     fn test_to_radians() {
1390         let pi: f64 = consts::PI;
1391         let nan: f64 = NAN;
1392         let inf: f64 = INFINITY;
1393         let neg_inf: f64 = NEG_INFINITY;
1394         assert_eq!(0.0f64.to_radians(), 0.0);
1395         assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1396         assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1397         assert_eq!(180.0f64.to_radians(), pi);
1398         assert!(nan.to_radians().is_nan());
1399         assert_eq!(inf.to_radians(), inf);
1400         assert_eq!(neg_inf.to_radians(), neg_inf);
1401     }
1402
1403     #[test]
1404     fn test_asinh() {
1405         assert_eq!(0.0f64.asinh(), 0.0f64);
1406         assert_eq!((-0.0f64).asinh(), -0.0f64);
1407
1408         let inf: f64 = INFINITY;
1409         let neg_inf: f64 = NEG_INFINITY;
1410         let nan: f64 = NAN;
1411         assert_eq!(inf.asinh(), inf);
1412         assert_eq!(neg_inf.asinh(), neg_inf);
1413         assert!(nan.asinh().is_nan());
1414         assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1415         assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1416     }
1417
1418     #[test]
1419     fn test_acosh() {
1420         assert_eq!(1.0f64.acosh(), 0.0f64);
1421         assert!(0.999f64.acosh().is_nan());
1422
1423         let inf: f64 = INFINITY;
1424         let neg_inf: f64 = NEG_INFINITY;
1425         let nan: f64 = NAN;
1426         assert_eq!(inf.acosh(), inf);
1427         assert!(neg_inf.acosh().is_nan());
1428         assert!(nan.acosh().is_nan());
1429         assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1430         assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1431     }
1432
1433     #[test]
1434     fn test_atanh() {
1435         assert_eq!(0.0f64.atanh(), 0.0f64);
1436         assert_eq!((-0.0f64).atanh(), -0.0f64);
1437
1438         let inf: f64 = INFINITY;
1439         let neg_inf: f64 = NEG_INFINITY;
1440         let nan: f64 = NAN;
1441         assert_eq!(1.0f64.atanh(), inf);
1442         assert_eq!((-1.0f64).atanh(), neg_inf);
1443         assert!(2f64.atanh().atanh().is_nan());
1444         assert!((-2f64).atanh().atanh().is_nan());
1445         assert!(inf.atanh().is_nan());
1446         assert!(neg_inf.atanh().is_nan());
1447         assert!(nan.atanh().is_nan());
1448         assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1449         assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1450     }
1451
1452     #[test]
1453     fn test_real_consts() {
1454         use super::consts;
1455         let pi: f64 = consts::PI;
1456         let frac_pi_2: f64 = consts::FRAC_PI_2;
1457         let frac_pi_3: f64 = consts::FRAC_PI_3;
1458         let frac_pi_4: f64 = consts::FRAC_PI_4;
1459         let frac_pi_6: f64 = consts::FRAC_PI_6;
1460         let frac_pi_8: f64 = consts::FRAC_PI_8;
1461         let frac_1_pi: f64 = consts::FRAC_1_PI;
1462         let frac_2_pi: f64 = consts::FRAC_2_PI;
1463         let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1464         let sqrt2: f64 = consts::SQRT_2;
1465         let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1466         let e: f64 = consts::E;
1467         let log2_e: f64 = consts::LOG2_E;
1468         let log10_e: f64 = consts::LOG10_E;
1469         let ln_2: f64 = consts::LN_2;
1470         let ln_10: f64 = consts::LN_10;
1471
1472         assert_approx_eq!(frac_pi_2, pi / 2f64);
1473         assert_approx_eq!(frac_pi_3, pi / 3f64);
1474         assert_approx_eq!(frac_pi_4, pi / 4f64);
1475         assert_approx_eq!(frac_pi_6, pi / 6f64);
1476         assert_approx_eq!(frac_pi_8, pi / 8f64);
1477         assert_approx_eq!(frac_1_pi, 1f64 / pi);
1478         assert_approx_eq!(frac_2_pi, 2f64 / pi);
1479         assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1480         assert_approx_eq!(sqrt2, 2f64.sqrt());
1481         assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1482         assert_approx_eq!(log2_e, e.log2());
1483         assert_approx_eq!(log10_e, e.log10());
1484         assert_approx_eq!(ln_2, 2f64.ln());
1485         assert_approx_eq!(ln_10, 10f64.ln());
1486     }
1487
1488     #[test]
1489     fn test_float_bits_conv() {
1490         assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1491         assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1492         assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1493         assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1494         assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1495         assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1496         assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1497         assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1498
1499         // Check that NaNs roundtrip their bits regardless of signalingness
1500         // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1501         let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1502         let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1503         assert!(f64::from_bits(masked_nan1).is_nan());
1504         assert!(f64::from_bits(masked_nan2).is_nan());
1505
1506         assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1507         assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
1508     }
1509 }