Skip to content

Commit 1878409

Browse files
author
Oren (electricessence)
committed
Tweaks.
1 parent 9395f41 commit 1878409

File tree

3 files changed

+4
-256
lines changed

3 files changed

+4
-256
lines changed

MathExtensions.cs

Lines changed: 0 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9-
using Open.Formatting;
109

1110
namespace Open.Arithmetic
1211
{
@@ -161,7 +160,6 @@ public static double Quotient(this IEnumerable<double> source)
161160
return result;
162161
}
163162

164-
165163
public static double QuotientOf(this IEnumerable<double> divisors, double numerator)
166164
{
167165
var any = false;
@@ -198,194 +196,6 @@ public static double Difference(this IEnumerable<double> source)
198196
}
199197

200198

201-
202-
/// <summary>
203-
/// Ensures addition tolerance by trimming off unexpected imprecision.
204-
/// </summary>
205-
public static double SumAccurate(this double source, double value)
206-
{
207-
var result = source + value;
208-
var vp = source.DecimalPlaces();
209-
if (vp > 15)
210-
return result;
211-
var ap = value.DecimalPlaces();
212-
if (ap > 15)
213-
return result;
214-
215-
var digits = Math.Max(vp, ap);
216-
217-
return Math.Round(result, digits);
218-
}
219-
220-
/// <summary>
221-
/// Ensures addition tolerance by trimming off unexpected imprecision.
222-
/// </summary>
223-
public static double ProductAccurate(this double source, double value)
224-
{
225-
var result = source * value;
226-
var vp = source.DecimalPlaces();
227-
if (vp > 15)
228-
return result;
229-
var ap = value.DecimalPlaces();
230-
if (ap > 15)
231-
return result;
232-
233-
var digits = Math.Max(vp, ap);
234-
235-
return Math.Round(result, digits);
236-
}
237-
238-
/// Ensures addition tolerance by using integer math.
239-
public static double SumUsingIntegers(this double source, double value)
240-
{
241-
var x = Math.Pow(10, Math.Max(source.DecimalPlaces(), value.DecimalPlaces()));
242-
243-
var v = (long)(source * x);
244-
var a = (long)(value * x);
245-
var result = v + a;
246-
return (double)result / x;
247-
}
248-
249-
250-
251-
public static bool IsPrime(this int n)
252-
{
253-
var a = System.Math.Abs(n);
254-
if (a == 2 || a == 3)
255-
return true;
256-
257-
if (a % 2 == 0 || a % 3 == 0)
258-
return false;
259-
260-
var divisor = 6;
261-
while (divisor * divisor - 2 * divisor + 1 <= a)
262-
{
263-
264-
if (a % (divisor - 1) == 0)
265-
return false;
266-
267-
if (a % (divisor + 1) == 0)
268-
return false;
269-
270-
divisor += 6;
271-
272-
}
273-
274-
return true;
275-
276-
}
277-
278-
public static bool IsPrime(this double n)
279-
{
280-
return (System.Math.Floor(n) == n)
281-
? IsPrime((int)n)
282-
: false;
283-
}
284-
285-
public static ulong NextPrime(this ulong n)
286-
{
287-
if (n == 0)
288-
return 0;
289-
290-
while (!IsPrime(n))
291-
{
292-
if (n == ulong.MaxValue)
293-
throw new Exception(string.Format("The next prime is greater than the UInt64.MaxValue of {0}.", ulong.MaxValue));
294-
295-
n++;
296-
}
297-
298-
return n;
299-
}
300-
301-
public static int NextPrime(this int n)
302-
{
303-
if (n == 0)
304-
return 0;
305-
306-
var sign = n < 0 ? -1 : 1;
307-
n = Math.Abs(n);
308-
309-
while (!IsPrime(++n))
310-
{ }
311-
312-
return sign * n;
313-
}
314-
315-
public static int NextPrime(this double a)
316-
{
317-
return NextPrime((int)a);
318-
}
319-
320-
public static IEnumerable<int> Multiples(this int a)
321-
{
322-
yield return a < 1 ? -1 : 1;
323-
if (a < 1) a = Math.Abs(a);
324-
325-
for (var i = 2; a != 1 && i <= a; i = i.NextPrime())
326-
{
327-
while (a % i == 0)
328-
{
329-
yield return i;
330-
a /= i;
331-
}
332-
}
333-
}
334-
335-
336-
public static IEnumerable<double> Multiples(this double a)
337-
{
338-
if (double.IsNaN(a))
339-
{
340-
yield return a;
341-
}
342-
else
343-
{
344-
yield return a < 1 ? -1 : 1;
345-
if (a < 1) a = Math.Abs(a);
346-
347-
if (double.IsInfinity(a) || Math.Floor(a) != a)
348-
{
349-
yield return a;
350-
}
351-
else
352-
{
353-
for (var i = 2; a != 1 && i <= a; i = i.NextPrime())
354-
{
355-
while (a % i == 0)
356-
{
357-
yield return i;
358-
a /= i;
359-
}
360-
}
361-
}
362-
}
363-
}
364-
365-
// returns all the primes that do not include 1 or the number itself.
366-
public static IEnumerable<double> DivisibleMultiples(this double a)
367-
{
368-
var m = a.Multiples().Skip(1);
369-
370-
foreach (var i in m)
371-
{
372-
if (i == a) break;
373-
yield return i;
374-
}
375-
}
376-
377-
// returns all the primes that do not include 1 or the number itself.
378-
public static IEnumerable<int> DivisibleMultiples(this int a)
379-
{
380-
var m = a.Multiples().Skip(1);
381-
382-
foreach (var i in m)
383-
{
384-
if (i == a) break;
385-
yield return i;
386-
}
387-
}
388-
389199
}
390200
}
391201

Open.Arithmetic.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ Part of the "Open" set of libraries.</Description>
1717
<PackageTags>dotnet, dotnet-core, dotnetcore, cs, math, dynamic, arithmetic</PackageTags>
1818
</PropertyGroup>
1919

20+
<ItemGroup>
21+
<PackageReference Include="Open.Collections" Version="1.1.0" />
22+
</ItemGroup>
23+
2024
</Project>

ProcedureResult.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)