float.rs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  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. // Use a constant for better precision.
  739. const PIS_IN_180: f32 = 57.2957795130823208767981548141051703_f32;
  740. self * PIS_IN_180
  741. }
  742. #[inline]
  743. #[cfg(not(feature = "std"))]
  744. fn to_radians(self) -> Self {
  745. self * (f32::consts::PI / 180.0)
  746. }
  747. #[cfg(feature = "std")]
  748. forward! {
  749. Self::is_nan(self) -> bool;
  750. Self::is_infinite(self) -> bool;
  751. Self::is_finite(self) -> bool;
  752. Self::is_normal(self) -> bool;
  753. Self::classify(self) -> FpCategory;
  754. Self::floor(self) -> Self;
  755. Self::ceil(self) -> Self;
  756. Self::round(self) -> Self;
  757. Self::trunc(self) -> Self;
  758. Self::fract(self) -> Self;
  759. Self::abs(self) -> Self;
  760. Self::signum(self) -> Self;
  761. Self::is_sign_positive(self) -> bool;
  762. Self::is_sign_negative(self) -> bool;
  763. Self::min(self, other: Self) -> Self;
  764. Self::max(self, other: Self) -> Self;
  765. Self::recip(self) -> Self;
  766. Self::powi(self, n: i32) -> Self;
  767. Self::to_degrees(self) -> Self;
  768. Self::to_radians(self) -> Self;
  769. }
  770. }
  771. impl FloatCore for f64 {
  772. constant! {
  773. infinity() -> f64::INFINITY;
  774. neg_infinity() -> f64::NEG_INFINITY;
  775. nan() -> f64::NAN;
  776. neg_zero() -> -0.0;
  777. min_value() -> f64::MIN;
  778. min_positive_value() -> f64::MIN_POSITIVE;
  779. epsilon() -> f64::EPSILON;
  780. max_value() -> f64::MAX;
  781. }
  782. #[inline]
  783. fn integer_decode(self) -> (u64, i16, i8) {
  784. integer_decode_f64(self)
  785. }
  786. #[inline]
  787. #[cfg(not(feature = "std"))]
  788. fn classify(self) -> FpCategory {
  789. const EXP_MASK: u64 = 0x7ff0000000000000;
  790. const MAN_MASK: u64 = 0x000fffffffffffff;
  791. let bits: u64 = unsafe { mem::transmute(self) };
  792. match (bits & MAN_MASK, bits & EXP_MASK) {
  793. (0, 0) => FpCategory::Zero,
  794. (_, 0) => FpCategory::Subnormal,
  795. (0, EXP_MASK) => FpCategory::Infinite,
  796. (_, EXP_MASK) => FpCategory::Nan,
  797. _ => FpCategory::Normal,
  798. }
  799. }
  800. #[inline]
  801. #[cfg(not(feature = "std"))]
  802. fn to_degrees(self) -> Self {
  803. // The division here is correctly rounded with respect to the true
  804. // value of 180/π. (This differs from f32, where a constant must be
  805. // used to ensure a correctly rounded result.)
  806. self * (180.0 / f64::consts::PI)
  807. }
  808. #[inline]
  809. #[cfg(not(feature = "std"))]
  810. fn to_radians(self) -> Self {
  811. self * (f64::consts::PI / 180.0)
  812. }
  813. #[cfg(feature = "std")]
  814. forward! {
  815. Self::is_nan(self) -> bool;
  816. Self::is_infinite(self) -> bool;
  817. Self::is_finite(self) -> bool;
  818. Self::is_normal(self) -> bool;
  819. Self::classify(self) -> FpCategory;
  820. Self::floor(self) -> Self;
  821. Self::ceil(self) -> Self;
  822. Self::round(self) -> Self;
  823. Self::trunc(self) -> Self;
  824. Self::fract(self) -> Self;
  825. Self::abs(self) -> Self;
  826. Self::signum(self) -> Self;
  827. Self::is_sign_positive(self) -> bool;
  828. Self::is_sign_negative(self) -> bool;
  829. Self::min(self, other: Self) -> Self;
  830. Self::max(self, other: Self) -> Self;
  831. Self::recip(self) -> Self;
  832. Self::powi(self, n: i32) -> Self;
  833. Self::to_degrees(self) -> Self;
  834. Self::to_radians(self) -> Self;
  835. }
  836. }
  837. // FIXME: these doctests aren't actually helpful, because they're using and
  838. // testing the inherent methods directly, not going through `Float`.
  839. /// Generic trait for floating point numbers
  840. ///
  841. /// This trait is only available with the `std` feature.
  842. #[cfg(feature = "std")]
  843. pub trait Float
  844. : Num
  845. + Copy
  846. + NumCast
  847. + PartialOrd
  848. + Neg<Output = Self>
  849. {
  850. /// Returns the `NaN` value.
  851. ///
  852. /// ```
  853. /// use num_traits::Float;
  854. ///
  855. /// let nan: f32 = Float::nan();
  856. ///
  857. /// assert!(nan.is_nan());
  858. /// ```
  859. fn nan() -> Self;
  860. /// Returns the infinite value.
  861. ///
  862. /// ```
  863. /// use num_traits::Float;
  864. /// use std::f32;
  865. ///
  866. /// let infinity: f32 = Float::infinity();
  867. ///
  868. /// assert!(infinity.is_infinite());
  869. /// assert!(!infinity.is_finite());
  870. /// assert!(infinity > f32::MAX);
  871. /// ```
  872. fn infinity() -> Self;
  873. /// Returns the negative infinite value.
  874. ///
  875. /// ```
  876. /// use num_traits::Float;
  877. /// use std::f32;
  878. ///
  879. /// let neg_infinity: f32 = Float::neg_infinity();
  880. ///
  881. /// assert!(neg_infinity.is_infinite());
  882. /// assert!(!neg_infinity.is_finite());
  883. /// assert!(neg_infinity < f32::MIN);
  884. /// ```
  885. fn neg_infinity() -> Self;
  886. /// Returns `-0.0`.
  887. ///
  888. /// ```
  889. /// use num_traits::{Zero, Float};
  890. ///
  891. /// let inf: f32 = Float::infinity();
  892. /// let zero: f32 = Zero::zero();
  893. /// let neg_zero: f32 = Float::neg_zero();
  894. ///
  895. /// assert_eq!(zero, neg_zero);
  896. /// assert_eq!(7.0f32/inf, zero);
  897. /// assert_eq!(zero * 10.0, zero);
  898. /// ```
  899. fn neg_zero() -> Self;
  900. /// Returns the smallest finite value that this type can represent.
  901. ///
  902. /// ```
  903. /// use num_traits::Float;
  904. /// use std::f64;
  905. ///
  906. /// let x: f64 = Float::min_value();
  907. ///
  908. /// assert_eq!(x, f64::MIN);
  909. /// ```
  910. fn min_value() -> Self;
  911. /// Returns the smallest positive, normalized value that this type can represent.
  912. ///
  913. /// ```
  914. /// use num_traits::Float;
  915. /// use std::f64;
  916. ///
  917. /// let x: f64 = Float::min_positive_value();
  918. ///
  919. /// assert_eq!(x, f64::MIN_POSITIVE);
  920. /// ```
  921. fn min_positive_value() -> Self;
  922. /// Returns epsilon, a small positive value.
  923. ///
  924. /// ```
  925. /// use num_traits::Float;
  926. /// use std::f64;
  927. ///
  928. /// let x: f64 = Float::epsilon();
  929. ///
  930. /// assert_eq!(x, f64::EPSILON);
  931. /// ```
  932. ///
  933. /// # Panics
  934. ///
  935. /// The default implementation will panic if `f32::EPSILON` cannot
  936. /// be cast to `Self`.
  937. fn epsilon() -> Self {
  938. Self::from(f32::EPSILON).expect("Unable to cast from f32::EPSILON")
  939. }
  940. /// Returns the largest finite value that this type can represent.
  941. ///
  942. /// ```
  943. /// use num_traits::Float;
  944. /// use std::f64;
  945. ///
  946. /// let x: f64 = Float::max_value();
  947. /// assert_eq!(x, f64::MAX);
  948. /// ```
  949. fn max_value() -> Self;
  950. /// Returns `true` if this value is `NaN` and false otherwise.
  951. ///
  952. /// ```
  953. /// use num_traits::Float;
  954. /// use std::f64;
  955. ///
  956. /// let nan = f64::NAN;
  957. /// let f = 7.0;
  958. ///
  959. /// assert!(nan.is_nan());
  960. /// assert!(!f.is_nan());
  961. /// ```
  962. fn is_nan(self) -> bool;
  963. /// Returns `true` if this value is positive infinity or negative infinity and
  964. /// false otherwise.
  965. ///
  966. /// ```
  967. /// use num_traits::Float;
  968. /// use std::f32;
  969. ///
  970. /// let f = 7.0f32;
  971. /// let inf: f32 = Float::infinity();
  972. /// let neg_inf: f32 = Float::neg_infinity();
  973. /// let nan: f32 = f32::NAN;
  974. ///
  975. /// assert!(!f.is_infinite());
  976. /// assert!(!nan.is_infinite());
  977. ///
  978. /// assert!(inf.is_infinite());
  979. /// assert!(neg_inf.is_infinite());
  980. /// ```
  981. fn is_infinite(self) -> bool;
  982. /// Returns `true` if this number is neither infinite nor `NaN`.
  983. ///
  984. /// ```
  985. /// use num_traits::Float;
  986. /// use std::f32;
  987. ///
  988. /// let f = 7.0f32;
  989. /// let inf: f32 = Float::infinity();
  990. /// let neg_inf: f32 = Float::neg_infinity();
  991. /// let nan: f32 = f32::NAN;
  992. ///
  993. /// assert!(f.is_finite());
  994. ///
  995. /// assert!(!nan.is_finite());
  996. /// assert!(!inf.is_finite());
  997. /// assert!(!neg_inf.is_finite());
  998. /// ```
  999. fn is_finite(self) -> bool;
  1000. /// Returns `true` if the number is neither zero, infinite,
  1001. /// [subnormal][subnormal], or `NaN`.
  1002. ///
  1003. /// ```
  1004. /// use num_traits::Float;
  1005. /// use std::f32;
  1006. ///
  1007. /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
  1008. /// let max = f32::MAX;
  1009. /// let lower_than_min = 1.0e-40_f32;
  1010. /// let zero = 0.0f32;
  1011. ///
  1012. /// assert!(min.is_normal());
  1013. /// assert!(max.is_normal());
  1014. ///
  1015. /// assert!(!zero.is_normal());
  1016. /// assert!(!f32::NAN.is_normal());
  1017. /// assert!(!f32::INFINITY.is_normal());
  1018. /// // Values between `0` and `min` are Subnormal.
  1019. /// assert!(!lower_than_min.is_normal());
  1020. /// ```
  1021. /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
  1022. fn is_normal(self) -> bool;
  1023. /// Returns the floating point category of the number. If only one property
  1024. /// is going to be tested, it is generally faster to use the specific
  1025. /// predicate instead.
  1026. ///
  1027. /// ```
  1028. /// use num_traits::Float;
  1029. /// use std::num::FpCategory;
  1030. /// use std::f32;
  1031. ///
  1032. /// let num = 12.4f32;
  1033. /// let inf = f32::INFINITY;
  1034. ///
  1035. /// assert_eq!(num.classify(), FpCategory::Normal);
  1036. /// assert_eq!(inf.classify(), FpCategory::Infinite);
  1037. /// ```
  1038. fn classify(self) -> FpCategory;
  1039. /// Returns the largest integer less than or equal to a number.
  1040. ///
  1041. /// ```
  1042. /// use num_traits::Float;
  1043. ///
  1044. /// let f = 3.99;
  1045. /// let g = 3.0;
  1046. ///
  1047. /// assert_eq!(f.floor(), 3.0);
  1048. /// assert_eq!(g.floor(), 3.0);
  1049. /// ```
  1050. fn floor(self) -> Self;
  1051. /// Returns the smallest integer greater than or equal to a number.
  1052. ///
  1053. /// ```
  1054. /// use num_traits::Float;
  1055. ///
  1056. /// let f = 3.01;
  1057. /// let g = 4.0;
  1058. ///
  1059. /// assert_eq!(f.ceil(), 4.0);
  1060. /// assert_eq!(g.ceil(), 4.0);
  1061. /// ```
  1062. fn ceil(self) -> Self;
  1063. /// Returns the nearest integer to a number. Round half-way cases away from
  1064. /// `0.0`.
  1065. ///
  1066. /// ```
  1067. /// use num_traits::Float;
  1068. ///
  1069. /// let f = 3.3;
  1070. /// let g = -3.3;
  1071. ///
  1072. /// assert_eq!(f.round(), 3.0);
  1073. /// assert_eq!(g.round(), -3.0);
  1074. /// ```
  1075. fn round(self) -> Self;
  1076. /// Return the integer part of a number.
  1077. ///
  1078. /// ```
  1079. /// use num_traits::Float;
  1080. ///
  1081. /// let f = 3.3;
  1082. /// let g = -3.7;
  1083. ///
  1084. /// assert_eq!(f.trunc(), 3.0);
  1085. /// assert_eq!(g.trunc(), -3.0);
  1086. /// ```
  1087. fn trunc(self) -> Self;
  1088. /// Returns the fractional part of a number.
  1089. ///
  1090. /// ```
  1091. /// use num_traits::Float;
  1092. ///
  1093. /// let x = 3.5;
  1094. /// let y = -3.5;
  1095. /// let abs_difference_x = (x.fract() - 0.5).abs();
  1096. /// let abs_difference_y = (y.fract() - (-0.5)).abs();
  1097. ///
  1098. /// assert!(abs_difference_x < 1e-10);
  1099. /// assert!(abs_difference_y < 1e-10);
  1100. /// ```
  1101. fn fract(self) -> Self;
  1102. /// Computes the absolute value of `self`. Returns `Float::nan()` if the
  1103. /// number is `Float::nan()`.
  1104. ///
  1105. /// ```
  1106. /// use num_traits::Float;
  1107. /// use std::f64;
  1108. ///
  1109. /// let x = 3.5;
  1110. /// let y = -3.5;
  1111. ///
  1112. /// let abs_difference_x = (x.abs() - x).abs();
  1113. /// let abs_difference_y = (y.abs() - (-y)).abs();
  1114. ///
  1115. /// assert!(abs_difference_x < 1e-10);
  1116. /// assert!(abs_difference_y < 1e-10);
  1117. ///
  1118. /// assert!(f64::NAN.abs().is_nan());
  1119. /// ```
  1120. fn abs(self) -> Self;
  1121. /// Returns a number that represents the sign of `self`.
  1122. ///
  1123. /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
  1124. /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
  1125. /// - `Float::nan()` if the number is `Float::nan()`
  1126. ///
  1127. /// ```
  1128. /// use num_traits::Float;
  1129. /// use std::f64;
  1130. ///
  1131. /// let f = 3.5;
  1132. ///
  1133. /// assert_eq!(f.signum(), 1.0);
  1134. /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
  1135. ///
  1136. /// assert!(f64::NAN.signum().is_nan());
  1137. /// ```
  1138. fn signum(self) -> Self;
  1139. /// Returns `true` if `self` is positive, including `+0.0`,
  1140. /// `Float::infinity()`, and since Rust 1.20 also `Float::nan()`.
  1141. ///
  1142. /// ```
  1143. /// use num_traits::Float;
  1144. /// use std::f64;
  1145. ///
  1146. /// let neg_nan: f64 = -f64::NAN;
  1147. ///
  1148. /// let f = 7.0;
  1149. /// let g = -7.0;
  1150. ///
  1151. /// assert!(f.is_sign_positive());
  1152. /// assert!(!g.is_sign_positive());
  1153. /// assert!(!neg_nan.is_sign_positive());
  1154. /// ```
  1155. fn is_sign_positive(self) -> bool;
  1156. /// Returns `true` if `self` is negative, including `-0.0`,
  1157. /// `Float::neg_infinity()`, and since Rust 1.20 also `-Float::nan()`.
  1158. ///
  1159. /// ```
  1160. /// use num_traits::Float;
  1161. /// use std::f64;
  1162. ///
  1163. /// let nan: f64 = f64::NAN;
  1164. ///
  1165. /// let f = 7.0;
  1166. /// let g = -7.0;
  1167. ///
  1168. /// assert!(!f.is_sign_negative());
  1169. /// assert!(g.is_sign_negative());
  1170. /// assert!(!nan.is_sign_negative());
  1171. /// ```
  1172. fn is_sign_negative(self) -> bool;
  1173. /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
  1174. /// error. This produces a more accurate result with better performance than
  1175. /// a separate multiplication operation followed by an add.
  1176. ///
  1177. /// ```
  1178. /// use num_traits::Float;
  1179. ///
  1180. /// let m = 10.0;
  1181. /// let x = 4.0;
  1182. /// let b = 60.0;
  1183. ///
  1184. /// // 100.0
  1185. /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
  1186. ///
  1187. /// assert!(abs_difference < 1e-10);
  1188. /// ```
  1189. fn mul_add(self, a: Self, b: Self) -> Self;
  1190. /// Take the reciprocal (inverse) of a number, `1/x`.
  1191. ///
  1192. /// ```
  1193. /// use num_traits::Float;
  1194. ///
  1195. /// let x = 2.0;
  1196. /// let abs_difference = (x.recip() - (1.0/x)).abs();
  1197. ///
  1198. /// assert!(abs_difference < 1e-10);
  1199. /// ```
  1200. fn recip(self) -> Self;
  1201. /// Raise a number to an integer power.
  1202. ///
  1203. /// Using this function is generally faster than using `powf`
  1204. ///
  1205. /// ```
  1206. /// use num_traits::Float;
  1207. ///
  1208. /// let x = 2.0;
  1209. /// let abs_difference = (x.powi(2) - x*x).abs();
  1210. ///
  1211. /// assert!(abs_difference < 1e-10);
  1212. /// ```
  1213. fn powi(self, n: i32) -> Self;
  1214. /// Raise a number to a floating point power.
  1215. ///
  1216. /// ```
  1217. /// use num_traits::Float;
  1218. ///
  1219. /// let x = 2.0;
  1220. /// let abs_difference = (x.powf(2.0) - x*x).abs();
  1221. ///
  1222. /// assert!(abs_difference < 1e-10);
  1223. /// ```
  1224. fn powf(self, n: Self) -> Self;
  1225. /// Take the square root of a number.
  1226. ///
  1227. /// Returns NaN if `self` is a negative number.
  1228. ///
  1229. /// ```
  1230. /// use num_traits::Float;
  1231. ///
  1232. /// let positive = 4.0;
  1233. /// let negative = -4.0;
  1234. ///
  1235. /// let abs_difference = (positive.sqrt() - 2.0).abs();
  1236. ///
  1237. /// assert!(abs_difference < 1e-10);
  1238. /// assert!(negative.sqrt().is_nan());
  1239. /// ```
  1240. fn sqrt(self) -> Self;
  1241. /// Returns `e^(self)`, (the exponential function).
  1242. ///
  1243. /// ```
  1244. /// use num_traits::Float;
  1245. ///
  1246. /// let one = 1.0;
  1247. /// // e^1
  1248. /// let e = one.exp();
  1249. ///
  1250. /// // ln(e) - 1 == 0
  1251. /// let abs_difference = (e.ln() - 1.0).abs();
  1252. ///
  1253. /// assert!(abs_difference < 1e-10);
  1254. /// ```
  1255. fn exp(self) -> Self;
  1256. /// Returns `2^(self)`.
  1257. ///
  1258. /// ```
  1259. /// use num_traits::Float;
  1260. ///
  1261. /// let f = 2.0;
  1262. ///
  1263. /// // 2^2 - 4 == 0
  1264. /// let abs_difference = (f.exp2() - 4.0).abs();
  1265. ///
  1266. /// assert!(abs_difference < 1e-10);
  1267. /// ```
  1268. fn exp2(self) -> Self;
  1269. /// Returns the natural logarithm of the number.
  1270. ///
  1271. /// ```
  1272. /// use num_traits::Float;
  1273. ///
  1274. /// let one = 1.0;
  1275. /// // e^1
  1276. /// let e = one.exp();
  1277. ///
  1278. /// // ln(e) - 1 == 0
  1279. /// let abs_difference = (e.ln() - 1.0).abs();
  1280. ///
  1281. /// assert!(abs_difference < 1e-10);
  1282. /// ```
  1283. fn ln(self) -> Self;
  1284. /// Returns the logarithm of the number with respect to an arbitrary base.
  1285. ///
  1286. /// ```
  1287. /// use num_traits::Float;
  1288. ///
  1289. /// let ten = 10.0;
  1290. /// let two = 2.0;
  1291. ///
  1292. /// // log10(10) - 1 == 0
  1293. /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
  1294. ///
  1295. /// // log2(2) - 1 == 0
  1296. /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
  1297. ///
  1298. /// assert!(abs_difference_10 < 1e-10);
  1299. /// assert!(abs_difference_2 < 1e-10);
  1300. /// ```
  1301. fn log(self, base: Self) -> Self;
  1302. /// Returns the base 2 logarithm of the number.
  1303. ///
  1304. /// ```
  1305. /// use num_traits::Float;
  1306. ///
  1307. /// let two = 2.0;
  1308. ///
  1309. /// // log2(2) - 1 == 0
  1310. /// let abs_difference = (two.log2() - 1.0).abs();
  1311. ///
  1312. /// assert!(abs_difference < 1e-10);
  1313. /// ```
  1314. fn log2(self) -> Self;
  1315. /// Returns the base 10 logarithm of the number.
  1316. ///
  1317. /// ```
  1318. /// use num_traits::Float;
  1319. ///
  1320. /// let ten = 10.0;
  1321. ///
  1322. /// // log10(10) - 1 == 0
  1323. /// let abs_difference = (ten.log10() - 1.0).abs();
  1324. ///
  1325. /// assert!(abs_difference < 1e-10);
  1326. /// ```
  1327. fn log10(self) -> Self;
  1328. /// Converts radians to degrees.
  1329. ///
  1330. /// ```
  1331. /// use std::f64::consts;
  1332. ///
  1333. /// let angle = consts::PI;
  1334. ///
  1335. /// let abs_difference = (angle.to_degrees() - 180.0).abs();
  1336. ///
  1337. /// assert!(abs_difference < 1e-10);
  1338. /// ```
  1339. #[inline]
  1340. fn to_degrees(self) -> Self {
  1341. let halfpi = Self::zero().acos();
  1342. let ninety = Self::from(90u8).unwrap();
  1343. self * ninety / halfpi
  1344. }
  1345. /// Converts degrees to radians.
  1346. ///
  1347. /// ```
  1348. /// use std::f64::consts;
  1349. ///
  1350. /// let angle = 180.0_f64;
  1351. ///
  1352. /// let abs_difference = (angle.to_radians() - consts::PI).abs();
  1353. ///
  1354. /// assert!(abs_difference < 1e-10);
  1355. /// ```
  1356. #[inline]
  1357. fn to_radians(self) -> Self {
  1358. let halfpi = Self::zero().acos();
  1359. let ninety = Self::from(90u8).unwrap();
  1360. self * halfpi / ninety
  1361. }
  1362. /// Returns the maximum of the two numbers.
  1363. ///
  1364. /// ```
  1365. /// use num_traits::Float;
  1366. ///
  1367. /// let x = 1.0;
  1368. /// let y = 2.0;
  1369. ///
  1370. /// assert_eq!(x.max(y), y);
  1371. /// ```
  1372. fn max(self, other: Self) -> Self;
  1373. /// Returns the minimum of the two numbers.
  1374. ///
  1375. /// ```
  1376. /// use num_traits::Float;
  1377. ///
  1378. /// let x = 1.0;
  1379. /// let y = 2.0;
  1380. ///
  1381. /// assert_eq!(x.min(y), x);
  1382. /// ```
  1383. fn min(self, other: Self) -> Self;
  1384. /// The positive difference of two numbers.
  1385. ///
  1386. /// * If `self <= other`: `0:0`
  1387. /// * Else: `self - other`
  1388. ///
  1389. /// ```
  1390. /// use num_traits::Float;
  1391. ///
  1392. /// let x = 3.0;
  1393. /// let y = -3.0;
  1394. ///
  1395. /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
  1396. /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
  1397. ///
  1398. /// assert!(abs_difference_x < 1e-10);
  1399. /// assert!(abs_difference_y < 1e-10);
  1400. /// ```
  1401. fn abs_sub(self, other: Self) -> Self;
  1402. /// Take the cubic root of a number.
  1403. ///
  1404. /// ```
  1405. /// use num_traits::Float;
  1406. ///
  1407. /// let x = 8.0;
  1408. ///
  1409. /// // x^(1/3) - 2 == 0
  1410. /// let abs_difference = (x.cbrt() - 2.0).abs();
  1411. ///
  1412. /// assert!(abs_difference < 1e-10);
  1413. /// ```
  1414. fn cbrt(self) -> Self;
  1415. /// Calculate the length of the hypotenuse of a right-angle triangle given
  1416. /// legs of length `x` and `y`.
  1417. ///
  1418. /// ```
  1419. /// use num_traits::Float;
  1420. ///
  1421. /// let x = 2.0;
  1422. /// let y = 3.0;
  1423. ///
  1424. /// // sqrt(x^2 + y^2)
  1425. /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
  1426. ///
  1427. /// assert!(abs_difference < 1e-10);
  1428. /// ```
  1429. fn hypot(self, other: Self) -> Self;
  1430. /// Computes the sine of a number (in radians).
  1431. ///
  1432. /// ```
  1433. /// use num_traits::Float;
  1434. /// use std::f64;
  1435. ///
  1436. /// let x = f64::consts::PI/2.0;
  1437. ///
  1438. /// let abs_difference = (x.sin() - 1.0).abs();
  1439. ///
  1440. /// assert!(abs_difference < 1e-10);
  1441. /// ```
  1442. fn sin(self) -> Self;
  1443. /// Computes the cosine of a number (in radians).
  1444. ///
  1445. /// ```
  1446. /// use num_traits::Float;
  1447. /// use std::f64;
  1448. ///
  1449. /// let x = 2.0*f64::consts::PI;
  1450. ///
  1451. /// let abs_difference = (x.cos() - 1.0).abs();
  1452. ///
  1453. /// assert!(abs_difference < 1e-10);
  1454. /// ```
  1455. fn cos(self) -> Self;
  1456. /// Computes the tangent of a number (in radians).
  1457. ///
  1458. /// ```
  1459. /// use num_traits::Float;
  1460. /// use std::f64;
  1461. ///
  1462. /// let x = f64::consts::PI/4.0;
  1463. /// let abs_difference = (x.tan() - 1.0).abs();
  1464. ///
  1465. /// assert!(abs_difference < 1e-14);
  1466. /// ```
  1467. fn tan(self) -> Self;
  1468. /// Computes the arcsine of a number. Return value is in radians in
  1469. /// the range [-pi/2, pi/2] or NaN if the number is outside the range
  1470. /// [-1, 1].
  1471. ///
  1472. /// ```
  1473. /// use num_traits::Float;
  1474. /// use std::f64;
  1475. ///
  1476. /// let f = f64::consts::PI / 2.0;
  1477. ///
  1478. /// // asin(sin(pi/2))
  1479. /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
  1480. ///
  1481. /// assert!(abs_difference < 1e-10);
  1482. /// ```
  1483. fn asin(self) -> Self;
  1484. /// Computes the arccosine of a number. Return value is in radians in
  1485. /// the range [0, pi] or NaN if the number is outside the range
  1486. /// [-1, 1].
  1487. ///
  1488. /// ```
  1489. /// use num_traits::Float;
  1490. /// use std::f64;
  1491. ///
  1492. /// let f = f64::consts::PI / 4.0;
  1493. ///
  1494. /// // acos(cos(pi/4))
  1495. /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
  1496. ///
  1497. /// assert!(abs_difference < 1e-10);
  1498. /// ```
  1499. fn acos(self) -> Self;
  1500. /// Computes the arctangent of a number. Return value is in radians in the
  1501. /// range [-pi/2, pi/2];
  1502. ///
  1503. /// ```
  1504. /// use num_traits::Float;
  1505. ///
  1506. /// let f = 1.0;
  1507. ///
  1508. /// // atan(tan(1))
  1509. /// let abs_difference = (f.tan().atan() - 1.0).abs();
  1510. ///
  1511. /// assert!(abs_difference < 1e-10);
  1512. /// ```
  1513. fn atan(self) -> Self;
  1514. /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
  1515. ///
  1516. /// * `x = 0`, `y = 0`: `0`
  1517. /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
  1518. /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
  1519. /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
  1520. ///
  1521. /// ```
  1522. /// use num_traits::Float;
  1523. /// use std::f64;
  1524. ///
  1525. /// let pi = f64::consts::PI;
  1526. /// // All angles from horizontal right (+x)
  1527. /// // 45 deg counter-clockwise
  1528. /// let x1 = 3.0;
  1529. /// let y1 = -3.0;
  1530. ///
  1531. /// // 135 deg clockwise
  1532. /// let x2 = -3.0;
  1533. /// let y2 = 3.0;
  1534. ///
  1535. /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
  1536. /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
  1537. ///
  1538. /// assert!(abs_difference_1 < 1e-10);
  1539. /// assert!(abs_difference_2 < 1e-10);
  1540. /// ```
  1541. fn atan2(self, other: Self) -> Self;
  1542. /// Simultaneously computes the sine and cosine of the number, `x`. Returns
  1543. /// `(sin(x), cos(x))`.
  1544. ///
  1545. /// ```
  1546. /// use num_traits::Float;
  1547. /// use std::f64;
  1548. ///
  1549. /// let x = f64::consts::PI/4.0;
  1550. /// let f = x.sin_cos();
  1551. ///
  1552. /// let abs_difference_0 = (f.0 - x.sin()).abs();
  1553. /// let abs_difference_1 = (f.1 - x.cos()).abs();
  1554. ///
  1555. /// assert!(abs_difference_0 < 1e-10);
  1556. /// assert!(abs_difference_0 < 1e-10);
  1557. /// ```
  1558. fn sin_cos(self) -> (Self, Self);
  1559. /// Returns `e^(self) - 1` in a way that is accurate even if the
  1560. /// number is close to zero.
  1561. ///
  1562. /// ```
  1563. /// use num_traits::Float;
  1564. ///
  1565. /// let x = 7.0;
  1566. ///
  1567. /// // e^(ln(7)) - 1
  1568. /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
  1569. ///
  1570. /// assert!(abs_difference < 1e-10);
  1571. /// ```
  1572. fn exp_m1(self) -> Self;
  1573. /// Returns `ln(1+n)` (natural logarithm) more accurately than if
  1574. /// the operations were performed separately.
  1575. ///
  1576. /// ```
  1577. /// use num_traits::Float;
  1578. /// use std::f64;
  1579. ///
  1580. /// let x = f64::consts::E - 1.0;
  1581. ///
  1582. /// // ln(1 + (e - 1)) == ln(e) == 1
  1583. /// let abs_difference = (x.ln_1p() - 1.0).abs();
  1584. ///
  1585. /// assert!(abs_difference < 1e-10);
  1586. /// ```
  1587. fn ln_1p(self) -> Self;
  1588. /// Hyperbolic sine function.
  1589. ///
  1590. /// ```
  1591. /// use num_traits::Float;
  1592. /// use std::f64;
  1593. ///
  1594. /// let e = f64::consts::E;
  1595. /// let x = 1.0;
  1596. ///
  1597. /// let f = x.sinh();
  1598. /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
  1599. /// let g = (e*e - 1.0)/(2.0*e);
  1600. /// let abs_difference = (f - g).abs();
  1601. ///
  1602. /// assert!(abs_difference < 1e-10);
  1603. /// ```
  1604. fn sinh(self) -> Self;
  1605. /// Hyperbolic cosine function.
  1606. ///
  1607. /// ```
  1608. /// use num_traits::Float;
  1609. /// use std::f64;
  1610. ///
  1611. /// let e = f64::consts::E;
  1612. /// let x = 1.0;
  1613. /// let f = x.cosh();
  1614. /// // Solving cosh() at 1 gives this result
  1615. /// let g = (e*e + 1.0)/(2.0*e);
  1616. /// let abs_difference = (f - g).abs();
  1617. ///
  1618. /// // Same result
  1619. /// assert!(abs_difference < 1.0e-10);
  1620. /// ```
  1621. fn cosh(self) -> Self;
  1622. /// Hyperbolic tangent function.
  1623. ///
  1624. /// ```
  1625. /// use num_traits::Float;
  1626. /// use std::f64;
  1627. ///
  1628. /// let e = f64::consts::E;
  1629. /// let x = 1.0;
  1630. ///
  1631. /// let f = x.tanh();
  1632. /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
  1633. /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
  1634. /// let abs_difference = (f - g).abs();
  1635. ///
  1636. /// assert!(abs_difference < 1.0e-10);
  1637. /// ```
  1638. fn tanh(self) -> Self;
  1639. /// Inverse hyperbolic sine function.
  1640. ///
  1641. /// ```
  1642. /// use num_traits::Float;
  1643. ///
  1644. /// let x = 1.0;
  1645. /// let f = x.sinh().asinh();
  1646. ///
  1647. /// let abs_difference = (f - x).abs();
  1648. ///
  1649. /// assert!(abs_difference < 1.0e-10);
  1650. /// ```
  1651. fn asinh(self) -> Self;
  1652. /// Inverse hyperbolic cosine function.
  1653. ///
  1654. /// ```
  1655. /// use num_traits::Float;
  1656. ///
  1657. /// let x = 1.0;
  1658. /// let f = x.cosh().acosh();
  1659. ///
  1660. /// let abs_difference = (f - x).abs();
  1661. ///
  1662. /// assert!(abs_difference < 1.0e-10);
  1663. /// ```
  1664. fn acosh(self) -> Self;
  1665. /// Inverse hyperbolic tangent function.
  1666. ///
  1667. /// ```
  1668. /// use num_traits::Float;
  1669. /// use std::f64;
  1670. ///
  1671. /// let e = f64::consts::E;
  1672. /// let f = e.tanh().atanh();
  1673. ///
  1674. /// let abs_difference = (f - e).abs();
  1675. ///
  1676. /// assert!(abs_difference < 1.0e-10);
  1677. /// ```
  1678. fn atanh(self) -> Self;
  1679. /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
  1680. /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
  1681. ///
  1682. /// ```
  1683. /// use num_traits::Float;
  1684. ///
  1685. /// let num = 2.0f32;
  1686. ///
  1687. /// // (8388608, -22, 1)
  1688. /// let (mantissa, exponent, sign) = Float::integer_decode(num);
  1689. /// let sign_f = sign as f32;
  1690. /// let mantissa_f = mantissa as f32;
  1691. /// let exponent_f = num.powf(exponent as f32);
  1692. ///
  1693. /// // 1 * 8388608 * 2^(-22) == 2
  1694. /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
  1695. ///
  1696. /// assert!(abs_difference < 1e-10);
  1697. /// ```
  1698. fn integer_decode(self) -> (u64, i16, i8);
  1699. }
  1700. #[cfg(feature = "std")]
  1701. macro_rules! float_impl {
  1702. ($T:ident $decode:ident) => (
  1703. impl Float for $T {
  1704. constant! {
  1705. nan() -> $T::NAN;
  1706. infinity() -> $T::INFINITY;
  1707. neg_infinity() -> $T::NEG_INFINITY;
  1708. neg_zero() -> -0.0;
  1709. min_value() -> $T::MIN;
  1710. min_positive_value() -> $T::MIN_POSITIVE;
  1711. epsilon() -> $T::EPSILON;
  1712. max_value() -> $T::MAX;
  1713. }
  1714. #[inline]
  1715. #[allow(deprecated)]
  1716. fn abs_sub(self, other: Self) -> Self {
  1717. <$T>::abs_sub(self, other)
  1718. }
  1719. #[inline]
  1720. fn integer_decode(self) -> (u64, i16, i8) {
  1721. $decode(self)
  1722. }
  1723. forward! {
  1724. Self::is_nan(self) -> bool;
  1725. Self::is_infinite(self) -> bool;
  1726. Self::is_finite(self) -> bool;
  1727. Self::is_normal(self) -> bool;
  1728. Self::classify(self) -> FpCategory;
  1729. Self::floor(self) -> Self;
  1730. Self::ceil(self) -> Self;
  1731. Self::round(self) -> Self;
  1732. Self::trunc(self) -> Self;
  1733. Self::fract(self) -> Self;
  1734. Self::abs(self) -> Self;
  1735. Self::signum(self) -> Self;
  1736. Self::is_sign_positive(self) -> bool;
  1737. Self::is_sign_negative(self) -> bool;
  1738. Self::mul_add(self, a: Self, b: Self) -> Self;
  1739. Self::recip(self) -> Self;
  1740. Self::powi(self, n: i32) -> Self;
  1741. Self::powf(self, n: Self) -> Self;
  1742. Self::sqrt(self) -> Self;
  1743. Self::exp(self) -> Self;
  1744. Self::exp2(self) -> Self;
  1745. Self::ln(self) -> Self;
  1746. Self::log(self, base: Self) -> Self;
  1747. Self::log2(self) -> Self;
  1748. Self::log10(self) -> Self;
  1749. Self::to_degrees(self) -> Self;
  1750. Self::to_radians(self) -> Self;
  1751. Self::max(self, other: Self) -> Self;
  1752. Self::min(self, other: Self) -> Self;
  1753. Self::cbrt(self) -> Self;
  1754. Self::hypot(self, other: Self) -> Self;
  1755. Self::sin(self) -> Self;
  1756. Self::cos(self) -> Self;
  1757. Self::tan(self) -> Self;
  1758. Self::asin(self) -> Self;
  1759. Self::acos(self) -> Self;
  1760. Self::atan(self) -> Self;
  1761. Self::atan2(self, other: Self) -> Self;
  1762. Self::sin_cos(self) -> (Self, Self);
  1763. Self::exp_m1(self) -> Self;
  1764. Self::ln_1p(self) -> Self;
  1765. Self::sinh(self) -> Self;
  1766. Self::cosh(self) -> Self;
  1767. Self::tanh(self) -> Self;
  1768. Self::asinh(self) -> Self;
  1769. Self::acosh(self) -> Self;
  1770. Self::atanh(self) -> Self;
  1771. }
  1772. }
  1773. )
  1774. }
  1775. fn integer_decode_f32(f: f32) -> (u64, i16, i8) {
  1776. let bits: u32 = unsafe { mem::transmute(f) };
  1777. let sign: i8 = if bits >> 31 == 0 {
  1778. 1
  1779. } else {
  1780. -1
  1781. };
  1782. let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
  1783. let mantissa = if exponent == 0 {
  1784. (bits & 0x7fffff) << 1
  1785. } else {
  1786. (bits & 0x7fffff) | 0x800000
  1787. };
  1788. // Exponent bias + mantissa shift
  1789. exponent -= 127 + 23;
  1790. (mantissa as u64, exponent, sign)
  1791. }
  1792. fn integer_decode_f64(f: f64) -> (u64, i16, i8) {
  1793. let bits: u64 = unsafe { mem::transmute(f) };
  1794. let sign: i8 = if bits >> 63 == 0 {
  1795. 1
  1796. } else {
  1797. -1
  1798. };
  1799. let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
  1800. let mantissa = if exponent == 0 {
  1801. (bits & 0xfffffffffffff) << 1
  1802. } else {
  1803. (bits & 0xfffffffffffff) | 0x10000000000000
  1804. };
  1805. // Exponent bias + mantissa shift
  1806. exponent -= 1023 + 52;
  1807. (mantissa, exponent, sign)
  1808. }
  1809. #[cfg(feature = "std")]
  1810. float_impl!(f32 integer_decode_f32);
  1811. #[cfg(feature = "std")]
  1812. float_impl!(f64 integer_decode_f64);
  1813. macro_rules! float_const_impl {
  1814. ($(#[$doc:meta] $constant:ident,)+) => (
  1815. #[allow(non_snake_case)]
  1816. pub trait FloatConst {
  1817. $(#[$doc] fn $constant() -> Self;)+
  1818. }
  1819. float_const_impl! { @float f32, $($constant,)+ }
  1820. float_const_impl! { @float f64, $($constant,)+ }
  1821. );
  1822. (@float $T:ident, $($constant:ident,)+) => (
  1823. impl FloatConst for $T {
  1824. constant! {
  1825. $( $constant() -> $T::consts::$constant; )+
  1826. }
  1827. }
  1828. );
  1829. }
  1830. float_const_impl! {
  1831. #[doc = "Return Euler’s number."]
  1832. E,
  1833. #[doc = "Return `1.0 / π`."]
  1834. FRAC_1_PI,
  1835. #[doc = "Return `1.0 / sqrt(2.0)`."]
  1836. FRAC_1_SQRT_2,
  1837. #[doc = "Return `2.0 / π`."]
  1838. FRAC_2_PI,
  1839. #[doc = "Return `2.0 / sqrt(π)`."]
  1840. FRAC_2_SQRT_PI,
  1841. #[doc = "Return `π / 2.0`."]
  1842. FRAC_PI_2,
  1843. #[doc = "Return `π / 3.0`."]
  1844. FRAC_PI_3,
  1845. #[doc = "Return `π / 4.0`."]
  1846. FRAC_PI_4,
  1847. #[doc = "Return `π / 6.0`."]
  1848. FRAC_PI_6,
  1849. #[doc = "Return `π / 8.0`."]
  1850. FRAC_PI_8,
  1851. #[doc = "Return `ln(10.0)`."]
  1852. LN_10,
  1853. #[doc = "Return `ln(2.0)`."]
  1854. LN_2,
  1855. #[doc = "Return `log10(e)`."]
  1856. LOG10_E,
  1857. #[doc = "Return `log2(e)`."]
  1858. LOG2_E,
  1859. #[doc = "Return Archimedes’ constant."]
  1860. PI,
  1861. #[doc = "Return `sqrt(2.0)`."]
  1862. SQRT_2,
  1863. }
  1864. #[cfg(test)]
  1865. mod tests {
  1866. use core::f64::consts;
  1867. const DEG_RAD_PAIRS: [(f64, f64); 7] = [
  1868. (0.0, 0.),
  1869. (22.5, consts::FRAC_PI_8),
  1870. (30.0, consts::FRAC_PI_6),
  1871. (45.0, consts::FRAC_PI_4),
  1872. (60.0, consts::FRAC_PI_3),
  1873. (90.0, consts::FRAC_PI_2),
  1874. (180.0, consts::PI),
  1875. ];
  1876. #[test]
  1877. fn convert_deg_rad() {
  1878. use float::FloatCore;
  1879. for &(deg, rad) in &DEG_RAD_PAIRS {
  1880. assert!((FloatCore::to_degrees(rad) - deg).abs() < 1e-6);
  1881. assert!((FloatCore::to_radians(deg) - rad).abs() < 1e-6);
  1882. let (deg, rad) = (deg as f32, rad as f32);
  1883. assert!((FloatCore::to_degrees(rad) - deg).abs() < 1e-5);
  1884. assert!((FloatCore::to_radians(deg) - rad).abs() < 1e-5);
  1885. }
  1886. }
  1887. #[cfg(feature = "std")]
  1888. #[test]
  1889. fn convert_deg_rad_std() {
  1890. for &(deg, rad) in &DEG_RAD_PAIRS {
  1891. use Float;
  1892. assert!((Float::to_degrees(rad) - deg).abs() < 1e-6);
  1893. assert!((Float::to_radians(deg) - rad).abs() < 1e-6);
  1894. let (deg, rad) = (deg as f32, rad as f32);
  1895. assert!((Float::to_degrees(rad) - deg).abs() < 1e-5);
  1896. assert!((Float::to_radians(deg) - rad).abs() < 1e-5);
  1897. }
  1898. }
  1899. #[test]
  1900. // This fails with the forwarded `std` implementation in Rust 1.8.
  1901. // To avoid the failure, the test is limited to `no_std` builds.
  1902. #[cfg(not(feature = "std"))]
  1903. fn to_degrees_rounding() {
  1904. use float::FloatCore;
  1905. assert_eq!(FloatCore::to_degrees(1_f32), 57.2957795130823208767981548141051703);
  1906. }
  1907. }