]> git.lizzy.rs Git - rust.git/blob - src/libstd/f64.rs
Rollup merge of #56917 - sinkuu:mir_build_logicop, r=davidtwco
[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 `self` and the sign of
180     /// `y`.
181     ///
182     /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
183     /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
184     /// `y` 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 `rem_euclid`.
233     ///
234     /// This computes the integer `n` such that
235     /// `self = n * rhs + self.rem_euclid(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_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
246     /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
247     /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
248     /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
249     /// ```
250     #[inline]
251     #[unstable(feature = "euclidean_division", issue = "49048")]
252     pub fn div_euclid(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 least nonnegative remainder of `self (mod rhs)`.
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_euclid(rhs) * rhs + self.rem_euclid(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.rem_euclid(b), 3.0);
278     /// assert_eq!((-a).rem_euclid(b), 1.0);
279     /// assert_eq!(a.rem_euclid(-b), 3.0);
280     /// assert_eq!((-a).rem_euclid(-b), 1.0);
281     /// // limitation due to round-off error
282     /// assert!((-std::f64::EPSILON).rem_euclid(3.0) != 0.0);
283     /// ```
284     #[inline]
285     #[unstable(feature = "euclidean_division", issue = "49048")]
286     pub fn rem_euclid(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)` \
495                                  except that `abs_sub` also propagates NaNs (also \
496                                  known as `fdim` in C). If you truly need the positive \
497                                  difference, consider using that expression or the C function \
498                                  `fdim`, depending on how you wish to handle NaN (please consider \
499                                  filing an issue describing your use-case too).")]
500      pub fn abs_sub(self, other: f64) -> f64 {
501          unsafe { cmath::fdim(self, other) }
502      }
503
504     /// Takes the cubic root of a number.
505     ///
506     /// # Examples
507     ///
508     /// ```
509     /// let x = 8.0_f64;
510     ///
511     /// // x^(1/3) - 2 == 0
512     /// let abs_difference = (x.cbrt() - 2.0).abs();
513     ///
514     /// assert!(abs_difference < 1e-10);
515     /// ```
516     #[stable(feature = "rust1", since = "1.0.0")]
517     #[inline]
518     pub fn cbrt(self) -> f64 {
519         unsafe { cmath::cbrt(self) }
520     }
521
522     /// Calculates the length of the hypotenuse of a right-angle triangle given
523     /// legs of length `x` and `y`.
524     ///
525     /// # Examples
526     ///
527     /// ```
528     /// let x = 2.0_f64;
529     /// let y = 3.0_f64;
530     ///
531     /// // sqrt(x^2 + y^2)
532     /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
533     ///
534     /// assert!(abs_difference < 1e-10);
535     /// ```
536     #[stable(feature = "rust1", since = "1.0.0")]
537     #[inline]
538     pub fn hypot(self, other: f64) -> f64 {
539         unsafe { cmath::hypot(self, other) }
540     }
541
542     /// Computes the sine of a number (in radians).
543     ///
544     /// # Examples
545     ///
546     /// ```
547     /// use std::f64;
548     ///
549     /// let x = f64::consts::PI/2.0;
550     ///
551     /// let abs_difference = (x.sin() - 1.0).abs();
552     ///
553     /// assert!(abs_difference < 1e-10);
554     /// ```
555     #[stable(feature = "rust1", since = "1.0.0")]
556     #[inline]
557     pub fn sin(self) -> f64 {
558         unsafe { intrinsics::sinf64(self) }
559     }
560
561     /// Computes the cosine of a number (in radians).
562     ///
563     /// # Examples
564     ///
565     /// ```
566     /// use std::f64;
567     ///
568     /// let x = 2.0*f64::consts::PI;
569     ///
570     /// let abs_difference = (x.cos() - 1.0).abs();
571     ///
572     /// assert!(abs_difference < 1e-10);
573     /// ```
574     #[stable(feature = "rust1", since = "1.0.0")]
575     #[inline]
576     pub fn cos(self) -> f64 {
577         unsafe { intrinsics::cosf64(self) }
578     }
579
580     /// Computes the tangent of a number (in radians).
581     ///
582     /// # Examples
583     ///
584     /// ```
585     /// use std::f64;
586     ///
587     /// let x = f64::consts::PI/4.0;
588     /// let abs_difference = (x.tan() - 1.0).abs();
589     ///
590     /// assert!(abs_difference < 1e-14);
591     /// ```
592     #[stable(feature = "rust1", since = "1.0.0")]
593     #[inline]
594     pub fn tan(self) -> f64 {
595         unsafe { cmath::tan(self) }
596     }
597
598     /// Computes the arcsine of a number. Return value is in radians in
599     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
600     /// [-1, 1].
601     ///
602     /// # Examples
603     ///
604     /// ```
605     /// use std::f64;
606     ///
607     /// let f = f64::consts::PI / 2.0;
608     ///
609     /// // asin(sin(pi/2))
610     /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
611     ///
612     /// assert!(abs_difference < 1e-10);
613     /// ```
614     #[stable(feature = "rust1", since = "1.0.0")]
615     #[inline]
616     pub fn asin(self) -> f64 {
617         unsafe { cmath::asin(self) }
618     }
619
620     /// Computes the arccosine of a number. Return value is in radians in
621     /// the range [0, pi] or NaN if the number is outside the range
622     /// [-1, 1].
623     ///
624     /// # Examples
625     ///
626     /// ```
627     /// use std::f64;
628     ///
629     /// let f = f64::consts::PI / 4.0;
630     ///
631     /// // acos(cos(pi/4))
632     /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
633     ///
634     /// assert!(abs_difference < 1e-10);
635     /// ```
636     #[stable(feature = "rust1", since = "1.0.0")]
637     #[inline]
638     pub fn acos(self) -> f64 {
639         unsafe { cmath::acos(self) }
640     }
641
642     /// Computes the arctangent of a number. Return value is in radians in the
643     /// range [-pi/2, pi/2];
644     ///
645     /// # Examples
646     ///
647     /// ```
648     /// let f = 1.0_f64;
649     ///
650     /// // atan(tan(1))
651     /// let abs_difference = (f.tan().atan() - 1.0).abs();
652     ///
653     /// assert!(abs_difference < 1e-10);
654     /// ```
655     #[stable(feature = "rust1", since = "1.0.0")]
656     #[inline]
657     pub fn atan(self) -> f64 {
658         unsafe { cmath::atan(self) }
659     }
660
661     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
662     ///
663     /// * `x = 0`, `y = 0`: `0`
664     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
665     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
666     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
667     ///
668     /// # Examples
669     ///
670     /// ```
671     /// use std::f64;
672     ///
673     /// let pi = f64::consts::PI;
674     /// // Positive angles measured counter-clockwise
675     /// // from positive x axis
676     /// // -pi/4 radians (45 deg clockwise)
677     /// let x1 = 3.0_f64;
678     /// let y1 = -3.0_f64;
679     ///
680     /// // 3pi/4 radians (135 deg counter-clockwise)
681     /// let x2 = -3.0_f64;
682     /// let y2 = 3.0_f64;
683     ///
684     /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
685     /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
686     ///
687     /// assert!(abs_difference_1 < 1e-10);
688     /// assert!(abs_difference_2 < 1e-10);
689     /// ```
690     #[stable(feature = "rust1", since = "1.0.0")]
691     #[inline]
692     pub fn atan2(self, other: f64) -> f64 {
693         unsafe { cmath::atan2(self, other) }
694     }
695
696     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
697     /// `(sin(x), cos(x))`.
698     ///
699     /// # Examples
700     ///
701     /// ```
702     /// use std::f64;
703     ///
704     /// let x = f64::consts::PI/4.0;
705     /// let f = x.sin_cos();
706     ///
707     /// let abs_difference_0 = (f.0 - x.sin()).abs();
708     /// let abs_difference_1 = (f.1 - x.cos()).abs();
709     ///
710     /// assert!(abs_difference_0 < 1e-10);
711     /// assert!(abs_difference_1 < 1e-10);
712     /// ```
713     #[stable(feature = "rust1", since = "1.0.0")]
714     #[inline]
715     pub fn sin_cos(self) -> (f64, f64) {
716         (self.sin(), self.cos())
717     }
718
719     /// Returns `e^(self) - 1` in a way that is accurate even if the
720     /// number is close to zero.
721     ///
722     /// # Examples
723     ///
724     /// ```
725     /// let x = 7.0_f64;
726     ///
727     /// // e^(ln(7)) - 1
728     /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
729     ///
730     /// assert!(abs_difference < 1e-10);
731     /// ```
732     #[stable(feature = "rust1", since = "1.0.0")]
733     #[inline]
734     pub fn exp_m1(self) -> f64 {
735         unsafe { cmath::expm1(self) }
736     }
737
738     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
739     /// the operations were performed separately.
740     ///
741     /// # Examples
742     ///
743     /// ```
744     /// use std::f64;
745     ///
746     /// let x = f64::consts::E - 1.0;
747     ///
748     /// // ln(1 + (e - 1)) == ln(e) == 1
749     /// let abs_difference = (x.ln_1p() - 1.0).abs();
750     ///
751     /// assert!(abs_difference < 1e-10);
752     /// ```
753     #[stable(feature = "rust1", since = "1.0.0")]
754     #[inline]
755     pub fn ln_1p(self) -> f64 {
756         unsafe { cmath::log1p(self) }
757     }
758
759     /// Hyperbolic sine function.
760     ///
761     /// # Examples
762     ///
763     /// ```
764     /// use std::f64;
765     ///
766     /// let e = f64::consts::E;
767     /// let x = 1.0_f64;
768     ///
769     /// let f = x.sinh();
770     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
771     /// let g = (e*e - 1.0)/(2.0*e);
772     /// let abs_difference = (f - g).abs();
773     ///
774     /// assert!(abs_difference < 1e-10);
775     /// ```
776     #[stable(feature = "rust1", since = "1.0.0")]
777     #[inline]
778     pub fn sinh(self) -> f64 {
779         unsafe { cmath::sinh(self) }
780     }
781
782     /// Hyperbolic cosine function.
783     ///
784     /// # Examples
785     ///
786     /// ```
787     /// use std::f64;
788     ///
789     /// let e = f64::consts::E;
790     /// let x = 1.0_f64;
791     /// let f = x.cosh();
792     /// // Solving cosh() at 1 gives this result
793     /// let g = (e*e + 1.0)/(2.0*e);
794     /// let abs_difference = (f - g).abs();
795     ///
796     /// // Same result
797     /// assert!(abs_difference < 1.0e-10);
798     /// ```
799     #[stable(feature = "rust1", since = "1.0.0")]
800     #[inline]
801     pub fn cosh(self) -> f64 {
802         unsafe { cmath::cosh(self) }
803     }
804
805     /// Hyperbolic tangent function.
806     ///
807     /// # Examples
808     ///
809     /// ```
810     /// use std::f64;
811     ///
812     /// let e = f64::consts::E;
813     /// let x = 1.0_f64;
814     ///
815     /// let f = x.tanh();
816     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
817     /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
818     /// let abs_difference = (f - g).abs();
819     ///
820     /// assert!(abs_difference < 1.0e-10);
821     /// ```
822     #[stable(feature = "rust1", since = "1.0.0")]
823     #[inline]
824     pub fn tanh(self) -> f64 {
825         unsafe { cmath::tanh(self) }
826     }
827
828     /// Inverse hyperbolic sine function.
829     ///
830     /// # Examples
831     ///
832     /// ```
833     /// let x = 1.0_f64;
834     /// let f = x.sinh().asinh();
835     ///
836     /// let abs_difference = (f - x).abs();
837     ///
838     /// assert!(abs_difference < 1.0e-10);
839     /// ```
840     #[stable(feature = "rust1", since = "1.0.0")]
841     #[inline]
842     pub fn asinh(self) -> f64 {
843         if self == NEG_INFINITY {
844             NEG_INFINITY
845         } else {
846             (self + ((self * self) + 1.0).sqrt()).ln()
847         }
848     }
849
850     /// Inverse hyperbolic cosine function.
851     ///
852     /// # Examples
853     ///
854     /// ```
855     /// let x = 1.0_f64;
856     /// let f = x.cosh().acosh();
857     ///
858     /// let abs_difference = (f - x).abs();
859     ///
860     /// assert!(abs_difference < 1.0e-10);
861     /// ```
862     #[stable(feature = "rust1", since = "1.0.0")]
863     #[inline]
864     pub fn acosh(self) -> f64 {
865         match self {
866             x if x < 1.0 => NAN,
867             x => (x + ((x * x) - 1.0).sqrt()).ln(),
868         }
869     }
870
871     /// Inverse hyperbolic tangent function.
872     ///
873     /// # Examples
874     ///
875     /// ```
876     /// use std::f64;
877     ///
878     /// let e = f64::consts::E;
879     /// let f = e.tanh().atanh();
880     ///
881     /// let abs_difference = (f - e).abs();
882     ///
883     /// assert!(abs_difference < 1.0e-10);
884     /// ```
885     #[stable(feature = "rust1", since = "1.0.0")]
886     #[inline]
887     pub fn atanh(self) -> f64 {
888         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
889     }
890
891     // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
892     // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
893     // of expected NaN).
894     fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
895         if !cfg!(target_os = "solaris") {
896             log_fn(self)
897         } else {
898             if self.is_finite() {
899                 if self > 0.0 {
900                     log_fn(self)
901                 } else if self == 0.0 {
902                     NEG_INFINITY // log(0) = -Inf
903                 } else {
904                     NAN // log(-n) = NaN
905                 }
906             } else if self.is_nan() {
907                 self // log(NaN) = NaN
908             } else if self > 0.0 {
909                 self // log(Inf) = Inf
910             } else {
911                 NAN // log(-Inf) = NaN
912             }
913         }
914     }
915 }
916
917 #[cfg(test)]
918 mod tests {
919     use f64;
920     use f64::*;
921     use num::*;
922     use num::FpCategory as Fp;
923
924     #[test]
925     fn test_num_f64() {
926         test_num(10f64, 2f64);
927     }
928
929     #[test]
930     fn test_min_nan() {
931         assert_eq!(NAN.min(2.0), 2.0);
932         assert_eq!(2.0f64.min(NAN), 2.0);
933     }
934
935     #[test]
936     fn test_max_nan() {
937         assert_eq!(NAN.max(2.0), 2.0);
938         assert_eq!(2.0f64.max(NAN), 2.0);
939     }
940
941     #[test]
942     fn test_nan() {
943         let nan: f64 = NAN;
944         assert!(nan.is_nan());
945         assert!(!nan.is_infinite());
946         assert!(!nan.is_finite());
947         assert!(!nan.is_normal());
948         assert!(nan.is_sign_positive());
949         assert!(!nan.is_sign_negative());
950         assert_eq!(Fp::Nan, nan.classify());
951     }
952
953     #[test]
954     fn test_infinity() {
955         let inf: f64 = INFINITY;
956         assert!(inf.is_infinite());
957         assert!(!inf.is_finite());
958         assert!(inf.is_sign_positive());
959         assert!(!inf.is_sign_negative());
960         assert!(!inf.is_nan());
961         assert!(!inf.is_normal());
962         assert_eq!(Fp::Infinite, inf.classify());
963     }
964
965     #[test]
966     fn test_neg_infinity() {
967         let neg_inf: f64 = NEG_INFINITY;
968         assert!(neg_inf.is_infinite());
969         assert!(!neg_inf.is_finite());
970         assert!(!neg_inf.is_sign_positive());
971         assert!(neg_inf.is_sign_negative());
972         assert!(!neg_inf.is_nan());
973         assert!(!neg_inf.is_normal());
974         assert_eq!(Fp::Infinite, neg_inf.classify());
975     }
976
977     #[test]
978     fn test_zero() {
979         let zero: f64 = 0.0f64;
980         assert_eq!(0.0, zero);
981         assert!(!zero.is_infinite());
982         assert!(zero.is_finite());
983         assert!(zero.is_sign_positive());
984         assert!(!zero.is_sign_negative());
985         assert!(!zero.is_nan());
986         assert!(!zero.is_normal());
987         assert_eq!(Fp::Zero, zero.classify());
988     }
989
990     #[test]
991     fn test_neg_zero() {
992         let neg_zero: f64 = -0.0;
993         assert_eq!(0.0, neg_zero);
994         assert!(!neg_zero.is_infinite());
995         assert!(neg_zero.is_finite());
996         assert!(!neg_zero.is_sign_positive());
997         assert!(neg_zero.is_sign_negative());
998         assert!(!neg_zero.is_nan());
999         assert!(!neg_zero.is_normal());
1000         assert_eq!(Fp::Zero, neg_zero.classify());
1001     }
1002
1003     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1004     #[test]
1005     fn test_one() {
1006         let one: f64 = 1.0f64;
1007         assert_eq!(1.0, one);
1008         assert!(!one.is_infinite());
1009         assert!(one.is_finite());
1010         assert!(one.is_sign_positive());
1011         assert!(!one.is_sign_negative());
1012         assert!(!one.is_nan());
1013         assert!(one.is_normal());
1014         assert_eq!(Fp::Normal, one.classify());
1015     }
1016
1017     #[test]
1018     fn test_is_nan() {
1019         let nan: f64 = NAN;
1020         let inf: f64 = INFINITY;
1021         let neg_inf: f64 = NEG_INFINITY;
1022         assert!(nan.is_nan());
1023         assert!(!0.0f64.is_nan());
1024         assert!(!5.3f64.is_nan());
1025         assert!(!(-10.732f64).is_nan());
1026         assert!(!inf.is_nan());
1027         assert!(!neg_inf.is_nan());
1028     }
1029
1030     #[test]
1031     fn test_is_infinite() {
1032         let nan: f64 = NAN;
1033         let inf: f64 = INFINITY;
1034         let neg_inf: f64 = NEG_INFINITY;
1035         assert!(!nan.is_infinite());
1036         assert!(inf.is_infinite());
1037         assert!(neg_inf.is_infinite());
1038         assert!(!0.0f64.is_infinite());
1039         assert!(!42.8f64.is_infinite());
1040         assert!(!(-109.2f64).is_infinite());
1041     }
1042
1043     #[test]
1044     fn test_is_finite() {
1045         let nan: f64 = NAN;
1046         let inf: f64 = INFINITY;
1047         let neg_inf: f64 = NEG_INFINITY;
1048         assert!(!nan.is_finite());
1049         assert!(!inf.is_finite());
1050         assert!(!neg_inf.is_finite());
1051         assert!(0.0f64.is_finite());
1052         assert!(42.8f64.is_finite());
1053         assert!((-109.2f64).is_finite());
1054     }
1055
1056     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1057     #[test]
1058     fn test_is_normal() {
1059         let nan: f64 = NAN;
1060         let inf: f64 = INFINITY;
1061         let neg_inf: f64 = NEG_INFINITY;
1062         let zero: f64 = 0.0f64;
1063         let neg_zero: f64 = -0.0;
1064         assert!(!nan.is_normal());
1065         assert!(!inf.is_normal());
1066         assert!(!neg_inf.is_normal());
1067         assert!(!zero.is_normal());
1068         assert!(!neg_zero.is_normal());
1069         assert!(1f64.is_normal());
1070         assert!(1e-307f64.is_normal());
1071         assert!(!1e-308f64.is_normal());
1072     }
1073
1074     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1075     #[test]
1076     fn test_classify() {
1077         let nan: f64 = NAN;
1078         let inf: f64 = INFINITY;
1079         let neg_inf: f64 = NEG_INFINITY;
1080         let zero: f64 = 0.0f64;
1081         let neg_zero: f64 = -0.0;
1082         assert_eq!(nan.classify(), Fp::Nan);
1083         assert_eq!(inf.classify(), Fp::Infinite);
1084         assert_eq!(neg_inf.classify(), Fp::Infinite);
1085         assert_eq!(zero.classify(), Fp::Zero);
1086         assert_eq!(neg_zero.classify(), Fp::Zero);
1087         assert_eq!(1e-307f64.classify(), Fp::Normal);
1088         assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1089     }
1090
1091     #[test]
1092     fn test_floor() {
1093         assert_approx_eq!(1.0f64.floor(), 1.0f64);
1094         assert_approx_eq!(1.3f64.floor(), 1.0f64);
1095         assert_approx_eq!(1.5f64.floor(), 1.0f64);
1096         assert_approx_eq!(1.7f64.floor(), 1.0f64);
1097         assert_approx_eq!(0.0f64.floor(), 0.0f64);
1098         assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1099         assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1100         assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1101         assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1102         assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1103     }
1104
1105     #[test]
1106     fn test_ceil() {
1107         assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1108         assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1109         assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1110         assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1111         assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1112         assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1113         assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1114         assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1115         assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1116         assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1117     }
1118
1119     #[test]
1120     fn test_round() {
1121         assert_approx_eq!(1.0f64.round(), 1.0f64);
1122         assert_approx_eq!(1.3f64.round(), 1.0f64);
1123         assert_approx_eq!(1.5f64.round(), 2.0f64);
1124         assert_approx_eq!(1.7f64.round(), 2.0f64);
1125         assert_approx_eq!(0.0f64.round(), 0.0f64);
1126         assert_approx_eq!((-0.0f64).round(), -0.0f64);
1127         assert_approx_eq!((-1.0f64).round(), -1.0f64);
1128         assert_approx_eq!((-1.3f64).round(), -1.0f64);
1129         assert_approx_eq!((-1.5f64).round(), -2.0f64);
1130         assert_approx_eq!((-1.7f64).round(), -2.0f64);
1131     }
1132
1133     #[test]
1134     fn test_trunc() {
1135         assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1136         assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1137         assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1138         assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1139         assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1140         assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1141         assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1142         assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1143         assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1144         assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1145     }
1146
1147     #[test]
1148     fn test_fract() {
1149         assert_approx_eq!(1.0f64.fract(), 0.0f64);
1150         assert_approx_eq!(1.3f64.fract(), 0.3f64);
1151         assert_approx_eq!(1.5f64.fract(), 0.5f64);
1152         assert_approx_eq!(1.7f64.fract(), 0.7f64);
1153         assert_approx_eq!(0.0f64.fract(), 0.0f64);
1154         assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1155         assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1156         assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1157         assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1158         assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1159     }
1160
1161     #[test]
1162     fn test_abs() {
1163         assert_eq!(INFINITY.abs(), INFINITY);
1164         assert_eq!(1f64.abs(), 1f64);
1165         assert_eq!(0f64.abs(), 0f64);
1166         assert_eq!((-0f64).abs(), 0f64);
1167         assert_eq!((-1f64).abs(), 1f64);
1168         assert_eq!(NEG_INFINITY.abs(), INFINITY);
1169         assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1170         assert!(NAN.abs().is_nan());
1171     }
1172
1173     #[test]
1174     fn test_signum() {
1175         assert_eq!(INFINITY.signum(), 1f64);
1176         assert_eq!(1f64.signum(), 1f64);
1177         assert_eq!(0f64.signum(), 1f64);
1178         assert_eq!((-0f64).signum(), -1f64);
1179         assert_eq!((-1f64).signum(), -1f64);
1180         assert_eq!(NEG_INFINITY.signum(), -1f64);
1181         assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1182         assert!(NAN.signum().is_nan());
1183     }
1184
1185     #[test]
1186     fn test_is_sign_positive() {
1187         assert!(INFINITY.is_sign_positive());
1188         assert!(1f64.is_sign_positive());
1189         assert!(0f64.is_sign_positive());
1190         assert!(!(-0f64).is_sign_positive());
1191         assert!(!(-1f64).is_sign_positive());
1192         assert!(!NEG_INFINITY.is_sign_positive());
1193         assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1194         assert!(NAN.is_sign_positive());
1195         assert!(!(-NAN).is_sign_positive());
1196     }
1197
1198     #[test]
1199     fn test_is_sign_negative() {
1200         assert!(!INFINITY.is_sign_negative());
1201         assert!(!1f64.is_sign_negative());
1202         assert!(!0f64.is_sign_negative());
1203         assert!((-0f64).is_sign_negative());
1204         assert!((-1f64).is_sign_negative());
1205         assert!(NEG_INFINITY.is_sign_negative());
1206         assert!((1f64/NEG_INFINITY).is_sign_negative());
1207         assert!(!NAN.is_sign_negative());
1208         assert!((-NAN).is_sign_negative());
1209     }
1210
1211     #[test]
1212     fn test_mul_add() {
1213         let nan: f64 = NAN;
1214         let inf: f64 = INFINITY;
1215         let neg_inf: f64 = NEG_INFINITY;
1216         assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1217         assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1218         assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1219         assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1220         assert!(nan.mul_add(7.8, 9.0).is_nan());
1221         assert_eq!(inf.mul_add(7.8, 9.0), inf);
1222         assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1223         assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1224         assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1225     }
1226
1227     #[test]
1228     fn test_recip() {
1229         let nan: f64 = NAN;
1230         let inf: f64 = INFINITY;
1231         let neg_inf: f64 = NEG_INFINITY;
1232         assert_eq!(1.0f64.recip(), 1.0);
1233         assert_eq!(2.0f64.recip(), 0.5);
1234         assert_eq!((-0.4f64).recip(), -2.5);
1235         assert_eq!(0.0f64.recip(), inf);
1236         assert!(nan.recip().is_nan());
1237         assert_eq!(inf.recip(), 0.0);
1238         assert_eq!(neg_inf.recip(), 0.0);
1239     }
1240
1241     #[test]
1242     fn test_powi() {
1243         let nan: f64 = NAN;
1244         let inf: f64 = INFINITY;
1245         let neg_inf: f64 = NEG_INFINITY;
1246         assert_eq!(1.0f64.powi(1), 1.0);
1247         assert_approx_eq!((-3.1f64).powi(2), 9.61);
1248         assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1249         assert_eq!(8.3f64.powi(0), 1.0);
1250         assert!(nan.powi(2).is_nan());
1251         assert_eq!(inf.powi(3), inf);
1252         assert_eq!(neg_inf.powi(2), inf);
1253     }
1254
1255     #[test]
1256     fn test_powf() {
1257         let nan: f64 = NAN;
1258         let inf: f64 = INFINITY;
1259         let neg_inf: f64 = NEG_INFINITY;
1260         assert_eq!(1.0f64.powf(1.0), 1.0);
1261         assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1262         assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1263         assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1264         assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1265         assert_eq!(8.3f64.powf(0.0), 1.0);
1266         assert!(nan.powf(2.0).is_nan());
1267         assert_eq!(inf.powf(2.0), inf);
1268         assert_eq!(neg_inf.powf(3.0), neg_inf);
1269     }
1270
1271     #[test]
1272     fn test_sqrt_domain() {
1273         assert!(NAN.sqrt().is_nan());
1274         assert!(NEG_INFINITY.sqrt().is_nan());
1275         assert!((-1.0f64).sqrt().is_nan());
1276         assert_eq!((-0.0f64).sqrt(), -0.0);
1277         assert_eq!(0.0f64.sqrt(), 0.0);
1278         assert_eq!(1.0f64.sqrt(), 1.0);
1279         assert_eq!(INFINITY.sqrt(), INFINITY);
1280     }
1281
1282     #[test]
1283     fn test_exp() {
1284         assert_eq!(1.0, 0.0f64.exp());
1285         assert_approx_eq!(2.718282, 1.0f64.exp());
1286         assert_approx_eq!(148.413159, 5.0f64.exp());
1287
1288         let inf: f64 = INFINITY;
1289         let neg_inf: f64 = NEG_INFINITY;
1290         let nan: f64 = NAN;
1291         assert_eq!(inf, inf.exp());
1292         assert_eq!(0.0, neg_inf.exp());
1293         assert!(nan.exp().is_nan());
1294     }
1295
1296     #[test]
1297     fn test_exp2() {
1298         assert_eq!(32.0, 5.0f64.exp2());
1299         assert_eq!(1.0, 0.0f64.exp2());
1300
1301         let inf: f64 = INFINITY;
1302         let neg_inf: f64 = NEG_INFINITY;
1303         let nan: f64 = NAN;
1304         assert_eq!(inf, inf.exp2());
1305         assert_eq!(0.0, neg_inf.exp2());
1306         assert!(nan.exp2().is_nan());
1307     }
1308
1309     #[test]
1310     fn test_ln() {
1311         let nan: f64 = NAN;
1312         let inf: f64 = INFINITY;
1313         let neg_inf: f64 = NEG_INFINITY;
1314         assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1315         assert!(nan.ln().is_nan());
1316         assert_eq!(inf.ln(), inf);
1317         assert!(neg_inf.ln().is_nan());
1318         assert!((-2.3f64).ln().is_nan());
1319         assert_eq!((-0.0f64).ln(), neg_inf);
1320         assert_eq!(0.0f64.ln(), neg_inf);
1321         assert_approx_eq!(4.0f64.ln(), 1.386294);
1322     }
1323
1324     #[test]
1325     fn test_log() {
1326         let nan: f64 = NAN;
1327         let inf: f64 = INFINITY;
1328         let neg_inf: f64 = NEG_INFINITY;
1329         assert_eq!(10.0f64.log(10.0), 1.0);
1330         assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1331         assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1332         assert!(1.0f64.log(1.0).is_nan());
1333         assert!(1.0f64.log(-13.9).is_nan());
1334         assert!(nan.log(2.3).is_nan());
1335         assert_eq!(inf.log(10.0), inf);
1336         assert!(neg_inf.log(8.8).is_nan());
1337         assert!((-2.3f64).log(0.1).is_nan());
1338         assert_eq!((-0.0f64).log(2.0), neg_inf);
1339         assert_eq!(0.0f64.log(7.0), neg_inf);
1340     }
1341
1342     #[test]
1343     fn test_log2() {
1344         let nan: f64 = NAN;
1345         let inf: f64 = INFINITY;
1346         let neg_inf: f64 = NEG_INFINITY;
1347         assert_approx_eq!(10.0f64.log2(), 3.321928);
1348         assert_approx_eq!(2.3f64.log2(), 1.201634);
1349         assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1350         assert!(nan.log2().is_nan());
1351         assert_eq!(inf.log2(), inf);
1352         assert!(neg_inf.log2().is_nan());
1353         assert!((-2.3f64).log2().is_nan());
1354         assert_eq!((-0.0f64).log2(), neg_inf);
1355         assert_eq!(0.0f64.log2(), neg_inf);
1356     }
1357
1358     #[test]
1359     fn test_log10() {
1360         let nan: f64 = NAN;
1361         let inf: f64 = INFINITY;
1362         let neg_inf: f64 = NEG_INFINITY;
1363         assert_eq!(10.0f64.log10(), 1.0);
1364         assert_approx_eq!(2.3f64.log10(), 0.361728);
1365         assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1366         assert_eq!(1.0f64.log10(), 0.0);
1367         assert!(nan.log10().is_nan());
1368         assert_eq!(inf.log10(), inf);
1369         assert!(neg_inf.log10().is_nan());
1370         assert!((-2.3f64).log10().is_nan());
1371         assert_eq!((-0.0f64).log10(), neg_inf);
1372         assert_eq!(0.0f64.log10(), neg_inf);
1373     }
1374
1375     #[test]
1376     fn test_to_degrees() {
1377         let pi: f64 = consts::PI;
1378         let nan: f64 = NAN;
1379         let inf: f64 = INFINITY;
1380         let neg_inf: f64 = NEG_INFINITY;
1381         assert_eq!(0.0f64.to_degrees(), 0.0);
1382         assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1383         assert_eq!(pi.to_degrees(), 180.0);
1384         assert!(nan.to_degrees().is_nan());
1385         assert_eq!(inf.to_degrees(), inf);
1386         assert_eq!(neg_inf.to_degrees(), neg_inf);
1387     }
1388
1389     #[test]
1390     fn test_to_radians() {
1391         let pi: f64 = consts::PI;
1392         let nan: f64 = NAN;
1393         let inf: f64 = INFINITY;
1394         let neg_inf: f64 = NEG_INFINITY;
1395         assert_eq!(0.0f64.to_radians(), 0.0);
1396         assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1397         assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1398         assert_eq!(180.0f64.to_radians(), pi);
1399         assert!(nan.to_radians().is_nan());
1400         assert_eq!(inf.to_radians(), inf);
1401         assert_eq!(neg_inf.to_radians(), neg_inf);
1402     }
1403
1404     #[test]
1405     fn test_asinh() {
1406         assert_eq!(0.0f64.asinh(), 0.0f64);
1407         assert_eq!((-0.0f64).asinh(), -0.0f64);
1408
1409         let inf: f64 = INFINITY;
1410         let neg_inf: f64 = NEG_INFINITY;
1411         let nan: f64 = NAN;
1412         assert_eq!(inf.asinh(), inf);
1413         assert_eq!(neg_inf.asinh(), neg_inf);
1414         assert!(nan.asinh().is_nan());
1415         assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1416         assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1417     }
1418
1419     #[test]
1420     fn test_acosh() {
1421         assert_eq!(1.0f64.acosh(), 0.0f64);
1422         assert!(0.999f64.acosh().is_nan());
1423
1424         let inf: f64 = INFINITY;
1425         let neg_inf: f64 = NEG_INFINITY;
1426         let nan: f64 = NAN;
1427         assert_eq!(inf.acosh(), inf);
1428         assert!(neg_inf.acosh().is_nan());
1429         assert!(nan.acosh().is_nan());
1430         assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1431         assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1432     }
1433
1434     #[test]
1435     fn test_atanh() {
1436         assert_eq!(0.0f64.atanh(), 0.0f64);
1437         assert_eq!((-0.0f64).atanh(), -0.0f64);
1438
1439         let inf: f64 = INFINITY;
1440         let neg_inf: f64 = NEG_INFINITY;
1441         let nan: f64 = NAN;
1442         assert_eq!(1.0f64.atanh(), inf);
1443         assert_eq!((-1.0f64).atanh(), neg_inf);
1444         assert!(2f64.atanh().atanh().is_nan());
1445         assert!((-2f64).atanh().atanh().is_nan());
1446         assert!(inf.atanh().is_nan());
1447         assert!(neg_inf.atanh().is_nan());
1448         assert!(nan.atanh().is_nan());
1449         assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1450         assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1451     }
1452
1453     #[test]
1454     fn test_real_consts() {
1455         use super::consts;
1456         let pi: f64 = consts::PI;
1457         let frac_pi_2: f64 = consts::FRAC_PI_2;
1458         let frac_pi_3: f64 = consts::FRAC_PI_3;
1459         let frac_pi_4: f64 = consts::FRAC_PI_4;
1460         let frac_pi_6: f64 = consts::FRAC_PI_6;
1461         let frac_pi_8: f64 = consts::FRAC_PI_8;
1462         let frac_1_pi: f64 = consts::FRAC_1_PI;
1463         let frac_2_pi: f64 = consts::FRAC_2_PI;
1464         let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1465         let sqrt2: f64 = consts::SQRT_2;
1466         let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1467         let e: f64 = consts::E;
1468         let log2_e: f64 = consts::LOG2_E;
1469         let log10_e: f64 = consts::LOG10_E;
1470         let ln_2: f64 = consts::LN_2;
1471         let ln_10: f64 = consts::LN_10;
1472
1473         assert_approx_eq!(frac_pi_2, pi / 2f64);
1474         assert_approx_eq!(frac_pi_3, pi / 3f64);
1475         assert_approx_eq!(frac_pi_4, pi / 4f64);
1476         assert_approx_eq!(frac_pi_6, pi / 6f64);
1477         assert_approx_eq!(frac_pi_8, pi / 8f64);
1478         assert_approx_eq!(frac_1_pi, 1f64 / pi);
1479         assert_approx_eq!(frac_2_pi, 2f64 / pi);
1480         assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1481         assert_approx_eq!(sqrt2, 2f64.sqrt());
1482         assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1483         assert_approx_eq!(log2_e, e.log2());
1484         assert_approx_eq!(log10_e, e.log10());
1485         assert_approx_eq!(ln_2, 2f64.ln());
1486         assert_approx_eq!(ln_10, 10f64.ln());
1487     }
1488
1489     #[test]
1490     fn test_float_bits_conv() {
1491         assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1492         assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1493         assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1494         assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1495         assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1496         assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1497         assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1498         assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1499
1500         // Check that NaNs roundtrip their bits regardless of signalingness
1501         // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1502         let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1503         let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1504         assert!(f64::from_bits(masked_nan1).is_nan());
1505         assert!(f64::from_bits(masked_nan2).is_nan());
1506
1507         assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1508         assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
1509     }
1510 }