float.rs 39 KB

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