]> git.lizzy.rs Git - rust.git/blob - src/libstd/f64.rs
Auto merge of #43651 - petrochenkov:foreign-life, r=eddyb
[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     // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
974     // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
975     // of expected NaN).
976     fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
977         if !cfg!(target_os = "solaris") {
978             log_fn(self)
979         } else {
980             if self.is_finite() {
981                 if self > 0.0 {
982                     log_fn(self)
983                 } else if self == 0.0 {
984                     NEG_INFINITY // log(0) = -Inf
985                 } else {
986                     NAN // log(-n) = NaN
987                 }
988             } else if self.is_nan() {
989                 self // log(NaN) = NaN
990             } else if self > 0.0 {
991                 self // log(Inf) = Inf
992             } else {
993                 NAN // log(-Inf) = NaN
994             }
995         }
996     }
997
998     /// Raw transmutation to `u64`.
999     ///
1000     /// Converts the `f64` into its raw memory representation,
1001     /// similar to the `transmute` function.
1002     ///
1003     /// Note that this function is distinct from casting.
1004     ///
1005     /// # Examples
1006     ///
1007     /// ```
1008     /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
1009     /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1010     ///
1011     /// ```
1012     #[stable(feature = "float_bits_conv", since = "1.20.0")]
1013     #[inline]
1014     pub fn to_bits(self) -> u64 {
1015         unsafe { ::mem::transmute(self) }
1016     }
1017
1018     /// Raw transmutation from `u64`.
1019     ///
1020     /// Converts the given `u64` containing the float's raw memory
1021     /// representation into the `f64` type, similar to the
1022     /// `transmute` function.
1023     ///
1024     /// There is only one difference to a bare `transmute`:
1025     /// Due to the implications onto Rust's safety promises being
1026     /// uncertain, if the representation of a signaling NaN "sNaN" float
1027     /// is passed to the function, the implementation is allowed to
1028     /// return a quiet NaN instead.
1029     ///
1030     /// Note that this function is distinct from casting.
1031     ///
1032     /// # Examples
1033     ///
1034     /// ```
1035     /// use std::f64;
1036     /// let v = f64::from_bits(0x4029000000000000);
1037     /// let difference = (v - 12.5).abs();
1038     /// assert!(difference <= 1e-5);
1039     /// // Example for a signaling NaN value:
1040     /// let snan = 0x7FF0000000000001;
1041     /// assert_ne!(f64::from_bits(snan).to_bits(), snan);
1042     /// ```
1043     #[stable(feature = "float_bits_conv", since = "1.20.0")]
1044     #[inline]
1045     pub fn from_bits(mut v: u64) -> Self {
1046         const EXP_MASK: u64   = 0x7FF0000000000000;
1047         const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;
1048         if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1049             // While IEEE 754-2008 specifies encodings for quiet NaNs
1050             // and signaling ones, certain MIPS and PA-RISC
1051             // CPUs treat signaling NaNs differently.
1052             // Therefore to be safe, we pass a known quiet NaN
1053             // if v is any kind of NaN.
1054             // The check above only assumes IEEE 754-1985 to be
1055             // valid.
1056             v = unsafe { ::mem::transmute(NAN) };
1057         }
1058         unsafe { ::mem::transmute(v) }
1059     }
1060 }
1061
1062 #[cfg(test)]
1063 mod tests {
1064     use f64;
1065     use f64::*;
1066     use num::*;
1067     use num::FpCategory as Fp;
1068
1069     #[test]
1070     fn test_num_f64() {
1071         test_num(10f64, 2f64);
1072     }
1073
1074     #[test]
1075     fn test_min_nan() {
1076         assert_eq!(NAN.min(2.0), 2.0);
1077         assert_eq!(2.0f64.min(NAN), 2.0);
1078     }
1079
1080     #[test]
1081     fn test_max_nan() {
1082         assert_eq!(NAN.max(2.0), 2.0);
1083         assert_eq!(2.0f64.max(NAN), 2.0);
1084     }
1085
1086     #[test]
1087     fn test_nan() {
1088         let nan: f64 = NAN;
1089         assert!(nan.is_nan());
1090         assert!(!nan.is_infinite());
1091         assert!(!nan.is_finite());
1092         assert!(!nan.is_normal());
1093         assert!(nan.is_sign_positive());
1094         assert!(!nan.is_sign_negative());
1095         assert_eq!(Fp::Nan, nan.classify());
1096     }
1097
1098     #[test]
1099     fn test_infinity() {
1100         let inf: f64 = INFINITY;
1101         assert!(inf.is_infinite());
1102         assert!(!inf.is_finite());
1103         assert!(inf.is_sign_positive());
1104         assert!(!inf.is_sign_negative());
1105         assert!(!inf.is_nan());
1106         assert!(!inf.is_normal());
1107         assert_eq!(Fp::Infinite, inf.classify());
1108     }
1109
1110     #[test]
1111     fn test_neg_infinity() {
1112         let neg_inf: f64 = NEG_INFINITY;
1113         assert!(neg_inf.is_infinite());
1114         assert!(!neg_inf.is_finite());
1115         assert!(!neg_inf.is_sign_positive());
1116         assert!(neg_inf.is_sign_negative());
1117         assert!(!neg_inf.is_nan());
1118         assert!(!neg_inf.is_normal());
1119         assert_eq!(Fp::Infinite, neg_inf.classify());
1120     }
1121
1122     #[test]
1123     fn test_zero() {
1124         let zero: f64 = 0.0f64;
1125         assert_eq!(0.0, zero);
1126         assert!(!zero.is_infinite());
1127         assert!(zero.is_finite());
1128         assert!(zero.is_sign_positive());
1129         assert!(!zero.is_sign_negative());
1130         assert!(!zero.is_nan());
1131         assert!(!zero.is_normal());
1132         assert_eq!(Fp::Zero, zero.classify());
1133     }
1134
1135     #[test]
1136     fn test_neg_zero() {
1137         let neg_zero: f64 = -0.0;
1138         assert_eq!(0.0, neg_zero);
1139         assert!(!neg_zero.is_infinite());
1140         assert!(neg_zero.is_finite());
1141         assert!(!neg_zero.is_sign_positive());
1142         assert!(neg_zero.is_sign_negative());
1143         assert!(!neg_zero.is_nan());
1144         assert!(!neg_zero.is_normal());
1145         assert_eq!(Fp::Zero, neg_zero.classify());
1146     }
1147
1148     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1149     #[test]
1150     fn test_one() {
1151         let one: f64 = 1.0f64;
1152         assert_eq!(1.0, one);
1153         assert!(!one.is_infinite());
1154         assert!(one.is_finite());
1155         assert!(one.is_sign_positive());
1156         assert!(!one.is_sign_negative());
1157         assert!(!one.is_nan());
1158         assert!(one.is_normal());
1159         assert_eq!(Fp::Normal, one.classify());
1160     }
1161
1162     #[test]
1163     fn test_is_nan() {
1164         let nan: f64 = NAN;
1165         let inf: f64 = INFINITY;
1166         let neg_inf: f64 = NEG_INFINITY;
1167         assert!(nan.is_nan());
1168         assert!(!0.0f64.is_nan());
1169         assert!(!5.3f64.is_nan());
1170         assert!(!(-10.732f64).is_nan());
1171         assert!(!inf.is_nan());
1172         assert!(!neg_inf.is_nan());
1173     }
1174
1175     #[test]
1176     fn test_is_infinite() {
1177         let nan: f64 = NAN;
1178         let inf: f64 = INFINITY;
1179         let neg_inf: f64 = NEG_INFINITY;
1180         assert!(!nan.is_infinite());
1181         assert!(inf.is_infinite());
1182         assert!(neg_inf.is_infinite());
1183         assert!(!0.0f64.is_infinite());
1184         assert!(!42.8f64.is_infinite());
1185         assert!(!(-109.2f64).is_infinite());
1186     }
1187
1188     #[test]
1189     fn test_is_finite() {
1190         let nan: f64 = NAN;
1191         let inf: f64 = INFINITY;
1192         let neg_inf: f64 = NEG_INFINITY;
1193         assert!(!nan.is_finite());
1194         assert!(!inf.is_finite());
1195         assert!(!neg_inf.is_finite());
1196         assert!(0.0f64.is_finite());
1197         assert!(42.8f64.is_finite());
1198         assert!((-109.2f64).is_finite());
1199     }
1200
1201     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1202     #[test]
1203     fn test_is_normal() {
1204         let nan: f64 = NAN;
1205         let inf: f64 = INFINITY;
1206         let neg_inf: f64 = NEG_INFINITY;
1207         let zero: f64 = 0.0f64;
1208         let neg_zero: f64 = -0.0;
1209         assert!(!nan.is_normal());
1210         assert!(!inf.is_normal());
1211         assert!(!neg_inf.is_normal());
1212         assert!(!zero.is_normal());
1213         assert!(!neg_zero.is_normal());
1214         assert!(1f64.is_normal());
1215         assert!(1e-307f64.is_normal());
1216         assert!(!1e-308f64.is_normal());
1217     }
1218
1219     #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1220     #[test]
1221     fn test_classify() {
1222         let nan: f64 = NAN;
1223         let inf: f64 = INFINITY;
1224         let neg_inf: f64 = NEG_INFINITY;
1225         let zero: f64 = 0.0f64;
1226         let neg_zero: f64 = -0.0;
1227         assert_eq!(nan.classify(), Fp::Nan);
1228         assert_eq!(inf.classify(), Fp::Infinite);
1229         assert_eq!(neg_inf.classify(), Fp::Infinite);
1230         assert_eq!(zero.classify(), Fp::Zero);
1231         assert_eq!(neg_zero.classify(), Fp::Zero);
1232         assert_eq!(1e-307f64.classify(), Fp::Normal);
1233         assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1234     }
1235
1236     #[test]
1237     fn test_floor() {
1238         assert_approx_eq!(1.0f64.floor(), 1.0f64);
1239         assert_approx_eq!(1.3f64.floor(), 1.0f64);
1240         assert_approx_eq!(1.5f64.floor(), 1.0f64);
1241         assert_approx_eq!(1.7f64.floor(), 1.0f64);
1242         assert_approx_eq!(0.0f64.floor(), 0.0f64);
1243         assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1244         assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1245         assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1246         assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1247         assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1248     }
1249
1250     #[test]
1251     fn test_ceil() {
1252         assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1253         assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1254         assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1255         assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1256         assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1257         assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1258         assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1259         assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1260         assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1261         assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1262     }
1263
1264     #[test]
1265     fn test_round() {
1266         assert_approx_eq!(1.0f64.round(), 1.0f64);
1267         assert_approx_eq!(1.3f64.round(), 1.0f64);
1268         assert_approx_eq!(1.5f64.round(), 2.0f64);
1269         assert_approx_eq!(1.7f64.round(), 2.0f64);
1270         assert_approx_eq!(0.0f64.round(), 0.0f64);
1271         assert_approx_eq!((-0.0f64).round(), -0.0f64);
1272         assert_approx_eq!((-1.0f64).round(), -1.0f64);
1273         assert_approx_eq!((-1.3f64).round(), -1.0f64);
1274         assert_approx_eq!((-1.5f64).round(), -2.0f64);
1275         assert_approx_eq!((-1.7f64).round(), -2.0f64);
1276     }
1277
1278     #[test]
1279     fn test_trunc() {
1280         assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1281         assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1282         assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1283         assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1284         assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1285         assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1286         assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1287         assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1288         assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1289         assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1290     }
1291
1292     #[test]
1293     fn test_fract() {
1294         assert_approx_eq!(1.0f64.fract(), 0.0f64);
1295         assert_approx_eq!(1.3f64.fract(), 0.3f64);
1296         assert_approx_eq!(1.5f64.fract(), 0.5f64);
1297         assert_approx_eq!(1.7f64.fract(), 0.7f64);
1298         assert_approx_eq!(0.0f64.fract(), 0.0f64);
1299         assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1300         assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1301         assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1302         assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1303         assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1304     }
1305
1306     #[test]
1307     fn test_abs() {
1308         assert_eq!(INFINITY.abs(), INFINITY);
1309         assert_eq!(1f64.abs(), 1f64);
1310         assert_eq!(0f64.abs(), 0f64);
1311         assert_eq!((-0f64).abs(), 0f64);
1312         assert_eq!((-1f64).abs(), 1f64);
1313         assert_eq!(NEG_INFINITY.abs(), INFINITY);
1314         assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1315         assert!(NAN.abs().is_nan());
1316     }
1317
1318     #[test]
1319     fn test_signum() {
1320         assert_eq!(INFINITY.signum(), 1f64);
1321         assert_eq!(1f64.signum(), 1f64);
1322         assert_eq!(0f64.signum(), 1f64);
1323         assert_eq!((-0f64).signum(), -1f64);
1324         assert_eq!((-1f64).signum(), -1f64);
1325         assert_eq!(NEG_INFINITY.signum(), -1f64);
1326         assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1327         assert!(NAN.signum().is_nan());
1328     }
1329
1330     #[test]
1331     fn test_is_sign_positive() {
1332         assert!(INFINITY.is_sign_positive());
1333         assert!(1f64.is_sign_positive());
1334         assert!(0f64.is_sign_positive());
1335         assert!(!(-0f64).is_sign_positive());
1336         assert!(!(-1f64).is_sign_positive());
1337         assert!(!NEG_INFINITY.is_sign_positive());
1338         assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1339         assert!(NAN.is_sign_positive());
1340         assert!(!(-NAN).is_sign_positive());
1341     }
1342
1343     #[test]
1344     fn test_is_sign_negative() {
1345         assert!(!INFINITY.is_sign_negative());
1346         assert!(!1f64.is_sign_negative());
1347         assert!(!0f64.is_sign_negative());
1348         assert!((-0f64).is_sign_negative());
1349         assert!((-1f64).is_sign_negative());
1350         assert!(NEG_INFINITY.is_sign_negative());
1351         assert!((1f64/NEG_INFINITY).is_sign_negative());
1352         assert!(!NAN.is_sign_negative());
1353         assert!((-NAN).is_sign_negative());
1354     }
1355
1356     #[test]
1357     fn test_mul_add() {
1358         let nan: f64 = NAN;
1359         let inf: f64 = INFINITY;
1360         let neg_inf: f64 = NEG_INFINITY;
1361         assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1362         assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1363         assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1364         assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1365         assert!(nan.mul_add(7.8, 9.0).is_nan());
1366         assert_eq!(inf.mul_add(7.8, 9.0), inf);
1367         assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1368         assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1369         assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1370     }
1371
1372     #[test]
1373     fn test_recip() {
1374         let nan: f64 = NAN;
1375         let inf: f64 = INFINITY;
1376         let neg_inf: f64 = NEG_INFINITY;
1377         assert_eq!(1.0f64.recip(), 1.0);
1378         assert_eq!(2.0f64.recip(), 0.5);
1379         assert_eq!((-0.4f64).recip(), -2.5);
1380         assert_eq!(0.0f64.recip(), inf);
1381         assert!(nan.recip().is_nan());
1382         assert_eq!(inf.recip(), 0.0);
1383         assert_eq!(neg_inf.recip(), 0.0);
1384     }
1385
1386     #[test]
1387     fn test_powi() {
1388         let nan: f64 = NAN;
1389         let inf: f64 = INFINITY;
1390         let neg_inf: f64 = NEG_INFINITY;
1391         assert_eq!(1.0f64.powi(1), 1.0);
1392         assert_approx_eq!((-3.1f64).powi(2), 9.61);
1393         assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1394         assert_eq!(8.3f64.powi(0), 1.0);
1395         assert!(nan.powi(2).is_nan());
1396         assert_eq!(inf.powi(3), inf);
1397         assert_eq!(neg_inf.powi(2), inf);
1398     }
1399
1400     #[test]
1401     fn test_powf() {
1402         let nan: f64 = NAN;
1403         let inf: f64 = INFINITY;
1404         let neg_inf: f64 = NEG_INFINITY;
1405         assert_eq!(1.0f64.powf(1.0), 1.0);
1406         assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1407         assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1408         assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1409         assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1410         assert_eq!(8.3f64.powf(0.0), 1.0);
1411         assert!(nan.powf(2.0).is_nan());
1412         assert_eq!(inf.powf(2.0), inf);
1413         assert_eq!(neg_inf.powf(3.0), neg_inf);
1414     }
1415
1416     #[test]
1417     fn test_sqrt_domain() {
1418         assert!(NAN.sqrt().is_nan());
1419         assert!(NEG_INFINITY.sqrt().is_nan());
1420         assert!((-1.0f64).sqrt().is_nan());
1421         assert_eq!((-0.0f64).sqrt(), -0.0);
1422         assert_eq!(0.0f64.sqrt(), 0.0);
1423         assert_eq!(1.0f64.sqrt(), 1.0);
1424         assert_eq!(INFINITY.sqrt(), INFINITY);
1425     }
1426
1427     #[test]
1428     fn test_exp() {
1429         assert_eq!(1.0, 0.0f64.exp());
1430         assert_approx_eq!(2.718282, 1.0f64.exp());
1431         assert_approx_eq!(148.413159, 5.0f64.exp());
1432
1433         let inf: f64 = INFINITY;
1434         let neg_inf: f64 = NEG_INFINITY;
1435         let nan: f64 = NAN;
1436         assert_eq!(inf, inf.exp());
1437         assert_eq!(0.0, neg_inf.exp());
1438         assert!(nan.exp().is_nan());
1439     }
1440
1441     #[test]
1442     fn test_exp2() {
1443         assert_eq!(32.0, 5.0f64.exp2());
1444         assert_eq!(1.0, 0.0f64.exp2());
1445
1446         let inf: f64 = INFINITY;
1447         let neg_inf: f64 = NEG_INFINITY;
1448         let nan: f64 = NAN;
1449         assert_eq!(inf, inf.exp2());
1450         assert_eq!(0.0, neg_inf.exp2());
1451         assert!(nan.exp2().is_nan());
1452     }
1453
1454     #[test]
1455     fn test_ln() {
1456         let nan: f64 = NAN;
1457         let inf: f64 = INFINITY;
1458         let neg_inf: f64 = NEG_INFINITY;
1459         assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1460         assert!(nan.ln().is_nan());
1461         assert_eq!(inf.ln(), inf);
1462         assert!(neg_inf.ln().is_nan());
1463         assert!((-2.3f64).ln().is_nan());
1464         assert_eq!((-0.0f64).ln(), neg_inf);
1465         assert_eq!(0.0f64.ln(), neg_inf);
1466         assert_approx_eq!(4.0f64.ln(), 1.386294);
1467     }
1468
1469     #[test]
1470     fn test_log() {
1471         let nan: f64 = NAN;
1472         let inf: f64 = INFINITY;
1473         let neg_inf: f64 = NEG_INFINITY;
1474         assert_eq!(10.0f64.log(10.0), 1.0);
1475         assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1476         assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1477         assert!(1.0f64.log(1.0).is_nan());
1478         assert!(1.0f64.log(-13.9).is_nan());
1479         assert!(nan.log(2.3).is_nan());
1480         assert_eq!(inf.log(10.0), inf);
1481         assert!(neg_inf.log(8.8).is_nan());
1482         assert!((-2.3f64).log(0.1).is_nan());
1483         assert_eq!((-0.0f64).log(2.0), neg_inf);
1484         assert_eq!(0.0f64.log(7.0), neg_inf);
1485     }
1486
1487     #[test]
1488     fn test_log2() {
1489         let nan: f64 = NAN;
1490         let inf: f64 = INFINITY;
1491         let neg_inf: f64 = NEG_INFINITY;
1492         assert_approx_eq!(10.0f64.log2(), 3.321928);
1493         assert_approx_eq!(2.3f64.log2(), 1.201634);
1494         assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1495         assert!(nan.log2().is_nan());
1496         assert_eq!(inf.log2(), inf);
1497         assert!(neg_inf.log2().is_nan());
1498         assert!((-2.3f64).log2().is_nan());
1499         assert_eq!((-0.0f64).log2(), neg_inf);
1500         assert_eq!(0.0f64.log2(), neg_inf);
1501     }
1502
1503     #[test]
1504     fn test_log10() {
1505         let nan: f64 = NAN;
1506         let inf: f64 = INFINITY;
1507         let neg_inf: f64 = NEG_INFINITY;
1508         assert_eq!(10.0f64.log10(), 1.0);
1509         assert_approx_eq!(2.3f64.log10(), 0.361728);
1510         assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1511         assert_eq!(1.0f64.log10(), 0.0);
1512         assert!(nan.log10().is_nan());
1513         assert_eq!(inf.log10(), inf);
1514         assert!(neg_inf.log10().is_nan());
1515         assert!((-2.3f64).log10().is_nan());
1516         assert_eq!((-0.0f64).log10(), neg_inf);
1517         assert_eq!(0.0f64.log10(), neg_inf);
1518     }
1519
1520     #[test]
1521     fn test_to_degrees() {
1522         let pi: f64 = consts::PI;
1523         let nan: f64 = NAN;
1524         let inf: f64 = INFINITY;
1525         let neg_inf: f64 = NEG_INFINITY;
1526         assert_eq!(0.0f64.to_degrees(), 0.0);
1527         assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1528         assert_eq!(pi.to_degrees(), 180.0);
1529         assert!(nan.to_degrees().is_nan());
1530         assert_eq!(inf.to_degrees(), inf);
1531         assert_eq!(neg_inf.to_degrees(), neg_inf);
1532     }
1533
1534     #[test]
1535     fn test_to_radians() {
1536         let pi: f64 = consts::PI;
1537         let nan: f64 = NAN;
1538         let inf: f64 = INFINITY;
1539         let neg_inf: f64 = NEG_INFINITY;
1540         assert_eq!(0.0f64.to_radians(), 0.0);
1541         assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1542         assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1543         assert_eq!(180.0f64.to_radians(), pi);
1544         assert!(nan.to_radians().is_nan());
1545         assert_eq!(inf.to_radians(), inf);
1546         assert_eq!(neg_inf.to_radians(), neg_inf);
1547     }
1548
1549     #[test]
1550     fn test_asinh() {
1551         assert_eq!(0.0f64.asinh(), 0.0f64);
1552         assert_eq!((-0.0f64).asinh(), -0.0f64);
1553
1554         let inf: f64 = INFINITY;
1555         let neg_inf: f64 = NEG_INFINITY;
1556         let nan: f64 = NAN;
1557         assert_eq!(inf.asinh(), inf);
1558         assert_eq!(neg_inf.asinh(), neg_inf);
1559         assert!(nan.asinh().is_nan());
1560         assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1561         assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1562     }
1563
1564     #[test]
1565     fn test_acosh() {
1566         assert_eq!(1.0f64.acosh(), 0.0f64);
1567         assert!(0.999f64.acosh().is_nan());
1568
1569         let inf: f64 = INFINITY;
1570         let neg_inf: f64 = NEG_INFINITY;
1571         let nan: f64 = NAN;
1572         assert_eq!(inf.acosh(), inf);
1573         assert!(neg_inf.acosh().is_nan());
1574         assert!(nan.acosh().is_nan());
1575         assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1576         assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1577     }
1578
1579     #[test]
1580     fn test_atanh() {
1581         assert_eq!(0.0f64.atanh(), 0.0f64);
1582         assert_eq!((-0.0f64).atanh(), -0.0f64);
1583
1584         let inf: f64 = INFINITY;
1585         let neg_inf: f64 = NEG_INFINITY;
1586         let nan: f64 = NAN;
1587         assert_eq!(1.0f64.atanh(), inf);
1588         assert_eq!((-1.0f64).atanh(), neg_inf);
1589         assert!(2f64.atanh().atanh().is_nan());
1590         assert!((-2f64).atanh().atanh().is_nan());
1591         assert!(inf.atanh().is_nan());
1592         assert!(neg_inf.atanh().is_nan());
1593         assert!(nan.atanh().is_nan());
1594         assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1595         assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1596     }
1597
1598     #[test]
1599     fn test_real_consts() {
1600         use super::consts;
1601         let pi: f64 = consts::PI;
1602         let frac_pi_2: f64 = consts::FRAC_PI_2;
1603         let frac_pi_3: f64 = consts::FRAC_PI_3;
1604         let frac_pi_4: f64 = consts::FRAC_PI_4;
1605         let frac_pi_6: f64 = consts::FRAC_PI_6;
1606         let frac_pi_8: f64 = consts::FRAC_PI_8;
1607         let frac_1_pi: f64 = consts::FRAC_1_PI;
1608         let frac_2_pi: f64 = consts::FRAC_2_PI;
1609         let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1610         let sqrt2: f64 = consts::SQRT_2;
1611         let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1612         let e: f64 = consts::E;
1613         let log2_e: f64 = consts::LOG2_E;
1614         let log10_e: f64 = consts::LOG10_E;
1615         let ln_2: f64 = consts::LN_2;
1616         let ln_10: f64 = consts::LN_10;
1617
1618         assert_approx_eq!(frac_pi_2, pi / 2f64);
1619         assert_approx_eq!(frac_pi_3, pi / 3f64);
1620         assert_approx_eq!(frac_pi_4, pi / 4f64);
1621         assert_approx_eq!(frac_pi_6, pi / 6f64);
1622         assert_approx_eq!(frac_pi_8, pi / 8f64);
1623         assert_approx_eq!(frac_1_pi, 1f64 / pi);
1624         assert_approx_eq!(frac_2_pi, 2f64 / pi);
1625         assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1626         assert_approx_eq!(sqrt2, 2f64.sqrt());
1627         assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1628         assert_approx_eq!(log2_e, e.log2());
1629         assert_approx_eq!(log10_e, e.log10());
1630         assert_approx_eq!(ln_2, 2f64.ln());
1631         assert_approx_eq!(ln_10, 10f64.ln());
1632     }
1633
1634     #[test]
1635     fn test_float_bits_conv() {
1636         assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1637         assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1638         assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1639         assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1640         assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1641         assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1642         assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1643         assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1644     }
1645 }