Skip to content

Commit b3bf8c8

Browse files
committed
Better PI calculator
1 parent 1191e7b commit b3bf8c8

3 files changed

Lines changed: 22 additions & 31 deletions

File tree

MattSourceGenHelpers.Examples/MattSourceGenHelpers.Examples.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@
1616
<ItemGroup>
1717
<ProjectReference Include="..\MattSourceGenHelpers.Abstractions\MattSourceGenHelpers.Abstractions.csproj" />
1818
</ItemGroup>
19+
20+
<ItemGroup>
21+
<PackageReference Include="ExtendedNumerics.BigDecimal" Version="3003.0.0.346" />
22+
</ItemGroup>
1923

2024
</Project>

MattSourceGenHelpers.Examples/PiExampleFluent.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public static int GetPiDecimal(int decimalNumber)
2626
case 0: return 3;
2727
case 1: return 1;
2828
case 2: return 4;
29-
case 300: return 2;
30-
case 302: return 9;
31-
case 303: return 8;
29+
case 300: return 3;
30+
case 301: return 7;
31+
case 302: return 2;
32+
case 303: return 4;
3233
default: return CalculatePiDecimal(decimalNumber);
3334
}
3435
}
Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1-
namespace MattSourceGenHelpers.Examples;
1+
using System.Globalization;
2+
using ExtendedNumerics;
23

3-
public class SlowMath
4-
{
5-
// Pi digits string (0-indexed: position 0 = '3', position 1 = '1', position 2 = '4', ...)
6-
private static readonly string PiDigits =
7-
"31415926535897932384626433832795028841971693993751" +
8-
"05820974944592307816406286208998628034825342117067" +
9-
"98214808651328230664709384460955058223172535940812" +
10-
"84811174502841027019385211055596446229489549303819" +
11-
"64428810975665933446128475648233786783165271201909" +
12-
"14564856692346034861045432664821339360726024914127" +
13-
"37245870066063155881748815209209628292540917153643" +
14-
"67892590360011330530548820466521384146951941511609" +
15-
"43305727036575959195309218611738193261179310511854" +
16-
"80744623799627495673518857527248912279381830119491" +
17-
"29833673362440656643086021394946395224737190702179" +
18-
"86094370277053921717629317675238467481846766940513" +
19-
"20005681271452635608277857713427577896091736371787" +
20-
"21468440901224953430146549585371050792279689258923" +
21-
"54201995611212902196086403441815981362977477130996" +
22-
"05187072113499999983729780499510597317328160963185" +
23-
"95024459455346908302642522308253344685035261931188" +
24-
"17101000313783875288658753320838142061717766914730" +
25-
"35982534904287554687311595628638823537875937519577" +
26-
"81857780532171226806613001927876611195909216420198";
4+
namespace MattSourceGenHelpers.Examples;
275

6+
public static class SlowMath
7+
{
288
public static int CalculatePiDecimal(int decimalNumber)
299
{
30-
if (decimalNumber >= 0 && decimalNumber < PiDigits.Length)
31-
return PiDigits[decimalNumber] - '0';
32-
return 0;
10+
if (decimalNumber < 0) throw new ArgumentOutOfRangeException(nameof(decimalNumber), "Decimal number must be non-negative.");
11+
if (decimalNumber == 0) return 3;
12+
13+
BigDecimal pi = BigDecimal.ApproximatePi(decimalNumber + 1);
14+
15+
if(pi.DecimalPlaces < decimalNumber) throw new ArgumentException($"Failed to calculate pi to {decimalNumber} decimal places.");
16+
17+
string piString = pi.ToString(CultureInfo.InvariantCulture);
18+
return int.Parse(piString.Substring(decimalNumber + 1, 1));
3319
}
3420
}

0 commit comments

Comments
 (0)