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