float.rs 32 KB

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