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