real.rs 22 KB

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