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