Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinomialCoefficient.cs
1using System;
6
8{
13 {
23 : base(Left, Right, Start, Length, Expression)
24 {
25 }
26
33 public override IElement Evaluate(double Left, double Right)
34 {
35 if (Left < 0 || Left > int.MaxValue || Right < 0 || Right > int.MaxValue || Left != Math.Truncate(Left) || Right != Math.Truncate(Right))
36 throw new ScriptRuntimeException("Expected non-negative integers.", this);
37
38 int n = (int)Left;
39 int k = (int)Right;
40 int n_k = n - k;
41 if (n_k < 0)
42 throw new ScriptRuntimeException("The Denominator in the OVER operator, cannot be larger than the numerator", this);
43
44 double Result = 1;
45
46 int Max = Math.Max(k, n_k);
47 int Min = Math.Min(k, n_k);
48
49 while (n > Max)
50 {
51 Result *= n;
52 n--;
53
54 while (Result > 1 && Min > 1)
55 {
56 Result /= Min;
57 Min--;
58 }
59 }
60
61 while (Min > 1)
62 {
63 Result /= Min;
64 Min--;
65 }
66
67 return new DoubleNumber(Math.Round(Result)); // Any decimals are a result of rounding errors induced by the algorithm.
68 }
69
70 }
71}
Class managing a script expression.
Definition: Expression.cs:39
Base class for binary double operators.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
BinomialCoefficient(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Binomial coefficient.
override IElement Evaluate(double Left, double Right)
Evaluates the double operator.
Basic interface for all types of elements.
Definition: IElement.cs:20