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