Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.52 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Numerics;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. [DebuggerDisplay("{" + nameof(DDisplay) + "}")]
  8. [Serializable]
  9. public struct BigRational : IComparable, IComparable<BigRational>, IEquatable<BigRational>
  10. {
  11. [StructLayout(LayoutKind.Explicit)]
  12. internal struct DoubleUlong
  13. {
  14. [FieldOffset(0)] public double dbl;
  15. [FieldOffset(0)] public ulong uu;
  16. }
  17. private const int DoubleMaxScale = 308;
  18. private static readonly BigRational PI;
  19. private static readonly BigInteger DoublePrecision = BigInteger.Pow(10, DoubleMaxScale);
  20. private static readonly BigInteger DoubleMaxValue = (BigInteger) double.MaxValue;
  21. private static readonly BigInteger DoubleMinValue = (BigInteger) double.MinValue;
  22. static BigRational()
  23. {
  24. PI = GetPI();
  25. }
  26. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  27. private string DDisplay => AsDecimal;
  28. [StructLayout(LayoutKind.Explicit)]
  29. internal struct DecimalUInt32
  30. {
  31. [FieldOffset(0)] public decimal dec;
  32. [FieldOffset(0)] public int flags;
  33. }
  34. private const int DecimalScaleMask = 0x00FF0000;
  35. private const int DecimalSignMask = unchecked((int) 0x80000000);
  36. private const int DecimalMaxScale = 100;
  37. private static readonly BigInteger DecimalPrecision = BigInteger.Pow(10, DecimalMaxScale);
  38. private static readonly BigInteger DecimalMaxValue = (BigInteger) decimal.MaxValue;
  39. private static readonly BigInteger DecimalMinValue = (BigInteger) decimal.MinValue;
  40. private const string Solidus = @"/";
  41. public static BigRational Zero { get; } = new BigRational(BigInteger.Zero);
  42. public static BigRational One { get; } = new BigRational(BigInteger.One);
  43. public static BigRational MinusOne { get; } =
  44. new BigRational(BigInteger.MinusOne);
  45. public int Sign => Numerator.Sign;
  46. public BigInteger Numerator { get; private set; }
  47. public BigInteger Denominator { get; private set; }
  48. public BigInteger GetWholePart => BigInteger.Divide(Numerator, Denominator);
  49. public bool IsFractionalPart
  50. {
  51. get
  52. {
  53. var fp = GetFractionPart;
  54. return fp.Numerator != 0 || fp.Denominator != 1;
  55. }
  56. }
  57. public BigInteger GetUnscaledAsDecimal => Numerator * DecimalPrecision / Denominator;
  58. public BigInteger Remainder => Numerator % Denominator;
  59. public string AsDecimal
  60. {
  61. get
  62. {
  63. var number = GetUnscaledAsDecimal.ToString("G");
  64. return number.Insert(number.Length - DecimalMaxScale, ".");
  65. }
  66. }
  67. public string CleanAsDecimal
  68. {
  69. get
  70. {
  71. var fpas = AsDecimal;
  72. var rs = fpas.Reverse();
  73. var fas = "";
  74. foreach (var c in rs)
  75. if (c == '0')
  76. continue;
  77. else
  78. fas += c;
  79. return fas.Reverse();
  80. }
  81. }
  82. public BigRational GetFractionPart
  83. {
  84. get
  85. {
  86. var rem = BigInteger.Remainder(Numerator, Denominator);
  87. return new BigRational(rem, Denominator);
  88. }
  89. }
  90. public override bool Equals(object obj)
  91. {
  92. if (obj == null)
  93. return false;
  94. if (!(obj is BigRational))
  95. return false;
  96. return Equals((BigRational) obj);
  97. }
  98. public override int GetHashCode()
  99. {
  100. return (Numerator / Denominator).GetHashCode();
  101. }
  102. int IComparable.CompareTo(object obj)
  103. {
  104. if (obj == null)
  105. return 1;
  106. if (!(obj is BigRational))
  107. throw new ArgumentException();
  108. return Compare(this, (BigRational) obj);
  109. }
  110. public int CompareTo(BigRational other)
  111. {
  112. return Compare(this, other);
  113. }
  114. public bool Equals(BigRational other)
  115. {
  116. if (Denominator == other.Denominator)
  117. return Numerator == other.Numerator;
  118. return Numerator * other.Denominator == Denominator * other.Numerator;
  119. }
  120. public BigRational(BigInteger numerator)
  121. {
  122. Numerator = numerator;
  123. Denominator = BigInteger.One;
  124. }
  125. public BigRational(string n, string d)
  126. {
  127. Numerator = new BigInteger().BigIntegerBase10(n);
  128. Denominator = new BigInteger().BigIntegerBase10(d);
  129. }
  130. public BigRational(string value)
  131. {
  132. if (!value.ContainsOnly("0123456789+-.eE"))
  133. throw new Exception(
  134. $"Input value must only contain these '0123456789+-.eE', value'{value}");
  135. var v1 = new BigDecimal(value);
  136. var (unscaledValue, scale) = v1.ToByteArrays();
  137. if (v1 == BigDecimal.Zero)
  138. {
  139. this = Zero;
  140. return;
  141. }
  142. Numerator = new BigInteger(unscaledValue);
  143. Denominator = BigInteger.Pow(10, BitConverter.ToInt32(scale, 0));
  144. Simplify();
  145. }
  146. public static bool TryParse(string parse, out BigRational result)
  147. {
  148. result = default;
  149. if (!parse.ContainsOnly("0123456789+-.eE"))
  150. throw new Exception(
  151. $"Input value must only contain these '0123456789+-.eE', value'{parse}");
  152. try
  153. {
  154. result = new BigRational(parse);
  155. }
  156. catch
  157. {
  158. return false;
  159. }
  160. return true;
  161. }
  162. public BigRational(double value) : this((decimal) value)
  163. {
  164. }
  165. public BigRational(BigDecimal value)
  166. {
  167. var bits = value.ToByteArrays();
  168. if (value == BigDecimal.Zero)
  169. {
  170. this = Zero;
  171. return;
  172. }
  173. Numerator = new BigInteger(bits.unscaledValue);
  174. Denominator = BigInteger.Pow(10, BitConverter.ToInt32(bits.scale, 0));
  175. Simplify();
  176. }
  177. public BigRational(decimal value)
  178. {
  179. var bits = decimal.GetBits(value);
  180. if (bits == null || bits.Length != 4 ||
  181. (bits[3] & ~(DecimalSignMask | DecimalScaleMask)) != 0 ||
  182. (bits[3] & DecimalScaleMask) > 28 << 16)
  183. throw new ArgumentException();
  184. if (value == decimal.Zero)
  185. {
  186. this = Zero;
  187. return;
  188. }
  189. var ul = ((ulong) (uint) bits[2] << 32) | (uint) bits[1];
  190. Numerator = (new BigInteger(ul) << 32) | (uint) bits[0];
  191. var isNegative = (bits[3] & DecimalSignMask) != 0;
  192. if (isNegative)
  193. Numerator = BigInteger.Negate(Numerator);
  194. var scale = (bits[3] & DecimalScaleMask) >> 16;
  195. Denominator = BigInteger.Pow(10, scale);
  196. Simplify();
  197. }
  198. public BigRational(BigInteger numerator, BigInteger denominator)
  199. {
  200. if (denominator.Sign == 0)
  201. throw new DivideByZeroException();
  202. if (numerator.Sign == 0)
  203. {
  204. Numerator = BigInteger.Zero;
  205. Denominator = BigInteger.One;
  206. }
  207. else if (denominator.Sign < 0)
  208. {
  209. Numerator = BigInteger.Negate(numerator);
  210. Denominator = BigInteger.Negate(denominator);
  211. }
  212. else
  213. {
  214. Numerator = numerator;
  215. Denominator = denominator;
  216. }
  217. Simplify();
  218. }
  219. public BigRational(BigInteger whole, BigInteger numerator, BigInteger denominator)
  220. {
  221. if (denominator.Sign == 0)
  222. throw new DivideByZeroException();
  223. if (numerator.Sign == 0 && whole.Sign == 0)
  224. {
  225. Numerator = BigInteger.Zero;
  226. Denominator = BigInteger.One;
  227. }
  228. else if (denominator.Sign < 0)
  229. {
  230. Denominator = BigInteger.Negate(denominator);
  231. Numerator = BigInteger.Negate(whole) * Denominator + BigInteger.Negate(numerator);
  232. }
  233. else
  234. {
  235. Denominator = denominator;
  236. Numerator = whole * denominator + numerator;
  237. }
  238. Simplify();
  239. }
  240. public static BigRational Abs(BigRational r)
  241. {
  242. return r.Numerator.Sign < 0
  243. ? new BigRational(BigInteger.Abs(r.Numerator), r.Denominator)
  244. : r;
  245. }
  246. public static BigRational Negate(BigRational r)
  247. {
  248. return new BigRational(BigInteger.Negate(r.Numerator), r.Denominator);
  249. }
  250. public static BigRational Invert(BigRational r)
  251. {
  252. return new BigRational(r.Denominator, r.Numerator);
  253. }
  254. public static BigRational Add(BigRational x, BigRational y)
  255. {
  256. return x + y;
  257. }
  258. public static BigRational Subtract(BigRational x, BigRational y)
  259. {
  260. return x - y;
  261. }
  262. public static BigRational Multiply(BigRational x, BigRational y)
  263. {
  264. return x * y;
  265. }
  266. public static BigRational Divide(BigRational dividend, BigRational divisor)
  267. {
  268. return dividend / divisor;
  269. }
  270. public static BigRational DivRem(BigRational dividend, BigRational divisor,
  271. out BigRational remainder)
  272. {
  273. var ad = dividend.Numerator * divisor.Denominator;
  274. var bc = dividend.Denominator * divisor.Numerator;
  275. var bd = dividend.Denominator * divisor.Denominator;
  276. remainder = new BigRational(ad % bc, bd);
  277. return new BigRational(ad, bc);
  278. }
  279. public static BigInteger LeastCommonDenominator(BigRational x, BigRational y)
  280. {
  281. return x.Denominator * y.Denominator /
  282. BigInteger.GreatestCommonDivisor(x.Denominator, y.Denominator);
  283. }
  284. public static int Compare(BigRational r1, BigRational r2)
  285. {
  286. return BigInteger.Compare(r1.Numerator * r2.Denominator, r2.Numerator * r1.Denominator);
  287. }
  288. public static bool operator ==(BigRational x, BigRational y)
  289. {
  290. return Compare(x, y) == 0;
  291. }
  292. public static bool operator !=(BigRational x, BigRational y)
  293. {
  294. return Compare(x, y) != 0;
  295. }
  296. public static bool operator <(BigRational x, BigRational y)
  297. {
  298. return Compare(x, y) < 0;
  299. }
  300. public static bool operator <=(BigRational x, BigRational y)
  301. {
  302. return Compare(x, y) <= 0;
  303. }
  304. public static bool operator >(BigRational x, BigRational y)
  305. {
  306. return Compare(x, y) > 0;
  307. }
  308. public static bool operator >=(BigRational x, BigRational y)
  309. {
  310. return Compare(x, y) >= 0;
  311. }
  312. public static BigRational operator +(BigRational r)
  313. {
  314. return r;
  315. }
  316. public static BigRational operator -(BigRational r)
  317. {
  318. return new BigRational(-r.Numerator, r.Denominator);
  319. }
  320. public static BigRational operator ++(BigRational r)
  321. {
  322. return r + One;
  323. }
  324. public static BigRational operator --(BigRational r)
  325. {
  326. return r - One;
  327. }
  328. public static BigRational operator +(BigRational r1, BigRational r2)
  329. {
  330. return new BigRational(r1.Numerator * r2.Denominator + r1.Denominator * r2.Numerator,
  331. r1.Denominator * r2.Denominator);
  332. }
  333. public static BigRational operator -(BigRational r1, BigRational r2)
  334. {
  335. return new BigRational(r1.Numerator * r2.Denominator - r1.Denominator * r2.Numerator,
  336. r1.Denominator * r2.Denominator);
  337. }
  338. public static BigRational operator *(BigRational r1, BigRational r2)
  339. {
  340. return new BigRational(r1.Numerator * r2.Numerator, r1.Denominator * r2.Denominator);
  341. }
  342. public static BigRational operator /(BigRational r1, BigRational r2)
  343. {
  344. return new BigRational(r1.Numerator * r2.Denominator, r1.Denominator * r2.Numerator);
  345. }
  346. public static BigRational operator %(BigRational r1, BigRational r2)
  347. {
  348. return new BigRational(r1.Numerator * r2.Denominator % (r1.Denominator * r2.Numerator),
  349. r1.Denominator * r2.Denominator);
  350. }
  351. public static explicit operator sbyte(BigRational value)
  352. {
  353. return (sbyte) BigInteger.Divide(value.Numerator, value.Denominator);
  354. }
  355. public static explicit operator ushort(BigRational value)
  356. {
  357. return (ushort) BigInteger.Divide(value.Numerator, value.Denominator);
  358. }
  359. public static explicit operator uint(BigRational value)
  360. {
  361. return (uint) BigInteger.Divide(value.Numerator, value.Denominator);
  362. }
  363. public static explicit operator ulong(BigRational value)
  364. {
  365. return (ulong) BigInteger.Divide(value.Numerator, value.Denominator);
  366. }
  367. public static explicit operator byte(BigRational value)
  368. {
  369. return (byte) BigInteger.Divide(value.Numerator, value.Denominator);
  370. }
  371. public static explicit operator short(BigRational value)
  372. {
  373. return (short) BigInteger.Divide(value.Numerator, value.Denominator);
  374. }
  375. public static explicit operator int(BigRational value)
  376. {
  377. return (int) BigInteger.Divide(value.Numerator, value.Denominator);
  378. }
  379. public static explicit operator long(BigRational value)
  380. {
  381. return (long) BigInteger.Divide(value.Numerator, value.Denominator);
  382. }
  383. public static explicit operator BigInteger(BigRational value)
  384. {
  385. return BigInteger.Divide(value.Numerator, value.Denominator);
  386. }
  387. public static explicit operator float(BigRational value)
  388. {
  389. return (float) (double) value;
  390. }
  391. public static explicit operator double(BigRational value)
  392. {
  393. if (SafeCastToDouble(value.Numerator) && SafeCastToDouble(value.Denominator))
  394. return (double) value.Numerator / (double) value.Denominator;
  395. var denormalized = value.Numerator * DoublePrecision / value.Denominator;
  396. if (denormalized.IsZero)
  397. return value.Sign < 0
  398. ? BitConverter.Int64BitsToDouble(unchecked((long) 0x8000000000000000))
  399. : 0d;
  400. double result = 0;
  401. var isDouble = false;
  402. var scale = DoubleMaxScale;
  403. while (scale > 0)
  404. {
  405. if (!isDouble)
  406. if (SafeCastToDouble(denormalized))
  407. {
  408. result = (double) denormalized;
  409. isDouble = true;
  410. }
  411. else
  412. {
  413. denormalized = denormalized / 10;
  414. }
  415. result = result / 10;
  416. scale--;
  417. }
  418. if (!isDouble)
  419. return value.Sign < 0 ? double.NegativeInfinity : double.PositiveInfinity;
  420. return result;
  421. }
  422. public static explicit operator BigDecimal(BigRational value)
  423. {
  424. var denormalized = value.Numerator * DecimalPrecision / value.Denominator;
  425. return new BigDecimal(denormalized, DecimalMaxScale);
  426. }
  427. public static explicit operator decimal(BigRational value)
  428. {
  429. if (SafeCastToDecimal(value.Numerator) && SafeCastToDecimal(value.Denominator))
  430. return (decimal) value.Numerator / (decimal) value.Denominator;
  431. var denormalized = value.Numerator * DecimalPrecision / value.Denominator;
  432. if (denormalized.IsZero)
  433. return decimal.Zero;
  434. for (var scale = DecimalMaxScale; scale >= 0; scale--)
  435. if (!SafeCastToDecimal(denormalized))
  436. {
  437. denormalized /= 10;
  438. }
  439. else
  440. {
  441. var dec = new DecimalUInt32();
  442. dec.dec = (decimal) denormalized;
  443. dec.flags = (dec.flags & ~DecimalScaleMask) | (scale << 16);
  444. return dec.dec;
  445. }
  446. throw new OverflowException();
  447. }
  448. public static implicit operator BigRational(sbyte value)
  449. {
  450. return new BigRational((BigInteger) value);
  451. }
  452. public static implicit operator BigRational(ushort value)
  453. {
  454. return new BigRational((BigInteger) value);
  455. }
  456. public static implicit operator BigRational(uint value)
  457. {
  458. return new BigRational((BigInteger) value);
  459. }
  460. public static implicit operator BigRational(ulong value)
  461. {
  462. return new BigRational((BigInteger) value);
  463. }
  464. public static implicit operator BigRational(byte value)
  465. {
  466. return new BigRational((BigInteger) value);
  467. }
  468. public static implicit operator BigRational(short value)
  469. {
  470. return new BigRational((BigInteger) value);
  471. }
  472. public static implicit operator BigRational(int value)
  473. {
  474. return new BigRational((BigInteger) value);
  475. }
  476. public static implicit operator BigRational(long value)
  477. {
  478. return new BigRational((BigInteger) value);
  479. }
  480. public static implicit operator BigRational(BigInteger value)
  481. {
  482. return new BigRational(value);
  483. }
  484. public static implicit operator BigRational(string value)
  485. {
  486. return new BigRational(value);
  487. }
  488. public static implicit operator BigRational(float value)
  489. {
  490. return new BigRational(value);
  491. }
  492. public static implicit operator BigRational(double value)
  493. {
  494. return new BigRational(value);
  495. }
  496. public static implicit operator BigRational(decimal value)
  497. {
  498. return new BigRational(value);
  499. }
  500. public static implicit operator BigRational(BigDecimal value)
  501. {
  502. return new BigRational(value);
  503. }
  504. private void Simplify()
  505. {
  506. if (Numerator == BigInteger.Zero)
  507. Denominator = BigInteger.One;
  508. var gcd = BigInteger.GreatestCommonDivisor(Numerator, Denominator);
  509. if (gcd > BigInteger.One)
  510. {
  511. Numerator = Numerator / gcd;
  512. Denominator = Denominator / gcd;
  513. }
  514. }
  515. private static bool SafeCastToDouble(BigInteger value)
  516. {
  517. return DoubleMinValue <= value && value <= DoubleMaxValue;
  518. }
  519. private static bool SafeCastToDecimal(BigInteger value)
  520. {
  521. return DecimalMinValue <= value && value <= DecimalMaxValue;
  522. }
  523. private static void SplitDoubleIntoParts(double dbl, out int sign, out int exp, out ulong man,
  524. out bool isFinite)
  525. {
  526. DoubleUlong du;
  527. du.uu = 0;
  528. du.dbl = dbl;
  529. sign = 1 - ((int) (du.uu >> 62) & 2);
  530. man = du.uu & 0x000FFFFFFFFFFFFF;
  531. exp = (int) (du.uu >> 52) & 0x7FF;
  532. if (exp == 0)
  533. {
  534. isFinite = true;
  535. if (man != 0)
  536. exp = -1074;
  537. }
  538. else if (exp == 0x7FF)
  539. {
  540. isFinite = false;
  541. exp = int.MaxValue;
  542. }
  543. else
  544. {
  545. isFinite = true;
  546. man |= 0x0010000000000000;
  547. exp -= 1075;
  548. }
  549. }
  550. public static double GetDoubleFromParts(int sign, int exp, ulong man)
  551. {
  552. DoubleUlong du;
  553. du.dbl = 0;
  554. if (man == 0)
  555. {
  556. du.uu = 0;
  557. }
  558. else
  559. {
  560. var cbitShift = CbitHighZero(man) - 11;
  561. if (cbitShift < 0)
  562. man >>= -cbitShift;
  563. else
  564. man <<= cbitShift;
  565. exp += 1075;
  566. if (exp >= 0x7FF)
  567. {
  568. du.uu = 0x7FF0000000000000;
  569. }
  570. else if (exp <= 0)
  571. {
  572. exp--;
  573. if (exp < -52)
  574. du.uu = 0;
  575. else
  576. du.uu = man >> -exp;
  577. }
  578. else
  579. {
  580. du.uu = (man & 0x000FFFFFFFFFFFFF) | ((ulong) exp << 52);
  581. }
  582. }
  583. if (sign < 0)
  584. du.uu |= 0x8000000000000000;
  585. return du.dbl;
  586. }
  587. private static int CbitHighZero(ulong uu)
  588. {
  589. if ((uu & 0xFFFFFFFF00000000) == 0)
  590. return 32 + CbitHighZero((uint) uu);
  591. return CbitHighZero((uint) (uu >> 32));
  592. }
  593. private static int CbitHighZero(uint u)
  594. {
  595. if (u == 0)
  596. return 32;
  597. var cbit = 0;
  598. if ((u & 0xFFFF0000) == 0)
  599. {
  600. cbit += 16;
  601. u <<= 16;
  602. }
  603. if ((u & 0xFF000000) == 0)
  604. {
  605. cbit += 8;
  606. u <<= 8;
  607. }
  608. if ((u & 0xF0000000) == 0)
  609. {
  610. cbit += 4;
  611. u <<= 4;
  612. }
  613. if ((u & 0xC0000000) == 0)
  614. {
  615. cbit += 2;
  616. u <<= 2;
  617. }
  618. if ((u & 0x80000000) == 0)
  619. cbit += 1;
  620. return cbit;
  621. }
  622. private static (BigRational High, BigRational Low) SqrtLimits(BigInteger number)
  623. {
  624. if (number == BigInteger.Zero) return (0, 0);
  625. var high = number >> 1;
  626. var low = BigInteger.Zero;
  627. while (high > low + 1)
  628. {
  629. var n = (high + low) >> 1;
  630. var p = n * n;
  631. if (number < p)
  632. high = n;
  633. else if (number > p)
  634. low = n;
  635. else
  636. break;
  637. }
  638. return (high, low);
  639. }
  640. public BigRational Sqrt()
  641. {
  642. if (this == 0) return 0;
  643. var hl = SqrtLimits(GetWholePart);
  644. BigRational n = 0, p = 0;
  645. if (hl.High == 0 && hl.Low == 0)
  646. return 0;
  647. var high = hl.High;
  648. var low = hl.Low;
  649. var d = DecimalPrecision;
  650. var pp = 1 / (BigRational) d;
  651. while (high > low + pp)
  652. {
  653. n = (high + low) / 2;
  654. p = n * n;
  655. if (this < p)
  656. high = n;
  657. else if (this > p)
  658. low = n;
  659. else
  660. break;
  661. }
  662. return this == p ? n : low;
  663. }
  664. public static BigRational ArcTangent(BigRational v, int iterations)
  665. {
  666. var retVal = v;
  667. for (var i = 1; i < iterations; i++)
  668. {
  669. var powRat = Pow(v, 2 * i + 1);
  670. retVal += new BigRational(powRat.Numerator * (BigInteger) Math.Pow(-1d, i),
  671. (2 * i + 1) * powRat.Denominator);
  672. }
  673. return retVal;
  674. }
  675. public static BigRational Reciprocal(BigRational v)
  676. {
  677. return new BigRational(v.Denominator, v.Numerator);
  678. }
  679. public static BigRational Inverse(BigRational v)
  680. {
  681. return One / new BigRational(v.Denominator, v.Numerator);
  682. }
  683. public static BigRational Pow(BigRational v, int e)
  684. {
  685. if (e < 1) throw new ArgumentException("Powers must be greater than or equal to one.");
  686. var retVal = new BigRational(v.Numerator, v.Denominator);
  687. for (var i = 1; i < e; i++)
  688. {
  689. retVal.Numerator *= v.Numerator;
  690. retVal.Denominator *= v.Denominator;
  691. }
  692. return retVal;
  693. }
  694. public static BigRational Min(BigRational r, BigRational l)
  695. {
  696. return l < r ? l : r;
  697. }
  698. public static BigRational Max(BigRational r, BigRational l)
  699. {
  700. return l > r ? l : r;
  701. }
  702. public static BigRational ToRadians(BigRational degrees)
  703. {
  704. return degrees * PI / 180;
  705. }
  706. public static BigRational ToDegrees(BigRational rads)
  707. {
  708. return rads * 180 / PI;
  709. }
  710. private static int ConversionIterations(BigRational v)
  711. {
  712. return (int) ((DecimalMaxScale + 1) / (2 * Math.Log10((double) Reciprocal(v))));
  713. }
  714. public static BigRational GetPI()
  715. {
  716. var oneFifth = new BigRational(1, 5);
  717. var oneTwoThirtyNine = new BigRational(1, 239);
  718. var arcTanOneFifth = ArcTangent(oneFifth, ConversionIterations(oneFifth));
  719. var arcTanOneTwoThirtyNine =
  720. ArcTangent(oneTwoThirtyNine, ConversionIterations(oneTwoThirtyNine));
  721. return arcTanOneFifth * 16 - arcTanOneTwoThirtyNine * 4;
  722. }
  723. public override string ToString()
  724. {
  725. var ret = new StringBuilder();
  726. ret.Append(Numerator.ToString("R", CultureInfo.InvariantCulture));
  727. ret.Append(Solidus);
  728. ret.Append(Denominator.ToString("R", CultureInfo.InvariantCulture));
  729. return ret.ToString();
  730. }
  731. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement