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