]> git.proxmox.com Git - rustc.git/blob - library/std/src/f32.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / library / std / src / f32.rs
1 //! Constants specific to 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 #[cfg(test)]
16 mod tests;
17
18 #[cfg(not(test))]
19 use crate::intrinsics;
20 #[cfg(not(test))]
21 use crate::sys::cmath;
22
23 #[stable(feature = "rust1", since = "1.0.0")]
24 #[allow(deprecated, deprecated_in_future)]
25 pub use core::f32::{
26 consts, DIGITS, EPSILON, INFINITY, MANTISSA_DIGITS, MAX, MAX_10_EXP, MAX_EXP, MIN, MIN_10_EXP,
27 MIN_EXP, MIN_POSITIVE, NAN, NEG_INFINITY, RADIX,
28 };
29
30 #[cfg(not(test))]
31 impl f32 {
32 /// Returns the largest integer less than or equal to `self`.
33 ///
34 /// # Examples
35 ///
36 /// ```
37 /// let f = 3.7_f32;
38 /// let g = 3.0_f32;
39 /// let h = -3.7_f32;
40 ///
41 /// assert_eq!(f.floor(), 3.0);
42 /// assert_eq!(g.floor(), 3.0);
43 /// assert_eq!(h.floor(), -4.0);
44 /// ```
45 #[rustc_allow_incoherent_impl]
46 #[must_use = "method returns a new number and does not mutate the original value"]
47 #[stable(feature = "rust1", since = "1.0.0")]
48 #[inline]
49 pub fn floor(self) -> f32 {
50 unsafe { intrinsics::floorf32(self) }
51 }
52
53 /// Returns the smallest integer greater than or equal to `self`.
54 ///
55 /// # Examples
56 ///
57 /// ```
58 /// let f = 3.01_f32;
59 /// let g = 4.0_f32;
60 ///
61 /// assert_eq!(f.ceil(), 4.0);
62 /// assert_eq!(g.ceil(), 4.0);
63 /// ```
64 #[rustc_allow_incoherent_impl]
65 #[must_use = "method returns a new number and does not mutate the original value"]
66 #[stable(feature = "rust1", since = "1.0.0")]
67 #[inline]
68 pub fn ceil(self) -> f32 {
69 unsafe { intrinsics::ceilf32(self) }
70 }
71
72 /// Returns the nearest integer to `self`. Round half-way cases away from
73 /// `0.0`.
74 ///
75 /// # Examples
76 ///
77 /// ```
78 /// let f = 3.3_f32;
79 /// let g = -3.3_f32;
80 ///
81 /// assert_eq!(f.round(), 3.0);
82 /// assert_eq!(g.round(), -3.0);
83 /// ```
84 #[rustc_allow_incoherent_impl]
85 #[must_use = "method returns a new number and does not mutate the original value"]
86 #[stable(feature = "rust1", since = "1.0.0")]
87 #[inline]
88 pub fn round(self) -> f32 {
89 unsafe { intrinsics::roundf32(self) }
90 }
91
92 /// Returns the integer part of `self`.
93 /// This means that non-integer numbers are always truncated towards zero.
94 ///
95 /// # Examples
96 ///
97 /// ```
98 /// let f = 3.7_f32;
99 /// let g = 3.0_f32;
100 /// let h = -3.7_f32;
101 ///
102 /// assert_eq!(f.trunc(), 3.0);
103 /// assert_eq!(g.trunc(), 3.0);
104 /// assert_eq!(h.trunc(), -3.0);
105 /// ```
106 #[rustc_allow_incoherent_impl]
107 #[must_use = "method returns a new number and does not mutate the original value"]
108 #[stable(feature = "rust1", since = "1.0.0")]
109 #[inline]
110 pub fn trunc(self) -> f32 {
111 unsafe { intrinsics::truncf32(self) }
112 }
113
114 /// Returns the fractional part of `self`.
115 ///
116 /// # Examples
117 ///
118 /// ```
119 /// let x = 3.6_f32;
120 /// let y = -3.6_f32;
121 /// let abs_difference_x = (x.fract() - 0.6).abs();
122 /// let abs_difference_y = (y.fract() - (-0.6)).abs();
123 ///
124 /// assert!(abs_difference_x <= f32::EPSILON);
125 /// assert!(abs_difference_y <= f32::EPSILON);
126 /// ```
127 #[rustc_allow_incoherent_impl]
128 #[must_use = "method returns a new number and does not mutate the original value"]
129 #[stable(feature = "rust1", since = "1.0.0")]
130 #[inline]
131 pub fn fract(self) -> f32 {
132 self - self.trunc()
133 }
134
135 /// Computes the absolute value of `self`.
136 ///
137 /// # Examples
138 ///
139 /// ```
140 /// let x = 3.5_f32;
141 /// let y = -3.5_f32;
142 ///
143 /// let abs_difference_x = (x.abs() - x).abs();
144 /// let abs_difference_y = (y.abs() - (-y)).abs();
145 ///
146 /// assert!(abs_difference_x <= f32::EPSILON);
147 /// assert!(abs_difference_y <= f32::EPSILON);
148 ///
149 /// assert!(f32::NAN.abs().is_nan());
150 /// ```
151 #[rustc_allow_incoherent_impl]
152 #[must_use = "method returns a new number and does not mutate the original value"]
153 #[stable(feature = "rust1", since = "1.0.0")]
154 #[inline]
155 pub fn abs(self) -> f32 {
156 unsafe { intrinsics::fabsf32(self) }
157 }
158
159 /// Returns a number that represents the sign of `self`.
160 ///
161 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
162 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
163 /// - NaN if the number is NaN
164 ///
165 /// # Examples
166 ///
167 /// ```
168 /// let f = 3.5_f32;
169 ///
170 /// assert_eq!(f.signum(), 1.0);
171 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
172 ///
173 /// assert!(f32::NAN.signum().is_nan());
174 /// ```
175 #[rustc_allow_incoherent_impl]
176 #[must_use = "method returns a new number and does not mutate the original value"]
177 #[stable(feature = "rust1", since = "1.0.0")]
178 #[inline]
179 pub fn signum(self) -> f32 {
180 if self.is_nan() { Self::NAN } else { 1.0_f32.copysign(self) }
181 }
182
183 /// Returns a number composed of the magnitude of `self` and the sign of
184 /// `sign`.
185 ///
186 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
187 /// equal to `-self`. If `self` is a NaN, then a NaN with the sign bit of
188 /// `sign` is returned. Note, however, that conserving the sign bit on NaN
189 /// across arithmetical operations is not generally guaranteed.
190 /// See [explanation of NaN as a special value](primitive@f32) for more info.
191 ///
192 /// # Examples
193 ///
194 /// ```
195 /// let f = 3.5_f32;
196 ///
197 /// assert_eq!(f.copysign(0.42), 3.5_f32);
198 /// assert_eq!(f.copysign(-0.42), -3.5_f32);
199 /// assert_eq!((-f).copysign(0.42), 3.5_f32);
200 /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
201 ///
202 /// assert!(f32::NAN.copysign(1.0).is_nan());
203 /// ```
204 #[rustc_allow_incoherent_impl]
205 #[must_use = "method returns a new number and does not mutate the original value"]
206 #[inline]
207 #[stable(feature = "copysign", since = "1.35.0")]
208 pub fn copysign(self, sign: f32) -> f32 {
209 unsafe { intrinsics::copysignf32(self, sign) }
210 }
211
212 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
213 /// error, yielding a more accurate result than an unfused multiply-add.
214 ///
215 /// Using `mul_add` *may* be more performant than an unfused multiply-add if
216 /// the target architecture has a dedicated `fma` CPU instruction. However,
217 /// this is not always true, and will be heavily dependant on designing
218 /// algorithms with specific target hardware in mind.
219 ///
220 /// # Examples
221 ///
222 /// ```
223 /// let m = 10.0_f32;
224 /// let x = 4.0_f32;
225 /// let b = 60.0_f32;
226 ///
227 /// // 100.0
228 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
229 ///
230 /// assert!(abs_difference <= f32::EPSILON);
231 /// ```
232 #[rustc_allow_incoherent_impl]
233 #[must_use = "method returns a new number and does not mutate the original value"]
234 #[stable(feature = "rust1", since = "1.0.0")]
235 #[inline]
236 pub fn mul_add(self, a: f32, b: f32) -> f32 {
237 unsafe { intrinsics::fmaf32(self, a, b) }
238 }
239
240 /// Calculates Euclidean division, the matching method for `rem_euclid`.
241 ///
242 /// This computes the integer `n` such that
243 /// `self = n * rhs + self.rem_euclid(rhs)`.
244 /// In other words, the result is `self / rhs` rounded to the integer `n`
245 /// such that `self >= n * rhs`.
246 ///
247 /// # Examples
248 ///
249 /// ```
250 /// let a: f32 = 7.0;
251 /// let b = 4.0;
252 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
253 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
254 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
255 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
256 /// ```
257 #[rustc_allow_incoherent_impl]
258 #[must_use = "method returns a new number and does not mutate the original value"]
259 #[inline]
260 #[stable(feature = "euclidean_division", since = "1.38.0")]
261 pub fn div_euclid(self, rhs: f32) -> f32 {
262 let q = (self / rhs).trunc();
263 if self % rhs < 0.0 {
264 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
265 }
266 q
267 }
268
269 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
270 ///
271 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
272 /// most cases. However, due to a floating point round-off error it can
273 /// result in `r == rhs.abs()`, violating the mathematical definition, if
274 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
275 /// This result is not an element of the function's codomain, but it is the
276 /// closest floating point number in the real numbers and thus fulfills the
277 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
278 /// approximatively.
279 ///
280 /// # Examples
281 ///
282 /// ```
283 /// let a: f32 = 7.0;
284 /// let b = 4.0;
285 /// assert_eq!(a.rem_euclid(b), 3.0);
286 /// assert_eq!((-a).rem_euclid(b), 1.0);
287 /// assert_eq!(a.rem_euclid(-b), 3.0);
288 /// assert_eq!((-a).rem_euclid(-b), 1.0);
289 /// // limitation due to round-off error
290 /// assert!((-f32::EPSILON).rem_euclid(3.0) != 0.0);
291 /// ```
292 #[rustc_allow_incoherent_impl]
293 #[must_use = "method returns a new number and does not mutate the original value"]
294 #[inline]
295 #[stable(feature = "euclidean_division", since = "1.38.0")]
296 pub fn rem_euclid(self, rhs: f32) -> f32 {
297 let r = self % rhs;
298 if r < 0.0 { r + rhs.abs() } else { r }
299 }
300
301 /// Raises a number to an integer power.
302 ///
303 /// Using this function is generally faster than using `powf`.
304 /// It might have a different sequence of rounding operations than `powf`,
305 /// so the results are not guaranteed to agree.
306 ///
307 /// # Examples
308 ///
309 /// ```
310 /// let x = 2.0_f32;
311 /// let abs_difference = (x.powi(2) - (x * x)).abs();
312 ///
313 /// assert!(abs_difference <= f32::EPSILON);
314 /// ```
315 #[rustc_allow_incoherent_impl]
316 #[must_use = "method returns a new number and does not mutate the original value"]
317 #[stable(feature = "rust1", since = "1.0.0")]
318 #[inline]
319 pub fn powi(self, n: i32) -> f32 {
320 unsafe { intrinsics::powif32(self, n) }
321 }
322
323 /// Raises a number to a floating point power.
324 ///
325 /// # Examples
326 ///
327 /// ```
328 /// let x = 2.0_f32;
329 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
330 ///
331 /// assert!(abs_difference <= f32::EPSILON);
332 /// ```
333 #[rustc_allow_incoherent_impl]
334 #[must_use = "method returns a new number and does not mutate the original value"]
335 #[stable(feature = "rust1", since = "1.0.0")]
336 #[inline]
337 pub fn powf(self, n: f32) -> f32 {
338 unsafe { intrinsics::powf32(self, n) }
339 }
340
341 /// Returns the square root of a number.
342 ///
343 /// Returns NaN if `self` is a negative number other than `-0.0`.
344 ///
345 /// # Examples
346 ///
347 /// ```
348 /// let positive = 4.0_f32;
349 /// let negative = -4.0_f32;
350 /// let negative_zero = -0.0_f32;
351 ///
352 /// let abs_difference = (positive.sqrt() - 2.0).abs();
353 ///
354 /// assert!(abs_difference <= f32::EPSILON);
355 /// assert!(negative.sqrt().is_nan());
356 /// assert!(negative_zero.sqrt() == negative_zero);
357 /// ```
358 #[rustc_allow_incoherent_impl]
359 #[must_use = "method returns a new number and does not mutate the original value"]
360 #[stable(feature = "rust1", since = "1.0.0")]
361 #[inline]
362 pub fn sqrt(self) -> f32 {
363 unsafe { intrinsics::sqrtf32(self) }
364 }
365
366 /// Returns `e^(self)`, (the exponential function).
367 ///
368 /// # Examples
369 ///
370 /// ```
371 /// let one = 1.0f32;
372 /// // e^1
373 /// let e = one.exp();
374 ///
375 /// // ln(e) - 1 == 0
376 /// let abs_difference = (e.ln() - 1.0).abs();
377 ///
378 /// assert!(abs_difference <= f32::EPSILON);
379 /// ```
380 #[rustc_allow_incoherent_impl]
381 #[must_use = "method returns a new number and does not mutate the original value"]
382 #[stable(feature = "rust1", since = "1.0.0")]
383 #[inline]
384 pub fn exp(self) -> f32 {
385 unsafe { intrinsics::expf32(self) }
386 }
387
388 /// Returns `2^(self)`.
389 ///
390 /// # Examples
391 ///
392 /// ```
393 /// let f = 2.0f32;
394 ///
395 /// // 2^2 - 4 == 0
396 /// let abs_difference = (f.exp2() - 4.0).abs();
397 ///
398 /// assert!(abs_difference <= f32::EPSILON);
399 /// ```
400 #[rustc_allow_incoherent_impl]
401 #[must_use = "method returns a new number and does not mutate the original value"]
402 #[stable(feature = "rust1", since = "1.0.0")]
403 #[inline]
404 pub fn exp2(self) -> f32 {
405 unsafe { intrinsics::exp2f32(self) }
406 }
407
408 /// Returns the natural logarithm of the number.
409 ///
410 /// # Examples
411 ///
412 /// ```
413 /// let one = 1.0f32;
414 /// // e^1
415 /// let e = one.exp();
416 ///
417 /// // ln(e) - 1 == 0
418 /// let abs_difference = (e.ln() - 1.0).abs();
419 ///
420 /// assert!(abs_difference <= f32::EPSILON);
421 /// ```
422 #[rustc_allow_incoherent_impl]
423 #[must_use = "method returns a new number and does not mutate the original value"]
424 #[stable(feature = "rust1", since = "1.0.0")]
425 #[inline]
426 pub fn ln(self) -> f32 {
427 unsafe { intrinsics::logf32(self) }
428 }
429
430 /// Returns the logarithm of the number with respect to an arbitrary base.
431 ///
432 /// The result might not be correctly rounded owing to implementation details;
433 /// `self.log2()` can produce more accurate results for base 2, and
434 /// `self.log10()` can produce more accurate results for base 10.
435 ///
436 /// # Examples
437 ///
438 /// ```
439 /// let five = 5.0f32;
440 ///
441 /// // log5(5) - 1 == 0
442 /// let abs_difference = (five.log(5.0) - 1.0).abs();
443 ///
444 /// assert!(abs_difference <= f32::EPSILON);
445 /// ```
446 #[rustc_allow_incoherent_impl]
447 #[must_use = "method returns a new number and does not mutate the original value"]
448 #[stable(feature = "rust1", since = "1.0.0")]
449 #[inline]
450 pub fn log(self, base: f32) -> f32 {
451 self.ln() / base.ln()
452 }
453
454 /// Returns the base 2 logarithm of the number.
455 ///
456 /// # Examples
457 ///
458 /// ```
459 /// let two = 2.0f32;
460 ///
461 /// // log2(2) - 1 == 0
462 /// let abs_difference = (two.log2() - 1.0).abs();
463 ///
464 /// assert!(abs_difference <= f32::EPSILON);
465 /// ```
466 #[rustc_allow_incoherent_impl]
467 #[must_use = "method returns a new number and does not mutate the original value"]
468 #[stable(feature = "rust1", since = "1.0.0")]
469 #[inline]
470 pub fn log2(self) -> f32 {
471 #[cfg(target_os = "android")]
472 return crate::sys::android::log2f32(self);
473 #[cfg(not(target_os = "android"))]
474 return unsafe { intrinsics::log2f32(self) };
475 }
476
477 /// Returns the base 10 logarithm of the number.
478 ///
479 /// # Examples
480 ///
481 /// ```
482 /// let ten = 10.0f32;
483 ///
484 /// // log10(10) - 1 == 0
485 /// let abs_difference = (ten.log10() - 1.0).abs();
486 ///
487 /// assert!(abs_difference <= f32::EPSILON);
488 /// ```
489 #[rustc_allow_incoherent_impl]
490 #[must_use = "method returns a new number and does not mutate the original value"]
491 #[stable(feature = "rust1", since = "1.0.0")]
492 #[inline]
493 pub fn log10(self) -> f32 {
494 unsafe { intrinsics::log10f32(self) }
495 }
496
497 /// The positive difference of two numbers.
498 ///
499 /// * If `self <= other`: `0:0`
500 /// * Else: `self - other`
501 ///
502 /// # Examples
503 ///
504 /// ```
505 /// let x = 3.0f32;
506 /// let y = -3.0f32;
507 ///
508 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
509 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
510 ///
511 /// assert!(abs_difference_x <= f32::EPSILON);
512 /// assert!(abs_difference_y <= f32::EPSILON);
513 /// ```
514 #[rustc_allow_incoherent_impl]
515 #[must_use = "method returns a new number and does not mutate the original value"]
516 #[stable(feature = "rust1", since = "1.0.0")]
517 #[inline]
518 #[deprecated(
519 since = "1.10.0",
520 note = "you probably meant `(self - other).abs()`: \
521 this operation is `(self - other).max(0.0)` \
522 except that `abs_sub` also propagates NaNs (also \
523 known as `fdimf` in C). If you truly need the positive \
524 difference, consider using that expression or the C function \
525 `fdimf`, depending on how you wish to handle NaN (please consider \
526 filing an issue describing your use-case too)."
527 )]
528 pub fn abs_sub(self, other: f32) -> f32 {
529 unsafe { cmath::fdimf(self, other) }
530 }
531
532 /// Returns the cube root of a number.
533 ///
534 /// # Examples
535 ///
536 /// ```
537 /// let x = 8.0f32;
538 ///
539 /// // x^(1/3) - 2 == 0
540 /// let abs_difference = (x.cbrt() - 2.0).abs();
541 ///
542 /// assert!(abs_difference <= f32::EPSILON);
543 /// ```
544 #[rustc_allow_incoherent_impl]
545 #[must_use = "method returns a new number and does not mutate the original value"]
546 #[stable(feature = "rust1", since = "1.0.0")]
547 #[inline]
548 pub fn cbrt(self) -> f32 {
549 unsafe { cmath::cbrtf(self) }
550 }
551
552 /// Calculates the length of the hypotenuse of a right-angle triangle given
553 /// legs of length `x` and `y`.
554 ///
555 /// # Examples
556 ///
557 /// ```
558 /// let x = 2.0f32;
559 /// let y = 3.0f32;
560 ///
561 /// // sqrt(x^2 + y^2)
562 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
563 ///
564 /// assert!(abs_difference <= f32::EPSILON);
565 /// ```
566 #[rustc_allow_incoherent_impl]
567 #[must_use = "method returns a new number and does not mutate the original value"]
568 #[stable(feature = "rust1", since = "1.0.0")]
569 #[inline]
570 pub fn hypot(self, other: f32) -> f32 {
571 unsafe { cmath::hypotf(self, other) }
572 }
573
574 /// Computes the sine of a number (in radians).
575 ///
576 /// # Examples
577 ///
578 /// ```
579 /// let x = std::f32::consts::FRAC_PI_2;
580 ///
581 /// let abs_difference = (x.sin() - 1.0).abs();
582 ///
583 /// assert!(abs_difference <= f32::EPSILON);
584 /// ```
585 #[rustc_allow_incoherent_impl]
586 #[must_use = "method returns a new number and does not mutate the original value"]
587 #[stable(feature = "rust1", since = "1.0.0")]
588 #[inline]
589 pub fn sin(self) -> f32 {
590 unsafe { intrinsics::sinf32(self) }
591 }
592
593 /// Computes the cosine of a number (in radians).
594 ///
595 /// # Examples
596 ///
597 /// ```
598 /// let x = 2.0 * std::f32::consts::PI;
599 ///
600 /// let abs_difference = (x.cos() - 1.0).abs();
601 ///
602 /// assert!(abs_difference <= f32::EPSILON);
603 /// ```
604 #[rustc_allow_incoherent_impl]
605 #[must_use = "method returns a new number and does not mutate the original value"]
606 #[stable(feature = "rust1", since = "1.0.0")]
607 #[inline]
608 pub fn cos(self) -> f32 {
609 unsafe { intrinsics::cosf32(self) }
610 }
611
612 /// Computes the tangent of a number (in radians).
613 ///
614 /// # Examples
615 ///
616 /// ```
617 /// let x = std::f32::consts::FRAC_PI_4;
618 /// let abs_difference = (x.tan() - 1.0).abs();
619 ///
620 /// assert!(abs_difference <= f32::EPSILON);
621 /// ```
622 #[rustc_allow_incoherent_impl]
623 #[must_use = "method returns a new number and does not mutate the original value"]
624 #[stable(feature = "rust1", since = "1.0.0")]
625 #[inline]
626 pub fn tan(self) -> f32 {
627 unsafe { cmath::tanf(self) }
628 }
629
630 /// Computes the arcsine of a number. Return value is in radians in
631 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
632 /// [-1, 1].
633 ///
634 /// # Examples
635 ///
636 /// ```
637 /// let f = std::f32::consts::FRAC_PI_2;
638 ///
639 /// // asin(sin(pi/2))
640 /// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
641 ///
642 /// assert!(abs_difference <= f32::EPSILON);
643 /// ```
644 #[rustc_allow_incoherent_impl]
645 #[must_use = "method returns a new number and does not mutate the original value"]
646 #[stable(feature = "rust1", since = "1.0.0")]
647 #[inline]
648 pub fn asin(self) -> f32 {
649 unsafe { cmath::asinf(self) }
650 }
651
652 /// Computes the arccosine of a number. Return value is in radians in
653 /// the range [0, pi] or NaN if the number is outside the range
654 /// [-1, 1].
655 ///
656 /// # Examples
657 ///
658 /// ```
659 /// let f = std::f32::consts::FRAC_PI_4;
660 ///
661 /// // acos(cos(pi/4))
662 /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
663 ///
664 /// assert!(abs_difference <= f32::EPSILON);
665 /// ```
666 #[rustc_allow_incoherent_impl]
667 #[must_use = "method returns a new number and does not mutate the original value"]
668 #[stable(feature = "rust1", since = "1.0.0")]
669 #[inline]
670 pub fn acos(self) -> f32 {
671 unsafe { cmath::acosf(self) }
672 }
673
674 /// Computes the arctangent of a number. Return value is in radians in the
675 /// range [-pi/2, pi/2];
676 ///
677 /// # Examples
678 ///
679 /// ```
680 /// let f = 1.0f32;
681 ///
682 /// // atan(tan(1))
683 /// let abs_difference = (f.tan().atan() - 1.0).abs();
684 ///
685 /// assert!(abs_difference <= f32::EPSILON);
686 /// ```
687 #[rustc_allow_incoherent_impl]
688 #[must_use = "method returns a new number and does not mutate the original value"]
689 #[stable(feature = "rust1", since = "1.0.0")]
690 #[inline]
691 pub fn atan(self) -> f32 {
692 unsafe { cmath::atanf(self) }
693 }
694
695 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
696 ///
697 /// * `x = 0`, `y = 0`: `0`
698 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
699 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
700 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
701 ///
702 /// # Examples
703 ///
704 /// ```
705 /// // Positive angles measured counter-clockwise
706 /// // from positive x axis
707 /// // -pi/4 radians (45 deg clockwise)
708 /// let x1 = 3.0f32;
709 /// let y1 = -3.0f32;
710 ///
711 /// // 3pi/4 radians (135 deg counter-clockwise)
712 /// let x2 = -3.0f32;
713 /// let y2 = 3.0f32;
714 ///
715 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
716 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
717 ///
718 /// assert!(abs_difference_1 <= f32::EPSILON);
719 /// assert!(abs_difference_2 <= f32::EPSILON);
720 /// ```
721 #[rustc_allow_incoherent_impl]
722 #[must_use = "method returns a new number and does not mutate the original value"]
723 #[stable(feature = "rust1", since = "1.0.0")]
724 #[inline]
725 pub fn atan2(self, other: f32) -> f32 {
726 unsafe { cmath::atan2f(self, other) }
727 }
728
729 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
730 /// `(sin(x), cos(x))`.
731 ///
732 /// # Examples
733 ///
734 /// ```
735 /// let x = std::f32::consts::FRAC_PI_4;
736 /// let f = x.sin_cos();
737 ///
738 /// let abs_difference_0 = (f.0 - x.sin()).abs();
739 /// let abs_difference_1 = (f.1 - x.cos()).abs();
740 ///
741 /// assert!(abs_difference_0 <= f32::EPSILON);
742 /// assert!(abs_difference_1 <= f32::EPSILON);
743 /// ```
744 #[rustc_allow_incoherent_impl]
745 #[stable(feature = "rust1", since = "1.0.0")]
746 #[inline]
747 pub fn sin_cos(self) -> (f32, f32) {
748 (self.sin(), self.cos())
749 }
750
751 /// Returns `e^(self) - 1` in a way that is accurate even if the
752 /// number is close to zero.
753 ///
754 /// # Examples
755 ///
756 /// ```
757 /// let x = 1e-8_f32;
758 ///
759 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
760 /// let approx = x + x * x / 2.0;
761 /// let abs_difference = (x.exp_m1() - approx).abs();
762 ///
763 /// assert!(abs_difference < 1e-10);
764 /// ```
765 #[rustc_allow_incoherent_impl]
766 #[must_use = "method returns a new number and does not mutate the original value"]
767 #[stable(feature = "rust1", since = "1.0.0")]
768 #[inline]
769 pub fn exp_m1(self) -> f32 {
770 unsafe { cmath::expm1f(self) }
771 }
772
773 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
774 /// the operations were performed separately.
775 ///
776 /// # Examples
777 ///
778 /// ```
779 /// let x = 1e-8_f32;
780 ///
781 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
782 /// let approx = x - x * x / 2.0;
783 /// let abs_difference = (x.ln_1p() - approx).abs();
784 ///
785 /// assert!(abs_difference < 1e-10);
786 /// ```
787 #[rustc_allow_incoherent_impl]
788 #[must_use = "method returns a new number and does not mutate the original value"]
789 #[stable(feature = "rust1", since = "1.0.0")]
790 #[inline]
791 pub fn ln_1p(self) -> f32 {
792 unsafe { cmath::log1pf(self) }
793 }
794
795 /// Hyperbolic sine function.
796 ///
797 /// # Examples
798 ///
799 /// ```
800 /// let e = std::f32::consts::E;
801 /// let x = 1.0f32;
802 ///
803 /// let f = x.sinh();
804 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
805 /// let g = ((e * e) - 1.0) / (2.0 * e);
806 /// let abs_difference = (f - g).abs();
807 ///
808 /// assert!(abs_difference <= f32::EPSILON);
809 /// ```
810 #[rustc_allow_incoherent_impl]
811 #[must_use = "method returns a new number and does not mutate the original value"]
812 #[stable(feature = "rust1", since = "1.0.0")]
813 #[inline]
814 pub fn sinh(self) -> f32 {
815 unsafe { cmath::sinhf(self) }
816 }
817
818 /// Hyperbolic cosine function.
819 ///
820 /// # Examples
821 ///
822 /// ```
823 /// let e = std::f32::consts::E;
824 /// let x = 1.0f32;
825 /// let f = x.cosh();
826 /// // Solving cosh() at 1 gives this result
827 /// let g = ((e * e) + 1.0) / (2.0 * e);
828 /// let abs_difference = (f - g).abs();
829 ///
830 /// // Same result
831 /// assert!(abs_difference <= f32::EPSILON);
832 /// ```
833 #[rustc_allow_incoherent_impl]
834 #[must_use = "method returns a new number and does not mutate the original value"]
835 #[stable(feature = "rust1", since = "1.0.0")]
836 #[inline]
837 pub fn cosh(self) -> f32 {
838 unsafe { cmath::coshf(self) }
839 }
840
841 /// Hyperbolic tangent function.
842 ///
843 /// # Examples
844 ///
845 /// ```
846 /// let e = std::f32::consts::E;
847 /// let x = 1.0f32;
848 ///
849 /// let f = x.tanh();
850 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
851 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
852 /// let abs_difference = (f - g).abs();
853 ///
854 /// assert!(abs_difference <= f32::EPSILON);
855 /// ```
856 #[rustc_allow_incoherent_impl]
857 #[must_use = "method returns a new number and does not mutate the original value"]
858 #[stable(feature = "rust1", since = "1.0.0")]
859 #[inline]
860 pub fn tanh(self) -> f32 {
861 unsafe { cmath::tanhf(self) }
862 }
863
864 /// Inverse hyperbolic sine function.
865 ///
866 /// # Examples
867 ///
868 /// ```
869 /// let x = 1.0f32;
870 /// let f = x.sinh().asinh();
871 ///
872 /// let abs_difference = (f - x).abs();
873 ///
874 /// assert!(abs_difference <= f32::EPSILON);
875 /// ```
876 #[rustc_allow_incoherent_impl]
877 #[must_use = "method returns a new number and does not mutate the original value"]
878 #[stable(feature = "rust1", since = "1.0.0")]
879 #[inline]
880 pub fn asinh(self) -> f32 {
881 (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
882 }
883
884 /// Inverse hyperbolic cosine function.
885 ///
886 /// # Examples
887 ///
888 /// ```
889 /// let x = 1.0f32;
890 /// let f = x.cosh().acosh();
891 ///
892 /// let abs_difference = (f - x).abs();
893 ///
894 /// assert!(abs_difference <= f32::EPSILON);
895 /// ```
896 #[rustc_allow_incoherent_impl]
897 #[must_use = "method returns a new number and does not mutate the original value"]
898 #[stable(feature = "rust1", since = "1.0.0")]
899 #[inline]
900 pub fn acosh(self) -> f32 {
901 if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
902 }
903
904 /// Inverse hyperbolic tangent function.
905 ///
906 /// # Examples
907 ///
908 /// ```
909 /// let e = std::f32::consts::E;
910 /// let f = e.tanh().atanh();
911 ///
912 /// let abs_difference = (f - e).abs();
913 ///
914 /// assert!(abs_difference <= 1e-5);
915 /// ```
916 #[rustc_allow_incoherent_impl]
917 #[must_use = "method returns a new number and does not mutate the original value"]
918 #[stable(feature = "rust1", since = "1.0.0")]
919 #[inline]
920 pub fn atanh(self) -> f32 {
921 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
922 }
923 }