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