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