std/num/
f32.rs

1//! Constants for the `f32` single-precision floating point type.
2//!
3//! *[See also the `f32` primitive type](primitive@f32).*
4//!
5//! Mathematically significant numbers are provided in the `consts` sub-module.
6//!
7//! For the constants defined directly in this module
8//! (as distinct from those defined in the `consts` sub-module),
9//! new code should instead use the associated constants
10//! defined directly on the `f32` type.
11
12#![stable(feature = "rust1", since = "1.0.0")]
13#![allow(missing_docs)]
14
15#[stable(feature = "rust1", since = "1.0.0")]
16#[allow(deprecated, deprecated_in_future)]
17pub use core::f32::{
18    DIGITS, EPSILON, INFINITY, MANTISSA_DIGITS, MAX, MAX_10_EXP, MAX_EXP, MIN, MIN_10_EXP, MIN_EXP,
19    MIN_POSITIVE, NAN, NEG_INFINITY, RADIX, consts,
20};
21
22#[cfg(not(test))]
23use crate::intrinsics;
24#[cfg(not(test))]
25use crate::sys::cmath;
26
27#[cfg(not(test))]
28impl f32 {
29    /// Returns the largest integer less than or equal to `self`.
30    ///
31    /// This function always returns the precise result.
32    ///
33    /// # Examples
34    ///
35    /// ```
36    /// let f = 3.7_f32;
37    /// let g = 3.0_f32;
38    /// let h = -3.7_f32;
39    ///
40    /// assert_eq!(f.floor(), 3.0);
41    /// assert_eq!(g.floor(), 3.0);
42    /// assert_eq!(h.floor(), -4.0);
43    /// ```
44    #[rustc_allow_incoherent_impl]
45    #[must_use = "method returns a new number and does not mutate the original value"]
46    #[stable(feature = "rust1", since = "1.0.0")]
47    #[rustc_const_stable(feature = "const_float_round_methods", since = "1.90.0")]
48    #[inline]
49    pub const fn floor(self) -> f32 {
50        core::f32::math::floor(self)
51    }
52
53    /// Returns the smallest integer greater than or equal to `self`.
54    ///
55    /// This function always returns the precise result.
56    ///
57    /// # Examples
58    ///
59    /// ```
60    /// let f = 3.01_f32;
61    /// let g = 4.0_f32;
62    ///
63    /// assert_eq!(f.ceil(), 4.0);
64    /// assert_eq!(g.ceil(), 4.0);
65    /// ```
66    #[doc(alias = "ceiling")]
67    #[rustc_allow_incoherent_impl]
68    #[must_use = "method returns a new number and does not mutate the original value"]
69    #[stable(feature = "rust1", since = "1.0.0")]
70    #[rustc_const_stable(feature = "const_float_round_methods", since = "1.90.0")]
71    #[inline]
72    pub const fn ceil(self) -> f32 {
73        core::f32::math::ceil(self)
74    }
75
76    /// Returns the nearest integer to `self`. If a value is half-way between two
77    /// integers, round away from `0.0`.
78    ///
79    /// This function always returns the precise result.
80    ///
81    /// # Examples
82    ///
83    /// ```
84    /// let f = 3.3_f32;
85    /// let g = -3.3_f32;
86    /// let h = -3.7_f32;
87    /// let i = 3.5_f32;
88    /// let j = 4.5_f32;
89    ///
90    /// assert_eq!(f.round(), 3.0);
91    /// assert_eq!(g.round(), -3.0);
92    /// assert_eq!(h.round(), -4.0);
93    /// assert_eq!(i.round(), 4.0);
94    /// assert_eq!(j.round(), 5.0);
95    /// ```
96    #[rustc_allow_incoherent_impl]
97    #[must_use = "method returns a new number and does not mutate the original value"]
98    #[stable(feature = "rust1", since = "1.0.0")]
99    #[rustc_const_stable(feature = "const_float_round_methods", since = "1.90.0")]
100    #[inline]
101    pub const fn round(self) -> f32 {
102        core::f32::math::round(self)
103    }
104
105    /// Returns the nearest integer to a number. Rounds half-way cases to the number
106    /// with an even least significant digit.
107    ///
108    /// This function always returns the precise result.
109    ///
110    /// # Examples
111    ///
112    /// ```
113    /// let f = 3.3_f32;
114    /// let g = -3.3_f32;
115    /// let h = 3.5_f32;
116    /// let i = 4.5_f32;
117    ///
118    /// assert_eq!(f.round_ties_even(), 3.0);
119    /// assert_eq!(g.round_ties_even(), -3.0);
120    /// assert_eq!(h.round_ties_even(), 4.0);
121    /// assert_eq!(i.round_ties_even(), 4.0);
122    /// ```
123    #[rustc_allow_incoherent_impl]
124    #[must_use = "method returns a new number and does not mutate the original value"]
125    #[stable(feature = "round_ties_even", since = "1.77.0")]
126    #[rustc_const_stable(feature = "const_float_round_methods", since = "1.90.0")]
127    #[inline]
128    pub const fn round_ties_even(self) -> f32 {
129        core::f32::math::round_ties_even(self)
130    }
131
132    /// Returns the integer part of `self`.
133    /// This means that non-integer numbers are always truncated towards zero.
134    ///
135    /// This function always returns the precise result.
136    ///
137    /// # Examples
138    ///
139    /// ```
140    /// let f = 3.7_f32;
141    /// let g = 3.0_f32;
142    /// let h = -3.7_f32;
143    ///
144    /// assert_eq!(f.trunc(), 3.0);
145    /// assert_eq!(g.trunc(), 3.0);
146    /// assert_eq!(h.trunc(), -3.0);
147    /// ```
148    #[doc(alias = "truncate")]
149    #[rustc_allow_incoherent_impl]
150    #[must_use = "method returns a new number and does not mutate the original value"]
151    #[stable(feature = "rust1", since = "1.0.0")]
152    #[rustc_const_stable(feature = "const_float_round_methods", since = "1.90.0")]
153    #[inline]
154    pub const fn trunc(self) -> f32 {
155        core::f32::math::trunc(self)
156    }
157
158    /// Returns the fractional part of `self`.
159    ///
160    /// This function always returns the precise result.
161    ///
162    /// # Examples
163    ///
164    /// ```
165    /// let x = 3.6_f32;
166    /// let y = -3.6_f32;
167    /// let abs_difference_x = (x.fract() - 0.6).abs();
168    /// let abs_difference_y = (y.fract() - (-0.6)).abs();
169    ///
170    /// assert!(abs_difference_x <= f32::EPSILON);
171    /// assert!(abs_difference_y <= f32::EPSILON);
172    /// ```
173    #[rustc_allow_incoherent_impl]
174    #[must_use = "method returns a new number and does not mutate the original value"]
175    #[stable(feature = "rust1", since = "1.0.0")]
176    #[rustc_const_stable(feature = "const_float_round_methods", since = "1.90.0")]
177    #[inline]
178    pub const fn fract(self) -> f32 {
179        core::f32::math::fract(self)
180    }
181
182    /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
183    /// error, yielding a more accurate result than an unfused multiply-add.
184    ///
185    /// Using `mul_add` *may* be more performant than an unfused multiply-add if
186    /// the target architecture has a dedicated `fma` CPU instruction. However,
187    /// this is not always true, and will be heavily dependant on designing
188    /// algorithms with specific target hardware in mind.
189    ///
190    /// # Precision
191    ///
192    /// The result of this operation is guaranteed to be the rounded
193    /// infinite-precision result. It is specified by IEEE 754 as
194    /// `fusedMultiplyAdd` and guaranteed not to change.
195    ///
196    /// # Examples
197    ///
198    /// ```
199    /// let m = 10.0_f32;
200    /// let x = 4.0_f32;
201    /// let b = 60.0_f32;
202    ///
203    /// assert_eq!(m.mul_add(x, b), 100.0);
204    /// assert_eq!(m * x + b, 100.0);
205    ///
206    /// let one_plus_eps = 1.0_f32 + f32::EPSILON;
207    /// let one_minus_eps = 1.0_f32 - f32::EPSILON;
208    /// let minus_one = -1.0_f32;
209    ///
210    /// // The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.
211    /// assert_eq!(one_plus_eps.mul_add(one_minus_eps, minus_one), -f32::EPSILON * f32::EPSILON);
212    /// // Different rounding with the non-fused multiply and add.
213    /// assert_eq!(one_plus_eps * one_minus_eps + minus_one, 0.0);
214    /// ```
215    #[rustc_allow_incoherent_impl]
216    #[doc(alias = "fmaf", alias = "fusedMultiplyAdd")]
217    #[must_use = "method returns a new number and does not mutate the original value"]
218    #[stable(feature = "rust1", since = "1.0.0")]
219    #[inline]
220    #[rustc_const_unstable(feature = "const_mul_add", issue = "146724")]
221    pub const fn mul_add(self, a: f32, b: f32) -> f32 {
222        core::f32::math::mul_add(self, a, b)
223    }
224
225    /// Calculates Euclidean division, the matching method for `rem_euclid`.
226    ///
227    /// This computes the integer `n` such that
228    /// `self = n * rhs + self.rem_euclid(rhs)`.
229    /// In other words, the result is `self / rhs` rounded to the integer `n`
230    /// such that `self >= n * rhs`.
231    ///
232    /// # Precision
233    ///
234    /// The result of this operation is guaranteed to be the rounded
235    /// infinite-precision result.
236    ///
237    /// # Examples
238    ///
239    /// ```
240    /// let a: f32 = 7.0;
241    /// let b = 4.0;
242    /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
243    /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
244    /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
245    /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
246    /// ```
247    #[rustc_allow_incoherent_impl]
248    #[must_use = "method returns a new number and does not mutate the original value"]
249    #[inline]
250    #[stable(feature = "euclidean_division", since = "1.38.0")]
251    pub fn div_euclid(self, rhs: f32) -> f32 {
252        core::f32::math::div_euclid(self, rhs)
253    }
254
255    /// Calculates the least nonnegative remainder of `self (mod rhs)`.
256    ///
257    /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
258    /// most cases. However, due to a floating point round-off error it can
259    /// result in `r == rhs.abs()`, violating the mathematical definition, if
260    /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
261    /// This result is not an element of the function's codomain, but it is the
262    /// closest floating point number in the real numbers and thus fulfills the
263    /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
264    /// approximately.
265    ///
266    /// # Precision
267    ///
268    /// The result of this operation is guaranteed to be the rounded
269    /// infinite-precision result.
270    ///
271    /// # Examples
272    ///
273    /// ```
274    /// let a: f32 = 7.0;
275    /// let b = 4.0;
276    /// assert_eq!(a.rem_euclid(b), 3.0);
277    /// assert_eq!((-a).rem_euclid(b), 1.0);
278    /// assert_eq!(a.rem_euclid(-b), 3.0);
279    /// assert_eq!((-a).rem_euclid(-b), 1.0);
280    /// // limitation due to round-off error
281    /// assert!((-f32::EPSILON).rem_euclid(3.0) != 0.0);
282    /// ```
283    #[doc(alias = "modulo", alias = "mod")]
284    #[rustc_allow_incoherent_impl]
285    #[must_use = "method returns a new number and does not mutate the original value"]
286    #[inline]
287    #[stable(feature = "euclidean_division", since = "1.38.0")]
288    pub fn rem_euclid(self, rhs: f32) -> f32 {
289        core::f32::math::rem_euclid(self, rhs)
290    }
291
292    /// Raises a number to an integer power.
293    ///
294    /// Using this function is generally faster than using `powf`.
295    /// It might have a different sequence of rounding operations than `powf`,
296    /// so the results are not guaranteed to agree.
297    ///
298    /// # Unspecified precision
299    ///
300    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
301    /// can even differ within the same execution from one invocation to the next.
302    ///
303    /// # Examples
304    ///
305    /// ```
306    /// let x = 2.0_f32;
307    /// let abs_difference = (x.powi(2) - (x * x)).abs();
308    /// assert!(abs_difference <= 1e-5);
309    ///
310    /// assert_eq!(f32::powi(f32::NAN, 0), 1.0);
311    /// assert_eq!(f32::powi(0.0, 0), 1.0);
312    /// ```
313    #[rustc_allow_incoherent_impl]
314    #[must_use = "method returns a new number and does not mutate the original value"]
315    #[stable(feature = "rust1", since = "1.0.0")]
316    #[inline]
317    pub fn powi(self, n: i32) -> f32 {
318        core::f32::math::powi(self, n)
319    }
320
321    /// Raises a number to a floating point power.
322    ///
323    /// # Unspecified precision
324    ///
325    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
326    /// can even differ within the same execution from one invocation to the next.
327    ///
328    /// # Examples
329    ///
330    /// ```
331    /// let x = 2.0_f32;
332    /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
333    /// assert!(abs_difference <= 1e-5);
334    ///
335    /// assert_eq!(f32::powf(1.0, f32::NAN), 1.0);
336    /// assert_eq!(f32::powf(f32::NAN, 0.0), 1.0);
337    /// assert_eq!(f32::powf(0.0, 0.0), 1.0);
338    /// ```
339    #[rustc_allow_incoherent_impl]
340    #[must_use = "method returns a new number and does not mutate the original value"]
341    #[stable(feature = "rust1", since = "1.0.0")]
342    #[inline]
343    pub fn powf(self, n: f32) -> f32 {
344        intrinsics::powf32(self, n)
345    }
346
347    /// Returns the square root of a number.
348    ///
349    /// Returns NaN if `self` is a negative number other than `-0.0`.
350    ///
351    /// # Precision
352    ///
353    /// The result of this operation is guaranteed to be the rounded
354    /// infinite-precision result. It is specified by IEEE 754 as `squareRoot`
355    /// and guaranteed not to change.
356    ///
357    /// # Examples
358    ///
359    /// ```
360    /// let positive = 4.0_f32;
361    /// let negative = -4.0_f32;
362    /// let negative_zero = -0.0_f32;
363    ///
364    /// assert_eq!(positive.sqrt(), 2.0);
365    /// assert!(negative.sqrt().is_nan());
366    /// assert!(negative_zero.sqrt() == negative_zero);
367    /// ```
368    #[doc(alias = "squareRoot")]
369    #[rustc_allow_incoherent_impl]
370    #[must_use = "method returns a new number and does not mutate the original value"]
371    #[stable(feature = "rust1", since = "1.0.0")]
372    #[inline]
373    pub fn sqrt(self) -> f32 {
374        core::f32::math::sqrt(self)
375    }
376
377    /// Returns `e^(self)`, (the exponential function).
378    ///
379    /// # Unspecified precision
380    ///
381    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
382    /// can even differ within the same execution from one invocation to the next.
383    ///
384    /// # Examples
385    ///
386    /// ```
387    /// let one = 1.0f32;
388    /// // e^1
389    /// let e = one.exp();
390    ///
391    /// // ln(e) - 1 == 0
392    /// let abs_difference = (e.ln() - 1.0).abs();
393    ///
394    /// assert!(abs_difference <= 1e-6);
395    /// ```
396    #[rustc_allow_incoherent_impl]
397    #[must_use = "method returns a new number and does not mutate the original value"]
398    #[stable(feature = "rust1", since = "1.0.0")]
399    #[inline]
400    pub fn exp(self) -> f32 {
401        intrinsics::expf32(self)
402    }
403
404    /// Returns `2^(self)`.
405    ///
406    /// # Unspecified precision
407    ///
408    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
409    /// can even differ within the same execution from one invocation to the next.
410    ///
411    /// # Examples
412    ///
413    /// ```
414    /// let f = 2.0f32;
415    ///
416    /// // 2^2 - 4 == 0
417    /// let abs_difference = (f.exp2() - 4.0).abs();
418    ///
419    /// assert!(abs_difference <= 1e-5);
420    /// ```
421    #[rustc_allow_incoherent_impl]
422    #[must_use = "method returns a new number and does not mutate the original value"]
423    #[stable(feature = "rust1", since = "1.0.0")]
424    #[inline]
425    pub fn exp2(self) -> f32 {
426        intrinsics::exp2f32(self)
427    }
428
429    /// Returns the natural logarithm of the number.
430    ///
431    /// This returns NaN when the number is negative, and negative infinity when number is zero.
432    ///
433    /// # Unspecified precision
434    ///
435    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
436    /// can even differ within the same execution from one invocation to the next.
437    ///
438    /// # Examples
439    ///
440    /// ```
441    /// let one = 1.0f32;
442    /// // e^1
443    /// let e = one.exp();
444    ///
445    /// // ln(e) - 1 == 0
446    /// let abs_difference = (e.ln() - 1.0).abs();
447    ///
448    /// assert!(abs_difference <= 1e-6);
449    /// ```
450    ///
451    /// Non-positive values:
452    /// ```
453    /// assert_eq!(0_f32.ln(), f32::NEG_INFINITY);
454    /// assert!((-42_f32).ln().is_nan());
455    /// ```
456    #[rustc_allow_incoherent_impl]
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        intrinsics::logf32(self)
462    }
463
464    /// Returns the logarithm of the number with respect to an arbitrary base.
465    ///
466    /// This returns NaN when the number is negative, and negative infinity when number is zero.
467    ///
468    /// The result might not be correctly rounded owing to implementation details;
469    /// `self.log2()` can produce more accurate results for base 2, and
470    /// `self.log10()` can produce more accurate results for base 10.
471    ///
472    /// # Unspecified precision
473    ///
474    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
475    /// can even differ within the same execution from one invocation to the next.
476    ///
477    /// # Examples
478    ///
479    /// ```
480    /// let five = 5.0f32;
481    ///
482    /// // log5(5) - 1 == 0
483    /// let abs_difference = (five.log(5.0) - 1.0).abs();
484    ///
485    /// assert!(abs_difference <= 1e-6);
486    /// ```
487    ///
488    /// Non-positive values:
489    /// ```
490    /// assert_eq!(0_f32.log(10.0), f32::NEG_INFINITY);
491    /// assert!((-42_f32).log(10.0).is_nan());
492    /// ```
493    #[rustc_allow_incoherent_impl]
494    #[must_use = "method returns a new number and does not mutate the original value"]
495    #[stable(feature = "rust1", since = "1.0.0")]
496    #[inline]
497    pub fn log(self, base: f32) -> f32 {
498        self.ln() / base.ln()
499    }
500
501    /// Returns the base 2 logarithm of the number.
502    ///
503    /// This returns NaN when the number is negative, and negative infinity when number is zero.
504    ///
505    /// # Unspecified precision
506    ///
507    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
508    /// can even differ within the same execution from one invocation to the next.
509    ///
510    /// # Examples
511    ///
512    /// ```
513    /// let two = 2.0f32;
514    ///
515    /// // log2(2) - 1 == 0
516    /// let abs_difference = (two.log2() - 1.0).abs();
517    ///
518    /// assert!(abs_difference <= 1e-6);
519    /// ```
520    ///
521    /// Non-positive values:
522    /// ```
523    /// assert_eq!(0_f32.log2(), f32::NEG_INFINITY);
524    /// assert!((-42_f32).log2().is_nan());
525    /// ```
526    #[rustc_allow_incoherent_impl]
527    #[must_use = "method returns a new number and does not mutate the original value"]
528    #[stable(feature = "rust1", since = "1.0.0")]
529    #[inline]
530    pub fn log2(self) -> f32 {
531        intrinsics::log2f32(self)
532    }
533
534    /// Returns the base 10 logarithm of the number.
535    ///
536    /// This returns NaN when the number is negative, and negative infinity when number is zero.
537    ///
538    /// # Unspecified precision
539    ///
540    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
541    /// can even differ within the same execution from one invocation to the next.
542    ///
543    /// # Examples
544    ///
545    /// ```
546    /// let ten = 10.0f32;
547    ///
548    /// // log10(10) - 1 == 0
549    /// let abs_difference = (ten.log10() - 1.0).abs();
550    ///
551    /// assert!(abs_difference <= 1e-6);
552    /// ```
553    ///
554    /// Non-positive values:
555    /// ```
556    /// assert_eq!(0_f32.log10(), f32::NEG_INFINITY);
557    /// assert!((-42_f32).log10().is_nan());
558    /// ```
559    #[rustc_allow_incoherent_impl]
560    #[must_use = "method returns a new number and does not mutate the original value"]
561    #[stable(feature = "rust1", since = "1.0.0")]
562    #[inline]
563    pub fn log10(self) -> f32 {
564        intrinsics::log10f32(self)
565    }
566
567    /// The positive difference of two numbers.
568    ///
569    /// * If `self <= other`: `0.0`
570    /// * Else: `self - other`
571    ///
572    /// # Unspecified precision
573    ///
574    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
575    /// can even differ within the same execution from one invocation to the next.
576    /// This function currently corresponds to the `fdimf` from libc on Unix
577    /// and Windows. Note that this might change in the future.
578    ///
579    /// # Examples
580    ///
581    /// ```
582    /// let x = 3.0f32;
583    /// let y = -3.0f32;
584    ///
585    /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
586    /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
587    ///
588    /// assert!(abs_difference_x <= 1e-6);
589    /// assert!(abs_difference_y <= 1e-6);
590    /// ```
591    #[rustc_allow_incoherent_impl]
592    #[must_use = "method returns a new number and does not mutate the original value"]
593    #[stable(feature = "rust1", since = "1.0.0")]
594    #[inline]
595    #[deprecated(
596        since = "1.10.0",
597        note = "you probably meant `(self - other).abs()`: \
598                this operation is `(self - other).max(0.0)` \
599                except that `abs_sub` also propagates NaNs (also \
600                known as `fdimf` in C). If you truly need the positive \
601                difference, consider using that expression or the C function \
602                `fdimf`, depending on how you wish to handle NaN (please consider \
603                filing an issue describing your use-case too)."
604    )]
605    pub fn abs_sub(self, other: f32) -> f32 {
606        #[allow(deprecated)]
607        core::f32::math::abs_sub(self, other)
608    }
609
610    /// Returns the cube root of a number.
611    ///
612    /// # Unspecified precision
613    ///
614    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
615    /// can even differ within the same execution from one invocation to the next.
616    /// This function currently corresponds to the `cbrtf` from libc on Unix
617    /// and Windows. Note that this might change in the future.
618    ///
619    /// # Examples
620    ///
621    /// ```
622    /// let x = 8.0f32;
623    ///
624    /// // x^(1/3) - 2 == 0
625    /// let abs_difference = (x.cbrt() - 2.0).abs();
626    ///
627    /// assert!(abs_difference <= 1e-6);
628    /// ```
629    #[rustc_allow_incoherent_impl]
630    #[must_use = "method returns a new number and does not mutate the original value"]
631    #[stable(feature = "rust1", since = "1.0.0")]
632    #[inline]
633    pub fn cbrt(self) -> f32 {
634        core::f32::math::cbrt(self)
635    }
636
637    /// Compute the distance between the origin and a point (`x`, `y`) on the
638    /// Euclidean plane. Equivalently, compute the length of the hypotenuse of a
639    /// right-angle triangle with other sides having length `x.abs()` and
640    /// `y.abs()`.
641    ///
642    /// # Unspecified precision
643    ///
644    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
645    /// can even differ within the same execution from one invocation to the next.
646    /// This function currently corresponds to the `hypotf` from libc on Unix
647    /// and Windows. Note that this might change in the future.
648    ///
649    /// # Examples
650    ///
651    /// ```
652    /// let x = 2.0f32;
653    /// let y = 3.0f32;
654    ///
655    /// // sqrt(x^2 + y^2)
656    /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
657    ///
658    /// assert!(abs_difference <= 1e-5);
659    /// ```
660    #[rustc_allow_incoherent_impl]
661    #[must_use = "method returns a new number and does not mutate the original value"]
662    #[stable(feature = "rust1", since = "1.0.0")]
663    #[inline]
664    pub fn hypot(self, other: f32) -> f32 {
665        cmath::hypotf(self, other)
666    }
667
668    /// Computes the sine of a number (in radians).
669    ///
670    /// # Unspecified precision
671    ///
672    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
673    /// can even differ within the same execution from one invocation to the next.
674    ///
675    /// # Examples
676    ///
677    /// ```
678    /// let x = std::f32::consts::FRAC_PI_2;
679    ///
680    /// let abs_difference = (x.sin() - 1.0).abs();
681    ///
682    /// assert!(abs_difference <= 1e-6);
683    /// ```
684    #[rustc_allow_incoherent_impl]
685    #[must_use = "method returns a new number and does not mutate the original value"]
686    #[stable(feature = "rust1", since = "1.0.0")]
687    #[inline]
688    pub fn sin(self) -> f32 {
689        intrinsics::sinf32(self)
690    }
691
692    /// Computes the cosine of a number (in radians).
693    ///
694    /// # Unspecified precision
695    ///
696    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
697    /// can even differ within the same execution from one invocation to the next.
698    ///
699    /// # Examples
700    ///
701    /// ```
702    /// let x = 2.0 * std::f32::consts::PI;
703    ///
704    /// let abs_difference = (x.cos() - 1.0).abs();
705    ///
706    /// assert!(abs_difference <= 1e-6);
707    /// ```
708    #[rustc_allow_incoherent_impl]
709    #[must_use = "method returns a new number and does not mutate the original value"]
710    #[stable(feature = "rust1", since = "1.0.0")]
711    #[inline]
712    pub fn cos(self) -> f32 {
713        intrinsics::cosf32(self)
714    }
715
716    /// Computes the tangent of a number (in radians).
717    ///
718    /// # Unspecified precision
719    ///
720    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
721    /// can even differ within the same execution from one invocation to the next.
722    /// This function currently corresponds to the `tanf` from libc on Unix and
723    /// Windows. Note that this might change in the future.
724    ///
725    /// # Examples
726    ///
727    /// ```
728    /// let x = std::f32::consts::FRAC_PI_4;
729    /// let abs_difference = (x.tan() - 1.0).abs();
730    ///
731    /// assert!(abs_difference <= 1e-6);
732    /// ```
733    #[rustc_allow_incoherent_impl]
734    #[must_use = "method returns a new number and does not mutate the original value"]
735    #[stable(feature = "rust1", since = "1.0.0")]
736    #[inline]
737    pub fn tan(self) -> f32 {
738        cmath::tanf(self)
739    }
740
741    /// Computes the arcsine of a number. Return value is in radians in
742    /// the range [-pi/2, pi/2] or NaN if the number is outside the range
743    /// [-1, 1].
744    ///
745    /// # Unspecified precision
746    ///
747    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
748    /// can even differ within the same execution from one invocation to the next.
749    /// This function currently corresponds to the `asinf` from libc on Unix
750    /// and Windows. Note that this might change in the future.
751    ///
752    /// # Examples
753    ///
754    /// ```
755    /// let f = std::f32::consts::FRAC_PI_4;
756    ///
757    /// // asin(sin(pi/2))
758    /// let abs_difference = (f.sin().asin() - f).abs();
759    ///
760    /// assert!(abs_difference <= 1e-6);
761    /// ```
762    #[doc(alias = "arcsin")]
763    #[rustc_allow_incoherent_impl]
764    #[must_use = "method returns a new number and does not mutate the original value"]
765    #[stable(feature = "rust1", since = "1.0.0")]
766    #[inline]
767    pub fn asin(self) -> f32 {
768        cmath::asinf(self)
769    }
770
771    /// Computes the arccosine of a number. Return value is in radians in
772    /// the range [0, pi] or NaN if the number is outside the range
773    /// [-1, 1].
774    ///
775    /// # Unspecified precision
776    ///
777    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
778    /// can even differ within the same execution from one invocation to the next.
779    /// This function currently corresponds to the `acosf` from libc on Unix
780    /// and Windows. Note that this might change in the future.
781    ///
782    /// # Examples
783    ///
784    /// ```
785    /// let f = std::f32::consts::FRAC_PI_4;
786    ///
787    /// // acos(cos(pi/4))
788    /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
789    ///
790    /// assert!(abs_difference <= 1e-6);
791    /// ```
792    #[doc(alias = "arccos")]
793    #[rustc_allow_incoherent_impl]
794    #[must_use = "method returns a new number and does not mutate the original value"]
795    #[stable(feature = "rust1", since = "1.0.0")]
796    #[inline]
797    pub fn acos(self) -> f32 {
798        cmath::acosf(self)
799    }
800
801    /// Computes the arctangent of a number. Return value is in radians in the
802    /// range [-pi/2, pi/2];
803    ///
804    /// # Unspecified precision
805    ///
806    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
807    /// can even differ within the same execution from one invocation to the next.
808    /// This function currently corresponds to the `atanf` from libc on Unix
809    /// and Windows. Note that this might change in the future.
810    ///
811    /// # Examples
812    ///
813    /// ```
814    /// let f = 1.0f32;
815    ///
816    /// // atan(tan(1))
817    /// let abs_difference = (f.tan().atan() - 1.0).abs();
818    ///
819    /// assert!(abs_difference <= 1e-6);
820    /// ```
821    #[doc(alias = "arctan")]
822    #[rustc_allow_incoherent_impl]
823    #[must_use = "method returns a new number and does not mutate the original value"]
824    #[stable(feature = "rust1", since = "1.0.0")]
825    #[inline]
826    pub fn atan(self) -> f32 {
827        cmath::atanf(self)
828    }
829
830    /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
831    ///
832    ///  | `x`     | `y`     | Piecewise Definition | Range         |
833    ///  |---------|---------|----------------------|---------------|
834    ///  | `>= +0` | `>= +0` | `arctan(y/x)`        | `[+0, +pi/2]` |
835    ///  | `>= +0` | `<= -0` | `arctan(y/x)`        | `[-pi/2, -0]` |
836    ///  | `<= -0` | `>= +0` | `arctan(y/x) + pi`   | `[+pi/2, +pi]`|
837    ///  | `<= -0` | `<= -0` | `arctan(y/x) - pi`   | `[-pi, -pi/2]`|
838    ///
839    /// # Unspecified precision
840    ///
841    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
842    /// can even differ within the same execution from one invocation to the next.
843    /// This function currently corresponds to the `atan2f` from libc on Unix
844    /// and Windows. Note that this might change in the future.
845    ///
846    /// # Examples
847    ///
848    /// ```
849    /// // Positive angles measured counter-clockwise
850    /// // from positive x axis
851    /// // -pi/4 radians (45 deg clockwise)
852    /// let x1 = 3.0f32;
853    /// let y1 = -3.0f32;
854    ///
855    /// // 3pi/4 radians (135 deg counter-clockwise)
856    /// let x2 = -3.0f32;
857    /// let y2 = 3.0f32;
858    ///
859    /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
860    /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
861    ///
862    /// assert!(abs_difference_1 <= 1e-5);
863    /// assert!(abs_difference_2 <= 1e-5);
864    /// ```
865    #[rustc_allow_incoherent_impl]
866    #[must_use = "method returns a new number and does not mutate the original value"]
867    #[stable(feature = "rust1", since = "1.0.0")]
868    #[inline]
869    pub fn atan2(self, other: f32) -> f32 {
870        cmath::atan2f(self, other)
871    }
872
873    /// Simultaneously computes the sine and cosine of the number, `x`. Returns
874    /// `(sin(x), cos(x))`.
875    ///
876    /// # Unspecified precision
877    ///
878    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
879    /// can even differ within the same execution from one invocation to the next.
880    /// This function currently corresponds to the `(f32::sin(x),
881    /// f32::cos(x))`. Note that this might change in the future.
882    ///
883    /// # Examples
884    ///
885    /// ```
886    /// let x = std::f32::consts::FRAC_PI_4;
887    /// let f = x.sin_cos();
888    ///
889    /// let abs_difference_0 = (f.0 - x.sin()).abs();
890    /// let abs_difference_1 = (f.1 - x.cos()).abs();
891    ///
892    /// assert!(abs_difference_0 <= 1e-4);
893    /// assert!(abs_difference_1 <= 1e-4);
894    /// ```
895    #[doc(alias = "sincos")]
896    #[rustc_allow_incoherent_impl]
897    #[stable(feature = "rust1", since = "1.0.0")]
898    #[inline]
899    pub fn sin_cos(self) -> (f32, f32) {
900        (self.sin(), self.cos())
901    }
902
903    /// Returns `e^(self) - 1` in a way that is accurate even if the
904    /// number is close to zero.
905    ///
906    /// # Unspecified precision
907    ///
908    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
909    /// can even differ within the same execution from one invocation to the next.
910    /// This function currently corresponds to the `expm1f` from libc on Unix
911    /// and Windows. Note that this might change in the future.
912    ///
913    /// # Examples
914    ///
915    /// ```
916    /// let x = 1e-8_f32;
917    ///
918    /// // for very small x, e^x is approximately 1 + x + x^2 / 2
919    /// let approx = x + x * x / 2.0;
920    /// let abs_difference = (x.exp_m1() - approx).abs();
921    ///
922    /// assert!(abs_difference < 1e-10);
923    /// ```
924    #[rustc_allow_incoherent_impl]
925    #[must_use = "method returns a new number and does not mutate the original value"]
926    #[stable(feature = "rust1", since = "1.0.0")]
927    #[inline]
928    pub fn exp_m1(self) -> f32 {
929        cmath::expm1f(self)
930    }
931
932    /// Returns `ln(1+n)` (natural logarithm) more accurately than if
933    /// the operations were performed separately.
934    ///
935    /// This returns NaN when `n < -1.0`, and negative infinity when `n == -1.0`.
936    ///
937    /// # Unspecified precision
938    ///
939    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
940    /// can even differ within the same execution from one invocation to the next.
941    /// This function currently corresponds to the `log1pf` from libc on Unix
942    /// and Windows. Note that this might change in the future.
943    ///
944    /// # Examples
945    ///
946    /// ```
947    /// let x = 1e-8_f32;
948    ///
949    /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
950    /// let approx = x - x * x / 2.0;
951    /// let abs_difference = (x.ln_1p() - approx).abs();
952    ///
953    /// assert!(abs_difference < 1e-10);
954    /// ```
955    ///
956    /// Out-of-range values:
957    /// ```
958    /// assert_eq!((-1.0_f32).ln_1p(), f32::NEG_INFINITY);
959    /// assert!((-2.0_f32).ln_1p().is_nan());
960    /// ```
961    #[doc(alias = "log1p")]
962    #[rustc_allow_incoherent_impl]
963    #[must_use = "method returns a new number and does not mutate the original value"]
964    #[stable(feature = "rust1", since = "1.0.0")]
965    #[inline]
966    pub fn ln_1p(self) -> f32 {
967        cmath::log1pf(self)
968    }
969
970    /// Hyperbolic sine function.
971    ///
972    /// # Unspecified precision
973    ///
974    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
975    /// can even differ within the same execution from one invocation to the next.
976    /// This function currently corresponds to the `sinhf` from libc on Unix
977    /// and Windows. Note that this might change in the future.
978    ///
979    /// # Examples
980    ///
981    /// ```
982    /// let e = std::f32::consts::E;
983    /// let x = 1.0f32;
984    ///
985    /// let f = x.sinh();
986    /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
987    /// let g = ((e * e) - 1.0) / (2.0 * e);
988    /// let abs_difference = (f - g).abs();
989    ///
990    /// assert!(abs_difference <= 1e-6);
991    /// ```
992    #[rustc_allow_incoherent_impl]
993    #[must_use = "method returns a new number and does not mutate the original value"]
994    #[stable(feature = "rust1", since = "1.0.0")]
995    #[inline]
996    pub fn sinh(self) -> f32 {
997        cmath::sinhf(self)
998    }
999
1000    /// Hyperbolic cosine function.
1001    ///
1002    /// # Unspecified precision
1003    ///
1004    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
1005    /// can even differ within the same execution from one invocation to the next.
1006    /// This function currently corresponds to the `coshf` from libc on Unix
1007    /// and Windows. Note that this might change in the future.
1008    ///
1009    /// # Examples
1010    ///
1011    /// ```
1012    /// let e = std::f32::consts::E;
1013    /// let x = 1.0f32;
1014    /// let f = x.cosh();
1015    /// // Solving cosh() at 1 gives this result
1016    /// let g = ((e * e) + 1.0) / (2.0 * e);
1017    /// let abs_difference = (f - g).abs();
1018    ///
1019    /// // Same result
1020    /// assert!(abs_difference <= 1e-6);
1021    /// ```
1022    #[rustc_allow_incoherent_impl]
1023    #[must_use = "method returns a new number and does not mutate the original value"]
1024    #[stable(feature = "rust1", since = "1.0.0")]
1025    #[inline]
1026    pub fn cosh(self) -> f32 {
1027        cmath::coshf(self)
1028    }
1029
1030    /// Hyperbolic tangent function.
1031    ///
1032    /// # Unspecified precision
1033    ///
1034    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
1035    /// can even differ within the same execution from one invocation to the next.
1036    /// This function currently corresponds to the `tanhf` from libc on Unix
1037    /// and Windows. Note that this might change in the future.
1038    ///
1039    /// # Examples
1040    ///
1041    /// ```
1042    /// let e = std::f32::consts::E;
1043    /// let x = 1.0f32;
1044    ///
1045    /// let f = x.tanh();
1046    /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1047    /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
1048    /// let abs_difference = (f - g).abs();
1049    ///
1050    /// assert!(abs_difference <= 1e-6);
1051    /// ```
1052    #[rustc_allow_incoherent_impl]
1053    #[must_use = "method returns a new number and does not mutate the original value"]
1054    #[stable(feature = "rust1", since = "1.0.0")]
1055    #[inline]
1056    pub fn tanh(self) -> f32 {
1057        cmath::tanhf(self)
1058    }
1059
1060    /// Inverse hyperbolic sine function.
1061    ///
1062    /// # Unspecified precision
1063    ///
1064    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
1065    /// can even differ within the same execution from one invocation to the next.
1066    ///
1067    /// # Examples
1068    ///
1069    /// ```
1070    /// let x = 1.0f32;
1071    /// let f = x.sinh().asinh();
1072    ///
1073    /// let abs_difference = (f - x).abs();
1074    ///
1075    /// assert!(abs_difference <= 1e-6);
1076    /// ```
1077    #[doc(alias = "arcsinh")]
1078    #[rustc_allow_incoherent_impl]
1079    #[must_use = "method returns a new number and does not mutate the original value"]
1080    #[stable(feature = "rust1", since = "1.0.0")]
1081    #[inline]
1082    pub fn asinh(self) -> f32 {
1083        let ax = self.abs();
1084        let ix = 1.0 / ax;
1085        (ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
1086    }
1087
1088    /// Inverse hyperbolic cosine function.
1089    ///
1090    /// # Unspecified precision
1091    ///
1092    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
1093    /// can even differ within the same execution from one invocation to the next.
1094    ///
1095    /// # Examples
1096    ///
1097    /// ```
1098    /// let x = 1.0f32;
1099    /// let f = x.cosh().acosh();
1100    ///
1101    /// let abs_difference = (f - x).abs();
1102    ///
1103    /// assert!(abs_difference <= 1e-6);
1104    /// ```
1105    #[doc(alias = "arccosh")]
1106    #[rustc_allow_incoherent_impl]
1107    #[must_use = "method returns a new number and does not mutate the original value"]
1108    #[stable(feature = "rust1", since = "1.0.0")]
1109    #[inline]
1110    pub fn acosh(self) -> f32 {
1111        if self < 1.0 {
1112            Self::NAN
1113        } else {
1114            (self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
1115        }
1116    }
1117
1118    /// Inverse hyperbolic tangent function.
1119    ///
1120    /// # Unspecified precision
1121    ///
1122    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
1123    /// can even differ within the same execution from one invocation to the next.
1124    ///
1125    /// # Examples
1126    ///
1127    /// ```
1128    /// let x = std::f32::consts::FRAC_PI_6;
1129    /// let f = x.tanh().atanh();
1130    ///
1131    /// let abs_difference = (f - x).abs();
1132    ///
1133    /// assert!(abs_difference <= 1e-5);
1134    /// ```
1135    #[doc(alias = "arctanh")]
1136    #[rustc_allow_incoherent_impl]
1137    #[must_use = "method returns a new number and does not mutate the original value"]
1138    #[stable(feature = "rust1", since = "1.0.0")]
1139    #[inline]
1140    pub fn atanh(self) -> f32 {
1141        0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1142    }
1143
1144    /// Gamma function.
1145    ///
1146    /// # Unspecified precision
1147    ///
1148    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
1149    /// can even differ within the same execution from one invocation to the next.
1150    /// This function currently corresponds to the `tgammaf` from libc on Unix
1151    /// and Windows. Note that this might change in the future.
1152    ///
1153    /// # Examples
1154    ///
1155    /// ```
1156    /// #![feature(float_gamma)]
1157    /// let x = 5.0f32;
1158    ///
1159    /// let abs_difference = (x.gamma() - 24.0).abs();
1160    ///
1161    /// assert!(abs_difference <= 1e-5);
1162    /// ```
1163    #[rustc_allow_incoherent_impl]
1164    #[must_use = "method returns a new number and does not mutate the original value"]
1165    #[unstable(feature = "float_gamma", issue = "99842")]
1166    #[inline]
1167    pub fn gamma(self) -> f32 {
1168        cmath::tgammaf(self)
1169    }
1170
1171    /// Natural logarithm of the absolute value of the gamma function
1172    ///
1173    /// The integer part of the tuple indicates the sign of the gamma function.
1174    ///
1175    /// # Unspecified precision
1176    ///
1177    /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and
1178    /// can even differ within the same execution from one invocation to the next.
1179    /// This function currently corresponds to the `lgamma_r` from libc on Unix
1180    /// and Windows. Note that this might change in the future.
1181    ///
1182    /// # Examples
1183    ///
1184    /// ```
1185    /// #![feature(float_gamma)]
1186    /// let x = 2.0f32;
1187    ///
1188    /// let abs_difference = (x.ln_gamma().0 - 0.0).abs();
1189    ///
1190    /// assert!(abs_difference <= f32::EPSILON);
1191    /// ```
1192    #[rustc_allow_incoherent_impl]
1193    #[must_use = "method returns a new number and does not mutate the original value"]
1194    #[unstable(feature = "float_gamma", issue = "99842")]
1195    #[inline]
1196    pub fn ln_gamma(self) -> (f32, i32) {
1197        let mut signgamp: i32 = 0;
1198        let x = cmath::lgammaf_r(self, &mut signgamp);
1199        (x, signgamp)
1200    }
1201
1202    /// Error function.
1203    ///
1204    /// # Unspecified precision
1205    ///
1206    /// The precision of this function is non-deterministic. This means it varies by platform,
1207    /// Rust version, and can even differ within the same execution from one invocation to the next.
1208    ///
1209    /// This function currently corresponds to the `erff` from libc on Unix
1210    /// and Windows. Note that this might change in the future.
1211    ///
1212    /// # Examples
1213    ///
1214    /// ```
1215    /// #![feature(float_erf)]
1216    /// /// The error function relates what percent of a normal distribution lies
1217    /// /// within `x` standard deviations (scaled by `1/sqrt(2)`).
1218    /// fn within_standard_deviations(x: f32) -> f32 {
1219    ///     (x * std::f32::consts::FRAC_1_SQRT_2).erf() * 100.0
1220    /// }
1221    ///
1222    /// // 68% of a normal distribution is within one standard deviation
1223    /// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.01);
1224    /// // 95% of a normal distribution is within two standard deviations
1225    /// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.01);
1226    /// // 99.7% of a normal distribution is within three standard deviations
1227    /// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.01);
1228    /// ```
1229    #[rustc_allow_incoherent_impl]
1230    #[must_use = "method returns a new number and does not mutate the original value"]
1231    #[unstable(feature = "float_erf", issue = "136321")]
1232    #[inline]
1233    pub fn erf(self) -> f32 {
1234        cmath::erff(self)
1235    }
1236
1237    /// Complementary error function.
1238    ///
1239    /// # Unspecified precision
1240    ///
1241    /// The precision of this function is non-deterministic. This means it varies by platform,
1242    /// Rust version, and can even differ within the same execution from one invocation to the next.
1243    ///
1244    /// This function currently corresponds to the `erfcf` from libc on Unix
1245    /// and Windows. Note that this might change in the future.
1246    ///
1247    /// # Examples
1248    ///
1249    /// ```
1250    /// #![feature(float_erf)]
1251    /// let x: f32 = 0.123;
1252    ///
1253    /// let one = x.erf() + x.erfc();
1254    /// let abs_difference = (one - 1.0).abs();
1255    ///
1256    /// assert!(abs_difference <= 1e-6);
1257    /// ```
1258    #[rustc_allow_incoherent_impl]
1259    #[must_use = "method returns a new number and does not mutate the original value"]
1260    #[unstable(feature = "float_erf", issue = "136321")]
1261    #[inline]
1262    pub fn erfc(self) -> f32 {
1263        cmath::erfcf(self)
1264    }
1265}