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