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