BSRational.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.11.03
  7. #pragma once
  8. #include <Mathematics/BSNumber.h>
  9. // See the comments in BSNumber.h about the UInteger requirements. The
  10. // denominator of a BSRational is chosen to be positive, which allows some
  11. // simplification of comparisons. Also see the comments about exposing the
  12. // GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE conditional define.
  13. namespace WwiseGTE
  14. {
  15. template <typename UInteger>
  16. class BSRational
  17. {
  18. public:
  19. // Construction. The default constructor generates the zero
  20. // BSRational. The constructors that take only numerators set the
  21. // denominators to one.
  22. BSRational()
  23. :
  24. mNumerator(0),
  25. mDenominator(1)
  26. {
  27. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  28. mValue = (double)*this;
  29. #endif
  30. }
  31. BSRational(BSRational const& r)
  32. {
  33. *this = r;
  34. }
  35. BSRational(float numerator)
  36. :
  37. mNumerator(numerator),
  38. mDenominator(1.0f)
  39. {
  40. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  41. mValue = (double)*this;
  42. #endif
  43. }
  44. BSRational(double numerator)
  45. :
  46. mNumerator(numerator),
  47. mDenominator(1.0)
  48. {
  49. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  50. mValue = (double)*this;
  51. #endif
  52. }
  53. BSRational(int32_t numerator)
  54. :
  55. mNumerator(numerator),
  56. mDenominator(1)
  57. {
  58. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  59. mValue = (double)*this;
  60. #endif
  61. }
  62. BSRational(uint32_t numerator)
  63. :
  64. mNumerator(numerator),
  65. mDenominator(1)
  66. {
  67. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  68. mValue = (double)*this;
  69. #endif
  70. }
  71. BSRational(int64_t numerator)
  72. :
  73. mNumerator(numerator),
  74. mDenominator(1)
  75. {
  76. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  77. mValue = (double)*this;
  78. #endif
  79. }
  80. BSRational(uint64_t numerator)
  81. :
  82. mNumerator(numerator),
  83. mDenominator(1)
  84. {
  85. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  86. mValue = (double)*this;
  87. #endif
  88. }
  89. BSRational(BSNumber<UInteger> const& numerator)
  90. :
  91. mNumerator(numerator),
  92. mDenominator(1)
  93. {
  94. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  95. mValue = (double)*this;
  96. #endif
  97. }
  98. BSRational(float numerator, float denominator)
  99. :
  100. mNumerator(numerator),
  101. mDenominator(denominator)
  102. {
  103. LogAssert(mDenominator.mSign != 0, "Division by zero.");
  104. if (mDenominator.mSign < 0)
  105. {
  106. mNumerator.mSign = -mNumerator.mSign;
  107. mDenominator.mSign = 1;
  108. }
  109. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  110. mValue = (double)*this;
  111. #endif
  112. }
  113. BSRational(double numerator, double denominator)
  114. :
  115. mNumerator(numerator),
  116. mDenominator(denominator)
  117. {
  118. LogAssert(mDenominator.mSign != 0, "Division by zero.");
  119. if (mDenominator.mSign < 0)
  120. {
  121. mNumerator.mSign = -mNumerator.mSign;
  122. mDenominator.mSign = 1;
  123. }
  124. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  125. mValue = (double)*this;
  126. #endif
  127. }
  128. BSRational(int32_t numerator, int32_t denominator)
  129. :
  130. mNumerator(numerator),
  131. mDenominator(denominator)
  132. {
  133. LogAssert(mDenominator.mSign != 0, "Division by zero.");
  134. if (mDenominator.mSign < 0)
  135. {
  136. mNumerator.mSign = -mNumerator.mSign;
  137. mDenominator.mSign = 1;
  138. }
  139. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  140. mValue = (double)*this;
  141. #endif
  142. }
  143. BSRational(uint32_t numerator, uint32_t denominator)
  144. :
  145. mNumerator(numerator),
  146. mDenominator(denominator)
  147. {
  148. LogAssert(mDenominator.mSign != 0, "Division by zero.");
  149. if (mDenominator.mSign < 0)
  150. {
  151. mNumerator.mSign = -mNumerator.mSign;
  152. mDenominator.mSign = 1;
  153. }
  154. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  155. mValue = (double)*this;
  156. #endif
  157. }
  158. BSRational(int64_t numerator, int64_t denominator)
  159. :
  160. mNumerator(numerator),
  161. mDenominator(denominator)
  162. {
  163. LogAssert(mDenominator.mSign != 0, "Division by zero.");
  164. if (mDenominator.mSign < 0)
  165. {
  166. mNumerator.mSign = -mNumerator.mSign;
  167. mDenominator.mSign = 1;
  168. }
  169. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  170. mValue = (double)*this;
  171. #endif
  172. }
  173. BSRational(uint64_t numerator, uint64_t denominator)
  174. :
  175. mNumerator(numerator),
  176. mDenominator(denominator)
  177. {
  178. LogAssert(mDenominator.mSign != 0, "Division by zero.");
  179. if (mDenominator.mSign < 0)
  180. {
  181. mNumerator.mSign = -mNumerator.mSign;
  182. mDenominator.mSign = 1;
  183. }
  184. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  185. mValue = (double)*this;
  186. #endif
  187. }
  188. BSRational(BSNumber<UInteger> const& numerator, BSNumber<UInteger> const& denominator)
  189. :
  190. mNumerator(numerator),
  191. mDenominator(denominator)
  192. {
  193. LogAssert(mDenominator.mSign != 0, "Division by zero.");
  194. if (mDenominator.mSign < 0)
  195. {
  196. mNumerator.mSign = -mNumerator.mSign;
  197. mDenominator.mSign = 1;
  198. }
  199. // Set the exponent of the denominator to zero, but you can do so
  200. // only by modifying the biased exponent. Adjust the numerator
  201. // accordingly. This prevents large growth of the exponents in
  202. // both numerator and denominator simultaneously.
  203. mNumerator.mBiasedExponent -= mDenominator.GetExponent();
  204. mDenominator.mBiasedExponent = -(mDenominator.GetUInteger().GetNumBits() - 1);
  205. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  206. mValue = (double)*this;
  207. #endif
  208. }
  209. BSRational(std::string const& number)
  210. {
  211. LogAssert(number.size() > 0, "A number must be specified.");
  212. // Get the leading '+' or '-' if it exists.
  213. std::string fpNumber;
  214. int sign;
  215. if (number[0] == '+')
  216. {
  217. fpNumber = number.substr(1);
  218. sign = +1;
  219. LogAssert(fpNumber.size() > 1, "Invalid number format.");
  220. }
  221. else if (number[0] == '-')
  222. {
  223. fpNumber = number.substr(1);
  224. sign = -1;
  225. LogAssert(fpNumber.size() > 1, "Invalid number format.");
  226. }
  227. else
  228. {
  229. fpNumber = number;
  230. sign = +1;
  231. }
  232. size_t decimal = fpNumber.find('.');
  233. if (decimal != std::string::npos)
  234. {
  235. if (decimal > 0)
  236. {
  237. if (decimal < fpNumber.size())
  238. {
  239. // The number is "x.y".
  240. BSNumber<UInteger> intPart = BSNumber<UInteger>::ConvertToInteger(fpNumber.substr(0, decimal));
  241. BSRational frcPart = ConvertToFraction(fpNumber.substr(decimal + 1));
  242. mNumerator = intPart * frcPart.mDenominator + frcPart.mNumerator;
  243. mDenominator = frcPart.mDenominator;
  244. }
  245. else
  246. {
  247. // The number is "x.".
  248. mNumerator = BSNumber<UInteger>::ConvertToInteger(fpNumber.substr(0,fpNumber.size()-1));
  249. mDenominator = 1;
  250. }
  251. }
  252. else
  253. {
  254. // The number is ".y".
  255. BSRational frcPart = ConvertToFraction(fpNumber.substr(1));
  256. mNumerator = frcPart.mNumerator;
  257. mDenominator = frcPart.mDenominator;
  258. }
  259. }
  260. else
  261. {
  262. // The number is "x".
  263. mNumerator = BSNumber<UInteger>::ConvertToInteger(fpNumber);
  264. mDenominator = 1;
  265. }
  266. mNumerator.SetSign(sign);
  267. }
  268. BSRational(const char* number)
  269. :
  270. BSRational(std::string(number))
  271. {
  272. }
  273. // Implicit conversions. These always use the default rounding mode,
  274. // round-to-nearest-ties-to-even.
  275. operator float() const
  276. {
  277. float output;
  278. Convert(*this, FE_TONEAREST, output);
  279. return output;
  280. }
  281. operator double() const
  282. {
  283. double output;
  284. Convert(*this, FE_TONEAREST, output);
  285. return output;
  286. }
  287. // Assignment.
  288. BSRational& operator=(BSRational const& r)
  289. {
  290. mNumerator = r.mNumerator;
  291. mDenominator = r.mDenominator;
  292. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  293. mValue = (double)*this;
  294. #endif
  295. return *this;
  296. }
  297. // Support for move semantics.
  298. BSRational(BSRational&& r)
  299. {
  300. *this = std::move(r);
  301. }
  302. BSRational& operator=(BSRational&& r)
  303. {
  304. mNumerator = std::move(r.mNumerator);
  305. mDenominator = std::move(r.mDenominator);
  306. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  307. mValue = (double)*this;
  308. #endif
  309. return *this;
  310. }
  311. // Member access.
  312. inline void SetSign(int sign)
  313. {
  314. mNumerator.SetSign(sign);
  315. mDenominator.SetSign(1);
  316. }
  317. inline int GetSign() const
  318. {
  319. return mNumerator.GetSign() * mDenominator.GetSign();
  320. }
  321. inline BSNumber<UInteger> const& GetNumerator() const
  322. {
  323. return mNumerator;
  324. }
  325. inline BSNumber<UInteger>& GetNumerator()
  326. {
  327. return mNumerator;
  328. }
  329. inline BSNumber<UInteger> const& GetDenominator() const
  330. {
  331. return mDenominator;
  332. }
  333. inline BSNumber<UInteger>& GetDenominator()
  334. {
  335. return mDenominator;
  336. }
  337. // Comparisons.
  338. bool operator==(BSRational const& r) const
  339. {
  340. // Do inexpensive sign tests first for optimum performance.
  341. if (mNumerator.mSign != r.mNumerator.mSign)
  342. {
  343. return false;
  344. }
  345. if (mNumerator.mSign == 0)
  346. {
  347. // The numbers are both zero.
  348. return true;
  349. }
  350. return mNumerator * r.mDenominator == mDenominator * r.mNumerator;
  351. }
  352. bool operator!=(BSRational const& r) const
  353. {
  354. return !operator==(r);
  355. }
  356. bool operator< (BSRational const& r) const
  357. {
  358. // Do inexpensive sign tests first for optimum performance.
  359. if (mNumerator.mSign > 0)
  360. {
  361. if (r.mNumerator.mSign <= 0)
  362. {
  363. return false;
  364. }
  365. }
  366. else if (mNumerator.mSign == 0)
  367. {
  368. return r.mNumerator.mSign > 0;
  369. }
  370. else if (mNumerator.mSign < 0)
  371. {
  372. if (r.mNumerator.mSign >= 0)
  373. {
  374. return true;
  375. }
  376. }
  377. return mNumerator * r.mDenominator < mDenominator * r.mNumerator;
  378. }
  379. bool operator<=(BSRational const& r) const
  380. {
  381. return !r.operator<(*this);
  382. }
  383. bool operator> (BSRational const& r) const
  384. {
  385. return r.operator<(*this);
  386. }
  387. bool operator>=(BSRational const& r) const
  388. {
  389. return !operator<(r);
  390. }
  391. // Unary operations.
  392. BSRational operator+() const
  393. {
  394. return *this;
  395. }
  396. BSRational operator-() const
  397. {
  398. return BSRational(-mNumerator, mDenominator);
  399. }
  400. // Arithmetic.
  401. BSRational operator+(BSRational const& r) const
  402. {
  403. BSNumber<UInteger> product0 = mNumerator * r.mDenominator;
  404. BSNumber<UInteger> product1 = mDenominator * r.mNumerator;
  405. BSNumber<UInteger> numerator = product0 + product1;
  406. // Complex expressions can lead to 0/denom, where denom is not 1.
  407. if (numerator.mSign != 0)
  408. {
  409. BSNumber<UInteger> denominator = mDenominator * r.mDenominator;
  410. return BSRational(numerator, denominator);
  411. }
  412. else
  413. {
  414. return BSRational(0);
  415. }
  416. }
  417. BSRational operator-(BSRational const& r) const
  418. {
  419. BSNumber<UInteger> product0 = mNumerator * r.mDenominator;
  420. BSNumber<UInteger> product1 = mDenominator * r.mNumerator;
  421. BSNumber<UInteger> numerator = product0 - product1;
  422. // Complex expressions can lead to 0/denom, where denom is not 1.
  423. if (numerator.mSign != 0)
  424. {
  425. BSNumber<UInteger> denominator = mDenominator * r.mDenominator;
  426. return BSRational(numerator, denominator);
  427. }
  428. else
  429. {
  430. return BSRational(0);
  431. }
  432. }
  433. BSRational operator*(BSRational const& r) const
  434. {
  435. BSNumber<UInteger> numerator = mNumerator * r.mNumerator;
  436. // Complex expressions can lead to 0/denom, where denom is not 1.
  437. if (numerator.mSign != 0)
  438. {
  439. BSNumber<UInteger> denominator = mDenominator * r.mDenominator;
  440. return BSRational(numerator, denominator);
  441. }
  442. else
  443. {
  444. return BSRational(0);
  445. }
  446. }
  447. BSRational operator/(BSRational const& r) const
  448. {
  449. LogAssert(r.mNumerator.mSign != 0, "Division by zero in BSRational::operator/.");
  450. BSNumber<UInteger> numerator = mNumerator * r.mDenominator;
  451. // Complex expressions can lead to 0/denom, where denom is not 1.
  452. if (numerator.mSign != 0)
  453. {
  454. BSNumber<UInteger> denominator = mDenominator * r.mNumerator;
  455. if (denominator.mSign < 0)
  456. {
  457. numerator.mSign = -numerator.mSign;
  458. denominator.mSign = 1;
  459. }
  460. return BSRational(numerator, denominator);
  461. }
  462. else
  463. {
  464. return BSRational(0);
  465. }
  466. }
  467. BSRational& operator+=(BSRational const& r)
  468. {
  469. *this = operator+(r);
  470. return *this;
  471. }
  472. BSRational& operator-=(BSRational const& r)
  473. {
  474. *this = operator-(r);
  475. return *this;
  476. }
  477. BSRational& operator*=(BSRational const& r)
  478. {
  479. *this = operator*(r);
  480. return *this;
  481. }
  482. BSRational& operator/=(BSRational const& r)
  483. {
  484. *this = operator/(r);
  485. return *this;
  486. }
  487. // Disk input/output. The fstream objects should be created using
  488. // std::ios::binary. The return value is 'true' iff the operation
  489. // was successful.
  490. bool Write(std::ostream& output) const
  491. {
  492. return mNumerator.Write(output) && mDenominator.Write(output);
  493. }
  494. bool Read(std::istream& input)
  495. {
  496. return mNumerator.Read(input) && mDenominator.Read(input);
  497. }
  498. private:
  499. // Helper for converting a string to a BSRational, where the string
  500. // is the fractional part "y" of the string "x.y".
  501. static BSRational ConvertToFraction(std::string const& number)
  502. {
  503. LogAssert(number.find_first_not_of("0123456789") == std::string::npos, "Invalid number format.");
  504. BSRational y(0), ten(10), pow10(10);
  505. for (size_t i = 0; i < number.size(); ++i)
  506. {
  507. int digit = static_cast<int>(number[i]) - static_cast<int>('0');
  508. if (digit > 0)
  509. {
  510. y += BSRational(digit) / pow10;
  511. }
  512. pow10 *= ten;
  513. }
  514. return y;
  515. }
  516. #if defined(GTE_BINARY_SCIENTIFIC_SHOW_DOUBLE)
  517. public:
  518. // List this first so that it shows up first in the debugger watch
  519. // window.
  520. double mValue;
  521. private:
  522. #endif
  523. BSNumber<UInteger> mNumerator, mDenominator;
  524. };
  525. // Explicit conversion to a user-specified precision. The rounding
  526. // mode is one of the flags provided in <cfenv>. The modes are
  527. // FE_TONEAREST: round to nearest ties to even
  528. // FE_DOWNWARD: round towards negative infinity
  529. // FE_TOWARDZERO: round towards zero
  530. // FE_UPWARD: round towards positive infinity
  531. template <typename UInteger>
  532. void Convert(BSRational<UInteger> const& input, int32_t precision,
  533. int32_t roundingMode, BSNumber<UInteger>& output)
  534. {
  535. if (precision <= 0)
  536. {
  537. LogError("Precision must be positive.");
  538. }
  539. int64_t const maxSize = static_cast<int64_t>(UInteger::GetMaxSize());
  540. int64_t const excess = 32LL * maxSize - static_cast<int64_t>(precision);
  541. if (excess <= 0)
  542. {
  543. LogError("The maximum precision has been exceeded.");
  544. }
  545. if (input.GetSign() == 0)
  546. {
  547. output = BSNumber<UInteger>(0);
  548. return;
  549. }
  550. BSNumber<UInteger> n = input.GetNumerator();
  551. BSNumber<UInteger> d = input.GetDenominator();
  552. // The ratio is abstractly of the form n/d = (1.u*2^p)/(1.v*2^q).
  553. // Convert to the form
  554. // (1.u/1.v)*2^{p-q}, if 1.u >= 1.v
  555. // 2*(1.u/1.v)*2^{p-q-1} if 1.u < 1.v
  556. // which are in the interval [1,2).
  557. int32_t sign = n.GetSign() * d.GetSign();
  558. n.SetSign(1);
  559. d.SetSign(1);
  560. int32_t pmq = n.GetExponent() - d.GetExponent();
  561. n.SetExponent(0);
  562. d.SetExponent(0);
  563. if (n < d)
  564. {
  565. n.SetExponent(n.GetExponent() + 1);
  566. --pmq;
  567. }
  568. // Let p = precision. At this time, n/d = 1.c in [1,2). Define the
  569. // sequence of bits w = 1c = w_{p-1} w_{p-2} ... w_0 r, where
  570. // w_{p-1} = 1. The bits r after w_0 are used for rounding based on
  571. // the user-specified rounding mode.
  572. // Compute p bits for w, the leading bit guaranteed to be 1 and
  573. // occurring at index (1 << (precision-1)).
  574. BSNumber<UInteger> one(1), two(2);
  575. UInteger& w = output.GetUInteger();
  576. w.SetNumBits(precision);
  577. w.SetAllBitsToZero();
  578. int32_t const size = w.GetSize();
  579. int32_t const precisionM1 = precision - 1;
  580. int32_t const leading = precisionM1 % 32;
  581. uint32_t mask = (1 << leading);
  582. auto& bits = w.GetBits();
  583. int32_t current = size - 1;
  584. int32_t lastBit = -1;
  585. for (int i = precisionM1; i >= 0; --i)
  586. {
  587. if (n < d)
  588. {
  589. n = two * n;
  590. lastBit = 0;
  591. }
  592. else
  593. {
  594. n = two * (n - d);
  595. bits[current] |= mask;
  596. lastBit = 1;
  597. }
  598. if (mask == 0x00000001u)
  599. {
  600. --current;
  601. mask = 0x80000000u;
  602. }
  603. else
  604. {
  605. mask >>= 1;
  606. }
  607. }
  608. // At this point as a sequence of bits, r = n/d = r0 r1 ...
  609. if (roundingMode == FE_TONEAREST)
  610. {
  611. n = n - d;
  612. if (n.GetSign() > 0 || (n.GetSign() == 0 && lastBit == 1))
  613. {
  614. // round up
  615. pmq += w.RoundUp();
  616. }
  617. // else round down, equivalent to truncating the r bits
  618. }
  619. else if (roundingMode == FE_UPWARD)
  620. {
  621. if (n.GetSign() > 0 && sign > 0)
  622. {
  623. // round up
  624. pmq += w.RoundUp();
  625. }
  626. // else round down, equivalent to truncating the r bits
  627. }
  628. else if (roundingMode == FE_DOWNWARD)
  629. {
  630. if (n.GetSign() > 0 && sign < 0)
  631. {
  632. // Round down. This is the round-up operation applied to
  633. // w, but the final sign is negative which amounts to
  634. // rounding down.
  635. pmq += w.RoundUp();
  636. }
  637. // else round down, equivalent to truncating the r bits
  638. }
  639. else if (roundingMode != FE_TOWARDZERO)
  640. {
  641. // Currently, no additional implementation-dependent modes
  642. // are supported for rounding.
  643. LogError("Implementation-dependent rounding mode not supported.");
  644. }
  645. // else roundingMode == FE_TOWARDZERO. Truncate the r bits, which
  646. // requires no additional work.
  647. // Do not use SetExponent(pmq) at this step. The number of
  648. // requested bits is 'precision' but w.GetNumBits() will be
  649. // different when round-up occurs, and SetExponent accesses
  650. // w.GetNumBits().
  651. output.SetSign(sign);
  652. output.SetBiasedExponent(pmq - precisionM1);
  653. }
  654. // This conversion is used to avoid having to expose BSNumber in the
  655. // APConversion class as well as other places where BSRational<UInteger>
  656. // is passed via a template parameter named Rational.
  657. template <typename UInteger>
  658. void Convert(BSRational<UInteger> const& input, int32_t precision,
  659. int32_t roundingMode, BSRational<UInteger>& output)
  660. {
  661. BSNumber<UInteger> numerator;
  662. Convert(input, precision, roundingMode, numerator);
  663. output = BSRational<UInteger>(numerator);
  664. }
  665. // Convert to 'float' or 'double' using the specified rounding mode.
  666. template <typename UInteger, typename FPType>
  667. void Convert(BSRational<UInteger> const& input, int32_t roundingMode, FPType& output)
  668. {
  669. static_assert(std::is_floating_point<FPType>::value, "Invalid floating-point type.");
  670. BSNumber<UInteger> number;
  671. Convert(input, std::numeric_limits<FPType>::digits, roundingMode, number);
  672. output = static_cast<FPType>(number);
  673. }
  674. }
  675. namespace std
  676. {
  677. // TODO: Allow for implementations of the math functions in which a
  678. // specified precision is used when computing the result.
  679. template <typename UInteger>
  680. inline WwiseGTE::BSRational<UInteger> acos(WwiseGTE::BSRational<UInteger> const& x)
  681. {
  682. return (WwiseGTE::BSRational<UInteger>)std::acos((double)x);
  683. }
  684. template <typename UInteger>
  685. inline WwiseGTE::BSRational<UInteger> acosh(WwiseGTE::BSRational<UInteger> const& x)
  686. {
  687. return (WwiseGTE::BSRational<UInteger>)std::acosh((double)x);
  688. }
  689. template <typename UInteger>
  690. inline WwiseGTE::BSRational<UInteger> asin(WwiseGTE::BSRational<UInteger> const& x)
  691. {
  692. return (WwiseGTE::BSRational<UInteger>)std::asin((double)x);
  693. }
  694. template <typename UInteger>
  695. inline WwiseGTE::BSRational<UInteger> asinh(WwiseGTE::BSRational<UInteger> const& x)
  696. {
  697. return (WwiseGTE::BSRational<UInteger>)std::asinh((double)x);
  698. }
  699. template <typename UInteger>
  700. inline WwiseGTE::BSRational<UInteger> atan(WwiseGTE::BSRational<UInteger> const& x)
  701. {
  702. return (WwiseGTE::BSRational<UInteger>)std::atan((double)x);
  703. }
  704. template <typename UInteger>
  705. inline WwiseGTE::BSRational<UInteger> atanh(WwiseGTE::BSRational<UInteger> const& x)
  706. {
  707. return (WwiseGTE::BSRational<UInteger>)std::atanh((double)x);
  708. }
  709. template <typename UInteger>
  710. inline WwiseGTE::BSRational<UInteger> atan2(WwiseGTE::BSRational<UInteger> const& y, WwiseGTE::BSRational<UInteger> const& x)
  711. {
  712. return (WwiseGTE::BSRational<UInteger>)std::atan2((double)y, (double)x);
  713. }
  714. template <typename UInteger>
  715. inline WwiseGTE::BSRational<UInteger> ceil(WwiseGTE::BSRational<UInteger> const& x)
  716. {
  717. return (WwiseGTE::BSRational<UInteger>)std::ceil((double)x);
  718. }
  719. template <typename UInteger>
  720. inline WwiseGTE::BSRational<UInteger> cos(WwiseGTE::BSRational<UInteger> const& x)
  721. {
  722. return (WwiseGTE::BSRational<UInteger>)std::cos((double)x);
  723. }
  724. template <typename UInteger>
  725. inline WwiseGTE::BSRational<UInteger> cosh(WwiseGTE::BSRational<UInteger> const& x)
  726. {
  727. return (WwiseGTE::BSRational<UInteger>)std::cosh((double)x);
  728. }
  729. template <typename UInteger>
  730. inline WwiseGTE::BSRational<UInteger> exp(WwiseGTE::BSRational<UInteger> const& x)
  731. {
  732. return (WwiseGTE::BSRational<UInteger>)std::exp((double)x);
  733. }
  734. template <typename UInteger>
  735. inline WwiseGTE::BSRational<UInteger> exp2(WwiseGTE::BSRational<UInteger> const& x)
  736. {
  737. return (WwiseGTE::BSRational<UInteger>)std::exp2((double)x);
  738. }
  739. template <typename UInteger>
  740. inline WwiseGTE::BSRational<UInteger> fabs(WwiseGTE::BSRational<UInteger> const& x)
  741. {
  742. return (x.GetSign() >= 0 ? x : -x);
  743. }
  744. template <typename UInteger>
  745. inline WwiseGTE::BSRational<UInteger> floor(WwiseGTE::BSRational<UInteger> const& x)
  746. {
  747. return (WwiseGTE::BSRational<UInteger>)std::floor((double)x);
  748. }
  749. template <typename UInteger>
  750. inline WwiseGTE::BSRational<UInteger> fmod(WwiseGTE::BSRational<UInteger> const& x, WwiseGTE::BSRational<UInteger> const& y)
  751. {
  752. return (WwiseGTE::BSRational<UInteger>)std::fmod((double)x, (double)y);
  753. }
  754. template <typename UInteger>
  755. inline WwiseGTE::BSRational<UInteger> frexp(WwiseGTE::BSRational<UInteger> const& x, int* exponent)
  756. {
  757. WwiseGTE::BSRational<UInteger> result = x;
  758. auto& numer = result.GetNumerator();
  759. auto& denom = result.GetDenominator();
  760. int32_t e = numer.GetExponent() - denom.GetExponent();
  761. numer.SetExponent(0);
  762. denom.SetExponent(0);
  763. int32_t saveSign = numer.GetSign();
  764. numer.SetSign(1);
  765. if (numer >= denom)
  766. {
  767. ++e;
  768. numer.SetExponent(-1);
  769. }
  770. numer.SetSign(saveSign);
  771. *exponent = e;
  772. return result;
  773. }
  774. template <typename UInteger>
  775. inline WwiseGTE::BSRational<UInteger> ldexp(WwiseGTE::BSRational<UInteger> const& x, int exponent)
  776. {
  777. WwiseGTE::BSRational<UInteger> result = x;
  778. int biasedExponent = result.GetNumerator().GetBiasedExponent();
  779. biasedExponent += exponent;
  780. result.GetNumerator().SetBiasedExponent(biasedExponent);
  781. return result;
  782. }
  783. template <typename UInteger>
  784. inline WwiseGTE::BSRational<UInteger> log(WwiseGTE::BSRational<UInteger> const& x)
  785. {
  786. return (WwiseGTE::BSRational<UInteger>)std::log((double)x);
  787. }
  788. template <typename UInteger>
  789. inline WwiseGTE::BSRational<UInteger> log2(WwiseGTE::BSRational<UInteger> const& x)
  790. {
  791. return (WwiseGTE::BSRational<UInteger>)std::log2((double)x);
  792. }
  793. template <typename UInteger>
  794. inline WwiseGTE::BSRational<UInteger> log10(WwiseGTE::BSRational<UInteger> const& x)
  795. {
  796. return (WwiseGTE::BSRational<UInteger>)std::log10((double)x);
  797. }
  798. template <typename UInteger>
  799. inline WwiseGTE::BSRational<UInteger> pow(WwiseGTE::BSRational<UInteger> const& x, WwiseGTE::BSRational<UInteger> const& y)
  800. {
  801. return (WwiseGTE::BSRational<UInteger>)std::pow((double)x, (double)y);
  802. }
  803. template <typename UInteger>
  804. inline WwiseGTE::BSRational<UInteger> sin(WwiseGTE::BSRational<UInteger> const& x)
  805. {
  806. return (WwiseGTE::BSRational<UInteger>)std::sin((double)x);
  807. }
  808. template <typename UInteger>
  809. inline WwiseGTE::BSRational<UInteger> sinh(WwiseGTE::BSRational<UInteger> const& x)
  810. {
  811. return (WwiseGTE::BSRational<UInteger>)std::sinh((double)x);
  812. }
  813. template <typename UInteger>
  814. inline WwiseGTE::BSRational<UInteger> sqrt(WwiseGTE::BSRational<UInteger> const& x)
  815. {
  816. return (WwiseGTE::BSRational<UInteger>)std::sqrt((double)x);
  817. }
  818. template <typename UInteger>
  819. inline WwiseGTE::BSRational<UInteger> tan(WwiseGTE::BSRational<UInteger> const& x)
  820. {
  821. return (WwiseGTE::BSRational<UInteger>)std::tan((double)x);
  822. }
  823. template <typename UInteger>
  824. inline WwiseGTE::BSRational<UInteger> tanh(WwiseGTE::BSRational<UInteger> const& x)
  825. {
  826. return (WwiseGTE::BSRational<UInteger>)std::tanh((double)x);
  827. }
  828. // Type trait that says BSRational is a signed type.
  829. template <typename UInteger>
  830. struct is_signed<WwiseGTE::BSRational<UInteger>> : true_type {};
  831. }
  832. namespace WwiseGTE
  833. {
  834. template <typename UInteger>
  835. inline BSRational<UInteger> atandivpi(BSRational<UInteger> const& x)
  836. {
  837. return (BSRational<UInteger>)atandivpi((double)x);
  838. }
  839. template <typename UInteger>
  840. inline BSRational<UInteger> atan2divpi(BSRational<UInteger> const& y, BSRational<UInteger> const& x)
  841. {
  842. return (BSRational<UInteger>)atan2divpi((double)y, (double)x);
  843. }
  844. template <typename UInteger>
  845. inline BSRational<UInteger> clamp(BSRational<UInteger> const& x, BSRational<UInteger> const& xmin, BSRational<UInteger> const& xmax)
  846. {
  847. return (BSRational<UInteger>)clamp((double)x, (double)xmin, (double)xmax);
  848. }
  849. template <typename UInteger>
  850. inline BSRational<UInteger> cospi(BSRational<UInteger> const& x)
  851. {
  852. return (BSRational<UInteger>)cospi((double)x);
  853. }
  854. template <typename UInteger>
  855. inline BSRational<UInteger> exp10(BSRational<UInteger> const& x)
  856. {
  857. return (BSRational<UInteger>)exp10((double)x);
  858. }
  859. template <typename UInteger>
  860. inline BSRational<UInteger> invsqrt(BSRational<UInteger> const& x)
  861. {
  862. return (BSRational<UInteger>)invsqrt((double)x);
  863. }
  864. template <typename UInteger>
  865. inline int isign(BSRational<UInteger> const& x)
  866. {
  867. return isign((double)x);
  868. }
  869. template <typename UInteger>
  870. inline BSRational<UInteger> saturate(BSRational<UInteger> const& x)
  871. {
  872. return (BSRational<UInteger>)saturate((double)x);
  873. }
  874. template <typename UInteger>
  875. inline BSRational<UInteger> sign(BSRational<UInteger> const& x)
  876. {
  877. return (BSRational<UInteger>)sign((double)x);
  878. }
  879. template <typename UInteger>
  880. inline BSRational<UInteger> sinpi(BSRational<UInteger> const& x)
  881. {
  882. return (BSRational<UInteger>)sinpi((double)x);
  883. }
  884. template <typename UInteger>
  885. inline BSRational<UInteger> sqr(BSRational<UInteger> const& x)
  886. {
  887. return (BSRational<UInteger>)sqr((double)x);
  888. }
  889. // See the comments in Math.h about traits is_arbitrary_precision
  890. // and has_division_operator.
  891. template <typename UInteger>
  892. struct is_arbitrary_precision_internal<BSRational<UInteger>> : std::true_type {};
  893. template <typename UInteger>
  894. struct has_division_operator_internal<BSRational<UInteger>> : std::true_type {};
  895. }