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