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