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