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