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