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