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 #[must_use = "this returns the result of the operation, without modifying the original"]
647 pub fn sin_cos(self) -> (f128, f128) {
648 (self.sin(), self.cos())
649 }
650
651 /// Returns `e^(self) - 1` in a way that is accurate even if the
652 /// number is close to zero.
653 ///
654 /// # Unspecified precision
655 ///
656 /// The precision of this function is non-deterministic. This means it varies by platform,
657 /// Rust version, and can even differ within the same execution from one invocation to the next.
658 ///
659 /// This function currently corresponds to the `expm1f128` from libc on Unix
660 /// and Windows. Note that this might change in the future.
661 ///
662 /// # Examples
663 ///
664 /// ```
665 /// #![feature(f128)]
666 /// # #[cfg(not(miri))]
667 /// # #[cfg(target_has_reliable_f128_math)] {
668 ///
669 /// let x = 1e-8_f128;
670 ///
671 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
672 /// let approx = x + x * x / 2.0;
673 /// let abs_difference = (x.exp_m1() - approx).abs();
674 ///
675 /// assert!(abs_difference < 1e-10);
676 /// # }
677 /// ```
678 #[inline]
679 #[rustc_allow_incoherent_impl]
680 #[unstable(feature = "f128", issue = "116909")]
681 #[must_use = "method returns a new number and does not mutate the original value"]
682 pub fn exp_m1(self) -> f128 {
683 cmath::expm1f128(self)
684 }
685
686 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
687 /// the operations were performed separately.
688 ///
689 /// This returns NaN when `n < -1.0`, and negative infinity when `n == -1.0`.
690 ///
691 /// # Unspecified precision
692 ///
693 /// The precision of this function is non-deterministic. This means it varies by platform,
694 /// Rust version, and can even differ within the same execution from one invocation to the next.
695 ///
696 /// This function currently corresponds to the `log1pf128` from libc on Unix
697 /// and Windows. Note that this might change in the future.
698 ///
699 /// # Examples
700 ///
701 /// ```
702 /// #![feature(f128)]
703 /// # #[cfg(not(miri))]
704 /// # #[cfg(target_has_reliable_f128_math)] {
705 ///
706 /// let x = 1e-8_f128;
707 ///
708 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
709 /// let approx = x - x * x / 2.0;
710 /// let abs_difference = (x.ln_1p() - approx).abs();
711 ///
712 /// assert!(abs_difference < 1e-10);
713 /// # }
714 /// ```
715 ///
716 /// Out-of-range values:
717 /// ```
718 /// #![feature(f128)]
719 /// # #[cfg(not(miri))]
720 /// # #[cfg(target_has_reliable_f128_math)] {
721 ///
722 /// assert_eq!((-1.0_f128).ln_1p(), f128::NEG_INFINITY);
723 /// assert!((-2.0_f128).ln_1p().is_nan());
724 /// # }
725 /// ```
726 #[inline]
727 #[doc(alias = "log1p")]
728 #[must_use = "method returns a new number and does not mutate the original value"]
729 #[rustc_allow_incoherent_impl]
730 #[unstable(feature = "f128", issue = "116909")]
731 pub fn ln_1p(self) -> f128 {
732 cmath::log1pf128(self)
733 }
734
735 /// Hyperbolic sine function.
736 ///
737 /// # Unspecified precision
738 ///
739 /// The precision of this function is non-deterministic. This means it varies by platform,
740 /// Rust version, and can even differ within the same execution from one invocation to the next.
741 ///
742 /// This function currently corresponds to the `sinhf128` from libc on Unix
743 /// and Windows. Note that this might change in the future.
744 ///
745 /// # Examples
746 ///
747 /// ```
748 /// #![feature(f128)]
749 /// # #[cfg(not(miri))]
750 /// # #[cfg(target_has_reliable_f128_math)] {
751 ///
752 /// let e = std::f128::consts::E;
753 /// let x = 1.0f128;
754 ///
755 /// let f = x.sinh();
756 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
757 /// let g = ((e * e) - 1.0) / (2.0 * e);
758 /// let abs_difference = (f - g).abs();
759 ///
760 /// assert!(abs_difference <= f128::EPSILON);
761 /// # }
762 /// ```
763 #[inline]
764 #[rustc_allow_incoherent_impl]
765 #[unstable(feature = "f128", issue = "116909")]
766 #[must_use = "method returns a new number and does not mutate the original value"]
767 pub fn sinh(self) -> f128 {
768 cmath::sinhf128(self)
769 }
770
771 /// Hyperbolic cosine function.
772 ///
773 /// # Unspecified precision
774 ///
775 /// The precision of this function is non-deterministic. This means it varies by platform,
776 /// Rust version, and can even differ within the same execution from one invocation to the next.
777 ///
778 /// This function currently corresponds to the `coshf128` from libc on Unix
779 /// and Windows. Note that this might change in the future.
780 ///
781 /// # Examples
782 ///
783 /// ```
784 /// #![feature(f128)]
785 /// # #[cfg(not(miri))]
786 /// # #[cfg(target_has_reliable_f128_math)] {
787 ///
788 /// let e = std::f128::consts::E;
789 /// let x = 1.0f128;
790 /// let f = x.cosh();
791 /// // Solving cosh() at 1 gives this result
792 /// let g = ((e * e) + 1.0) / (2.0 * e);
793 /// let abs_difference = (f - g).abs();
794 ///
795 /// // Same result
796 /// assert!(abs_difference <= f128::EPSILON);
797 /// # }
798 /// ```
799 #[inline]
800 #[rustc_allow_incoherent_impl]
801 #[unstable(feature = "f128", issue = "116909")]
802 #[must_use = "method returns a new number and does not mutate the original value"]
803 pub fn cosh(self) -> f128 {
804 cmath::coshf128(self)
805 }
806
807 /// Hyperbolic tangent function.
808 ///
809 /// # Unspecified precision
810 ///
811 /// The precision of this function is non-deterministic. This means it varies by platform,
812 /// Rust version, and can even differ within the same execution from one invocation to the next.
813 ///
814 /// This function currently corresponds to the `tanhf128` from libc on Unix
815 /// and Windows. Note that this might change in the future.
816 ///
817 /// # Examples
818 ///
819 /// ```
820 /// #![feature(f128)]
821 /// # #[cfg(not(miri))]
822 /// # #[cfg(target_has_reliable_f128_math)] {
823 ///
824 /// let e = std::f128::consts::E;
825 /// let x = 1.0f128;
826 ///
827 /// let f = x.tanh();
828 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
829 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
830 /// let abs_difference = (f - g).abs();
831 ///
832 /// assert!(abs_difference <= f128::EPSILON);
833 /// # }
834 /// ```
835 #[inline]
836 #[rustc_allow_incoherent_impl]
837 #[unstable(feature = "f128", issue = "116909")]
838 #[must_use = "method returns a new number and does not mutate the original value"]
839 pub fn tanh(self) -> f128 {
840 cmath::tanhf128(self)
841 }
842
843 /// Inverse hyperbolic sine function.
844 ///
845 /// # Unspecified precision
846 ///
847 /// The precision of this function is non-deterministic. This means it varies by platform,
848 /// Rust version, and can even differ within the same execution from one invocation to the next.
849 ///
850 /// # Examples
851 ///
852 /// ```
853 /// #![feature(f128)]
854 /// # #[cfg(not(miri))]
855 /// # #[cfg(target_has_reliable_f128_math)] {
856 ///
857 /// let x = 1.0f128;
858 /// let f = x.sinh().asinh();
859 ///
860 /// let abs_difference = (f - x).abs();
861 ///
862 /// assert!(abs_difference <= f128::EPSILON);
863 /// # }
864 /// ```
865 #[inline]
866 #[doc(alias = "arcsinh")]
867 #[rustc_allow_incoherent_impl]
868 #[unstable(feature = "f128", issue = "116909")]
869 #[must_use = "method returns a new number and does not mutate the original value"]
870 pub fn asinh(self) -> f128 {
871 cmath::asinhf128(self)
872 }
873
874 /// Inverse hyperbolic cosine function.
875 ///
876 /// # Unspecified precision
877 ///
878 /// The precision of this function is non-deterministic. This means it varies by platform,
879 /// Rust version, and can even differ within the same execution from one invocation to the next.
880 ///
881 /// # Examples
882 ///
883 /// ```
884 /// #![feature(f128)]
885 /// # #[cfg(not(miri))]
886 /// # #[cfg(target_has_reliable_f128_math)] {
887 ///
888 /// let x = 1.0f128;
889 /// let f = x.cosh().acosh();
890 ///
891 /// let abs_difference = (f - x).abs();
892 ///
893 /// assert!(abs_difference <= f128::EPSILON);
894 /// # }
895 /// ```
896 #[inline]
897 #[doc(alias = "arccosh")]
898 #[rustc_allow_incoherent_impl]
899 #[unstable(feature = "f128", issue = "116909")]
900 #[must_use = "method returns a new number and does not mutate the original value"]
901 pub fn acosh(self) -> f128 {
902 cmath::acoshf128(self)
903 }
904
905 /// Inverse hyperbolic tangent function.
906 ///
907 /// # Unspecified precision
908 ///
909 /// The precision of this function is non-deterministic. This means it varies by platform,
910 /// Rust version, and can even differ within the same execution from one invocation to the next.
911 ///
912 /// # Examples
913 ///
914 /// ```
915 /// #![feature(f128)]
916 /// # #[cfg(not(miri))]
917 /// # #[cfg(target_has_reliable_f128_math)] {
918 ///
919 /// let x = std::f128::consts::FRAC_PI_6;
920 /// let f = x.tanh().atanh();
921 ///
922 /// let abs_difference = (f - x).abs();
923 ///
924 /// assert!(abs_difference <= 1e-5);
925 /// # }
926 /// ```
927 #[inline]
928 #[doc(alias = "arctanh")]
929 #[rustc_allow_incoherent_impl]
930 #[unstable(feature = "f128", issue = "116909")]
931 #[must_use = "method returns a new number and does not mutate the original value"]
932 pub fn atanh(self) -> f128 {
933 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
934 }
935
936 /// Gamma function.
937 ///
938 /// # Unspecified precision
939 ///
940 /// The precision of this function is non-deterministic. This means it varies by platform,
941 /// Rust version, and can even differ within the same execution from one invocation to the next.
942 ///
943 /// This function currently corresponds to the `tgammaf128` from libc on Unix
944 /// and Windows. Note that this might change in the future.
945 ///
946 /// # Examples
947 ///
948 /// ```
949 /// #![feature(f128)]
950 /// #![feature(float_gamma)]
951 /// # #[cfg(not(miri))]
952 /// # #[cfg(target_has_reliable_f128_math)] {
953 ///
954 /// let x = 5.0f128;
955 ///
956 /// let abs_difference = (x.gamma() - 24.0).abs();
957 ///
958 /// assert!(abs_difference <= f128::EPSILON);
959 /// # }
960 /// ```
961 #[inline]
962 #[rustc_allow_incoherent_impl]
963 #[unstable(feature = "f128", issue = "116909")]
964 // #[unstable(feature = "float_gamma", issue = "99842")]
965 #[must_use = "method returns a new number and does not mutate the original value"]
966 pub fn gamma(self) -> f128 {
967 cmath::tgammaf128(self)
968 }
969
970 /// Natural logarithm of the absolute value of the gamma function
971 ///
972 /// The integer part of the tuple indicates the sign of the gamma function.
973 ///
974 /// # Unspecified precision
975 ///
976 /// The precision of this function is non-deterministic. This means it varies by platform,
977 /// Rust version, and can even differ within the same execution from one invocation to the next.
978 ///
979 /// This function currently corresponds to the `lgammaf128_r` from libc on Unix
980 /// and Windows. Note that this might change in the future.
981 ///
982 /// # Examples
983 ///
984 /// ```
985 /// #![feature(f128)]
986 /// #![feature(float_gamma)]
987 /// # #[cfg(not(miri))]
988 /// # #[cfg(target_has_reliable_f128_math)] {
989 ///
990 /// let x = 2.0f128;
991 ///
992 /// let abs_difference = (x.ln_gamma().0 - 0.0).abs();
993 ///
994 /// assert!(abs_difference <= f128::EPSILON);
995 /// # }
996 /// ```
997 #[inline]
998 #[rustc_allow_incoherent_impl]
999 #[unstable(feature = "f128", issue = "116909")]
1000 // #[unstable(feature = "float_gamma", issue = "99842")]
1001 #[must_use = "method returns a new number and does not mutate the original value"]
1002 pub fn ln_gamma(self) -> (f128, i32) {
1003 let mut signgamp: i32 = 0;
1004 let x = cmath::lgammaf128_r(self, &mut signgamp);
1005 (x, signgamp)
1006 }
1007
1008 /// Error function.
1009 ///
1010 /// # Unspecified precision
1011 ///
1012 /// The precision of this function is non-deterministic. This means it varies by platform,
1013 /// Rust version, and can even differ within the same execution from one invocation to the next.
1014 ///
1015 /// This function currently corresponds to the `erff128` from libc on Unix
1016 /// and Windows. Note that this might change in the future.
1017 ///
1018 /// # Examples
1019 ///
1020 /// ```
1021 /// #![feature(f128)]
1022 /// #![feature(float_erf)]
1023 /// # #[cfg(not(miri))]
1024 /// # #[cfg(target_has_reliable_f128_math)] {
1025 /// /// The error function relates what percent of a normal distribution lies
1026 /// /// within `x` standard deviations (scaled by `1/sqrt(2)`).
1027 /// fn within_standard_deviations(x: f128) -> f128 {
1028 /// (x * std::f128::consts::FRAC_1_SQRT_2).erf() * 100.0
1029 /// }
1030 ///
1031 /// // 68% of a normal distribution is within one standard deviation
1032 /// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.01);
1033 /// // 95% of a normal distribution is within two standard deviations
1034 /// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.01);
1035 /// // 99.7% of a normal distribution is within three standard deviations
1036 /// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.01);
1037 /// # }
1038 /// ```
1039 #[rustc_allow_incoherent_impl]
1040 #[must_use = "method returns a new number and does not mutate the original value"]
1041 #[unstable(feature = "f128", issue = "116909")]
1042 // #[unstable(feature = "float_erf", issue = "136321")]
1043 #[inline]
1044 pub fn erf(self) -> f128 {
1045 cmath::erff128(self)
1046 }
1047
1048 /// Complementary error function.
1049 ///
1050 /// # Unspecified precision
1051 ///
1052 /// The precision of this function is non-deterministic. This means it varies by platform,
1053 /// Rust version, and can even differ within the same execution from one invocation to the next.
1054 ///
1055 /// This function currently corresponds to the `erfcf128` from libc on Unix
1056 /// and Windows. Note that this might change in the future.
1057 ///
1058 /// # Examples
1059 ///
1060 /// ```
1061 /// #![feature(f128)]
1062 /// #![feature(float_erf)]
1063 /// # #[cfg(not(miri))]
1064 /// # #[cfg(target_has_reliable_f128_math)] {
1065 /// let x: f128 = 0.123;
1066 ///
1067 /// let one = x.erf() + x.erfc();
1068 /// let abs_difference = (one - 1.0).abs();
1069 ///
1070 /// assert!(abs_difference <= f128::EPSILON);
1071 /// # }
1072 /// ```
1073 #[rustc_allow_incoherent_impl]
1074 #[must_use = "method returns a new number and does not mutate the original value"]
1075 #[unstable(feature = "f128", issue = "116909")]
1076 // #[unstable(feature = "float_erf", issue = "136321")]
1077 #[inline]
1078 pub fn erfc(self) -> f128 {
1079 cmath::erfcf128(self)
1080 }
1081}