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