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