std/num/f128.rs
1//! Constants for the `f128` quadruple-precision floating point type.
2//!
3//! *[See also the `f128` primitive type](primitive@f128).*
4//!
5//! Mathematically significant numbers are provided in the `consts` sub-module.
6
7#![unstable(feature = "f128", issue = "116909")]
8#![doc(test(attr(feature(cfg_target_has_reliable_f16_f128), expect(internal_features))))]
9
10#[unstable(feature = "f128", issue = "116909")]
11pub use core::f128::consts;
12
13#[cfg(not(test))]
14use crate::intrinsics;
15#[cfg(not(test))]
16use crate::sys::cmath;
17
18#[cfg(not(test))]
19#[doc(test(attr(allow(unused_features))))]
20impl f128 {
21 /// Raises a number to a floating point power.
22 ///
23 /// Note that this function is special in that it can return non-NaN results for NaN inputs. For
24 /// example, `f128::powf(f128::NAN, 0.0)` returns `1.0`. However, if an input is a *signaling*
25 /// NaN, then the result is non-deterministically either a NaN or the result that the
26 /// corresponding quiet NaN would produce.
27 ///
28 /// # Unspecified precision
29 ///
30 /// The precision of this function is non-deterministic. This means it varies by platform,
31 /// Rust version, and can even differ within the same execution from one invocation to the next.
32 ///
33 /// # Examples
34 ///
35 /// ```
36 /// #![feature(f128)]
37 /// # #[cfg(not(miri))]
38 /// # #[cfg(target_has_reliable_f128_math)] {
39 ///
40 /// let x = 2.0_f128;
41 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
42 /// assert!(abs_difference <= f128::EPSILON);
43 ///
44 /// assert_eq!(f128::powf(1.0, f128::NAN), 1.0);
45 /// assert_eq!(f128::powf(f128::NAN, 0.0), 1.0);
46 /// assert_eq!(f128::powf(0.0, 0.0), 1.0);
47 /// # }
48 /// ```
49 #[inline]
50 #[rustc_allow_incoherent_impl]
51 #[unstable(feature = "f128", issue = "116909")]
52 #[must_use = "method returns a new number and does not mutate the original value"]
53 pub fn powf(self, n: f128) -> f128 {
54 intrinsics::powf128(self, n)
55 }
56
57 /// Returns `e^(self)`, (the exponential function).
58 ///
59 /// # Unspecified precision
60 ///
61 /// The precision of this function is non-deterministic. This means it varies by platform,
62 /// Rust version, and can even differ within the same execution from one invocation to the next.
63 ///
64 /// # Examples
65 ///
66 /// ```
67 /// #![feature(f128)]
68 /// # #[cfg(not(miri))]
69 /// # #[cfg(target_has_reliable_f128_math)] {
70 ///
71 /// let one = 1.0f128;
72 /// // e^1
73 /// let e = one.exp();
74 ///
75 /// // ln(e) - 1 == 0
76 /// let abs_difference = (e.ln() - 1.0).abs();
77 ///
78 /// assert!(abs_difference <= f128::EPSILON);
79 /// # }
80 /// ```
81 #[inline]
82 #[rustc_allow_incoherent_impl]
83 #[unstable(feature = "f128", issue = "116909")]
84 #[must_use = "method returns a new number and does not mutate the original value"]
85 pub fn exp(self) -> f128 {
86 intrinsics::expf128(self)
87 }
88
89 /// Returns `2^(self)`.
90 ///
91 /// # Unspecified precision
92 ///
93 /// The precision of this function is non-deterministic. This means it varies by platform,
94 /// Rust version, and can even differ within the same execution from one invocation to the next.
95 ///
96 /// # Examples
97 ///
98 /// ```
99 /// #![feature(f128)]
100 /// # #[cfg(not(miri))]
101 /// # #[cfg(target_has_reliable_f128_math)] {
102 ///
103 /// let f = 2.0f128;
104 ///
105 /// // 2^2 - 4 == 0
106 /// let abs_difference = (f.exp2() - 4.0).abs();
107 ///
108 /// assert!(abs_difference <= f128::EPSILON);
109 /// # }
110 /// ```
111 #[inline]
112 #[rustc_allow_incoherent_impl]
113 #[unstable(feature = "f128", issue = "116909")]
114 #[must_use = "method returns a new number and does not mutate the original value"]
115 pub fn exp2(self) -> f128 {
116 intrinsics::exp2f128(self)
117 }
118
119 /// Returns the natural logarithm of the number.
120 ///
121 /// This returns NaN when the number is negative, and negative infinity when number is zero.
122 ///
123 /// # Unspecified precision
124 ///
125 /// The precision of this function is non-deterministic. This means it varies by platform,
126 /// Rust version, and can even differ within the same execution from one invocation to the next.
127 ///
128 /// # Examples
129 ///
130 /// ```
131 /// #![feature(f128)]
132 /// # #[cfg(not(miri))]
133 /// # #[cfg(target_has_reliable_f128_math)] {
134 ///
135 /// let one = 1.0f128;
136 /// // e^1
137 /// let e = one.exp();
138 ///
139 /// // ln(e) - 1 == 0
140 /// let abs_difference = (e.ln() - 1.0).abs();
141 ///
142 /// assert!(abs_difference <= f128::EPSILON);
143 /// # }
144 /// ```
145 ///
146 /// Non-positive values:
147 /// ```
148 /// #![feature(f128)]
149 /// # #[cfg(not(miri))]
150 /// # #[cfg(target_has_reliable_f128_math)] {
151 ///
152 /// assert_eq!(0_f128.ln(), f128::NEG_INFINITY);
153 /// assert!((-42_f128).ln().is_nan());
154 /// # }
155 /// ```
156 #[inline]
157 #[rustc_allow_incoherent_impl]
158 #[unstable(feature = "f128", issue = "116909")]
159 #[must_use = "method returns a new number and does not mutate the original value"]
160 pub fn ln(self) -> f128 {
161 intrinsics::logf128(self)
162 }
163
164 /// Returns the logarithm of the number with respect to an arbitrary base.
165 ///
166 /// This returns NaN when the number is negative, and negative infinity when number is zero.
167 ///
168 /// The result might not be correctly rounded owing to implementation details;
169 /// `self.log2()` can produce more accurate results for base 2, and
170 /// `self.log10()` can produce more accurate results for base 10.
171 ///
172 /// # Unspecified precision
173 ///
174 /// The precision of this function is non-deterministic. This means it varies by platform,
175 /// Rust version, and can even differ within the same execution from one invocation to the next.
176 ///
177 /// # Examples
178 ///
179 /// ```
180 /// #![feature(f128)]
181 /// # #[cfg(not(miri))]
182 /// # #[cfg(target_has_reliable_f128_math)] {
183 ///
184 /// let five = 5.0f128;
185 ///
186 /// // log5(5) - 1 == 0
187 /// let abs_difference = (five.log(5.0) - 1.0).abs();
188 ///
189 /// assert!(abs_difference <= f128::EPSILON);
190 /// # }
191 /// ```
192 ///
193 /// Non-positive values:
194 /// ```
195 /// #![feature(f128)]
196 /// # #[cfg(not(miri))]
197 /// # #[cfg(target_has_reliable_f128_math)] {
198 ///
199 /// assert_eq!(0_f128.log(10.0), f128::NEG_INFINITY);
200 /// assert!((-42_f128).log(10.0).is_nan());
201 /// # }
202 /// ```
203 #[inline]
204 #[rustc_allow_incoherent_impl]
205 #[unstable(feature = "f128", issue = "116909")]
206 #[must_use = "method returns a new number and does not mutate the original value"]
207 pub fn log(self, base: f128) -> f128 {
208 self.ln() / base.ln()
209 }
210
211 /// Returns the base 2 logarithm of the number.
212 ///
213 /// This returns NaN when the number is negative, and negative infinity when number is zero.
214 ///
215 /// # Unspecified precision
216 ///
217 /// The precision of this function is non-deterministic. This means it varies by platform,
218 /// Rust version, and can even differ within the same execution from one invocation to the next.
219 ///
220 /// # Examples
221 ///
222 /// ```
223 /// #![feature(f128)]
224 /// # #[cfg(not(miri))]
225 /// # #[cfg(target_has_reliable_f128_math)] {
226 ///
227 /// let two = 2.0f128;
228 ///
229 /// // log2(2) - 1 == 0
230 /// let abs_difference = (two.log2() - 1.0).abs();
231 ///
232 /// assert!(abs_difference <= f128::EPSILON);
233 /// # }
234 /// ```
235 ///
236 /// Non-positive values:
237 /// ```
238 /// #![feature(f128)]
239 /// # #[cfg(not(miri))]
240 /// # #[cfg(target_has_reliable_f128_math)] {
241 ///
242 /// assert_eq!(0_f128.log2(), f128::NEG_INFINITY);
243 /// assert!((-42_f128).log2().is_nan());
244 /// # }
245 /// ```
246 #[inline]
247 #[rustc_allow_incoherent_impl]
248 #[unstable(feature = "f128", issue = "116909")]
249 #[must_use = "method returns a new number and does not mutate the original value"]
250 pub fn log2(self) -> f128 {
251 intrinsics::log2f128(self)
252 }
253
254 /// Returns the base 10 logarithm of the number.
255 ///
256 /// This returns NaN when the number is negative, and negative infinity when number is zero.
257 ///
258 /// # Unspecified precision
259 ///
260 /// The precision of this function is non-deterministic. This means it varies by platform,
261 /// Rust version, and can even differ within the same execution from one invocation to the next.
262 ///
263 /// # Examples
264 ///
265 /// ```
266 /// #![feature(f128)]
267 /// # #[cfg(not(miri))]
268 /// # #[cfg(target_has_reliable_f128_math)] {
269 ///
270 /// let ten = 10.0f128;
271 ///
272 /// // log10(10) - 1 == 0
273 /// let abs_difference = (ten.log10() - 1.0).abs();
274 ///
275 /// assert!(abs_difference <= f128::EPSILON);
276 /// # }
277 /// ```
278 ///
279 /// Non-positive values:
280 /// ```
281 /// #![feature(f128)]
282 /// # #[cfg(not(miri))]
283 /// # #[cfg(target_has_reliable_f128_math)] {
284 ///
285 /// assert_eq!(0_f128.log10(), f128::NEG_INFINITY);
286 /// assert!((-42_f128).log10().is_nan());
287 /// # }
288 /// ```
289 #[inline]
290 #[rustc_allow_incoherent_impl]
291 #[unstable(feature = "f128", issue = "116909")]
292 #[must_use = "method returns a new number and does not mutate the original value"]
293 pub fn log10(self) -> f128 {
294 intrinsics::log10f128(self)
295 }
296
297 /// Returns the cube root of a number.
298 ///
299 /// # Unspecified precision
300 ///
301 /// The precision of this function is non-deterministic. This means it varies by platform,
302 /// Rust version, and can even differ within the same execution from one invocation to the next.
303 ///
304 ///
305 /// This function currently corresponds to the `cbrtf128` from libc on Unix
306 /// and Windows. Note that this might change in the future.
307 ///
308 /// # Examples
309 ///
310 /// ```
311 /// #![feature(f128)]
312 /// # #[cfg(not(miri))]
313 /// # #[cfg(target_has_reliable_f128_math)] {
314 ///
315 /// let x = 8.0f128;
316 ///
317 /// // x^(1/3) - 2 == 0
318 /// let abs_difference = (x.cbrt() - 2.0).abs();
319 ///
320 /// assert!(abs_difference <= f128::EPSILON);
321 /// # }
322 /// ```
323 #[inline]
324 #[rustc_allow_incoherent_impl]
325 #[unstable(feature = "f128", issue = "116909")]
326 #[must_use = "method returns a new number and does not mutate the original value"]
327 pub fn cbrt(self) -> f128 {
328 cmath::cbrtf128(self)
329 }
330
331 /// Compute the distance between the origin and a point (`x`, `y`) on the
332 /// Euclidean plane. Equivalently, compute the length of the hypotenuse of a
333 /// right-angle triangle with other sides having length `x.abs()` and
334 /// `y.abs()`.
335 ///
336 /// # Unspecified precision
337 ///
338 /// The precision of this function is non-deterministic. This means it varies by platform,
339 /// Rust version, and can even differ within the same execution from one invocation to the next.
340 ///
341 ///
342 /// This function currently corresponds to the `hypotf128` from libc on Unix
343 /// and Windows. Note that this might change in the future.
344 ///
345 /// # Examples
346 ///
347 /// ```
348 /// #![feature(f128)]
349 /// # #[cfg(not(miri))]
350 /// # #[cfg(target_has_reliable_f128_math)] {
351 ///
352 /// let x = 2.0f128;
353 /// let y = 3.0f128;
354 ///
355 /// // sqrt(x^2 + y^2)
356 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
357 ///
358 /// assert!(abs_difference <= f128::EPSILON);
359 /// # }
360 /// ```
361 #[inline]
362 #[rustc_allow_incoherent_impl]
363 #[unstable(feature = "f128", issue = "116909")]
364 #[must_use = "method returns a new number and does not mutate the original value"]
365 pub fn hypot(self, other: f128) -> f128 {
366 cmath::hypotf128(self, other)
367 }
368
369 /// Computes the sine of a number (in radians).
370 ///
371 /// # Unspecified precision
372 ///
373 /// The precision of this function is non-deterministic. This means it varies by platform,
374 /// Rust version, and can even differ within the same execution from one invocation to the next.
375 ///
376 /// # Examples
377 ///
378 /// ```
379 /// #![feature(f128)]
380 /// # #[cfg(not(miri))]
381 /// # #[cfg(target_has_reliable_f128_math)] {
382 ///
383 /// let x = std::f128::consts::FRAC_PI_2;
384 ///
385 /// let abs_difference = (x.sin() - 1.0).abs();
386 ///
387 /// assert!(abs_difference <= f128::EPSILON);
388 /// # }
389 /// ```
390 #[inline]
391 #[rustc_allow_incoherent_impl]
392 #[unstable(feature = "f128", issue = "116909")]
393 #[must_use = "method returns a new number and does not mutate the original value"]
394 pub fn sin(self) -> f128 {
395 intrinsics::sinf128(self)
396 }
397
398 /// Computes the cosine of a number (in radians).
399 ///
400 /// # Unspecified precision
401 ///
402 /// The precision of this function is non-deterministic. This means it varies by platform,
403 /// Rust version, and can even differ within the same execution from one invocation to the next.
404 ///
405 /// # Examples
406 ///
407 /// ```
408 /// #![feature(f128)]
409 /// # #[cfg(not(miri))]
410 /// # #[cfg(target_has_reliable_f128_math)] {
411 ///
412 /// let x = 2.0 * std::f128::consts::PI;
413 ///
414 /// let abs_difference = (x.cos() - 1.0).abs();
415 ///
416 /// assert!(abs_difference <= f128::EPSILON);
417 /// # }
418 /// ```
419 #[inline]
420 #[rustc_allow_incoherent_impl]
421 #[unstable(feature = "f128", issue = "116909")]
422 #[must_use = "method returns a new number and does not mutate the original value"]
423 pub fn cos(self) -> f128 {
424 intrinsics::cosf128(self)
425 }
426
427 /// Computes the tangent of a number (in radians).
428 ///
429 /// # Unspecified precision
430 ///
431 /// The precision of this function is non-deterministic. This means it varies by platform,
432 /// Rust version, and can even differ within the same execution from one invocation to the next.
433 ///
434 /// This function currently corresponds to the `tanf128` from libc on Unix and
435 /// Windows. Note that this might change in the future.
436 ///
437 /// # Examples
438 ///
439 /// ```
440 /// #![feature(f128)]
441 /// # #[cfg(not(miri))]
442 /// # #[cfg(target_has_reliable_f128_math)] {
443 ///
444 /// let x = std::f128::consts::FRAC_PI_4;
445 /// let abs_difference = (x.tan() - 1.0).abs();
446 ///
447 /// assert!(abs_difference <= f128::EPSILON);
448 /// # }
449 /// ```
450 #[inline]
451 #[rustc_allow_incoherent_impl]
452 #[unstable(feature = "f128", issue = "116909")]
453 #[must_use = "method returns a new number and does not mutate the original value"]
454 pub fn tan(self) -> f128 {
455 cmath::tanf128(self)
456 }
457
458 /// Computes the arcsine of a number. Return value is in radians in
459 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
460 /// [-1, 1].
461 ///
462 /// # Unspecified precision
463 ///
464 /// The precision of this function is non-deterministic. This means it varies by platform,
465 /// Rust version, and can even differ within the same execution from one invocation to the next.
466 ///
467 /// This function currently corresponds to the `asinf128` from libc on Unix
468 /// and Windows. Note that this might change in the future.
469 ///
470 /// # Examples
471 ///
472 /// ```
473 /// #![feature(f128)]
474 /// # #[cfg(not(miri))]
475 /// # #[cfg(target_has_reliable_f128_math)] {
476 ///
477 /// let f = std::f128::consts::FRAC_PI_4;
478 ///
479 /// // asin(sin(pi/2))
480 /// let abs_difference = (f.sin().asin() - f).abs();
481 ///
482 /// assert!(abs_difference <= f128::EPSILON);
483 /// # }
484 /// ```
485 #[inline]
486 #[doc(alias = "arcsin")]
487 #[rustc_allow_incoherent_impl]
488 #[unstable(feature = "f128", issue = "116909")]
489 #[must_use = "method returns a new number and does not mutate the original value"]
490 pub fn asin(self) -> f128 {
491 cmath::asinf128(self)
492 }
493
494 /// Computes the arccosine of a number. Return value is in radians in
495 /// the range [0, pi] or NaN if the number is outside the range
496 /// [-1, 1].
497 ///
498 /// # Unspecified precision
499 ///
500 /// The precision of this function is non-deterministic. This means it varies by platform,
501 /// Rust version, and can even differ within the same execution from one invocation to the next.
502 ///
503 /// This function currently corresponds to the `acosf128` from libc on Unix
504 /// and Windows. Note that this might change in the future.
505 ///
506 /// # Examples
507 ///
508 /// ```
509 /// #![feature(f128)]
510 /// # #[cfg(not(miri))]
511 /// # #[cfg(target_has_reliable_f128_math)] {
512 ///
513 /// let f = std::f128::consts::FRAC_PI_4;
514 ///
515 /// // acos(cos(pi/4))
516 /// let abs_difference = (f.cos().acos() - std::f128::consts::FRAC_PI_4).abs();
517 ///
518 /// assert!(abs_difference <= f128::EPSILON);
519 /// # }
520 /// ```
521 #[inline]
522 #[doc(alias = "arccos")]
523 #[rustc_allow_incoherent_impl]
524 #[unstable(feature = "f128", issue = "116909")]
525 #[must_use = "method returns a new number and does not mutate the original value"]
526 pub fn acos(self) -> f128 {
527 cmath::acosf128(self)
528 }
529
530 /// Computes the arctangent of a number. Return value is in radians in the
531 /// range [-pi/2, pi/2];
532 ///
533 /// # Unspecified precision
534 ///
535 /// The precision of this function is non-deterministic. This means it varies by platform,
536 /// Rust version, and can even differ within the same execution from one invocation to the next.
537 ///
538 /// This function currently corresponds to the `atanf128` from libc on Unix
539 /// and Windows. Note that this might change in the future.
540 ///
541 /// # Examples
542 ///
543 /// ```
544 /// #![feature(f128)]
545 /// # #[cfg(not(miri))]
546 /// # #[cfg(target_has_reliable_f128_math)] {
547 ///
548 /// let f = 1.0f128;
549 ///
550 /// // atan(tan(1))
551 /// let abs_difference = (f.tan().atan() - 1.0).abs();
552 ///
553 /// assert!(abs_difference <= f128::EPSILON);
554 /// # }
555 /// ```
556 #[inline]
557 #[doc(alias = "arctan")]
558 #[rustc_allow_incoherent_impl]
559 #[unstable(feature = "f128", issue = "116909")]
560 #[must_use = "method returns a new number and does not mutate the original value"]
561 pub fn atan(self) -> f128 {
562 cmath::atanf128(self)
563 }
564
565 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
566 ///
567 /// | `x` | `y` | Piecewise Definition | Range |
568 /// |---------|---------|----------------------|---------------|
569 /// | `>= +0` | `>= +0` | `arctan(y/x)` | `[+0, +pi/2]` |
570 /// | `>= +0` | `<= -0` | `arctan(y/x)` | `[-pi/2, -0]` |
571 /// | `<= -0` | `>= +0` | `arctan(y/x) + pi` | `[+pi/2, +pi]`|
572 /// | `<= -0` | `<= -0` | `arctan(y/x) - pi` | `[-pi, -pi/2]`|
573 ///
574 /// # Unspecified precision
575 ///
576 /// The precision of this function is non-deterministic. This means it varies by platform,
577 /// Rust version, and can even differ within the same execution from one invocation to the next.
578 ///
579 /// This function currently corresponds to the `atan2f128` from libc on Unix
580 /// and Windows. Note that this might change in the future.
581 ///
582 /// # Examples
583 ///
584 /// ```
585 /// #![feature(f128)]
586 /// # #[cfg(not(miri))]
587 /// # #[cfg(target_has_reliable_f128_math)] {
588 ///
589 /// // Positive angles measured counter-clockwise
590 /// // from positive x axis
591 /// // -pi/4 radians (45 deg clockwise)
592 /// let x1 = 3.0f128;
593 /// let y1 = -3.0f128;
594 ///
595 /// // 3pi/4 radians (135 deg counter-clockwise)
596 /// let x2 = -3.0f128;
597 /// let y2 = 3.0f128;
598 ///
599 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f128::consts::FRAC_PI_4)).abs();
600 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f128::consts::FRAC_PI_4)).abs();
601 ///
602 /// assert!(abs_difference_1 <= f128::EPSILON);
603 /// assert!(abs_difference_2 <= f128::EPSILON);
604 /// # }
605 /// ```
606 #[inline]
607 #[rustc_allow_incoherent_impl]
608 #[unstable(feature = "f128", issue = "116909")]
609 #[must_use = "method returns a new number and does not mutate the original value"]
610 pub fn atan2(self, other: f128) -> f128 {
611 cmath::atan2f128(self, other)
612 }
613
614 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
615 /// `(sin(x), cos(x))`.
616 ///
617 /// # Unspecified precision
618 ///
619 /// The precision of this function is non-deterministic. This means it varies by platform,
620 /// Rust version, and can even differ within the same execution from one invocation to the next.
621 ///
622 /// This function currently corresponds to the `(f128::sin(x),
623 /// f128::cos(x))`. Note that this might change in the future.
624 ///
625 /// # Examples
626 ///
627 /// ```
628 /// #![feature(f128)]
629 /// # #[cfg(not(miri))]
630 /// # #[cfg(target_has_reliable_f128_math)] {
631 ///
632 /// let x = std::f128::consts::FRAC_PI_4;
633 /// let f = x.sin_cos();
634 ///
635 /// let abs_difference_0 = (f.0 - x.sin()).abs();
636 /// let abs_difference_1 = (f.1 - x.cos()).abs();
637 ///
638 /// assert!(abs_difference_0 <= f128::EPSILON);
639 /// assert!(abs_difference_1 <= f128::EPSILON);
640 /// # }
641 /// ```
642 #[inline]
643 #[doc(alias = "sincos")]
644 #[rustc_allow_incoherent_impl]
645 #[unstable(feature = "f128", issue = "116909")]
646 pub fn sin_cos(self) -> (f128, f128) {
647 (self.sin(), self.cos())
648 }
649
650 /// Returns `e^(self) - 1` in a way that is accurate even if the
651 /// number is close to zero.
652 ///
653 /// # Unspecified precision
654 ///
655 /// The precision of this function is non-deterministic. This means it varies by platform,
656 /// Rust version, and can even differ within the same execution from one invocation to the next.
657 ///
658 /// This function currently corresponds to the `expm1f128` from libc on Unix
659 /// and Windows. Note that this might change in the future.
660 ///
661 /// # Examples
662 ///
663 /// ```
664 /// #![feature(f128)]
665 /// # #[cfg(not(miri))]
666 /// # #[cfg(target_has_reliable_f128_math)] {
667 ///
668 /// let x = 1e-8_f128;
669 ///
670 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
671 /// let approx = x + x * x / 2.0;
672 /// let abs_difference = (x.exp_m1() - approx).abs();
673 ///
674 /// assert!(abs_difference < 1e-10);
675 /// # }
676 /// ```
677 #[inline]
678 #[rustc_allow_incoherent_impl]
679 #[unstable(feature = "f128", issue = "116909")]
680 #[must_use = "method returns a new number and does not mutate the original value"]
681 pub fn exp_m1(self) -> f128 {
682 cmath::expm1f128(self)
683 }
684
685 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
686 /// the operations were performed separately.
687 ///
688 /// This returns NaN when `n < -1.0`, and negative infinity when `n == -1.0`.
689 ///
690 /// # Unspecified precision
691 ///
692 /// The precision of this function is non-deterministic. This means it varies by platform,
693 /// Rust version, and can even differ within the same execution from one invocation to the next.
694 ///
695 /// This function currently corresponds to the `log1pf128` from libc on Unix
696 /// and Windows. Note that this might change in the future.
697 ///
698 /// # Examples
699 ///
700 /// ```
701 /// #![feature(f128)]
702 /// # #[cfg(not(miri))]
703 /// # #[cfg(target_has_reliable_f128_math)] {
704 ///
705 /// let x = 1e-8_f128;
706 ///
707 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
708 /// let approx = x - x * x / 2.0;
709 /// let abs_difference = (x.ln_1p() - approx).abs();
710 ///
711 /// assert!(abs_difference < 1e-10);
712 /// # }
713 /// ```
714 ///
715 /// Out-of-range values:
716 /// ```
717 /// #![feature(f128)]
718 /// # #[cfg(not(miri))]
719 /// # #[cfg(target_has_reliable_f128_math)] {
720 ///
721 /// assert_eq!((-1.0_f128).ln_1p(), f128::NEG_INFINITY);
722 /// assert!((-2.0_f128).ln_1p().is_nan());
723 /// # }
724 /// ```
725 #[inline]
726 #[doc(alias = "log1p")]
727 #[must_use = "method returns a new number and does not mutate the original value"]
728 #[rustc_allow_incoherent_impl]
729 #[unstable(feature = "f128", issue = "116909")]
730 pub fn ln_1p(self) -> f128 {
731 cmath::log1pf128(self)
732 }
733
734 /// Hyperbolic sine function.
735 ///
736 /// # Unspecified precision
737 ///
738 /// The precision of this function is non-deterministic. This means it varies by platform,
739 /// Rust version, and can even differ within the same execution from one invocation to the next.
740 ///
741 /// This function currently corresponds to the `sinhf128` from libc on Unix
742 /// and Windows. Note that this might change in the future.
743 ///
744 /// # Examples
745 ///
746 /// ```
747 /// #![feature(f128)]
748 /// # #[cfg(not(miri))]
749 /// # #[cfg(target_has_reliable_f128_math)] {
750 ///
751 /// let e = std::f128::consts::E;
752 /// let x = 1.0f128;
753 ///
754 /// let f = x.sinh();
755 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
756 /// let g = ((e * e) - 1.0) / (2.0 * e);
757 /// let abs_difference = (f - g).abs();
758 ///
759 /// assert!(abs_difference <= f128::EPSILON);
760 /// # }
761 /// ```
762 #[inline]
763 #[rustc_allow_incoherent_impl]
764 #[unstable(feature = "f128", issue = "116909")]
765 #[must_use = "method returns a new number and does not mutate the original value"]
766 pub fn sinh(self) -> f128 {
767 cmath::sinhf128(self)
768 }
769
770 /// Hyperbolic cosine function.
771 ///
772 /// # Unspecified precision
773 ///
774 /// The precision of this function is non-deterministic. This means it varies by platform,
775 /// Rust version, and can even differ within the same execution from one invocation to the next.
776 ///
777 /// This function currently corresponds to the `coshf128` from libc on Unix
778 /// and Windows. Note that this might change in the future.
779 ///
780 /// # Examples
781 ///
782 /// ```
783 /// #![feature(f128)]
784 /// # #[cfg(not(miri))]
785 /// # #[cfg(target_has_reliable_f128_math)] {
786 ///
787 /// let e = std::f128::consts::E;
788 /// let x = 1.0f128;
789 /// let f = x.cosh();
790 /// // Solving cosh() at 1 gives this result
791 /// let g = ((e * e) + 1.0) / (2.0 * e);
792 /// let abs_difference = (f - g).abs();
793 ///
794 /// // Same result
795 /// assert!(abs_difference <= f128::EPSILON);
796 /// # }
797 /// ```
798 #[inline]
799 #[rustc_allow_incoherent_impl]
800 #[unstable(feature = "f128", issue = "116909")]
801 #[must_use = "method returns a new number and does not mutate the original value"]
802 pub fn cosh(self) -> f128 {
803 cmath::coshf128(self)
804 }
805
806 /// Hyperbolic tangent function.
807 ///
808 /// # Unspecified precision
809 ///
810 /// The precision of this function is non-deterministic. This means it varies by platform,
811 /// Rust version, and can even differ within the same execution from one invocation to the next.
812 ///
813 /// This function currently corresponds to the `tanhf128` from libc on Unix
814 /// and Windows. Note that this might change in the future.
815 ///
816 /// # Examples
817 ///
818 /// ```
819 /// #![feature(f128)]
820 /// # #[cfg(not(miri))]
821 /// # #[cfg(target_has_reliable_f128_math)] {
822 ///
823 /// let e = std::f128::consts::E;
824 /// let x = 1.0f128;
825 ///
826 /// let f = x.tanh();
827 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
828 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
829 /// let abs_difference = (f - g).abs();
830 ///
831 /// assert!(abs_difference <= f128::EPSILON);
832 /// # }
833 /// ```
834 #[inline]
835 #[rustc_allow_incoherent_impl]
836 #[unstable(feature = "f128", issue = "116909")]
837 #[must_use = "method returns a new number and does not mutate the original value"]
838 pub fn tanh(self) -> f128 {
839 cmath::tanhf128(self)
840 }
841
842 /// Inverse hyperbolic sine function.
843 ///
844 /// # Unspecified precision
845 ///
846 /// The precision of this function is non-deterministic. This means it varies by platform,
847 /// Rust version, and can even differ within the same execution from one invocation to the next.
848 ///
849 /// # Examples
850 ///
851 /// ```
852 /// #![feature(f128)]
853 /// # #[cfg(not(miri))]
854 /// # #[cfg(target_has_reliable_f128_math)] {
855 ///
856 /// let x = 1.0f128;
857 /// let f = x.sinh().asinh();
858 ///
859 /// let abs_difference = (f - x).abs();
860 ///
861 /// assert!(abs_difference <= f128::EPSILON);
862 /// # }
863 /// ```
864 #[inline]
865 #[doc(alias = "arcsinh")]
866 #[rustc_allow_incoherent_impl]
867 #[unstable(feature = "f128", issue = "116909")]
868 #[must_use = "method returns a new number and does not mutate the original value"]
869 pub fn asinh(self) -> f128 {
870 let ax = self.abs();
871 let ix = 1.0 / ax;
872 (ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
873 }
874
875 /// Inverse hyperbolic cosine function.
876 ///
877 /// # Unspecified precision
878 ///
879 /// The precision of this function is non-deterministic. This means it varies by platform,
880 /// Rust version, and can even differ within the same execution from one invocation to the next.
881 ///
882 /// # Examples
883 ///
884 /// ```
885 /// #![feature(f128)]
886 /// # #[cfg(not(miri))]
887 /// # #[cfg(target_has_reliable_f128_math)] {
888 ///
889 /// let x = 1.0f128;
890 /// let f = x.cosh().acosh();
891 ///
892 /// let abs_difference = (f - x).abs();
893 ///
894 /// assert!(abs_difference <= f128::EPSILON);
895 /// # }
896 /// ```
897 #[inline]
898 #[doc(alias = "arccosh")]
899 #[rustc_allow_incoherent_impl]
900 #[unstable(feature = "f128", issue = "116909")]
901 #[must_use = "method returns a new number and does not mutate the original value"]
902 pub fn acosh(self) -> f128 {
903 if self < 1.0 {
904 Self::NAN
905 } else {
906 (self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
907 }
908 }
909
910 /// Inverse hyperbolic tangent function.
911 ///
912 /// # Unspecified precision
913 ///
914 /// The precision of this function is non-deterministic. This means it varies by platform,
915 /// Rust version, and can even differ within the same execution from one invocation to the next.
916 ///
917 /// # Examples
918 ///
919 /// ```
920 /// #![feature(f128)]
921 /// # #[cfg(not(miri))]
922 /// # #[cfg(target_has_reliable_f128_math)] {
923 ///
924 /// let x = std::f128::consts::FRAC_PI_6;
925 /// let f = x.tanh().atanh();
926 ///
927 /// let abs_difference = (f - x).abs();
928 ///
929 /// assert!(abs_difference <= 1e-5);
930 /// # }
931 /// ```
932 #[inline]
933 #[doc(alias = "arctanh")]
934 #[rustc_allow_incoherent_impl]
935 #[unstable(feature = "f128", issue = "116909")]
936 #[must_use = "method returns a new number and does not mutate the original value"]
937 pub fn atanh(self) -> f128 {
938 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
939 }
940
941 /// Gamma function.
942 ///
943 /// # Unspecified precision
944 ///
945 /// The precision of this function is non-deterministic. This means it varies by platform,
946 /// Rust version, and can even differ within the same execution from one invocation to the next.
947 ///
948 /// This function currently corresponds to the `tgammaf128` from libc on Unix
949 /// and Windows. Note that this might change in the future.
950 ///
951 /// # Examples
952 ///
953 /// ```
954 /// #![feature(f128)]
955 /// #![feature(float_gamma)]
956 /// # #[cfg(not(miri))]
957 /// # #[cfg(target_has_reliable_f128_math)] {
958 ///
959 /// let x = 5.0f128;
960 ///
961 /// let abs_difference = (x.gamma() - 24.0).abs();
962 ///
963 /// assert!(abs_difference <= f128::EPSILON);
964 /// # }
965 /// ```
966 #[inline]
967 #[rustc_allow_incoherent_impl]
968 #[unstable(feature = "f128", issue = "116909")]
969 // #[unstable(feature = "float_gamma", issue = "99842")]
970 #[must_use = "method returns a new number and does not mutate the original value"]
971 pub fn gamma(self) -> f128 {
972 cmath::tgammaf128(self)
973 }
974
975 /// Natural logarithm of the absolute value of the gamma function
976 ///
977 /// The integer part of the tuple indicates the sign of the gamma function.
978 ///
979 /// # Unspecified precision
980 ///
981 /// The precision of this function is non-deterministic. This means it varies by platform,
982 /// Rust version, and can even differ within the same execution from one invocation to the next.
983 ///
984 /// This function currently corresponds to the `lgammaf128_r` from libc on Unix
985 /// and Windows. Note that this might change in the future.
986 ///
987 /// # Examples
988 ///
989 /// ```
990 /// #![feature(f128)]
991 /// #![feature(float_gamma)]
992 /// # #[cfg(not(miri))]
993 /// # #[cfg(target_has_reliable_f128_math)] {
994 ///
995 /// let x = 2.0f128;
996 ///
997 /// let abs_difference = (x.ln_gamma().0 - 0.0).abs();
998 ///
999 /// assert!(abs_difference <= f128::EPSILON);
1000 /// # }
1001 /// ```
1002 #[inline]
1003 #[rustc_allow_incoherent_impl]
1004 #[unstable(feature = "f128", issue = "116909")]
1005 // #[unstable(feature = "float_gamma", issue = "99842")]
1006 #[must_use = "method returns a new number and does not mutate the original value"]
1007 pub fn ln_gamma(self) -> (f128, i32) {
1008 let mut signgamp: i32 = 0;
1009 let x = cmath::lgammaf128_r(self, &mut signgamp);
1010 (x, signgamp)
1011 }
1012
1013 /// Error function.
1014 ///
1015 /// # Unspecified precision
1016 ///
1017 /// The precision of this function is non-deterministic. This means it varies by platform,
1018 /// Rust version, and can even differ within the same execution from one invocation to the next.
1019 ///
1020 /// This function currently corresponds to the `erff128` from libc on Unix
1021 /// and Windows. Note that this might change in the future.
1022 ///
1023 /// # Examples
1024 ///
1025 /// ```
1026 /// #![feature(f128)]
1027 /// #![feature(float_erf)]
1028 /// # #[cfg(not(miri))]
1029 /// # #[cfg(target_has_reliable_f128_math)] {
1030 /// /// The error function relates what percent of a normal distribution lies
1031 /// /// within `x` standard deviations (scaled by `1/sqrt(2)`).
1032 /// fn within_standard_deviations(x: f128) -> f128 {
1033 /// (x * std::f128::consts::FRAC_1_SQRT_2).erf() * 100.0
1034 /// }
1035 ///
1036 /// // 68% of a normal distribution is within one standard deviation
1037 /// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.01);
1038 /// // 95% of a normal distribution is within two standard deviations
1039 /// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.01);
1040 /// // 99.7% of a normal distribution is within three standard deviations
1041 /// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.01);
1042 /// # }
1043 /// ```
1044 #[rustc_allow_incoherent_impl]
1045 #[must_use = "method returns a new number and does not mutate the original value"]
1046 #[unstable(feature = "f128", issue = "116909")]
1047 // #[unstable(feature = "float_erf", issue = "136321")]
1048 #[inline]
1049 pub fn erf(self) -> f128 {
1050 cmath::erff128(self)
1051 }
1052
1053 /// Complementary error function.
1054 ///
1055 /// # Unspecified precision
1056 ///
1057 /// The precision of this function is non-deterministic. This means it varies by platform,
1058 /// Rust version, and can even differ within the same execution from one invocation to the next.
1059 ///
1060 /// This function currently corresponds to the `erfcf128` from libc on Unix
1061 /// and Windows. Note that this might change in the future.
1062 ///
1063 /// # Examples
1064 ///
1065 /// ```
1066 /// #![feature(f128)]
1067 /// #![feature(float_erf)]
1068 /// # #[cfg(not(miri))]
1069 /// # #[cfg(target_has_reliable_f128_math)] {
1070 /// let x: f128 = 0.123;
1071 ///
1072 /// let one = x.erf() + x.erfc();
1073 /// let abs_difference = (one - 1.0).abs();
1074 ///
1075 /// assert!(abs_difference <= f128::EPSILON);
1076 /// # }
1077 /// ```
1078 #[rustc_allow_incoherent_impl]
1079 #[must_use = "method returns a new number and does not mutate the original value"]
1080 #[unstable(feature = "f128", issue = "116909")]
1081 // #[unstable(feature = "float_erf", issue = "136321")]
1082 #[inline]
1083 pub fn erfc(self) -> f128 {
1084 cmath::erfcf128(self)
1085 }
1086}