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