float.rs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  1. use core::mem;
  2. use core::ops::Neg;
  3. use core::num::FpCategory;
  4. use core::f32;
  5. use core::f64;
  6. use {Num, NumCast, ToPrimitive};
  7. /// Generic trait for floating point numbers that works with `no_std`.
  8. ///
  9. /// This trait implements a subset of the `Float` trait.
  10. pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
  11. /// Returns positive infinity.
  12. ///
  13. /// # Examples
  14. ///
  15. /// ```
  16. /// use num_traits::float::FloatCore;
  17. /// use std::{f32, f64};
  18. ///
  19. /// fn check<T: FloatCore>(x: T) {
  20. /// assert!(T::infinity() == x);
  21. /// }
  22. ///
  23. /// check(f32::INFINITY);
  24. /// check(f64::INFINITY);
  25. /// ```
  26. fn infinity() -> Self;
  27. /// Returns negative infinity.
  28. ///
  29. /// # Examples
  30. ///
  31. /// ```
  32. /// use num_traits::float::FloatCore;
  33. /// use std::{f32, f64};
  34. ///
  35. /// fn check<T: FloatCore>(x: T) {
  36. /// assert!(T::neg_infinity() == x);
  37. /// }
  38. ///
  39. /// check(f32::NEG_INFINITY);
  40. /// check(f64::NEG_INFINITY);
  41. /// ```
  42. fn neg_infinity() -> Self;
  43. /// Returns NaN.
  44. ///
  45. /// # Examples
  46. ///
  47. /// ```
  48. /// use num_traits::float::FloatCore;
  49. ///
  50. /// fn check<T: FloatCore>() {
  51. /// let n = T::nan();
  52. /// assert!(n != n);
  53. /// }
  54. ///
  55. /// check::<f32>();
  56. /// check::<f64>();
  57. /// ```
  58. fn nan() -> Self;
  59. /// Returns `-0.0`.
  60. ///
  61. /// # Examples
  62. ///
  63. /// ```
  64. /// use num_traits::float::FloatCore;
  65. /// use std::{f32, f64};
  66. ///
  67. /// fn check<T: FloatCore>(n: T) {
  68. /// let z = T::neg_zero();
  69. /// assert!(z.is_zero());
  70. /// assert!(T::one() / z == n);
  71. /// }
  72. ///
  73. /// check(f32::NEG_INFINITY);
  74. /// check(f64::NEG_INFINITY);
  75. /// ```
  76. fn neg_zero() -> Self;
  77. /// Returns the smallest finite value that this type can represent.
  78. ///
  79. /// # Examples
  80. ///
  81. /// ```
  82. /// use num_traits::float::FloatCore;
  83. /// use std::{f32, f64};
  84. ///
  85. /// fn check<T: FloatCore>(x: T) {
  86. /// assert!(T::min_value() == x);
  87. /// }
  88. ///
  89. /// check(f32::MIN);
  90. /// check(f64::MIN);
  91. /// ```
  92. fn min_value() -> Self;
  93. /// Returns the smallest positive, normalized value that this type can represent.
  94. ///
  95. /// # Examples
  96. ///
  97. /// ```
  98. /// use num_traits::float::FloatCore;
  99. /// use std::{f32, f64};
  100. ///
  101. /// fn check<T: FloatCore>(x: T) {
  102. /// assert!(T::min_positive_value() == x);
  103. /// }
  104. ///
  105. /// check(f32::MIN_POSITIVE);
  106. /// check(f64::MIN_POSITIVE);
  107. /// ```
  108. fn min_positive_value() -> Self;
  109. /// Returns epsilon, a small positive value.
  110. ///
  111. /// # Examples
  112. ///
  113. /// ```
  114. /// use num_traits::float::FloatCore;
  115. /// use std::{f32, f64};
  116. ///
  117. /// fn check<T: FloatCore>(x: T) {
  118. /// assert!(T::epsilon() == x);
  119. /// }
  120. ///
  121. /// check(f32::EPSILON);
  122. /// check(f64::EPSILON);
  123. /// ```
  124. fn epsilon() -> Self;
  125. /// Returns the largest finite value that this type can represent.
  126. ///
  127. /// # Examples
  128. ///
  129. /// ```
  130. /// use num_traits::float::FloatCore;
  131. /// use std::{f32, f64};
  132. ///
  133. /// fn check<T: FloatCore>(x: T) {
  134. /// assert!(T::max_value() == x);
  135. /// }
  136. ///
  137. /// check(f32::MAX);
  138. /// check(f64::MAX);
  139. /// ```
  140. fn max_value() -> Self;
  141. /// Returns `true` if the number is NaN.
  142. ///
  143. /// # Examples
  144. ///
  145. /// ```
  146. /// use num_traits::float::FloatCore;
  147. /// use std::{f32, f64};
  148. ///
  149. /// fn check<T: FloatCore>(x: T, p: bool) {
  150. /// assert!(x.is_nan() == p);
  151. /// }
  152. ///
  153. /// check(f32::NAN, true);
  154. /// check(f32::INFINITY, false);
  155. /// check(f64::NAN, true);
  156. /// check(0.0f64, false);
  157. /// ```
  158. #[inline]
  159. fn is_nan(self) -> bool {
  160. self != self
  161. }
  162. /// Returns `true` if the number is infinite.
  163. ///
  164. /// # Examples
  165. ///
  166. /// ```
  167. /// use num_traits::float::FloatCore;
  168. /// use std::{f32, f64};
  169. ///
  170. /// fn check<T: FloatCore>(x: T, p: bool) {
  171. /// assert!(x.is_infinite() == p);
  172. /// }
  173. ///
  174. /// check(f32::INFINITY, true);
  175. /// check(f32::NEG_INFINITY, true);
  176. /// check(f32::NAN, false);
  177. /// check(f64::INFINITY, true);
  178. /// check(f64::NEG_INFINITY, true);
  179. /// check(0.0f64, false);
  180. /// ```
  181. #[inline]
  182. fn is_infinite(self) -> bool {
  183. self == Self::infinity() || self == Self::neg_infinity()
  184. }
  185. /// Returns `true` if the number is neither infinite or NaN.
  186. ///
  187. /// # Examples
  188. ///
  189. /// ```
  190. /// use num_traits::float::FloatCore;
  191. /// use std::{f32, f64};
  192. ///
  193. /// fn check<T: FloatCore>(x: T, p: bool) {
  194. /// assert!(x.is_finite() == p);
  195. /// }
  196. ///
  197. /// check(f32::INFINITY, false);
  198. /// check(f32::MAX, true);
  199. /// check(f64::NEG_INFINITY, false);
  200. /// check(f64::MIN_POSITIVE, true);
  201. /// check(f64::NAN, false);
  202. /// ```
  203. #[inline]
  204. fn is_finite(self) -> bool {
  205. !(self.is_nan() || self.is_infinite())
  206. }
  207. /// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
  208. ///
  209. /// # Examples
  210. ///
  211. /// ```
  212. /// use num_traits::float::FloatCore;
  213. /// use std::{f32, f64};
  214. ///
  215. /// fn check<T: FloatCore>(x: T, p: bool) {
  216. /// assert!(x.is_normal() == p);
  217. /// }
  218. ///
  219. /// check(f32::INFINITY, false);
  220. /// check(f32::MAX, true);
  221. /// check(f64::NEG_INFINITY, false);
  222. /// check(f64::MIN_POSITIVE, true);
  223. /// check(0.0f64, false);
  224. /// ```
  225. #[inline]
  226. fn is_normal(self) -> bool {
  227. self.classify() == FpCategory::Normal
  228. }
  229. /// Returns the floating point category of the number. If only one property
  230. /// is going to be tested, it is generally faster to use the specific
  231. /// predicate instead.
  232. ///
  233. /// # Examples
  234. ///
  235. /// ```
  236. /// use num_traits::float::FloatCore;
  237. /// use std::{f32, f64};
  238. /// use std::num::FpCategory;
  239. ///
  240. /// fn check<T: FloatCore>(x: T, c: FpCategory) {
  241. /// assert!(x.classify() == c);
  242. /// }
  243. ///
  244. /// check(f32::INFINITY, FpCategory::Infinite);
  245. /// check(f32::MAX, FpCategory::Normal);
  246. /// check(f64::NAN, FpCategory::Nan);
  247. /// check(f64::MIN_POSITIVE, FpCategory::Normal);
  248. /// check(f64::MIN_POSITIVE / 2.0, FpCategory::Subnormal);
  249. /// check(0.0f64, FpCategory::Zero);
  250. /// ```
  251. fn classify(self) -> FpCategory;
  252. /// Returns the largest integer less than or equal to a number.
  253. ///
  254. /// # Examples
  255. ///
  256. /// ```
  257. /// use num_traits::float::FloatCore;
  258. /// use std::{f32, f64};
  259. ///
  260. /// fn check<T: FloatCore>(x: T, y: T) {
  261. /// assert!(x.floor() == y);
  262. /// }
  263. ///
  264. /// check(f32::INFINITY, f32::INFINITY);
  265. /// check(0.9f32, 0.0);
  266. /// check(1.0f32, 1.0);
  267. /// check(1.1f32, 1.0);
  268. /// check(-0.0f64, 0.0);
  269. /// check(-0.9f64, -1.0);
  270. /// check(-1.0f64, -1.0);
  271. /// check(-1.1f64, -2.0);
  272. /// check(f64::MIN, f64::MIN);
  273. /// ```
  274. #[inline]
  275. fn floor(self) -> Self {
  276. let f = self.fract();
  277. if f.is_nan() || f.is_zero() {
  278. self
  279. } else if self < Self::zero() {
  280. self - f - Self::one()
  281. } else {
  282. self - f
  283. }
  284. }
  285. /// Returns the smallest integer greater than or equal to a number.
  286. ///
  287. /// # Examples
  288. ///
  289. /// ```
  290. /// use num_traits::float::FloatCore;
  291. /// use std::{f32, f64};
  292. ///
  293. /// fn check<T: FloatCore>(x: T, y: T) {
  294. /// assert!(x.ceil() == y);
  295. /// }
  296. ///
  297. /// check(f32::INFINITY, f32::INFINITY);
  298. /// check(0.9f32, 1.0);
  299. /// check(1.0f32, 1.0);
  300. /// check(1.1f32, 2.0);
  301. /// check(-0.0f64, 0.0);
  302. /// check(-0.9f64, -0.0);
  303. /// check(-1.0f64, -1.0);
  304. /// check(-1.1f64, -1.0);
  305. /// check(f64::MIN, f64::MIN);
  306. /// ```
  307. #[inline]
  308. fn ceil(self) -> Self {
  309. let f = self.fract();
  310. if f.is_nan() || f.is_zero() {
  311. self
  312. } else if self > Self::zero() {
  313. self - f + Self::one()
  314. } else {
  315. self - f
  316. }
  317. }
  318. /// Returns the nearest integer to a number. Round half-way cases away from `0.0`.
  319. ///
  320. /// # Examples
  321. ///
  322. /// ```
  323. /// use num_traits::float::FloatCore;
  324. /// use std::{f32, f64};
  325. ///
  326. /// fn check<T: FloatCore>(x: T, y: T) {
  327. /// assert!(x.round() == y);
  328. /// }
  329. ///
  330. /// check(f32::INFINITY, f32::INFINITY);
  331. /// check(0.4f32, 0.0);
  332. /// check(0.5f32, 1.0);
  333. /// check(0.6f32, 1.0);
  334. /// check(-0.4f64, 0.0);
  335. /// check(-0.5f64, -1.0);
  336. /// check(-0.6f64, -1.0);
  337. /// check(f64::MIN, f64::MIN);
  338. /// ```
  339. #[inline]
  340. fn round(self) -> Self {
  341. let one = Self::one();
  342. let h = Self::from(0.5).expect("Unable to cast from 0.5");
  343. let f = self.fract();
  344. if f.is_nan() || f.is_zero() {
  345. self
  346. } else if self > Self::zero() {
  347. if f < h {
  348. self - f
  349. } else {
  350. self - f + one
  351. }
  352. } else {
  353. if -f < h {
  354. self - f
  355. } else {
  356. self - f - one
  357. }
  358. }
  359. }
  360. /// Return the integer part of a number.
  361. ///
  362. /// # Examples
  363. ///
  364. /// ```
  365. /// use num_traits::float::FloatCore;
  366. /// use std::{f32, f64};
  367. ///
  368. /// fn check<T: FloatCore>(x: T, y: T) {
  369. /// assert!(x.trunc() == y);
  370. /// }
  371. ///
  372. /// check(f32::INFINITY, f32::INFINITY);
  373. /// check(0.9f32, 0.0);
  374. /// check(1.0f32, 1.0);
  375. /// check(1.1f32, 1.0);
  376. /// check(-0.0f64, 0.0);
  377. /// check(-0.9f64, -0.0);
  378. /// check(-1.0f64, -1.0);
  379. /// check(-1.1f64, -1.0);
  380. /// check(f64::MIN, f64::MIN);
  381. /// ```
  382. #[inline]
  383. fn trunc(self) -> Self {
  384. let f = self.fract();
  385. if f.is_nan() {
  386. self
  387. } else {
  388. self - f
  389. }
  390. }
  391. /// Returns the fractional part of a number.
  392. ///
  393. /// # Examples
  394. ///
  395. /// ```
  396. /// use num_traits::float::FloatCore;
  397. /// use std::{f32, f64};
  398. ///
  399. /// fn check<T: FloatCore>(x: T, y: T) {
  400. /// assert!(x.fract() == y);
  401. /// }
  402. ///
  403. /// check(f32::MAX, 0.0);
  404. /// check(0.75f32, 0.75);
  405. /// check(1.0f32, 0.0);
  406. /// check(1.25f32, 0.25);
  407. /// check(-0.0f64, 0.0);
  408. /// check(-0.75f64, -0.75);
  409. /// check(-1.0f64, 0.0);
  410. /// check(-1.25f64, -0.25);
  411. /// check(f64::MIN, 0.0);
  412. /// ```
  413. #[inline]
  414. fn fract(self) -> Self {
  415. if self.is_zero() {
  416. Self::zero()
  417. } else {
  418. self % Self::one()
  419. }
  420. }
  421. /// Computes the absolute value of `self`. Returns `FloatCore::nan()` if the
  422. /// number is `FloatCore::nan()`.
  423. ///
  424. /// # Examples
  425. ///
  426. /// ```
  427. /// use num_traits::float::FloatCore;
  428. /// use std::{f32, f64};
  429. ///
  430. /// fn check<T: FloatCore>(x: T, y: T) {
  431. /// assert!(x.abs() == y);
  432. /// }
  433. ///
  434. /// check(f32::INFINITY, f32::INFINITY);
  435. /// check(1.0f32, 1.0);
  436. /// check(0.0f64, 0.0);
  437. /// check(-0.0f64, 0.0);
  438. /// check(-1.0f64, 1.0);
  439. /// check(f64::MIN, f64::MAX);
  440. /// ```
  441. #[inline]
  442. fn abs(self) -> Self {
  443. if self.is_sign_positive() {
  444. return self;
  445. }
  446. if self.is_sign_negative() {
  447. return -self;
  448. }
  449. Self::nan()
  450. }
  451. /// Returns a number that represents the sign of `self`.
  452. ///
  453. /// - `1.0` if the number is positive, `+0.0` or `FloatCore::infinity()`
  454. /// - `-1.0` if the number is negative, `-0.0` or `FloatCore::neg_infinity()`
  455. /// - `FloatCore::nan()` if the number is `FloatCore::nan()`
  456. ///
  457. /// # Examples
  458. ///
  459. /// ```
  460. /// use num_traits::float::FloatCore;
  461. /// use std::{f32, f64};
  462. ///
  463. /// fn check<T: FloatCore>(x: T, y: T) {
  464. /// assert!(x.signum() == y);
  465. /// }
  466. ///
  467. /// check(f32::INFINITY, 1.0);
  468. /// check(3.0f32, 1.0);
  469. /// check(0.0f32, 1.0);
  470. /// check(-0.0f64, -1.0);
  471. /// check(-3.0f64, -1.0);
  472. /// check(f64::MIN, -1.0);
  473. /// ```
  474. #[inline]
  475. fn signum(self) -> Self {
  476. if self.is_nan() {
  477. Self::nan()
  478. } else if self.is_sign_negative() {
  479. -Self::one()
  480. } else {
  481. Self::one()
  482. }
  483. }
  484. /// Returns `true` if `self` is positive, including `+0.0` and
  485. /// `FloatCore::infinity()`, and since Rust 1.20 also
  486. /// `FloatCore::nan()`.
  487. ///
  488. /// # Examples
  489. ///
  490. /// ```
  491. /// use num_traits::float::FloatCore;
  492. /// use std::{f32, f64};
  493. ///
  494. /// fn check<T: FloatCore>(x: T, p: bool) {
  495. /// assert!(x.is_sign_positive() == p);
  496. /// }
  497. ///
  498. /// check(f32::INFINITY, true);
  499. /// check(f32::MAX, true);
  500. /// check(0.0f32, true);
  501. /// check(-0.0f64, false);
  502. /// check(f64::NEG_INFINITY, false);
  503. /// check(f64::MIN_POSITIVE, true);
  504. /// check(-f64::NAN, false);
  505. /// ```
  506. #[inline]
  507. fn is_sign_positive(self) -> bool {
  508. !self.is_sign_negative()
  509. }
  510. /// Returns `true` if `self` is negative, including `-0.0` and
  511. /// `FloatCore::neg_infinity()`, and since Rust 1.20 also
  512. /// `-FloatCore::nan()`.
  513. ///
  514. /// # Examples
  515. ///
  516. /// ```
  517. /// use num_traits::float::FloatCore;
  518. /// use std::{f32, f64};
  519. ///
  520. /// fn check<T: FloatCore>(x: T, p: bool) {
  521. /// assert!(x.is_sign_negative() == p);
  522. /// }
  523. ///
  524. /// check(f32::INFINITY, false);
  525. /// check(f32::MAX, false);
  526. /// check(0.0f32, false);
  527. /// check(-0.0f64, true);
  528. /// check(f64::NEG_INFINITY, true);
  529. /// check(f64::MIN_POSITIVE, false);
  530. /// check(f64::NAN, false);
  531. /// ```
  532. #[inline]
  533. fn is_sign_negative(self) -> bool {
  534. let (_, _, sign) = self.integer_decode();
  535. sign < 0
  536. }
  537. /// Returns the minimum of the two numbers.
  538. ///
  539. /// If one of the arguments is NaN, then the other argument is returned.
  540. ///
  541. /// # Examples
  542. ///
  543. /// ```
  544. /// use num_traits::float::FloatCore;
  545. /// use std::{f32, f64};
  546. ///
  547. /// fn check<T: FloatCore>(x: T, y: T, min: T) {
  548. /// assert!(x.min(y) == min);
  549. /// }
  550. ///
  551. /// check(1.0f32, 2.0, 1.0);
  552. /// check(f32::NAN, 2.0, 2.0);
  553. /// check(1.0f64, -2.0, -2.0);
  554. /// check(1.0f64, f64::NAN, 1.0);
  555. /// ```
  556. #[inline]
  557. fn min(self, other: Self) -> Self {
  558. if self.is_nan() {
  559. return other;
  560. }
  561. if other.is_nan() {
  562. return self;
  563. }
  564. if self < other { self } else { other }
  565. }
  566. /// Returns the maximum of the two numbers.
  567. ///
  568. /// If one of the arguments is NaN, then the other argument is returned.
  569. ///
  570. /// # Examples
  571. ///
  572. /// ```
  573. /// use num_traits::float::FloatCore;
  574. /// use std::{f32, f64};
  575. ///
  576. /// fn check<T: FloatCore>(x: T, y: T, min: T) {
  577. /// assert!(x.max(y) == min);
  578. /// }
  579. ///
  580. /// check(1.0f32, 2.0, 2.0);
  581. /// check(1.0f32, f32::NAN, 1.0);
  582. /// check(-1.0f64, 2.0, 2.0);
  583. /// check(-1.0f64, f64::NAN, -1.0);
  584. /// ```
  585. #[inline]
  586. fn max(self, other: Self) -> Self {
  587. if self.is_nan() {
  588. return other;
  589. }
  590. if other.is_nan() {
  591. return self;
  592. }
  593. if self > other { self } else { other }
  594. }
  595. /// Returns the reciprocal (multiplicative inverse) of the number.
  596. ///
  597. /// # Examples
  598. ///
  599. /// ```
  600. /// use num_traits::float::FloatCore;
  601. /// use std::{f32, f64};
  602. ///
  603. /// fn check<T: FloatCore>(x: T, y: T) {
  604. /// assert!(x.recip() == y);
  605. /// assert!(y.recip() == x);
  606. /// }
  607. ///
  608. /// check(f32::INFINITY, 0.0);
  609. /// check(2.0f32, 0.5);
  610. /// check(-0.25f64, -4.0);
  611. /// check(-0.0f64, f64::NEG_INFINITY);
  612. /// ```
  613. #[inline]
  614. fn recip(self) -> Self {
  615. Self::one() / self
  616. }
  617. /// Raise a number to an integer power.
  618. ///
  619. /// Using this function is generally faster than using `powf`
  620. ///
  621. /// # Examples
  622. ///
  623. /// ```
  624. /// use num_traits::float::FloatCore;
  625. ///
  626. /// fn check<T: FloatCore>(x: T, exp: i32, powi: T) {
  627. /// assert!(x.powi(exp) == powi);
  628. /// }
  629. ///
  630. /// check(9.0f32, 2, 81.0);
  631. /// check(1.0f32, -2, 1.0);
  632. /// check(10.0f64, 20, 1e20);
  633. /// check(4.0f64, -2, 0.0625);
  634. /// check(-1.0f64, std::i32::MIN, 1.0);
  635. /// ```
  636. #[inline]
  637. fn powi(mut self, mut exp: i32) -> Self {
  638. if exp < 0 {
  639. exp = exp.wrapping_neg();
  640. self = self.recip();
  641. }
  642. // It should always be possible to convert a positive `i32` to a `usize`.
  643. // Note, `i32::MIN` will wrap and still be negative, so we need to convert
  644. // to `u32` without sign-extension before growing to `usize`.
  645. super::pow(self, (exp as u32).to_usize().unwrap())
  646. }
  647. /// Converts to degrees, assuming the number is in radians.
  648. ///
  649. /// # Examples
  650. ///
  651. /// ```
  652. /// use num_traits::float::FloatCore;
  653. /// use std::{f32, f64};
  654. ///
  655. /// fn check<T: FloatCore>(rad: T, deg: T) {
  656. /// assert!(rad.to_degrees() == deg);
  657. /// }
  658. ///
  659. /// check(0.0f32, 0.0);
  660. /// check(f32::consts::PI, 180.0);
  661. /// check(f64::consts::FRAC_PI_4, 45.0);
  662. /// check(f64::INFINITY, f64::INFINITY);
  663. /// ```
  664. fn to_degrees(self) -> Self;
  665. /// Converts to radians, assuming the number is in degrees.
  666. ///
  667. /// # Examples
  668. ///
  669. /// ```
  670. /// use num_traits::float::FloatCore;
  671. /// use std::{f32, f64};
  672. ///
  673. /// fn check<T: FloatCore>(deg: T, rad: T) {
  674. /// assert!(deg.to_radians() == rad);
  675. /// }
  676. ///
  677. /// check(0.0f32, 0.0);
  678. /// check(180.0, f32::consts::PI);
  679. /// check(45.0, f64::consts::FRAC_PI_4);
  680. /// check(f64::INFINITY, f64::INFINITY);
  681. /// ```
  682. fn to_radians(self) -> Self;
  683. /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
  684. /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
  685. ///
  686. /// # Examples
  687. ///
  688. /// ```
  689. /// use num_traits::float::FloatCore;
  690. /// use std::{f32, f64};
  691. ///
  692. /// fn check<T: FloatCore>(x: T, m: u64, e: i16, s:i8) {
  693. /// let (mantissa, exponent, sign) = x.integer_decode();
  694. /// assert_eq!(mantissa, m);
  695. /// assert_eq!(exponent, e);
  696. /// assert_eq!(sign, s);
  697. /// }
  698. ///
  699. /// check(2.0f32, 1 << 23, -22, 1);
  700. /// check(-2.0f32, 1 << 23, -22, -1);
  701. /// check(f32::INFINITY, 1 << 23, 105, 1);
  702. /// check(f64::NEG_INFINITY, 1 << 52, 972, -1);
  703. /// ```
  704. fn integer_decode(self) -> (u64, i16, i8);
  705. }
  706. impl FloatCore for f32 {
  707. constant! {
  708. infinity() -> f32::INFINITY;
  709. neg_infinity() -> f32::NEG_INFINITY;
  710. nan() -> f32::NAN;
  711. neg_zero() -> -0.0;
  712. min_value() -> f32::MIN;
  713. min_positive_value() -> f32::MIN_POSITIVE;
  714. epsilon() -> f32::EPSILON;
  715. max_value() -> f32::MAX;
  716. }
  717. #[inline]
  718. fn integer_decode(self) -> (u64, i16, i8) {
  719. integer_decode_f32(self)
  720. }
  721. #[inline]
  722. #[cfg(not(feature = "std"))]
  723. fn classify(self) -> FpCategory {
  724. const EXP_MASK: u32 = 0x7f800000;
  725. const MAN_MASK: u32 = 0x007fffff;
  726. let bits: u32 = unsafe { mem::transmute(self) };
  727. match (bits & MAN_MASK, bits & EXP_MASK) {
  728. (0, 0) => FpCategory::Zero,
  729. (_, 0) => FpCategory::Subnormal,
  730. (0, EXP_MASK) => FpCategory::Infinite,
  731. (_, EXP_MASK) => FpCategory::Nan,
  732. _ => FpCategory::Normal,
  733. }
  734. }
  735. #[inline]
  736. #[cfg(not(feature = "std"))]
  737. fn to_degrees(self) -> Self {
  738. self * (180.0 / f32::consts::PI)
  739. }
  740. #[inline]
  741. #[cfg(not(feature = "std"))]
  742. fn to_radians(self) -> Self {
  743. self * (f32::consts::PI / 180.0)
  744. }
  745. #[cfg(feature = "std")]
  746. forward! {
  747. Self::is_nan(self) -> bool;
  748. Self::is_infinite(self) -> bool;
  749. Self::is_finite(self) -> bool;
  750. Self::is_normal(self) -> bool;
  751. Self::classify(self) -> FpCategory;
  752. Self::floor(self) -> Self;
  753. Self::ceil(self) -> Self;
  754. Self::round(self) -> Self;
  755. Self::trunc(self) -> Self;
  756. Self::fract(self) -> Self;
  757. Self::abs(self) -> Self;
  758. Self::signum(self) -> Self;
  759. Self::is_sign_positive(self) -> bool;
  760. Self::is_sign_negative(self) -> bool;
  761. Self::min(self, other: Self) -> Self;
  762. Self::max(self, other: Self) -> Self;
  763. Self::recip(self) -> Self;
  764. Self::powi(self, n: i32) -> Self;
  765. Self::to_degrees(self) -> Self;
  766. Self::to_radians(self) -> Self;
  767. }
  768. }
  769. impl FloatCore for f64 {
  770. constant! {
  771. infinity() -> f64::INFINITY;
  772. neg_infinity() -> f64::NEG_INFINITY;
  773. nan() -> f64::NAN;
  774. neg_zero() -> -0.0;
  775. min_value() -> f64::MIN;
  776. min_positive_value() -> f64::MIN_POSITIVE;
  777. epsilon() -> f64::EPSILON;
  778. max_value() -> f64::MAX;
  779. }
  780. #[inline]
  781. fn integer_decode(self) -> (u64, i16, i8) {
  782. integer_decode_f64(self)
  783. }
  784. #[inline]
  785. #[cfg(not(feature = "std"))]
  786. fn classify(self) -> FpCategory {
  787. const EXP_MASK: u64 = 0x7ff0000000000000;
  788. const MAN_MASK: u64 = 0x000fffffffffffff;
  789. let bits: u64 = unsafe { mem::transmute(self) };
  790. match (bits & MAN_MASK, bits & EXP_MASK) {
  791. (0, 0) => FpCategory::Zero,
  792. (_, 0) => FpCategory::Subnormal,
  793. (0, EXP_MASK) => FpCategory::Infinite,
  794. (_, EXP_MASK) => FpCategory::Nan,
  795. _ => FpCategory::Normal,
  796. }
  797. }
  798. #[inline]
  799. #[cfg(not(feature = "std"))]
  800. fn to_degrees(self) -> Self {
  801. self * (180.0 / f64::consts::PI)
  802. }
  803. #[inline]
  804. #[cfg(not(feature = "std"))]
  805. fn to_radians(self) -> Self {
  806. self * (f64::consts::PI / 180.0)
  807. }
  808. #[cfg(feature = "std")]
  809. forward! {
  810. Self::is_nan(self) -> bool;
  811. Self::is_infinite(self) -> bool;
  812. Self::is_finite(self) -> bool;
  813. Self::is_normal(self) -> bool;
  814. Self::classify(self) -> FpCategory;
  815. Self::floor(self) -> Self;
  816. Self::ceil(self) -> Self;
  817. Self::round(self) -> Self;
  818. Self::trunc(self) -> Self;
  819. Self::fract(self) -> Self;
  820. Self::abs(self) -> Self;
  821. Self::signum(self) -> Self;
  822. Self::is_sign_positive(self) -> bool;
  823. Self::is_sign_negative(self) -> bool;
  824. Self::min(self, other: Self) -> Self;
  825. Self::max(self, other: Self) -> Self;
  826. Self::recip(self) -> Self;
  827. Self::powi(self, n: i32) -> Self;
  828. Self::to_degrees(self) -> Self;
  829. Self::to_radians(self) -> Self;
  830. }
  831. }
  832. // FIXME: these doctests aren't actually helpful, because they're using and
  833. // testing the inherent methods directly, not going through `Float`.
  834. /// Generic trait for floating point numbers
  835. ///
  836. /// This trait is only available with the `std` feature.
  837. #[cfg(feature = "std")]
  838. pub trait Float
  839. : Num
  840. + Copy
  841. + NumCast
  842. + PartialOrd
  843. + Neg<Output = Self>
  844. {
  845. /// Returns the `NaN` value.
  846. ///
  847. /// ```
  848. /// use num_traits::Float;
  849. ///
  850. /// let nan: f32 = Float::nan();
  851. ///
  852. /// assert!(nan.is_nan());
  853. /// ```
  854. fn nan() -> Self;
  855. /// Returns the infinite value.
  856. ///
  857. /// ```
  858. /// use num_traits::Float;
  859. /// use std::f32;
  860. ///
  861. /// let infinity: f32 = Float::infinity();
  862. ///
  863. /// assert!(infinity.is_infinite());
  864. /// assert!(!infinity.is_finite());
  865. /// assert!(infinity > f32::MAX);
  866. /// ```
  867. fn infinity() -> Self;
  868. /// Returns the negative infinite value.
  869. ///
  870. /// ```
  871. /// use num_traits::Float;
  872. /// use std::f32;
  873. ///
  874. /// let neg_infinity: f32 = Float::neg_infinity();
  875. ///
  876. /// assert!(neg_infinity.is_infinite());
  877. /// assert!(!neg_infinity.is_finite());
  878. /// assert!(neg_infinity < f32::MIN);
  879. /// ```
  880. fn neg_infinity() -> Self;
  881. /// Returns `-0.0`.
  882. ///
  883. /// ```
  884. /// use num_traits::{Zero, Float};
  885. ///
  886. /// let inf: f32 = Float::infinity();
  887. /// let zero: f32 = Zero::zero();
  888. /// let neg_zero: f32 = Float::neg_zero();
  889. ///
  890. /// assert_eq!(zero, neg_zero);
  891. /// assert_eq!(7.0f32/inf, zero);
  892. /// assert_eq!(zero * 10.0, zero);
  893. /// ```
  894. fn neg_zero() -> Self;
  895. /// Returns the smallest finite value that this type can represent.
  896. ///
  897. /// ```
  898. /// use num_traits::Float;
  899. /// use std::f64;
  900. ///
  901. /// let x: f64 = Float::min_value();
  902. ///
  903. /// assert_eq!(x, f64::MIN);
  904. /// ```
  905. fn min_value() -> Self;
  906. /// Returns the smallest positive, normalized value that this type can represent.
  907. ///
  908. /// ```
  909. /// use num_traits::Float;
  910. /// use std::f64;
  911. ///
  912. /// let x: f64 = Float::min_positive_value();
  913. ///
  914. /// assert_eq!(x, f64::MIN_POSITIVE);
  915. /// ```
  916. fn min_positive_value() -> Self;
  917. /// Returns epsilon, a small positive value.
  918. ///
  919. /// ```
  920. /// use num_traits::Float;
  921. /// use std::f64;
  922. ///
  923. /// let x: f64 = Float::epsilon();
  924. ///
  925. /// assert_eq!(x, f64::EPSILON);
  926. /// ```
  927. ///
  928. /// # Panics
  929. ///
  930. /// The default implementation will panic if `f32::EPSILON` cannot
  931. /// be cast to `Self`.
  932. fn epsilon() -> Self {
  933. Self::from(f32::EPSILON).expect("Unable to cast from f32::EPSILON")
  934. }
  935. /// Returns the largest finite value that this type can represent.
  936. ///
  937. /// ```
  938. /// use num_traits::Float;
  939. /// use std::f64;
  940. ///
  941. /// let x: f64 = Float::max_value();
  942. /// assert_eq!(x, f64::MAX);
  943. /// ```
  944. fn max_value() -> Self;
  945. /// Returns `true` if this value is `NaN` and false otherwise.
  946. ///
  947. /// ```
  948. /// use num_traits::Float;
  949. /// use std::f64;
  950. ///
  951. /// let nan = f64::NAN;
  952. /// let f = 7.0;
  953. ///
  954. /// assert!(nan.is_nan());
  955. /// assert!(!f.is_nan());
  956. /// ```
  957. fn is_nan(self) -> bool;
  958. /// Returns `true` if this value is positive infinity or negative infinity and
  959. /// false otherwise.
  960. ///
  961. /// ```
  962. /// use num_traits::Float;
  963. /// use std::f32;
  964. ///
  965. /// let f = 7.0f32;
  966. /// let inf: f32 = Float::infinity();
  967. /// let neg_inf: f32 = Float::neg_infinity();
  968. /// let nan: f32 = f32::NAN;
  969. ///
  970. /// assert!(!f.is_infinite());
  971. /// assert!(!nan.is_infinite());
  972. ///
  973. /// assert!(inf.is_infinite());
  974. /// assert!(neg_inf.is_infinite());
  975. /// ```
  976. fn is_infinite(self) -> bool;
  977. /// Returns `true` if this number is neither infinite nor `NaN`.
  978. ///
  979. /// ```
  980. /// use num_traits::Float;
  981. /// use std::f32;
  982. ///
  983. /// let f = 7.0f32;
  984. /// let inf: f32 = Float::infinity();
  985. /// let neg_inf: f32 = Float::neg_infinity();
  986. /// let nan: f32 = f32::NAN;
  987. ///
  988. /// assert!(f.is_finite());
  989. ///
  990. /// assert!(!nan.is_finite());
  991. /// assert!(!inf.is_finite());
  992. /// assert!(!neg_inf.is_finite());
  993. /// ```
  994. fn is_finite(self) -> bool;
  995. /// Returns `true` if the number is neither zero, infinite,
  996. /// [subnormal][subnormal], or `NaN`.
  997. ///
  998. /// ```
  999. /// use num_traits::Float;
  1000. /// use std::f32;
  1001. ///
  1002. /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
  1003. /// let max = f32::MAX;
  1004. /// let lower_than_min = 1.0e-40_f32;
  1005. /// let zero = 0.0f32;
  1006. ///
  1007. /// assert!(min.is_normal());
  1008. /// assert!(max.is_normal());
  1009. ///
  1010. /// assert!(!zero.is_normal());
  1011. /// assert!(!f32::NAN.is_normal());
  1012. /// assert!(!f32::INFINITY.is_normal());
  1013. /// // Values between `0` and `min` are Subnormal.
  1014. /// assert!(!lower_than_min.is_normal());
  1015. /// ```
  1016. /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
  1017. fn is_normal(self) -> bool;
  1018. /// Returns the floating point category of the number. If only one property
  1019. /// is going to be tested, it is generally faster to use the specific
  1020. /// predicate instead.
  1021. ///
  1022. /// ```
  1023. /// use num_traits::Float;
  1024. /// use std::num::FpCategory;
  1025. /// use std::f32;
  1026. ///
  1027. /// let num = 12.4f32;
  1028. /// let inf = f32::INFINITY;
  1029. ///
  1030. /// assert_eq!(num.classify(), FpCategory::Normal);
  1031. /// assert_eq!(inf.classify(), FpCategory::Infinite);
  1032. /// ```
  1033. fn classify(self) -> FpCategory;
  1034. /// Returns the largest integer less than or equal to a number.
  1035. ///
  1036. /// ```
  1037. /// use num_traits::Float;
  1038. ///
  1039. /// let f = 3.99;
  1040. /// let g = 3.0;
  1041. ///
  1042. /// assert_eq!(f.floor(), 3.0);
  1043. /// assert_eq!(g.floor(), 3.0);
  1044. /// ```
  1045. fn floor(self) -> Self;
  1046. /// Returns the smallest integer greater than or equal to a number.
  1047. ///
  1048. /// ```
  1049. /// use num_traits::Float;
  1050. ///
  1051. /// let f = 3.01;
  1052. /// let g = 4.0;
  1053. ///
  1054. /// assert_eq!(f.ceil(), 4.0);
  1055. /// assert_eq!(g.ceil(), 4.0);
  1056. /// ```
  1057. fn ceil(self) -> Self;
  1058. /// Returns the nearest integer to a number. Round half-way cases away from
  1059. /// `0.0`.
  1060. ///
  1061. /// ```
  1062. /// use num_traits::Float;
  1063. ///
  1064. /// let f = 3.3;
  1065. /// let g = -3.3;
  1066. ///
  1067. /// assert_eq!(f.round(), 3.0);
  1068. /// assert_eq!(g.round(), -3.0);
  1069. /// ```
  1070. fn round(self) -> Self;
  1071. /// Return the integer part of a number.
  1072. ///
  1073. /// ```
  1074. /// use num_traits::Float;
  1075. ///
  1076. /// let f = 3.3;
  1077. /// let g = -3.7;
  1078. ///
  1079. /// assert_eq!(f.trunc(), 3.0);
  1080. /// assert_eq!(g.trunc(), -3.0);
  1081. /// ```
  1082. fn trunc(self) -> Self;
  1083. /// Returns the fractional part of a number.
  1084. ///
  1085. /// ```
  1086. /// use num_traits::Float;
  1087. ///
  1088. /// let x = 3.5;
  1089. /// let y = -3.5;
  1090. /// let abs_difference_x = (x.fract() - 0.5).abs();
  1091. /// let abs_difference_y = (y.fract() - (-0.5)).abs();
  1092. ///
  1093. /// assert!(abs_difference_x < 1e-10);
  1094. /// assert!(abs_difference_y < 1e-10);
  1095. /// ```
  1096. fn fract(self) -> Self;
  1097. /// Computes the absolute value of `self`. Returns `Float::nan()` if the
  1098. /// number is `Float::nan()`.
  1099. ///
  1100. /// ```
  1101. /// use num_traits::Float;
  1102. /// use std::f64;
  1103. ///
  1104. /// let x = 3.5;
  1105. /// let y = -3.5;
  1106. ///
  1107. /// let abs_difference_x = (x.abs() - x).abs();
  1108. /// let abs_difference_y = (y.abs() - (-y)).abs();
  1109. ///
  1110. /// assert!(abs_difference_x < 1e-10);
  1111. /// assert!(abs_difference_y < 1e-10);
  1112. ///
  1113. /// assert!(f64::NAN.abs().is_nan());
  1114. /// ```
  1115. fn abs(self) -> Self;
  1116. /// Returns a number that represents the sign of `self`.
  1117. ///
  1118. /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
  1119. /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
  1120. /// - `Float::nan()` if the number is `Float::nan()`
  1121. ///
  1122. /// ```
  1123. /// use num_traits::Float;
  1124. /// use std::f64;
  1125. ///
  1126. /// let f = 3.5;
  1127. ///
  1128. /// assert_eq!(f.signum(), 1.0);
  1129. /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
  1130. ///
  1131. /// assert!(f64::NAN.signum().is_nan());
  1132. /// ```
  1133. fn signum(self) -> Self;
  1134. /// Returns `true` if `self` is positive, including `+0.0`,
  1135. /// `Float::infinity()`, and since Rust 1.20 also `Float::nan()`.
  1136. ///
  1137. /// ```
  1138. /// use num_traits::Float;
  1139. /// use std::f64;
  1140. ///
  1141. /// let neg_nan: f64 = -f64::NAN;
  1142. ///
  1143. /// let f = 7.0;
  1144. /// let g = -7.0;
  1145. ///
  1146. /// assert!(f.is_sign_positive());
  1147. /// assert!(!g.is_sign_positive());
  1148. /// assert!(!neg_nan.is_sign_positive());
  1149. /// ```
  1150. fn is_sign_positive(self) -> bool;
  1151. /// Returns `true` if `self` is negative, including `-0.0`,
  1152. /// `Float::neg_infinity()`, and since Rust 1.20 also `-Float::nan()`.
  1153. ///
  1154. /// ```
  1155. /// use num_traits::Float;
  1156. /// use std::f64;
  1157. ///
  1158. /// let nan: f64 = f64::NAN;
  1159. ///
  1160. /// let f = 7.0;
  1161. /// let g = -7.0;
  1162. ///
  1163. /// assert!(!f.is_sign_negative());
  1164. /// assert!(g.is_sign_negative());
  1165. /// assert!(!nan.is_sign_negative());
  1166. /// ```
  1167. fn is_sign_negative(self) -> bool;
  1168. /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
  1169. /// error. This produces a more accurate result with better performance than
  1170. /// a separate multiplication operation followed by an add.
  1171. ///
  1172. /// ```
  1173. /// use num_traits::Float;
  1174. ///
  1175. /// let m = 10.0;
  1176. /// let x = 4.0;
  1177. /// let b = 60.0;
  1178. ///
  1179. /// // 100.0
  1180. /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
  1181. ///
  1182. /// assert!(abs_difference < 1e-10);
  1183. /// ```
  1184. fn mul_add(self, a: Self, b: Self) -> Self;
  1185. /// Take the reciprocal (inverse) of a number, `1/x`.
  1186. ///
  1187. /// ```
  1188. /// use num_traits::Float;
  1189. ///
  1190. /// let x = 2.0;
  1191. /// let abs_difference = (x.recip() - (1.0/x)).abs();
  1192. ///
  1193. /// assert!(abs_difference < 1e-10);
  1194. /// ```
  1195. fn recip(self) -> Self;
  1196. /// Raise a number to an integer power.
  1197. ///
  1198. /// Using this function is generally faster than using `powf`
  1199. ///
  1200. /// ```
  1201. /// use num_traits::Float;
  1202. ///
  1203. /// let x = 2.0;
  1204. /// let abs_difference = (x.powi(2) - x*x).abs();
  1205. ///
  1206. /// assert!(abs_difference < 1e-10);
  1207. /// ```
  1208. fn powi(self, n: i32) -> Self;
  1209. /// Raise a number to a floating point power.
  1210. ///
  1211. /// ```
  1212. /// use num_traits::Float;
  1213. ///
  1214. /// let x = 2.0;
  1215. /// let abs_difference = (x.powf(2.0) - x*x).abs();
  1216. ///
  1217. /// assert!(abs_difference < 1e-10);
  1218. /// ```
  1219. fn powf(self, n: Self) -> Self;
  1220. /// Take the square root of a number.
  1221. ///
  1222. /// Returns NaN if `self` is a negative number.
  1223. ///
  1224. /// ```
  1225. /// use num_traits::Float;
  1226. ///
  1227. /// let positive = 4.0;
  1228. /// let negative = -4.0;
  1229. ///
  1230. /// let abs_difference = (positive.sqrt() - 2.0).abs();
  1231. ///
  1232. /// assert!(abs_difference < 1e-10);
  1233. /// assert!(negative.sqrt().is_nan());
  1234. /// ```
  1235. fn sqrt(self) -> Self;
  1236. /// Returns `e^(self)`, (the exponential function).
  1237. ///
  1238. /// ```
  1239. /// use num_traits::Float;
  1240. ///
  1241. /// let one = 1.0;
  1242. /// // e^1
  1243. /// let e = one.exp();
  1244. ///
  1245. /// // ln(e) - 1 == 0
  1246. /// let abs_difference = (e.ln() - 1.0).abs();
  1247. ///
  1248. /// assert!(abs_difference < 1e-10);
  1249. /// ```
  1250. fn exp(self) -> Self;
  1251. /// Returns `2^(self)`.
  1252. ///
  1253. /// ```
  1254. /// use num_traits::Float;
  1255. ///
  1256. /// let f = 2.0;
  1257. ///
  1258. /// // 2^2 - 4 == 0
  1259. /// let abs_difference = (f.exp2() - 4.0).abs();
  1260. ///
  1261. /// assert!(abs_difference < 1e-10);
  1262. /// ```
  1263. fn exp2(self) -> Self;
  1264. /// Returns the natural logarithm of the number.
  1265. ///
  1266. /// ```
  1267. /// use num_traits::Float;
  1268. ///
  1269. /// let one = 1.0;
  1270. /// // e^1
  1271. /// let e = one.exp();
  1272. ///
  1273. /// // ln(e) - 1 == 0
  1274. /// let abs_difference = (e.ln() - 1.0).abs();
  1275. ///
  1276. /// assert!(abs_difference < 1e-10);
  1277. /// ```
  1278. fn ln(self) -> Self;
  1279. /// Returns the logarithm of the number with respect to an arbitrary base.
  1280. ///
  1281. /// ```
  1282. /// use num_traits::Float;
  1283. ///
  1284. /// let ten = 10.0;
  1285. /// let two = 2.0;
  1286. ///
  1287. /// // log10(10) - 1 == 0
  1288. /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
  1289. ///
  1290. /// // log2(2) - 1 == 0
  1291. /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
  1292. ///
  1293. /// assert!(abs_difference_10 < 1e-10);
  1294. /// assert!(abs_difference_2 < 1e-10);
  1295. /// ```
  1296. fn log(self, base: Self) -> Self;
  1297. /// Returns the base 2 logarithm of the number.
  1298. ///
  1299. /// ```
  1300. /// use num_traits::Float;
  1301. ///
  1302. /// let two = 2.0;
  1303. ///
  1304. /// // log2(2) - 1 == 0
  1305. /// let abs_difference = (two.log2() - 1.0).abs();
  1306. ///
  1307. /// assert!(abs_difference < 1e-10);
  1308. /// ```
  1309. fn log2(self) -> Self;
  1310. /// Returns the base 10 logarithm of the number.
  1311. ///
  1312. /// ```
  1313. /// use num_traits::Float;
  1314. ///
  1315. /// let ten = 10.0;
  1316. ///
  1317. /// // log10(10) - 1 == 0
  1318. /// let abs_difference = (ten.log10() - 1.0).abs();
  1319. ///
  1320. /// assert!(abs_difference < 1e-10);
  1321. /// ```
  1322. fn log10(self) -> Self;
  1323. /// Converts radians to degrees.
  1324. ///
  1325. /// ```
  1326. /// use std::f64::consts;
  1327. ///
  1328. /// let angle = consts::PI;
  1329. ///
  1330. /// let abs_difference = (angle.to_degrees() - 180.0).abs();
  1331. ///
  1332. /// assert!(abs_difference < 1e-10);
  1333. /// ```
  1334. #[inline]
  1335. fn to_degrees(self) -> Self {
  1336. let halfpi = Self::zero().acos();
  1337. let ninety = Self::from(90u8).unwrap();
  1338. self * ninety / halfpi
  1339. }
  1340. /// Converts degrees to radians.
  1341. ///
  1342. /// ```
  1343. /// use std::f64::consts;
  1344. ///
  1345. /// let angle = 180.0_f64;
  1346. ///
  1347. /// let abs_difference = (angle.to_radians() - consts::PI).abs();
  1348. ///
  1349. /// assert!(abs_difference < 1e-10);
  1350. /// ```
  1351. #[inline]
  1352. fn to_radians(self) -> Self {
  1353. let halfpi = Self::zero().acos();
  1354. let ninety = Self::from(90u8).unwrap();
  1355. self * halfpi / ninety
  1356. }
  1357. /// Returns the maximum of the two numbers.
  1358. ///
  1359. /// ```
  1360. /// use num_traits::Float;
  1361. ///
  1362. /// let x = 1.0;
  1363. /// let y = 2.0;
  1364. ///
  1365. /// assert_eq!(x.max(y), y);
  1366. /// ```
  1367. fn max(self, other: Self) -> Self;
  1368. /// Returns the minimum of the two numbers.
  1369. ///
  1370. /// ```
  1371. /// use num_traits::Float;
  1372. ///
  1373. /// let x = 1.0;
  1374. /// let y = 2.0;
  1375. ///
  1376. /// assert_eq!(x.min(y), x);
  1377. /// ```
  1378. fn min(self, other: Self) -> Self;
  1379. /// The positive difference of two numbers.
  1380. ///
  1381. /// * If `self <= other`: `0:0`
  1382. /// * Else: `self - other`
  1383. ///
  1384. /// ```
  1385. /// use num_traits::Float;
  1386. ///
  1387. /// let x = 3.0;
  1388. /// let y = -3.0;
  1389. ///
  1390. /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
  1391. /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
  1392. ///
  1393. /// assert!(abs_difference_x < 1e-10);
  1394. /// assert!(abs_difference_y < 1e-10);
  1395. /// ```
  1396. fn abs_sub(self, other: Self) -> Self;
  1397. /// Take the cubic root of a number.
  1398. ///
  1399. /// ```
  1400. /// use num_traits::Float;
  1401. ///
  1402. /// let x = 8.0;
  1403. ///
  1404. /// // x^(1/3) - 2 == 0
  1405. /// let abs_difference = (x.cbrt() - 2.0).abs();
  1406. ///
  1407. /// assert!(abs_difference < 1e-10);
  1408. /// ```
  1409. fn cbrt(self) -> Self;
  1410. /// Calculate the length of the hypotenuse of a right-angle triangle given
  1411. /// legs of length `x` and `y`.
  1412. ///
  1413. /// ```
  1414. /// use num_traits::Float;
  1415. ///
  1416. /// let x = 2.0;
  1417. /// let y = 3.0;
  1418. ///
  1419. /// // sqrt(x^2 + y^2)
  1420. /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
  1421. ///
  1422. /// assert!(abs_difference < 1e-10);
  1423. /// ```
  1424. fn hypot(self, other: Self) -> Self;
  1425. /// Computes the sine of a number (in radians).
  1426. ///
  1427. /// ```
  1428. /// use num_traits::Float;
  1429. /// use std::f64;
  1430. ///
  1431. /// let x = f64::consts::PI/2.0;
  1432. ///
  1433. /// let abs_difference = (x.sin() - 1.0).abs();
  1434. ///
  1435. /// assert!(abs_difference < 1e-10);
  1436. /// ```
  1437. fn sin(self) -> Self;
  1438. /// Computes the cosine of a number (in radians).
  1439. ///
  1440. /// ```
  1441. /// use num_traits::Float;
  1442. /// use std::f64;
  1443. ///
  1444. /// let x = 2.0*f64::consts::PI;
  1445. ///
  1446. /// let abs_difference = (x.cos() - 1.0).abs();
  1447. ///
  1448. /// assert!(abs_difference < 1e-10);
  1449. /// ```
  1450. fn cos(self) -> Self;
  1451. /// Computes the tangent of a number (in radians).
  1452. ///
  1453. /// ```
  1454. /// use num_traits::Float;
  1455. /// use std::f64;
  1456. ///
  1457. /// let x = f64::consts::PI/4.0;
  1458. /// let abs_difference = (x.tan() - 1.0).abs();
  1459. ///
  1460. /// assert!(abs_difference < 1e-14);
  1461. /// ```
  1462. fn tan(self) -> Self;
  1463. /// Computes the arcsine of a number. Return value is in radians in
  1464. /// the range [-pi/2, pi/2] or NaN if the number is outside the range
  1465. /// [-1, 1].
  1466. ///
  1467. /// ```
  1468. /// use num_traits::Float;
  1469. /// use std::f64;
  1470. ///
  1471. /// let f = f64::consts::PI / 2.0;
  1472. ///
  1473. /// // asin(sin(pi/2))
  1474. /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
  1475. ///
  1476. /// assert!(abs_difference < 1e-10);
  1477. /// ```
  1478. fn asin(self) -> Self;
  1479. /// Computes the arccosine of a number. Return value is in radians in
  1480. /// the range [0, pi] or NaN if the number is outside the range
  1481. /// [-1, 1].
  1482. ///
  1483. /// ```
  1484. /// use num_traits::Float;
  1485. /// use std::f64;
  1486. ///
  1487. /// let f = f64::consts::PI / 4.0;
  1488. ///
  1489. /// // acos(cos(pi/4))
  1490. /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
  1491. ///
  1492. /// assert!(abs_difference < 1e-10);
  1493. /// ```
  1494. fn acos(self) -> Self;
  1495. /// Computes the arctangent of a number. Return value is in radians in the
  1496. /// range [-pi/2, pi/2];
  1497. ///
  1498. /// ```
  1499. /// use num_traits::Float;
  1500. ///
  1501. /// let f = 1.0;
  1502. ///
  1503. /// // atan(tan(1))
  1504. /// let abs_difference = (f.tan().atan() - 1.0).abs();
  1505. ///
  1506. /// assert!(abs_difference < 1e-10);
  1507. /// ```
  1508. fn atan(self) -> Self;
  1509. /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
  1510. ///
  1511. /// * `x = 0`, `y = 0`: `0`
  1512. /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
  1513. /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
  1514. /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
  1515. ///
  1516. /// ```
  1517. /// use num_traits::Float;
  1518. /// use std::f64;
  1519. ///
  1520. /// let pi = f64::consts::PI;
  1521. /// // All angles from horizontal right (+x)
  1522. /// // 45 deg counter-clockwise
  1523. /// let x1 = 3.0;
  1524. /// let y1 = -3.0;
  1525. ///
  1526. /// // 135 deg clockwise
  1527. /// let x2 = -3.0;
  1528. /// let y2 = 3.0;
  1529. ///
  1530. /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
  1531. /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
  1532. ///
  1533. /// assert!(abs_difference_1 < 1e-10);
  1534. /// assert!(abs_difference_2 < 1e-10);
  1535. /// ```
  1536. fn atan2(self, other: Self) -> Self;
  1537. /// Simultaneously computes the sine and cosine of the number, `x`. Returns
  1538. /// `(sin(x), cos(x))`.
  1539. ///
  1540. /// ```
  1541. /// use num_traits::Float;
  1542. /// use std::f64;
  1543. ///
  1544. /// let x = f64::consts::PI/4.0;
  1545. /// let f = x.sin_cos();
  1546. ///
  1547. /// let abs_difference_0 = (f.0 - x.sin()).abs();
  1548. /// let abs_difference_1 = (f.1 - x.cos()).abs();
  1549. ///
  1550. /// assert!(abs_difference_0 < 1e-10);
  1551. /// assert!(abs_difference_0 < 1e-10);
  1552. /// ```
  1553. fn sin_cos(self) -> (Self, Self);
  1554. /// Returns `e^(self) - 1` in a way that is accurate even if the
  1555. /// number is close to zero.
  1556. ///
  1557. /// ```
  1558. /// use num_traits::Float;
  1559. ///
  1560. /// let x = 7.0;
  1561. ///
  1562. /// // e^(ln(7)) - 1
  1563. /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
  1564. ///
  1565. /// assert!(abs_difference < 1e-10);
  1566. /// ```
  1567. fn exp_m1(self) -> Self;
  1568. /// Returns `ln(1+n)` (natural logarithm) more accurately than if
  1569. /// the operations were performed separately.
  1570. ///
  1571. /// ```
  1572. /// use num_traits::Float;
  1573. /// use std::f64;
  1574. ///
  1575. /// let x = f64::consts::E - 1.0;
  1576. ///
  1577. /// // ln(1 + (e - 1)) == ln(e) == 1
  1578. /// let abs_difference = (x.ln_1p() - 1.0).abs();
  1579. ///
  1580. /// assert!(abs_difference < 1e-10);
  1581. /// ```
  1582. fn ln_1p(self) -> Self;
  1583. /// Hyperbolic sine function.
  1584. ///
  1585. /// ```
  1586. /// use num_traits::Float;
  1587. /// use std::f64;
  1588. ///
  1589. /// let e = f64::consts::E;
  1590. /// let x = 1.0;
  1591. ///
  1592. /// let f = x.sinh();
  1593. /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
  1594. /// let g = (e*e - 1.0)/(2.0*e);
  1595. /// let abs_difference = (f - g).abs();
  1596. ///
  1597. /// assert!(abs_difference < 1e-10);
  1598. /// ```
  1599. fn sinh(self) -> Self;
  1600. /// Hyperbolic cosine function.
  1601. ///
  1602. /// ```
  1603. /// use num_traits::Float;
  1604. /// use std::f64;
  1605. ///
  1606. /// let e = f64::consts::E;
  1607. /// let x = 1.0;
  1608. /// let f = x.cosh();
  1609. /// // Solving cosh() at 1 gives this result
  1610. /// let g = (e*e + 1.0)/(2.0*e);
  1611. /// let abs_difference = (f - g).abs();
  1612. ///
  1613. /// // Same result
  1614. /// assert!(abs_difference < 1.0e-10);
  1615. /// ```
  1616. fn cosh(self) -> Self;
  1617. /// Hyperbolic tangent function.
  1618. ///
  1619. /// ```
  1620. /// use num_traits::Float;
  1621. /// use std::f64;
  1622. ///
  1623. /// let e = f64::consts::E;
  1624. /// let x = 1.0;
  1625. ///
  1626. /// let f = x.tanh();
  1627. /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
  1628. /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
  1629. /// let abs_difference = (f - g).abs();
  1630. ///
  1631. /// assert!(abs_difference < 1.0e-10);
  1632. /// ```
  1633. fn tanh(self) -> Self;
  1634. /// Inverse hyperbolic sine function.
  1635. ///
  1636. /// ```
  1637. /// use num_traits::Float;
  1638. ///
  1639. /// let x = 1.0;
  1640. /// let f = x.sinh().asinh();
  1641. ///
  1642. /// let abs_difference = (f - x).abs();
  1643. ///
  1644. /// assert!(abs_difference < 1.0e-10);
  1645. /// ```
  1646. fn asinh(self) -> Self;
  1647. /// Inverse hyperbolic cosine function.
  1648. ///
  1649. /// ```
  1650. /// use num_traits::Float;
  1651. ///
  1652. /// let x = 1.0;
  1653. /// let f = x.cosh().acosh();
  1654. ///
  1655. /// let abs_difference = (f - x).abs();
  1656. ///
  1657. /// assert!(abs_difference < 1.0e-10);
  1658. /// ```
  1659. fn acosh(self) -> Self;
  1660. /// Inverse hyperbolic tangent function.
  1661. ///
  1662. /// ```
  1663. /// use num_traits::Float;
  1664. /// use std::f64;
  1665. ///
  1666. /// let e = f64::consts::E;
  1667. /// let f = e.tanh().atanh();
  1668. ///
  1669. /// let abs_difference = (f - e).abs();
  1670. ///
  1671. /// assert!(abs_difference < 1.0e-10);
  1672. /// ```
  1673. fn atanh(self) -> Self;
  1674. /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
  1675. /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
  1676. ///
  1677. /// ```
  1678. /// use num_traits::Float;
  1679. ///
  1680. /// let num = 2.0f32;
  1681. ///
  1682. /// // (8388608, -22, 1)
  1683. /// let (mantissa, exponent, sign) = Float::integer_decode(num);
  1684. /// let sign_f = sign as f32;
  1685. /// let mantissa_f = mantissa as f32;
  1686. /// let exponent_f = num.powf(exponent as f32);
  1687. ///
  1688. /// // 1 * 8388608 * 2^(-22) == 2
  1689. /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
  1690. ///
  1691. /// assert!(abs_difference < 1e-10);
  1692. /// ```
  1693. fn integer_decode(self) -> (u64, i16, i8);
  1694. }
  1695. #[cfg(feature = "std")]
  1696. macro_rules! float_impl {
  1697. ($T:ident $decode:ident) => (
  1698. impl Float for $T {
  1699. constant! {
  1700. nan() -> $T::NAN;
  1701. infinity() -> $T::INFINITY;
  1702. neg_infinity() -> $T::NEG_INFINITY;
  1703. neg_zero() -> -0.0;
  1704. min_value() -> $T::MIN;
  1705. min_positive_value() -> $T::MIN_POSITIVE;
  1706. epsilon() -> $T::EPSILON;
  1707. max_value() -> $T::MAX;
  1708. }
  1709. #[inline]
  1710. #[allow(deprecated)]
  1711. fn abs_sub(self, other: Self) -> Self {
  1712. <$T>::abs_sub(self, other)
  1713. }
  1714. #[inline]
  1715. fn integer_decode(self) -> (u64, i16, i8) {
  1716. $decode(self)
  1717. }
  1718. forward! {
  1719. Self::is_nan(self) -> bool;
  1720. Self::is_infinite(self) -> bool;
  1721. Self::is_finite(self) -> bool;
  1722. Self::is_normal(self) -> bool;
  1723. Self::classify(self) -> FpCategory;
  1724. Self::floor(self) -> Self;
  1725. Self::ceil(self) -> Self;
  1726. Self::round(self) -> Self;
  1727. Self::trunc(self) -> Self;
  1728. Self::fract(self) -> Self;
  1729. Self::abs(self) -> Self;
  1730. Self::signum(self) -> Self;
  1731. Self::is_sign_positive(self) -> bool;
  1732. Self::is_sign_negative(self) -> bool;
  1733. Self::mul_add(self, a: Self, b: Self) -> Self;
  1734. Self::recip(self) -> Self;
  1735. Self::powi(self, n: i32) -> Self;
  1736. Self::powf(self, n: Self) -> Self;
  1737. Self::sqrt(self) -> Self;
  1738. Self::exp(self) -> Self;
  1739. Self::exp2(self) -> Self;
  1740. Self::ln(self) -> Self;
  1741. Self::log(self, base: Self) -> Self;
  1742. Self::log2(self) -> Self;
  1743. Self::log10(self) -> Self;
  1744. Self::to_degrees(self) -> Self;
  1745. Self::to_radians(self) -> Self;
  1746. Self::max(self, other: Self) -> Self;
  1747. Self::min(self, other: Self) -> Self;
  1748. Self::cbrt(self) -> Self;
  1749. Self::hypot(self, other: Self) -> Self;
  1750. Self::sin(self) -> Self;
  1751. Self::cos(self) -> Self;
  1752. Self::tan(self) -> Self;
  1753. Self::asin(self) -> Self;
  1754. Self::acos(self) -> Self;
  1755. Self::atan(self) -> Self;
  1756. Self::atan2(self, other: Self) -> Self;
  1757. Self::sin_cos(self) -> (Self, Self);
  1758. Self::exp_m1(self) -> Self;
  1759. Self::ln_1p(self) -> Self;
  1760. Self::sinh(self) -> Self;
  1761. Self::cosh(self) -> Self;
  1762. Self::tanh(self) -> Self;
  1763. Self::asinh(self) -> Self;
  1764. Self::acosh(self) -> Self;
  1765. Self::atanh(self) -> Self;
  1766. }
  1767. }
  1768. )
  1769. }
  1770. fn integer_decode_f32(f: f32) -> (u64, i16, i8) {
  1771. let bits: u32 = unsafe { mem::transmute(f) };
  1772. let sign: i8 = if bits >> 31 == 0 {
  1773. 1
  1774. } else {
  1775. -1
  1776. };
  1777. let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
  1778. let mantissa = if exponent == 0 {
  1779. (bits & 0x7fffff) << 1
  1780. } else {
  1781. (bits & 0x7fffff) | 0x800000
  1782. };
  1783. // Exponent bias + mantissa shift
  1784. exponent -= 127 + 23;
  1785. (mantissa as u64, exponent, sign)
  1786. }
  1787. fn integer_decode_f64(f: f64) -> (u64, i16, i8) {
  1788. let bits: u64 = unsafe { mem::transmute(f) };
  1789. let sign: i8 = if bits >> 63 == 0 {
  1790. 1
  1791. } else {
  1792. -1
  1793. };
  1794. let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
  1795. let mantissa = if exponent == 0 {
  1796. (bits & 0xfffffffffffff) << 1
  1797. } else {
  1798. (bits & 0xfffffffffffff) | 0x10000000000000
  1799. };
  1800. // Exponent bias + mantissa shift
  1801. exponent -= 1023 + 52;
  1802. (mantissa, exponent, sign)
  1803. }
  1804. #[cfg(feature = "std")]
  1805. float_impl!(f32 integer_decode_f32);
  1806. #[cfg(feature = "std")]
  1807. float_impl!(f64 integer_decode_f64);
  1808. macro_rules! float_const_impl {
  1809. ($(#[$doc:meta] $constant:ident,)+) => (
  1810. #[allow(non_snake_case)]
  1811. pub trait FloatConst {
  1812. $(#[$doc] fn $constant() -> Self;)+
  1813. }
  1814. float_const_impl! { @float f32, $($constant,)+ }
  1815. float_const_impl! { @float f64, $($constant,)+ }
  1816. );
  1817. (@float $T:ident, $($constant:ident,)+) => (
  1818. impl FloatConst for $T {
  1819. constant! {
  1820. $( $constant() -> $T::consts::$constant; )+
  1821. }
  1822. }
  1823. );
  1824. }
  1825. float_const_impl! {
  1826. #[doc = "Return Euler’s number."]
  1827. E,
  1828. #[doc = "Return `1.0 / π`."]
  1829. FRAC_1_PI,
  1830. #[doc = "Return `1.0 / sqrt(2.0)`."]
  1831. FRAC_1_SQRT_2,
  1832. #[doc = "Return `2.0 / π`."]
  1833. FRAC_2_PI,
  1834. #[doc = "Return `2.0 / sqrt(π)`."]
  1835. FRAC_2_SQRT_PI,
  1836. #[doc = "Return `π / 2.0`."]
  1837. FRAC_PI_2,
  1838. #[doc = "Return `π / 3.0`."]
  1839. FRAC_PI_3,
  1840. #[doc = "Return `π / 4.0`."]
  1841. FRAC_PI_4,
  1842. #[doc = "Return `π / 6.0`."]
  1843. FRAC_PI_6,
  1844. #[doc = "Return `π / 8.0`."]
  1845. FRAC_PI_8,
  1846. #[doc = "Return `ln(10.0)`."]
  1847. LN_10,
  1848. #[doc = "Return `ln(2.0)`."]
  1849. LN_2,
  1850. #[doc = "Return `log10(e)`."]
  1851. LOG10_E,
  1852. #[doc = "Return `log2(e)`."]
  1853. LOG2_E,
  1854. #[doc = "Return Archimedes’ constant."]
  1855. PI,
  1856. #[doc = "Return `sqrt(2.0)`."]
  1857. SQRT_2,
  1858. }
  1859. #[cfg(test)]
  1860. mod tests {
  1861. use core::f64::consts;
  1862. const DEG_RAD_PAIRS: [(f64, f64); 7] = [
  1863. (0.0, 0.),
  1864. (22.5, consts::FRAC_PI_8),
  1865. (30.0, consts::FRAC_PI_6),
  1866. (45.0, consts::FRAC_PI_4),
  1867. (60.0, consts::FRAC_PI_3),
  1868. (90.0, consts::FRAC_PI_2),
  1869. (180.0, consts::PI),
  1870. ];
  1871. #[test]
  1872. fn convert_deg_rad() {
  1873. use float::FloatCore;
  1874. for &(deg, rad) in &DEG_RAD_PAIRS {
  1875. assert!((FloatCore::to_degrees(rad) - deg).abs() < 1e-6);
  1876. assert!((FloatCore::to_radians(deg) - rad).abs() < 1e-6);
  1877. let (deg, rad) = (deg as f32, rad as f32);
  1878. assert!((FloatCore::to_degrees(rad) - deg).abs() < 1e-5);
  1879. assert!((FloatCore::to_radians(deg) - rad).abs() < 1e-5);
  1880. }
  1881. }
  1882. #[cfg(feature = "std")]
  1883. #[test]
  1884. fn convert_deg_rad_std() {
  1885. for &(deg, rad) in &DEG_RAD_PAIRS {
  1886. use Float;
  1887. assert!((Float::to_degrees(rad) - deg).abs() < 1e-6);
  1888. assert!((Float::to_radians(deg) - rad).abs() < 1e-6);
  1889. let (deg, rad) = (deg as f32, rad as f32);
  1890. assert!((Float::to_degrees(rad) - deg).abs() < 1e-5);
  1891. assert!((Float::to_radians(deg) - rad).abs() < 1e-5);
  1892. }
  1893. }
  1894. }