real.rs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #![cfg(any(feature = "std", feature = "libm"))]
  2. use core::ops::Neg;
  3. use {Float, Num, NumCast};
  4. // NOTE: These doctests have the same issue as those in src/float.rs.
  5. // They're testing the inherent methods directly, and not those of `Real`.
  6. /// A trait for real number types that do not necessarily have
  7. /// floating-point-specific characteristics such as NaN and infinity.
  8. ///
  9. /// See [this Wikipedia article](https://en.wikipedia.org/wiki/Real_data_type)
  10. /// for a list of data types that could meaningfully implement this trait.
  11. ///
  12. /// This trait is only available with the `std` feature, or with the `libm` feature otherwise.
  13. pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
  14. /// Returns the smallest finite value that this type can represent.
  15. ///
  16. /// ```
  17. /// use num_traits::real::Real;
  18. /// use std::f64;
  19. ///
  20. /// let x: f64 = Real::min_value();
  21. ///
  22. /// assert_eq!(x, f64::MIN);
  23. /// ```
  24. fn min_value() -> Self;
  25. /// Returns the smallest positive, normalized value that this type can represent.
  26. ///
  27. /// ```
  28. /// use num_traits::real::Real;
  29. /// use std::f64;
  30. ///
  31. /// let x: f64 = Real::min_positive_value();
  32. ///
  33. /// assert_eq!(x, f64::MIN_POSITIVE);
  34. /// ```
  35. fn min_positive_value() -> Self;
  36. /// Returns epsilon, a small positive value.
  37. ///
  38. /// ```
  39. /// use num_traits::real::Real;
  40. /// use std::f64;
  41. ///
  42. /// let x: f64 = Real::epsilon();
  43. ///
  44. /// assert_eq!(x, f64::EPSILON);
  45. /// ```
  46. ///
  47. /// # Panics
  48. ///
  49. /// The default implementation will panic if `f32::EPSILON` cannot
  50. /// be cast to `Self`.
  51. fn epsilon() -> Self;
  52. /// Returns the largest finite value that this type can represent.
  53. ///
  54. /// ```
  55. /// use num_traits::real::Real;
  56. /// use std::f64;
  57. ///
  58. /// let x: f64 = Real::max_value();
  59. /// assert_eq!(x, f64::MAX);
  60. /// ```
  61. fn max_value() -> Self;
  62. /// Returns the largest integer less than or equal to a number.
  63. ///
  64. /// ```
  65. /// use num_traits::real::Real;
  66. ///
  67. /// let f = 3.99;
  68. /// let g = 3.0;
  69. ///
  70. /// assert_eq!(f.floor(), 3.0);
  71. /// assert_eq!(g.floor(), 3.0);
  72. /// ```
  73. fn floor(self) -> Self;
  74. /// Returns the smallest integer greater than or equal to a number.
  75. ///
  76. /// ```
  77. /// use num_traits::real::Real;
  78. ///
  79. /// let f = 3.01;
  80. /// let g = 4.0;
  81. ///
  82. /// assert_eq!(f.ceil(), 4.0);
  83. /// assert_eq!(g.ceil(), 4.0);
  84. /// ```
  85. fn ceil(self) -> Self;
  86. /// Returns the nearest integer to a number. Round half-way cases away from
  87. /// `0.0`.
  88. ///
  89. /// ```
  90. /// use num_traits::real::Real;
  91. ///
  92. /// let f = 3.3;
  93. /// let g = -3.3;
  94. ///
  95. /// assert_eq!(f.round(), 3.0);
  96. /// assert_eq!(g.round(), -3.0);
  97. /// ```
  98. fn round(self) -> Self;
  99. /// Return the integer part of a number.
  100. ///
  101. /// ```
  102. /// use num_traits::real::Real;
  103. ///
  104. /// let f = 3.3;
  105. /// let g = -3.7;
  106. ///
  107. /// assert_eq!(f.trunc(), 3.0);
  108. /// assert_eq!(g.trunc(), -3.0);
  109. /// ```
  110. fn trunc(self) -> Self;
  111. /// Returns the fractional part of a number.
  112. ///
  113. /// ```
  114. /// use num_traits::real::Real;
  115. ///
  116. /// let x = 3.5;
  117. /// let y = -3.5;
  118. /// let abs_difference_x = (x.fract() - 0.5).abs();
  119. /// let abs_difference_y = (y.fract() - (-0.5)).abs();
  120. ///
  121. /// assert!(abs_difference_x < 1e-10);
  122. /// assert!(abs_difference_y < 1e-10);
  123. /// ```
  124. fn fract(self) -> Self;
  125. /// Computes the absolute value of `self`. Returns `Float::nan()` if the
  126. /// number is `Float::nan()`.
  127. ///
  128. /// ```
  129. /// use num_traits::real::Real;
  130. /// use std::f64;
  131. ///
  132. /// let x = 3.5;
  133. /// let y = -3.5;
  134. ///
  135. /// let abs_difference_x = (x.abs() - x).abs();
  136. /// let abs_difference_y = (y.abs() - (-y)).abs();
  137. ///
  138. /// assert!(abs_difference_x < 1e-10);
  139. /// assert!(abs_difference_y < 1e-10);
  140. ///
  141. /// assert!(::num_traits::Float::is_nan(f64::NAN.abs()));
  142. /// ```
  143. fn abs(self) -> Self;
  144. /// Returns a number that represents the sign of `self`.
  145. ///
  146. /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
  147. /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
  148. /// - `Float::nan()` if the number is `Float::nan()`
  149. ///
  150. /// ```
  151. /// use num_traits::real::Real;
  152. /// use std::f64;
  153. ///
  154. /// let f = 3.5;
  155. ///
  156. /// assert_eq!(f.signum(), 1.0);
  157. /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
  158. ///
  159. /// assert!(f64::NAN.signum().is_nan());
  160. /// ```
  161. fn signum(self) -> Self;
  162. /// Returns `true` if `self` is positive, including `+0.0`,
  163. /// `Float::infinity()`, and with newer versions of Rust `f64::NAN`.
  164. ///
  165. /// ```
  166. /// use num_traits::real::Real;
  167. /// use std::f64;
  168. ///
  169. /// let neg_nan: f64 = -f64::NAN;
  170. ///
  171. /// let f = 7.0;
  172. /// let g = -7.0;
  173. ///
  174. /// assert!(f.is_sign_positive());
  175. /// assert!(!g.is_sign_positive());
  176. /// assert!(!neg_nan.is_sign_positive());
  177. /// ```
  178. fn is_sign_positive(self) -> bool;
  179. /// Returns `true` if `self` is negative, including `-0.0`,
  180. /// `Float::neg_infinity()`, and with newer versions of Rust `-f64::NAN`.
  181. ///
  182. /// ```
  183. /// use num_traits::real::Real;
  184. /// use std::f64;
  185. ///
  186. /// let nan: f64 = f64::NAN;
  187. ///
  188. /// let f = 7.0;
  189. /// let g = -7.0;
  190. ///
  191. /// assert!(!f.is_sign_negative());
  192. /// assert!(g.is_sign_negative());
  193. /// assert!(!nan.is_sign_negative());
  194. /// ```
  195. fn is_sign_negative(self) -> bool;
  196. /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
  197. /// error, yielding a more accurate result than an unfused multiply-add.
  198. ///
  199. /// Using `mul_add` can be more performant than an unfused multiply-add if
  200. /// the target architecture has a dedicated `fma` CPU instruction.
  201. ///
  202. /// ```
  203. /// use num_traits::real::Real;
  204. ///
  205. /// let m = 10.0;
  206. /// let x = 4.0;
  207. /// let b = 60.0;
  208. ///
  209. /// // 100.0
  210. /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
  211. ///
  212. /// assert!(abs_difference < 1e-10);
  213. /// ```
  214. fn mul_add(self, a: Self, b: Self) -> Self;
  215. /// Take the reciprocal (inverse) of a number, `1/x`.
  216. ///
  217. /// ```
  218. /// use num_traits::real::Real;
  219. ///
  220. /// let x = 2.0;
  221. /// let abs_difference = (x.recip() - (1.0/x)).abs();
  222. ///
  223. /// assert!(abs_difference < 1e-10);
  224. /// ```
  225. fn recip(self) -> Self;
  226. /// Raise a number to an integer power.
  227. ///
  228. /// Using this function is generally faster than using `powf`
  229. ///
  230. /// ```
  231. /// use num_traits::real::Real;
  232. ///
  233. /// let x = 2.0;
  234. /// let abs_difference = (x.powi(2) - x*x).abs();
  235. ///
  236. /// assert!(abs_difference < 1e-10);
  237. /// ```
  238. fn powi(self, n: i32) -> Self;
  239. /// Raise a number to a real number power.
  240. ///
  241. /// ```
  242. /// use num_traits::real::Real;
  243. ///
  244. /// let x = 2.0;
  245. /// let abs_difference = (x.powf(2.0) - x*x).abs();
  246. ///
  247. /// assert!(abs_difference < 1e-10);
  248. /// ```
  249. fn powf(self, n: Self) -> Self;
  250. /// Take the square root of a number.
  251. ///
  252. /// Returns NaN if `self` is a negative floating-point number.
  253. ///
  254. /// # Panics
  255. ///
  256. /// If the implementing type doesn't support NaN, this method should panic if `self < 0`.
  257. ///
  258. /// ```
  259. /// use num_traits::real::Real;
  260. ///
  261. /// let positive = 4.0;
  262. /// let negative = -4.0;
  263. ///
  264. /// let abs_difference = (positive.sqrt() - 2.0).abs();
  265. ///
  266. /// assert!(abs_difference < 1e-10);
  267. /// assert!(::num_traits::Float::is_nan(negative.sqrt()));
  268. /// ```
  269. fn sqrt(self) -> Self;
  270. /// Returns `e^(self)`, (the exponential function).
  271. ///
  272. /// ```
  273. /// use num_traits::real::Real;
  274. ///
  275. /// let one = 1.0;
  276. /// // e^1
  277. /// let e = one.exp();
  278. ///
  279. /// // ln(e) - 1 == 0
  280. /// let abs_difference = (e.ln() - 1.0).abs();
  281. ///
  282. /// assert!(abs_difference < 1e-10);
  283. /// ```
  284. fn exp(self) -> Self;
  285. /// Returns `2^(self)`.
  286. ///
  287. /// ```
  288. /// use num_traits::real::Real;
  289. ///
  290. /// let f = 2.0;
  291. ///
  292. /// // 2^2 - 4 == 0
  293. /// let abs_difference = (f.exp2() - 4.0).abs();
  294. ///
  295. /// assert!(abs_difference < 1e-10);
  296. /// ```
  297. fn exp2(self) -> Self;
  298. /// Returns the natural logarithm of the number.
  299. ///
  300. /// # Panics
  301. ///
  302. /// If `self <= 0` and this type does not support a NaN representation, this function should panic.
  303. ///
  304. /// ```
  305. /// use num_traits::real::Real;
  306. ///
  307. /// let one = 1.0;
  308. /// // e^1
  309. /// let e = one.exp();
  310. ///
  311. /// // ln(e) - 1 == 0
  312. /// let abs_difference = (e.ln() - 1.0).abs();
  313. ///
  314. /// assert!(abs_difference < 1e-10);
  315. /// ```
  316. fn ln(self) -> Self;
  317. /// Returns the logarithm of the number with respect to an arbitrary base.
  318. ///
  319. /// # Panics
  320. ///
  321. /// If `self <= 0` and this type does not support a NaN representation, this function should panic.
  322. ///
  323. /// ```
  324. /// use num_traits::real::Real;
  325. ///
  326. /// let ten = 10.0;
  327. /// let two = 2.0;
  328. ///
  329. /// // log10(10) - 1 == 0
  330. /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
  331. ///
  332. /// // log2(2) - 1 == 0
  333. /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
  334. ///
  335. /// assert!(abs_difference_10 < 1e-10);
  336. /// assert!(abs_difference_2 < 1e-10);
  337. /// ```
  338. fn log(self, base: Self) -> Self;
  339. /// Returns the base 2 logarithm of the number.
  340. ///
  341. /// # Panics
  342. ///
  343. /// If `self <= 0` and this type does not support a NaN representation, this function should panic.
  344. ///
  345. /// ```
  346. /// use num_traits::real::Real;
  347. ///
  348. /// let two = 2.0;
  349. ///
  350. /// // log2(2) - 1 == 0
  351. /// let abs_difference = (two.log2() - 1.0).abs();
  352. ///
  353. /// assert!(abs_difference < 1e-10);
  354. /// ```
  355. fn log2(self) -> Self;
  356. /// Returns the base 10 logarithm of the number.
  357. ///
  358. /// # Panics
  359. ///
  360. /// If `self <= 0` and this type does not support a NaN representation, this function should panic.
  361. ///
  362. ///
  363. /// ```
  364. /// use num_traits::real::Real;
  365. ///
  366. /// let ten = 10.0;
  367. ///
  368. /// // log10(10) - 1 == 0
  369. /// let abs_difference = (ten.log10() - 1.0).abs();
  370. ///
  371. /// assert!(abs_difference < 1e-10);
  372. /// ```
  373. fn log10(self) -> Self;
  374. /// Converts radians to degrees.
  375. ///
  376. /// ```
  377. /// use std::f64::consts;
  378. ///
  379. /// let angle = consts::PI;
  380. ///
  381. /// let abs_difference = (angle.to_degrees() - 180.0).abs();
  382. ///
  383. /// assert!(abs_difference < 1e-10);
  384. /// ```
  385. fn to_degrees(self) -> Self;
  386. /// Converts degrees to radians.
  387. ///
  388. /// ```
  389. /// use std::f64::consts;
  390. ///
  391. /// let angle = 180.0_f64;
  392. ///
  393. /// let abs_difference = (angle.to_radians() - consts::PI).abs();
  394. ///
  395. /// assert!(abs_difference < 1e-10);
  396. /// ```
  397. fn to_radians(self) -> Self;
  398. /// Returns the maximum of the two numbers.
  399. ///
  400. /// ```
  401. /// use num_traits::real::Real;
  402. ///
  403. /// let x = 1.0;
  404. /// let y = 2.0;
  405. ///
  406. /// assert_eq!(x.max(y), y);
  407. /// ```
  408. fn max(self, other: Self) -> Self;
  409. /// Returns the minimum of the two numbers.
  410. ///
  411. /// ```
  412. /// use num_traits::real::Real;
  413. ///
  414. /// let x = 1.0;
  415. /// let y = 2.0;
  416. ///
  417. /// assert_eq!(x.min(y), x);
  418. /// ```
  419. fn min(self, other: Self) -> Self;
  420. /// The positive difference of two numbers.
  421. ///
  422. /// * If `self <= other`: `0:0`
  423. /// * Else: `self - other`
  424. ///
  425. /// ```
  426. /// use num_traits::real::Real;
  427. ///
  428. /// let x = 3.0;
  429. /// let y = -3.0;
  430. ///
  431. /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
  432. /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
  433. ///
  434. /// assert!(abs_difference_x < 1e-10);
  435. /// assert!(abs_difference_y < 1e-10);
  436. /// ```
  437. fn abs_sub(self, other: Self) -> Self;
  438. /// Take the cubic root of a number.
  439. ///
  440. /// ```
  441. /// use num_traits::real::Real;
  442. ///
  443. /// let x = 8.0;
  444. ///
  445. /// // x^(1/3) - 2 == 0
  446. /// let abs_difference = (x.cbrt() - 2.0).abs();
  447. ///
  448. /// assert!(abs_difference < 1e-10);
  449. /// ```
  450. fn cbrt(self) -> Self;
  451. /// Calculate the length of the hypotenuse of a right-angle triangle given
  452. /// legs of length `x` and `y`.
  453. ///
  454. /// ```
  455. /// use num_traits::real::Real;
  456. ///
  457. /// let x = 2.0;
  458. /// let y = 3.0;
  459. ///
  460. /// // sqrt(x^2 + y^2)
  461. /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
  462. ///
  463. /// assert!(abs_difference < 1e-10);
  464. /// ```
  465. fn hypot(self, other: Self) -> Self;
  466. /// Computes the sine of a number (in radians).
  467. ///
  468. /// ```
  469. /// use num_traits::real::Real;
  470. /// use std::f64;
  471. ///
  472. /// let x = f64::consts::PI/2.0;
  473. ///
  474. /// let abs_difference = (x.sin() - 1.0).abs();
  475. ///
  476. /// assert!(abs_difference < 1e-10);
  477. /// ```
  478. fn sin(self) -> Self;
  479. /// Computes the cosine of a number (in radians).
  480. ///
  481. /// ```
  482. /// use num_traits::real::Real;
  483. /// use std::f64;
  484. ///
  485. /// let x = 2.0*f64::consts::PI;
  486. ///
  487. /// let abs_difference = (x.cos() - 1.0).abs();
  488. ///
  489. /// assert!(abs_difference < 1e-10);
  490. /// ```
  491. fn cos(self) -> Self;
  492. /// Computes the tangent of a number (in radians).
  493. ///
  494. /// ```
  495. /// use num_traits::real::Real;
  496. /// use std::f64;
  497. ///
  498. /// let x = f64::consts::PI/4.0;
  499. /// let abs_difference = (x.tan() - 1.0).abs();
  500. ///
  501. /// assert!(abs_difference < 1e-14);
  502. /// ```
  503. fn tan(self) -> Self;
  504. /// Computes the arcsine of a number. Return value is in radians in
  505. /// the range [-pi/2, pi/2] or NaN if the number is outside the range
  506. /// [-1, 1].
  507. ///
  508. /// # Panics
  509. ///
  510. /// If this type does not support a NaN representation, this function should panic
  511. /// if the number is outside the range [-1, 1].
  512. ///
  513. /// ```
  514. /// use num_traits::real::Real;
  515. /// use std::f64;
  516. ///
  517. /// let f = f64::consts::PI / 2.0;
  518. ///
  519. /// // asin(sin(pi/2))
  520. /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
  521. ///
  522. /// assert!(abs_difference < 1e-10);
  523. /// ```
  524. fn asin(self) -> Self;
  525. /// Computes the arccosine of a number. Return value is in radians in
  526. /// the range [0, pi] or NaN if the number is outside the range
  527. /// [-1, 1].
  528. ///
  529. /// # Panics
  530. ///
  531. /// If this type does not support a NaN representation, this function should panic
  532. /// if the number is outside the range [-1, 1].
  533. ///
  534. /// ```
  535. /// use num_traits::real::Real;
  536. /// use std::f64;
  537. ///
  538. /// let f = f64::consts::PI / 4.0;
  539. ///
  540. /// // acos(cos(pi/4))
  541. /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
  542. ///
  543. /// assert!(abs_difference < 1e-10);
  544. /// ```
  545. fn acos(self) -> Self;
  546. /// Computes the arctangent of a number. Return value is in radians in the
  547. /// range [-pi/2, pi/2];
  548. ///
  549. /// ```
  550. /// use num_traits::real::Real;
  551. ///
  552. /// let f = 1.0;
  553. ///
  554. /// // atan(tan(1))
  555. /// let abs_difference = (f.tan().atan() - 1.0).abs();
  556. ///
  557. /// assert!(abs_difference < 1e-10);
  558. /// ```
  559. fn atan(self) -> Self;
  560. /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
  561. ///
  562. /// * `x = 0`, `y = 0`: `0`
  563. /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
  564. /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
  565. /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
  566. ///
  567. /// ```
  568. /// use num_traits::real::Real;
  569. /// use std::f64;
  570. ///
  571. /// let pi = f64::consts::PI;
  572. /// // All angles from horizontal right (+x)
  573. /// // 45 deg counter-clockwise
  574. /// let x1 = 3.0;
  575. /// let y1 = -3.0;
  576. ///
  577. /// // 135 deg clockwise
  578. /// let x2 = -3.0;
  579. /// let y2 = 3.0;
  580. ///
  581. /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
  582. /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
  583. ///
  584. /// assert!(abs_difference_1 < 1e-10);
  585. /// assert!(abs_difference_2 < 1e-10);
  586. /// ```
  587. fn atan2(self, other: Self) -> Self;
  588. /// Simultaneously computes the sine and cosine of the number, `x`. Returns
  589. /// `(sin(x), cos(x))`.
  590. ///
  591. /// ```
  592. /// use num_traits::real::Real;
  593. /// use std::f64;
  594. ///
  595. /// let x = f64::consts::PI/4.0;
  596. /// let f = x.sin_cos();
  597. ///
  598. /// let abs_difference_0 = (f.0 - x.sin()).abs();
  599. /// let abs_difference_1 = (f.1 - x.cos()).abs();
  600. ///
  601. /// assert!(abs_difference_0 < 1e-10);
  602. /// assert!(abs_difference_0 < 1e-10);
  603. /// ```
  604. fn sin_cos(self) -> (Self, Self);
  605. /// Returns `e^(self) - 1` in a way that is accurate even if the
  606. /// number is close to zero.
  607. ///
  608. /// ```
  609. /// use num_traits::real::Real;
  610. ///
  611. /// let x = 7.0;
  612. ///
  613. /// // e^(ln(7)) - 1
  614. /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
  615. ///
  616. /// assert!(abs_difference < 1e-10);
  617. /// ```
  618. fn exp_m1(self) -> Self;
  619. /// Returns `ln(1+n)` (natural logarithm) more accurately than if
  620. /// the operations were performed separately.
  621. ///
  622. /// # Panics
  623. ///
  624. /// If this type does not support a NaN representation, this function should panic
  625. /// if `self-1 <= 0`.
  626. ///
  627. /// ```
  628. /// use num_traits::real::Real;
  629. /// use std::f64;
  630. ///
  631. /// let x = f64::consts::E - 1.0;
  632. ///
  633. /// // ln(1 + (e - 1)) == ln(e) == 1
  634. /// let abs_difference = (x.ln_1p() - 1.0).abs();
  635. ///
  636. /// assert!(abs_difference < 1e-10);
  637. /// ```
  638. fn ln_1p(self) -> Self;
  639. /// Hyperbolic sine function.
  640. ///
  641. /// ```
  642. /// use num_traits::real::Real;
  643. /// use std::f64;
  644. ///
  645. /// let e = f64::consts::E;
  646. /// let x = 1.0;
  647. ///
  648. /// let f = x.sinh();
  649. /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
  650. /// let g = (e*e - 1.0)/(2.0*e);
  651. /// let abs_difference = (f - g).abs();
  652. ///
  653. /// assert!(abs_difference < 1e-10);
  654. /// ```
  655. fn sinh(self) -> Self;
  656. /// Hyperbolic cosine function.
  657. ///
  658. /// ```
  659. /// use num_traits::real::Real;
  660. /// use std::f64;
  661. ///
  662. /// let e = f64::consts::E;
  663. /// let x = 1.0;
  664. /// let f = x.cosh();
  665. /// // Solving cosh() at 1 gives this result
  666. /// let g = (e*e + 1.0)/(2.0*e);
  667. /// let abs_difference = (f - g).abs();
  668. ///
  669. /// // Same result
  670. /// assert!(abs_difference < 1.0e-10);
  671. /// ```
  672. fn cosh(self) -> Self;
  673. /// Hyperbolic tangent function.
  674. ///
  675. /// ```
  676. /// use num_traits::real::Real;
  677. /// use std::f64;
  678. ///
  679. /// let e = f64::consts::E;
  680. /// let x = 1.0;
  681. ///
  682. /// let f = x.tanh();
  683. /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
  684. /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
  685. /// let abs_difference = (f - g).abs();
  686. ///
  687. /// assert!(abs_difference < 1.0e-10);
  688. /// ```
  689. fn tanh(self) -> Self;
  690. /// Inverse hyperbolic sine function.
  691. ///
  692. /// ```
  693. /// use num_traits::real::Real;
  694. ///
  695. /// let x = 1.0;
  696. /// let f = x.sinh().asinh();
  697. ///
  698. /// let abs_difference = (f - x).abs();
  699. ///
  700. /// assert!(abs_difference < 1.0e-10);
  701. /// ```
  702. fn asinh(self) -> Self;
  703. /// Inverse hyperbolic cosine function.
  704. ///
  705. /// ```
  706. /// use num_traits::real::Real;
  707. ///
  708. /// let x = 1.0;
  709. /// let f = x.cosh().acosh();
  710. ///
  711. /// let abs_difference = (f - x).abs();
  712. ///
  713. /// assert!(abs_difference < 1.0e-10);
  714. /// ```
  715. fn acosh(self) -> Self;
  716. /// Inverse hyperbolic tangent function.
  717. ///
  718. /// ```
  719. /// use num_traits::real::Real;
  720. /// use std::f64;
  721. ///
  722. /// let e = f64::consts::E;
  723. /// let f = e.tanh().atanh();
  724. ///
  725. /// let abs_difference = (f - e).abs();
  726. ///
  727. /// assert!(abs_difference < 1.0e-10);
  728. /// ```
  729. fn atanh(self) -> Self;
  730. }
  731. impl<T: Float> Real for T {
  732. forward! {
  733. Float::min_value() -> Self;
  734. Float::min_positive_value() -> Self;
  735. Float::epsilon() -> Self;
  736. Float::max_value() -> Self;
  737. }
  738. forward! {
  739. Float::floor(self) -> Self;
  740. Float::ceil(self) -> Self;
  741. Float::round(self) -> Self;
  742. Float::trunc(self) -> Self;
  743. Float::fract(self) -> Self;
  744. Float::abs(self) -> Self;
  745. Float::signum(self) -> Self;
  746. Float::is_sign_positive(self) -> bool;
  747. Float::is_sign_negative(self) -> bool;
  748. Float::mul_add(self, a: Self, b: Self) -> Self;
  749. Float::recip(self) -> Self;
  750. Float::powi(self, n: i32) -> Self;
  751. Float::powf(self, n: Self) -> Self;
  752. Float::sqrt(self) -> Self;
  753. Float::exp(self) -> Self;
  754. Float::exp2(self) -> Self;
  755. Float::ln(self) -> Self;
  756. Float::log(self, base: Self) -> Self;
  757. Float::log2(self) -> Self;
  758. Float::log10(self) -> Self;
  759. Float::to_degrees(self) -> Self;
  760. Float::to_radians(self) -> Self;
  761. Float::max(self, other: Self) -> Self;
  762. Float::min(self, other: Self) -> Self;
  763. Float::abs_sub(self, other: Self) -> Self;
  764. Float::cbrt(self) -> Self;
  765. Float::hypot(self, other: Self) -> Self;
  766. Float::sin(self) -> Self;
  767. Float::cos(self) -> Self;
  768. Float::tan(self) -> Self;
  769. Float::asin(self) -> Self;
  770. Float::acos(self) -> Self;
  771. Float::atan(self) -> Self;
  772. Float::atan2(self, other: Self) -> Self;
  773. Float::sin_cos(self) -> (Self, Self);
  774. Float::exp_m1(self) -> Self;
  775. Float::ln_1p(self) -> Self;
  776. Float::sinh(self) -> Self;
  777. Float::cosh(self) -> Self;
  778. Float::tanh(self) -> Self;
  779. Float::asinh(self) -> Self;
  780. Float::acosh(self) -> Self;
  781. Float::atanh(self) -> Self;
  782. }
  783. }