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