float.rs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. use std::mem;
  2. use std::ops::Neg;
  3. use std::num::FpCategory;
  4. // Used for default implementation of `epsilon`
  5. use std::f32;
  6. use {Num, NumCast};
  7. // FIXME: these doctests aren't actually helpful, because they're using and
  8. // testing the inherent methods directly, not going through `Float`.
  9. pub trait Float
  10. : Num
  11. + Copy
  12. + NumCast
  13. + PartialOrd
  14. + Neg<Output = Self>
  15. {
  16. /// Returns the `NaN` value.
  17. ///
  18. /// ```
  19. /// use num_traits::Float;
  20. ///
  21. /// let nan: f32 = Float::nan();
  22. ///
  23. /// assert!(nan.is_nan());
  24. /// ```
  25. fn nan() -> Self;
  26. /// Returns the infinite value.
  27. ///
  28. /// ```
  29. /// use num_traits::Float;
  30. /// use std::f32;
  31. ///
  32. /// let infinity: f32 = Float::infinity();
  33. ///
  34. /// assert!(infinity.is_infinite());
  35. /// assert!(!infinity.is_finite());
  36. /// assert!(infinity > f32::MAX);
  37. /// ```
  38. fn infinity() -> Self;
  39. /// Returns the negative infinite value.
  40. ///
  41. /// ```
  42. /// use num_traits::Float;
  43. /// use std::f32;
  44. ///
  45. /// let neg_infinity: f32 = Float::neg_infinity();
  46. ///
  47. /// assert!(neg_infinity.is_infinite());
  48. /// assert!(!neg_infinity.is_finite());
  49. /// assert!(neg_infinity < f32::MIN);
  50. /// ```
  51. fn neg_infinity() -> Self;
  52. /// Returns `-0.0`.
  53. ///
  54. /// ```
  55. /// use num_traits::{Zero, Float};
  56. ///
  57. /// let inf: f32 = Float::infinity();
  58. /// let zero: f32 = Zero::zero();
  59. /// let neg_zero: f32 = Float::neg_zero();
  60. ///
  61. /// assert_eq!(zero, neg_zero);
  62. /// assert_eq!(7.0f32/inf, zero);
  63. /// assert_eq!(zero * 10.0, zero);
  64. /// ```
  65. fn neg_zero() -> Self;
  66. /// Returns the smallest finite value that this type can represent.
  67. ///
  68. /// ```
  69. /// use num_traits::Float;
  70. /// use std::f64;
  71. ///
  72. /// let x: f64 = Float::min_value();
  73. ///
  74. /// assert_eq!(x, f64::MIN);
  75. /// ```
  76. fn min_value() -> Self;
  77. /// Returns the smallest positive, normalized value that this type can represent.
  78. ///
  79. /// ```
  80. /// use num_traits::Float;
  81. /// use std::f64;
  82. ///
  83. /// let x: f64 = Float::min_positive_value();
  84. ///
  85. /// assert_eq!(x, f64::MIN_POSITIVE);
  86. /// ```
  87. fn min_positive_value() -> Self;
  88. /// Returns epsilon, a small positive value.
  89. ///
  90. /// ```
  91. /// use num_traits::Float;
  92. /// use std::f64;
  93. ///
  94. /// let x: f64 = Float::epsilon();
  95. ///
  96. /// assert_eq!(x, f64::EPSILON);
  97. /// ```
  98. ///
  99. /// # Panics
  100. ///
  101. /// The default implementation will panic if `f32::EPSILON` cannot
  102. /// be cast to `Self`.
  103. fn epsilon() -> Self {
  104. Self::from(f32::EPSILON).expect("Unable to cast from f32::EPSILON")
  105. }
  106. /// Returns the largest finite value that this type can represent.
  107. ///
  108. /// ```
  109. /// use num_traits::Float;
  110. /// use std::f64;
  111. ///
  112. /// let x: f64 = Float::max_value();
  113. /// assert_eq!(x, f64::MAX);
  114. /// ```
  115. fn max_value() -> Self;
  116. /// Returns `true` if this value is `NaN` and false otherwise.
  117. ///
  118. /// ```
  119. /// use num_traits::Float;
  120. /// use std::f64;
  121. ///
  122. /// let nan = f64::NAN;
  123. /// let f = 7.0;
  124. ///
  125. /// assert!(nan.is_nan());
  126. /// assert!(!f.is_nan());
  127. /// ```
  128. fn is_nan(self) -> bool;
  129. /// Returns `true` if this value is positive infinity or negative infinity and
  130. /// false otherwise.
  131. ///
  132. /// ```
  133. /// use num_traits::Float;
  134. /// use std::f32;
  135. ///
  136. /// let f = 7.0f32;
  137. /// let inf: f32 = Float::infinity();
  138. /// let neg_inf: f32 = Float::neg_infinity();
  139. /// let nan: f32 = f32::NAN;
  140. ///
  141. /// assert!(!f.is_infinite());
  142. /// assert!(!nan.is_infinite());
  143. ///
  144. /// assert!(inf.is_infinite());
  145. /// assert!(neg_inf.is_infinite());
  146. /// ```
  147. fn is_infinite(self) -> bool;
  148. /// Returns `true` if this number is neither infinite nor `NaN`.
  149. ///
  150. /// ```
  151. /// use num_traits::Float;
  152. /// use std::f32;
  153. ///
  154. /// let f = 7.0f32;
  155. /// let inf: f32 = Float::infinity();
  156. /// let neg_inf: f32 = Float::neg_infinity();
  157. /// let nan: f32 = f32::NAN;
  158. ///
  159. /// assert!(f.is_finite());
  160. ///
  161. /// assert!(!nan.is_finite());
  162. /// assert!(!inf.is_finite());
  163. /// assert!(!neg_inf.is_finite());
  164. /// ```
  165. fn is_finite(self) -> bool;
  166. /// Returns `true` if the number is neither zero, infinite,
  167. /// [subnormal][subnormal], or `NaN`.
  168. ///
  169. /// ```
  170. /// use num_traits::Float;
  171. /// use std::f32;
  172. ///
  173. /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
  174. /// let max = f32::MAX;
  175. /// let lower_than_min = 1.0e-40_f32;
  176. /// let zero = 0.0f32;
  177. ///
  178. /// assert!(min.is_normal());
  179. /// assert!(max.is_normal());
  180. ///
  181. /// assert!(!zero.is_normal());
  182. /// assert!(!f32::NAN.is_normal());
  183. /// assert!(!f32::INFINITY.is_normal());
  184. /// // Values between `0` and `min` are Subnormal.
  185. /// assert!(!lower_than_min.is_normal());
  186. /// ```
  187. /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
  188. fn is_normal(self) -> bool;
  189. /// Returns the floating point category of the number. If only one property
  190. /// is going to be tested, it is generally faster to use the specific
  191. /// predicate instead.
  192. ///
  193. /// ```
  194. /// use num_traits::Float;
  195. /// use std::num::FpCategory;
  196. /// use std::f32;
  197. ///
  198. /// let num = 12.4f32;
  199. /// let inf = f32::INFINITY;
  200. ///
  201. /// assert_eq!(num.classify(), FpCategory::Normal);
  202. /// assert_eq!(inf.classify(), FpCategory::Infinite);
  203. /// ```
  204. fn classify(self) -> FpCategory;
  205. /// Returns the largest integer less than or equal to a number.
  206. ///
  207. /// ```
  208. /// use num_traits::Float;
  209. ///
  210. /// let f = 3.99;
  211. /// let g = 3.0;
  212. ///
  213. /// assert_eq!(f.floor(), 3.0);
  214. /// assert_eq!(g.floor(), 3.0);
  215. /// ```
  216. fn floor(self) -> Self;
  217. /// Returns the smallest integer greater than or equal to a number.
  218. ///
  219. /// ```
  220. /// use num_traits::Float;
  221. ///
  222. /// let f = 3.01;
  223. /// let g = 4.0;
  224. ///
  225. /// assert_eq!(f.ceil(), 4.0);
  226. /// assert_eq!(g.ceil(), 4.0);
  227. /// ```
  228. fn ceil(self) -> Self;
  229. /// Returns the nearest integer to a number. Round half-way cases away from
  230. /// `0.0`.
  231. ///
  232. /// ```
  233. /// use num_traits::Float;
  234. ///
  235. /// let f = 3.3;
  236. /// let g = -3.3;
  237. ///
  238. /// assert_eq!(f.round(), 3.0);
  239. /// assert_eq!(g.round(), -3.0);
  240. /// ```
  241. fn round(self) -> Self;
  242. /// Return the integer part of a number.
  243. ///
  244. /// ```
  245. /// use num_traits::Float;
  246. ///
  247. /// let f = 3.3;
  248. /// let g = -3.7;
  249. ///
  250. /// assert_eq!(f.trunc(), 3.0);
  251. /// assert_eq!(g.trunc(), -3.0);
  252. /// ```
  253. fn trunc(self) -> Self;
  254. /// Returns the fractional part of a number.
  255. ///
  256. /// ```
  257. /// use num_traits::Float;
  258. ///
  259. /// let x = 3.5;
  260. /// let y = -3.5;
  261. /// let abs_difference_x = (x.fract() - 0.5).abs();
  262. /// let abs_difference_y = (y.fract() - (-0.5)).abs();
  263. ///
  264. /// assert!(abs_difference_x < 1e-10);
  265. /// assert!(abs_difference_y < 1e-10);
  266. /// ```
  267. fn fract(self) -> Self;
  268. /// Computes the absolute value of `self`. Returns `Float::nan()` if the
  269. /// number is `Float::nan()`.
  270. ///
  271. /// ```
  272. /// use num_traits::Float;
  273. /// use std::f64;
  274. ///
  275. /// let x = 3.5;
  276. /// let y = -3.5;
  277. ///
  278. /// let abs_difference_x = (x.abs() - x).abs();
  279. /// let abs_difference_y = (y.abs() - (-y)).abs();
  280. ///
  281. /// assert!(abs_difference_x < 1e-10);
  282. /// assert!(abs_difference_y < 1e-10);
  283. ///
  284. /// assert!(f64::NAN.abs().is_nan());
  285. /// ```
  286. fn abs(self) -> Self;
  287. /// Returns a number that represents the sign of `self`.
  288. ///
  289. /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
  290. /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
  291. /// - `Float::nan()` if the number is `Float::nan()`
  292. ///
  293. /// ```
  294. /// use num_traits::Float;
  295. /// use std::f64;
  296. ///
  297. /// let f = 3.5;
  298. ///
  299. /// assert_eq!(f.signum(), 1.0);
  300. /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
  301. ///
  302. /// assert!(f64::NAN.signum().is_nan());
  303. /// ```
  304. fn signum(self) -> Self;
  305. /// Returns `true` if `self` is positive, including `+0.0`,
  306. /// `Float::infinity()`, and with newer versions of Rust `f64::NAN`.
  307. ///
  308. /// ```
  309. /// use num_traits::Float;
  310. /// use std::f64;
  311. ///
  312. /// let neg_nan: f64 = -f64::NAN;
  313. ///
  314. /// let f = 7.0;
  315. /// let g = -7.0;
  316. ///
  317. /// assert!(f.is_sign_positive());
  318. /// assert!(!g.is_sign_positive());
  319. /// assert!(!neg_nan.is_sign_positive());
  320. /// ```
  321. fn is_sign_positive(self) -> bool;
  322. /// Returns `true` if `self` is negative, including `-0.0`,
  323. /// `Float::neg_infinity()`, and with newer versions of Rust `-f64::NAN`.
  324. ///
  325. /// ```
  326. /// use num_traits::Float;
  327. /// use std::f64;
  328. ///
  329. /// let nan: f64 = f64::NAN;
  330. ///
  331. /// let f = 7.0;
  332. /// let g = -7.0;
  333. ///
  334. /// assert!(!f.is_sign_negative());
  335. /// assert!(g.is_sign_negative());
  336. /// assert!(!nan.is_sign_negative());
  337. /// ```
  338. fn is_sign_negative(self) -> bool;
  339. /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
  340. /// error. This produces a more accurate result with better performance than
  341. /// a separate multiplication operation followed by an add.
  342. ///
  343. /// ```
  344. /// use num_traits::Float;
  345. ///
  346. /// let m = 10.0;
  347. /// let x = 4.0;
  348. /// let b = 60.0;
  349. ///
  350. /// // 100.0
  351. /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
  352. ///
  353. /// assert!(abs_difference < 1e-10);
  354. /// ```
  355. fn mul_add(self, a: Self, b: Self) -> Self;
  356. /// Take the reciprocal (inverse) of a number, `1/x`.
  357. ///
  358. /// ```
  359. /// use num_traits::Float;
  360. ///
  361. /// let x = 2.0;
  362. /// let abs_difference = (x.recip() - (1.0/x)).abs();
  363. ///
  364. /// assert!(abs_difference < 1e-10);
  365. /// ```
  366. fn recip(self) -> Self;
  367. /// Raise a number to an integer power.
  368. ///
  369. /// Using this function is generally faster than using `powf`
  370. ///
  371. /// ```
  372. /// use num_traits::Float;
  373. ///
  374. /// let x = 2.0;
  375. /// let abs_difference = (x.powi(2) - x*x).abs();
  376. ///
  377. /// assert!(abs_difference < 1e-10);
  378. /// ```
  379. fn powi(self, n: i32) -> Self;
  380. /// Raise a number to a floating point power.
  381. ///
  382. /// ```
  383. /// use num_traits::Float;
  384. ///
  385. /// let x = 2.0;
  386. /// let abs_difference = (x.powf(2.0) - x*x).abs();
  387. ///
  388. /// assert!(abs_difference < 1e-10);
  389. /// ```
  390. fn powf(self, n: Self) -> Self;
  391. /// Take the square root of a number.
  392. ///
  393. /// Returns NaN if `self` is a negative number.
  394. ///
  395. /// ```
  396. /// use num_traits::Float;
  397. ///
  398. /// let positive = 4.0;
  399. /// let negative = -4.0;
  400. ///
  401. /// let abs_difference = (positive.sqrt() - 2.0).abs();
  402. ///
  403. /// assert!(abs_difference < 1e-10);
  404. /// assert!(negative.sqrt().is_nan());
  405. /// ```
  406. fn sqrt(self) -> Self;
  407. /// Returns `e^(self)`, (the exponential function).
  408. ///
  409. /// ```
  410. /// use num_traits::Float;
  411. ///
  412. /// let one = 1.0;
  413. /// // e^1
  414. /// let e = one.exp();
  415. ///
  416. /// // ln(e) - 1 == 0
  417. /// let abs_difference = (e.ln() - 1.0).abs();
  418. ///
  419. /// assert!(abs_difference < 1e-10);
  420. /// ```
  421. fn exp(self) -> Self;
  422. /// Returns `2^(self)`.
  423. ///
  424. /// ```
  425. /// use num_traits::Float;
  426. ///
  427. /// let f = 2.0;
  428. ///
  429. /// // 2^2 - 4 == 0
  430. /// let abs_difference = (f.exp2() - 4.0).abs();
  431. ///
  432. /// assert!(abs_difference < 1e-10);
  433. /// ```
  434. fn exp2(self) -> Self;
  435. /// Returns the natural logarithm of the number.
  436. ///
  437. /// ```
  438. /// use num_traits::Float;
  439. ///
  440. /// let one = 1.0;
  441. /// // e^1
  442. /// let e = one.exp();
  443. ///
  444. /// // ln(e) - 1 == 0
  445. /// let abs_difference = (e.ln() - 1.0).abs();
  446. ///
  447. /// assert!(abs_difference < 1e-10);
  448. /// ```
  449. fn ln(self) -> Self;
  450. /// Returns the logarithm of the number with respect to an arbitrary base.
  451. ///
  452. /// ```
  453. /// use num_traits::Float;
  454. ///
  455. /// let ten = 10.0;
  456. /// let two = 2.0;
  457. ///
  458. /// // log10(10) - 1 == 0
  459. /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
  460. ///
  461. /// // log2(2) - 1 == 0
  462. /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
  463. ///
  464. /// assert!(abs_difference_10 < 1e-10);
  465. /// assert!(abs_difference_2 < 1e-10);
  466. /// ```
  467. fn log(self, base: Self) -> Self;
  468. /// Returns the base 2 logarithm of the number.
  469. ///
  470. /// ```
  471. /// use num_traits::Float;
  472. ///
  473. /// let two = 2.0;
  474. ///
  475. /// // log2(2) - 1 == 0
  476. /// let abs_difference = (two.log2() - 1.0).abs();
  477. ///
  478. /// assert!(abs_difference < 1e-10);
  479. /// ```
  480. fn log2(self) -> Self;
  481. /// Returns the base 10 logarithm of the number.
  482. ///
  483. /// ```
  484. /// use num_traits::Float;
  485. ///
  486. /// let ten = 10.0;
  487. ///
  488. /// // log10(10) - 1 == 0
  489. /// let abs_difference = (ten.log10() - 1.0).abs();
  490. ///
  491. /// assert!(abs_difference < 1e-10);
  492. /// ```
  493. fn log10(self) -> Self;
  494. /// Converts radians to degrees.
  495. ///
  496. /// ```
  497. /// use std::f64::consts;
  498. ///
  499. /// let angle = consts::PI;
  500. ///
  501. /// let abs_difference = (angle.to_degrees() - 180.0).abs();
  502. ///
  503. /// assert!(abs_difference < 1e-10);
  504. /// ```
  505. #[inline]
  506. fn to_degrees(self) -> Self {
  507. let halfpi = Self::zero().acos();
  508. let ninety = Self::from(90u8).unwrap();
  509. self * ninety / halfpi
  510. }
  511. /// Converts degrees to radians.
  512. ///
  513. /// ```
  514. /// use std::f64::consts;
  515. ///
  516. /// let angle = 180.0_f64;
  517. ///
  518. /// let abs_difference = (angle.to_radians() - consts::PI).abs();
  519. ///
  520. /// assert!(abs_difference < 1e-10);
  521. /// ```
  522. #[inline]
  523. fn to_radians(self) -> Self {
  524. let halfpi = Self::zero().acos();
  525. let ninety = Self::from(90u8).unwrap();
  526. self * halfpi / ninety
  527. }
  528. /// Returns the maximum of the two numbers.
  529. ///
  530. /// ```
  531. /// use num_traits::Float;
  532. ///
  533. /// let x = 1.0;
  534. /// let y = 2.0;
  535. ///
  536. /// assert_eq!(x.max(y), y);
  537. /// ```
  538. fn max(self, other: Self) -> Self;
  539. /// Returns the minimum of the two numbers.
  540. ///
  541. /// ```
  542. /// use num_traits::Float;
  543. ///
  544. /// let x = 1.0;
  545. /// let y = 2.0;
  546. ///
  547. /// assert_eq!(x.min(y), x);
  548. /// ```
  549. fn min(self, other: Self) -> Self;
  550. /// The positive difference of two numbers.
  551. ///
  552. /// * If `self <= other`: `0:0`
  553. /// * Else: `self - other`
  554. ///
  555. /// ```
  556. /// use num_traits::Float;
  557. ///
  558. /// let x = 3.0;
  559. /// let y = -3.0;
  560. ///
  561. /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
  562. /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
  563. ///
  564. /// assert!(abs_difference_x < 1e-10);
  565. /// assert!(abs_difference_y < 1e-10);
  566. /// ```
  567. fn abs_sub(self, other: Self) -> Self;
  568. /// Take the cubic root of a number.
  569. ///
  570. /// ```
  571. /// use num_traits::Float;
  572. ///
  573. /// let x = 8.0;
  574. ///
  575. /// // x^(1/3) - 2 == 0
  576. /// let abs_difference = (x.cbrt() - 2.0).abs();
  577. ///
  578. /// assert!(abs_difference < 1e-10);
  579. /// ```
  580. fn cbrt(self) -> Self;
  581. /// Calculate the length of the hypotenuse of a right-angle triangle given
  582. /// legs of length `x` and `y`.
  583. ///
  584. /// ```
  585. /// use num_traits::Float;
  586. ///
  587. /// let x = 2.0;
  588. /// let y = 3.0;
  589. ///
  590. /// // sqrt(x^2 + y^2)
  591. /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
  592. ///
  593. /// assert!(abs_difference < 1e-10);
  594. /// ```
  595. fn hypot(self, other: Self) -> Self;
  596. /// Computes the sine of a number (in radians).
  597. ///
  598. /// ```
  599. /// use num_traits::Float;
  600. /// use std::f64;
  601. ///
  602. /// let x = f64::consts::PI/2.0;
  603. ///
  604. /// let abs_difference = (x.sin() - 1.0).abs();
  605. ///
  606. /// assert!(abs_difference < 1e-10);
  607. /// ```
  608. fn sin(self) -> Self;
  609. /// Computes the cosine of a number (in radians).
  610. ///
  611. /// ```
  612. /// use num_traits::Float;
  613. /// use std::f64;
  614. ///
  615. /// let x = 2.0*f64::consts::PI;
  616. ///
  617. /// let abs_difference = (x.cos() - 1.0).abs();
  618. ///
  619. /// assert!(abs_difference < 1e-10);
  620. /// ```
  621. fn cos(self) -> Self;
  622. /// Computes the tangent of a number (in radians).
  623. ///
  624. /// ```
  625. /// use num_traits::Float;
  626. /// use std::f64;
  627. ///
  628. /// let x = f64::consts::PI/4.0;
  629. /// let abs_difference = (x.tan() - 1.0).abs();
  630. ///
  631. /// assert!(abs_difference < 1e-14);
  632. /// ```
  633. fn tan(self) -> Self;
  634. /// Computes the arcsine of a number. Return value is in radians in
  635. /// the range [-pi/2, pi/2] or NaN if the number is outside the range
  636. /// [-1, 1].
  637. ///
  638. /// ```
  639. /// use num_traits::Float;
  640. /// use std::f64;
  641. ///
  642. /// let f = f64::consts::PI / 2.0;
  643. ///
  644. /// // asin(sin(pi/2))
  645. /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
  646. ///
  647. /// assert!(abs_difference < 1e-10);
  648. /// ```
  649. fn asin(self) -> Self;
  650. /// Computes the arccosine of a number. Return value is in radians in
  651. /// the range [0, pi] or NaN if the number is outside the range
  652. /// [-1, 1].
  653. ///
  654. /// ```
  655. /// use num_traits::Float;
  656. /// use std::f64;
  657. ///
  658. /// let f = f64::consts::PI / 4.0;
  659. ///
  660. /// // acos(cos(pi/4))
  661. /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
  662. ///
  663. /// assert!(abs_difference < 1e-10);
  664. /// ```
  665. fn acos(self) -> Self;
  666. /// Computes the arctangent of a number. Return value is in radians in the
  667. /// range [-pi/2, pi/2];
  668. ///
  669. /// ```
  670. /// use num_traits::Float;
  671. ///
  672. /// let f = 1.0;
  673. ///
  674. /// // atan(tan(1))
  675. /// let abs_difference = (f.tan().atan() - 1.0).abs();
  676. ///
  677. /// assert!(abs_difference < 1e-10);
  678. /// ```
  679. fn atan(self) -> Self;
  680. /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
  681. ///
  682. /// * `x = 0`, `y = 0`: `0`
  683. /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
  684. /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
  685. /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
  686. ///
  687. /// ```
  688. /// use num_traits::Float;
  689. /// use std::f64;
  690. ///
  691. /// let pi = f64::consts::PI;
  692. /// // All angles from horizontal right (+x)
  693. /// // 45 deg counter-clockwise
  694. /// let x1 = 3.0;
  695. /// let y1 = -3.0;
  696. ///
  697. /// // 135 deg clockwise
  698. /// let x2 = -3.0;
  699. /// let y2 = 3.0;
  700. ///
  701. /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
  702. /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
  703. ///
  704. /// assert!(abs_difference_1 < 1e-10);
  705. /// assert!(abs_difference_2 < 1e-10);
  706. /// ```
  707. fn atan2(self, other: Self) -> Self;
  708. /// Simultaneously computes the sine and cosine of the number, `x`. Returns
  709. /// `(sin(x), cos(x))`.
  710. ///
  711. /// ```
  712. /// use num_traits::Float;
  713. /// use std::f64;
  714. ///
  715. /// let x = f64::consts::PI/4.0;
  716. /// let f = x.sin_cos();
  717. ///
  718. /// let abs_difference_0 = (f.0 - x.sin()).abs();
  719. /// let abs_difference_1 = (f.1 - x.cos()).abs();
  720. ///
  721. /// assert!(abs_difference_0 < 1e-10);
  722. /// assert!(abs_difference_0 < 1e-10);
  723. /// ```
  724. fn sin_cos(self) -> (Self, Self);
  725. /// Returns `e^(self) - 1` in a way that is accurate even if the
  726. /// number is close to zero.
  727. ///
  728. /// ```
  729. /// use num_traits::Float;
  730. ///
  731. /// let x = 7.0;
  732. ///
  733. /// // e^(ln(7)) - 1
  734. /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
  735. ///
  736. /// assert!(abs_difference < 1e-10);
  737. /// ```
  738. fn exp_m1(self) -> Self;
  739. /// Returns `ln(1+n)` (natural logarithm) more accurately than if
  740. /// the operations were performed separately.
  741. ///
  742. /// ```
  743. /// use num_traits::Float;
  744. /// use std::f64;
  745. ///
  746. /// let x = f64::consts::E - 1.0;
  747. ///
  748. /// // ln(1 + (e - 1)) == ln(e) == 1
  749. /// let abs_difference = (x.ln_1p() - 1.0).abs();
  750. ///
  751. /// assert!(abs_difference < 1e-10);
  752. /// ```
  753. fn ln_1p(self) -> Self;
  754. /// Hyperbolic sine function.
  755. ///
  756. /// ```
  757. /// use num_traits::Float;
  758. /// use std::f64;
  759. ///
  760. /// let e = f64::consts::E;
  761. /// let x = 1.0;
  762. ///
  763. /// let f = x.sinh();
  764. /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
  765. /// let g = (e*e - 1.0)/(2.0*e);
  766. /// let abs_difference = (f - g).abs();
  767. ///
  768. /// assert!(abs_difference < 1e-10);
  769. /// ```
  770. fn sinh(self) -> Self;
  771. /// Hyperbolic cosine function.
  772. ///
  773. /// ```
  774. /// use num_traits::Float;
  775. /// use std::f64;
  776. ///
  777. /// let e = f64::consts::E;
  778. /// let x = 1.0;
  779. /// let f = x.cosh();
  780. /// // Solving cosh() at 1 gives this result
  781. /// let g = (e*e + 1.0)/(2.0*e);
  782. /// let abs_difference = (f - g).abs();
  783. ///
  784. /// // Same result
  785. /// assert!(abs_difference < 1.0e-10);
  786. /// ```
  787. fn cosh(self) -> Self;
  788. /// Hyperbolic tangent function.
  789. ///
  790. /// ```
  791. /// use num_traits::Float;
  792. /// use std::f64;
  793. ///
  794. /// let e = f64::consts::E;
  795. /// let x = 1.0;
  796. ///
  797. /// let f = x.tanh();
  798. /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
  799. /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
  800. /// let abs_difference = (f - g).abs();
  801. ///
  802. /// assert!(abs_difference < 1.0e-10);
  803. /// ```
  804. fn tanh(self) -> Self;
  805. /// Inverse hyperbolic sine function.
  806. ///
  807. /// ```
  808. /// use num_traits::Float;
  809. ///
  810. /// let x = 1.0;
  811. /// let f = x.sinh().asinh();
  812. ///
  813. /// let abs_difference = (f - x).abs();
  814. ///
  815. /// assert!(abs_difference < 1.0e-10);
  816. /// ```
  817. fn asinh(self) -> Self;
  818. /// Inverse hyperbolic cosine function.
  819. ///
  820. /// ```
  821. /// use num_traits::Float;
  822. ///
  823. /// let x = 1.0;
  824. /// let f = x.cosh().acosh();
  825. ///
  826. /// let abs_difference = (f - x).abs();
  827. ///
  828. /// assert!(abs_difference < 1.0e-10);
  829. /// ```
  830. fn acosh(self) -> Self;
  831. /// Inverse hyperbolic tangent function.
  832. ///
  833. /// ```
  834. /// use num_traits::Float;
  835. /// use std::f64;
  836. ///
  837. /// let e = f64::consts::E;
  838. /// let f = e.tanh().atanh();
  839. ///
  840. /// let abs_difference = (f - e).abs();
  841. ///
  842. /// assert!(abs_difference < 1.0e-10);
  843. /// ```
  844. fn atanh(self) -> Self;
  845. /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
  846. /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
  847. /// The floating point encoding is documented in the [Reference][floating-point].
  848. ///
  849. /// ```
  850. /// use num_traits::Float;
  851. ///
  852. /// let num = 2.0f32;
  853. ///
  854. /// // (8388608, -22, 1)
  855. /// let (mantissa, exponent, sign) = Float::integer_decode(num);
  856. /// let sign_f = sign as f32;
  857. /// let mantissa_f = mantissa as f32;
  858. /// let exponent_f = num.powf(exponent as f32);
  859. ///
  860. /// // 1 * 8388608 * 2^(-22) == 2
  861. /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
  862. ///
  863. /// assert!(abs_difference < 1e-10);
  864. /// ```
  865. /// [floating-point]: ../../../../../reference.html#machine-types
  866. fn integer_decode(self) -> (u64, i16, i8);
  867. }
  868. macro_rules! float_impl {
  869. ($T:ident $decode:ident) => (
  870. impl Float for $T {
  871. #[inline]
  872. fn nan() -> Self {
  873. ::std::$T::NAN
  874. }
  875. #[inline]
  876. fn infinity() -> Self {
  877. ::std::$T::INFINITY
  878. }
  879. #[inline]
  880. fn neg_infinity() -> Self {
  881. ::std::$T::NEG_INFINITY
  882. }
  883. #[inline]
  884. fn neg_zero() -> Self {
  885. -0.0
  886. }
  887. #[inline]
  888. fn min_value() -> Self {
  889. ::std::$T::MIN
  890. }
  891. #[inline]
  892. fn min_positive_value() -> Self {
  893. ::std::$T::MIN_POSITIVE
  894. }
  895. #[inline]
  896. fn epsilon() -> Self {
  897. ::std::$T::EPSILON
  898. }
  899. #[inline]
  900. fn max_value() -> Self {
  901. ::std::$T::MAX
  902. }
  903. #[inline]
  904. fn is_nan(self) -> bool {
  905. <$T>::is_nan(self)
  906. }
  907. #[inline]
  908. fn is_infinite(self) -> bool {
  909. <$T>::is_infinite(self)
  910. }
  911. #[inline]
  912. fn is_finite(self) -> bool {
  913. <$T>::is_finite(self)
  914. }
  915. #[inline]
  916. fn is_normal(self) -> bool {
  917. <$T>::is_normal(self)
  918. }
  919. #[inline]
  920. fn classify(self) -> FpCategory {
  921. <$T>::classify(self)
  922. }
  923. #[inline]
  924. fn floor(self) -> Self {
  925. <$T>::floor(self)
  926. }
  927. #[inline]
  928. fn ceil(self) -> Self {
  929. <$T>::ceil(self)
  930. }
  931. #[inline]
  932. fn round(self) -> Self {
  933. <$T>::round(self)
  934. }
  935. #[inline]
  936. fn trunc(self) -> Self {
  937. <$T>::trunc(self)
  938. }
  939. #[inline]
  940. fn fract(self) -> Self {
  941. <$T>::fract(self)
  942. }
  943. #[inline]
  944. fn abs(self) -> Self {
  945. <$T>::abs(self)
  946. }
  947. #[inline]
  948. fn signum(self) -> Self {
  949. <$T>::signum(self)
  950. }
  951. #[inline]
  952. fn is_sign_positive(self) -> bool {
  953. <$T>::is_sign_positive(self)
  954. }
  955. #[inline]
  956. fn is_sign_negative(self) -> bool {
  957. <$T>::is_sign_negative(self)
  958. }
  959. #[inline]
  960. fn mul_add(self, a: Self, b: Self) -> Self {
  961. <$T>::mul_add(self, a, b)
  962. }
  963. #[inline]
  964. fn recip(self) -> Self {
  965. <$T>::recip(self)
  966. }
  967. #[inline]
  968. fn powi(self, n: i32) -> Self {
  969. <$T>::powi(self, n)
  970. }
  971. #[inline]
  972. fn powf(self, n: Self) -> Self {
  973. <$T>::powf(self, n)
  974. }
  975. #[inline]
  976. fn sqrt(self) -> Self {
  977. <$T>::sqrt(self)
  978. }
  979. #[inline]
  980. fn exp(self) -> Self {
  981. <$T>::exp(self)
  982. }
  983. #[inline]
  984. fn exp2(self) -> Self {
  985. <$T>::exp2(self)
  986. }
  987. #[inline]
  988. fn ln(self) -> Self {
  989. <$T>::ln(self)
  990. }
  991. #[inline]
  992. fn log(self, base: Self) -> Self {
  993. <$T>::log(self, base)
  994. }
  995. #[inline]
  996. fn log2(self) -> Self {
  997. <$T>::log2(self)
  998. }
  999. #[inline]
  1000. fn log10(self) -> Self {
  1001. <$T>::log10(self)
  1002. }
  1003. #[inline]
  1004. fn to_degrees(self) -> Self {
  1005. // NB: `f32` didn't stabilize this until 1.7
  1006. // <$T>::to_degrees(self)
  1007. self * (180. / ::std::$T::consts::PI)
  1008. }
  1009. #[inline]
  1010. fn to_radians(self) -> Self {
  1011. // NB: `f32` didn't stabilize this until 1.7
  1012. // <$T>::to_radians(self)
  1013. self * (::std::$T::consts::PI / 180.)
  1014. }
  1015. #[inline]
  1016. fn max(self, other: Self) -> Self {
  1017. <$T>::max(self, other)
  1018. }
  1019. #[inline]
  1020. fn min(self, other: Self) -> Self {
  1021. <$T>::min(self, other)
  1022. }
  1023. #[inline]
  1024. #[allow(deprecated)]
  1025. fn abs_sub(self, other: Self) -> Self {
  1026. <$T>::abs_sub(self, other)
  1027. }
  1028. #[inline]
  1029. fn cbrt(self) -> Self {
  1030. <$T>::cbrt(self)
  1031. }
  1032. #[inline]
  1033. fn hypot(self, other: Self) -> Self {
  1034. <$T>::hypot(self, other)
  1035. }
  1036. #[inline]
  1037. fn sin(self) -> Self {
  1038. <$T>::sin(self)
  1039. }
  1040. #[inline]
  1041. fn cos(self) -> Self {
  1042. <$T>::cos(self)
  1043. }
  1044. #[inline]
  1045. fn tan(self) -> Self {
  1046. <$T>::tan(self)
  1047. }
  1048. #[inline]
  1049. fn asin(self) -> Self {
  1050. <$T>::asin(self)
  1051. }
  1052. #[inline]
  1053. fn acos(self) -> Self {
  1054. <$T>::acos(self)
  1055. }
  1056. #[inline]
  1057. fn atan(self) -> Self {
  1058. <$T>::atan(self)
  1059. }
  1060. #[inline]
  1061. fn atan2(self, other: Self) -> Self {
  1062. <$T>::atan2(self, other)
  1063. }
  1064. #[inline]
  1065. fn sin_cos(self) -> (Self, Self) {
  1066. <$T>::sin_cos(self)
  1067. }
  1068. #[inline]
  1069. fn exp_m1(self) -> Self {
  1070. <$T>::exp_m1(self)
  1071. }
  1072. #[inline]
  1073. fn ln_1p(self) -> Self {
  1074. <$T>::ln_1p(self)
  1075. }
  1076. #[inline]
  1077. fn sinh(self) -> Self {
  1078. <$T>::sinh(self)
  1079. }
  1080. #[inline]
  1081. fn cosh(self) -> Self {
  1082. <$T>::cosh(self)
  1083. }
  1084. #[inline]
  1085. fn tanh(self) -> Self {
  1086. <$T>::tanh(self)
  1087. }
  1088. #[inline]
  1089. fn asinh(self) -> Self {
  1090. <$T>::asinh(self)
  1091. }
  1092. #[inline]
  1093. fn acosh(self) -> Self {
  1094. <$T>::acosh(self)
  1095. }
  1096. #[inline]
  1097. fn atanh(self) -> Self {
  1098. <$T>::atanh(self)
  1099. }
  1100. #[inline]
  1101. fn integer_decode(self) -> (u64, i16, i8) {
  1102. $decode(self)
  1103. }
  1104. }
  1105. )
  1106. }
  1107. fn integer_decode_f32(f: f32) -> (u64, i16, i8) {
  1108. let bits: u32 = unsafe { mem::transmute(f) };
  1109. let sign: i8 = if bits >> 31 == 0 {
  1110. 1
  1111. } else {
  1112. -1
  1113. };
  1114. let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
  1115. let mantissa = if exponent == 0 {
  1116. (bits & 0x7fffff) << 1
  1117. } else {
  1118. (bits & 0x7fffff) | 0x800000
  1119. };
  1120. // Exponent bias + mantissa shift
  1121. exponent -= 127 + 23;
  1122. (mantissa as u64, exponent, sign)
  1123. }
  1124. fn integer_decode_f64(f: f64) -> (u64, i16, i8) {
  1125. let bits: u64 = unsafe { mem::transmute(f) };
  1126. let sign: i8 = if bits >> 63 == 0 {
  1127. 1
  1128. } else {
  1129. -1
  1130. };
  1131. let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
  1132. let mantissa = if exponent == 0 {
  1133. (bits & 0xfffffffffffff) << 1
  1134. } else {
  1135. (bits & 0xfffffffffffff) | 0x10000000000000
  1136. };
  1137. // Exponent bias + mantissa shift
  1138. exponent -= 1023 + 52;
  1139. (mantissa, exponent, sign)
  1140. }
  1141. float_impl!(f32 integer_decode_f32);
  1142. float_impl!(f64 integer_decode_f64);
  1143. macro_rules! float_const_impl {
  1144. ($(#[$doc:meta] $constant:ident,)+) => (
  1145. #[allow(non_snake_case)]
  1146. pub trait FloatConst {
  1147. $(#[$doc] fn $constant() -> Self;)+
  1148. }
  1149. float_const_impl! { @float f32, $($constant,)+ }
  1150. float_const_impl! { @float f64, $($constant,)+ }
  1151. );
  1152. (@float $T:ident, $($constant:ident,)+) => (
  1153. impl FloatConst for $T {
  1154. $(
  1155. #[inline]
  1156. fn $constant() -> Self {
  1157. ::std::$T::consts::$constant
  1158. }
  1159. )+
  1160. }
  1161. );
  1162. }
  1163. float_const_impl! {
  1164. #[doc = "Return Euler’s number."]
  1165. E,
  1166. #[doc = "Return `1.0 / π`."]
  1167. FRAC_1_PI,
  1168. #[doc = "Return `1.0 / sqrt(2.0)`."]
  1169. FRAC_1_SQRT_2,
  1170. #[doc = "Return `2.0 / π`."]
  1171. FRAC_2_PI,
  1172. #[doc = "Return `2.0 / sqrt(π)`."]
  1173. FRAC_2_SQRT_PI,
  1174. #[doc = "Return `π / 2.0`."]
  1175. FRAC_PI_2,
  1176. #[doc = "Return `π / 3.0`."]
  1177. FRAC_PI_3,
  1178. #[doc = "Return `π / 4.0`."]
  1179. FRAC_PI_4,
  1180. #[doc = "Return `π / 6.0`."]
  1181. FRAC_PI_6,
  1182. #[doc = "Return `π / 8.0`."]
  1183. FRAC_PI_8,
  1184. #[doc = "Return `ln(10.0)`."]
  1185. LN_10,
  1186. #[doc = "Return `ln(2.0)`."]
  1187. LN_2,
  1188. #[doc = "Return `log10(e)`."]
  1189. LOG10_E,
  1190. #[doc = "Return `log2(e)`."]
  1191. LOG2_E,
  1192. #[doc = "Return Archimedes’ constant."]
  1193. PI,
  1194. #[doc = "Return `sqrt(2.0)`."]
  1195. SQRT_2,
  1196. }
  1197. #[cfg(test)]
  1198. mod tests {
  1199. use Float;
  1200. #[test]
  1201. fn convert_deg_rad() {
  1202. use std::f64::consts;
  1203. const DEG_RAD_PAIRS: [(f64, f64); 7] = [
  1204. (0.0, 0.),
  1205. (22.5, consts::FRAC_PI_8),
  1206. (30.0, consts::FRAC_PI_6),
  1207. (45.0, consts::FRAC_PI_4),
  1208. (60.0, consts::FRAC_PI_3),
  1209. (90.0, consts::FRAC_PI_2),
  1210. (180.0, consts::PI),
  1211. ];
  1212. for &(deg, rad) in &DEG_RAD_PAIRS {
  1213. assert!((Float::to_degrees(rad) - deg).abs() < 1e-6);
  1214. assert!((Float::to_radians(deg) - rad).abs() < 1e-6);
  1215. let (deg, rad) = (deg as f32, rad as f32);
  1216. assert!((Float::to_degrees(rad) - deg).abs() < 1e-6);
  1217. assert!((Float::to_radians(deg) - rad).abs() < 1e-6);
  1218. }
  1219. }
  1220. }