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