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