/// <summary> /// Gets the next precision of Pi value based on the previous value. If there were no previous value, a new Pi value is created from scratch. /// Follows the Liebniz formula for pi. ref http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 /// </summary> /// <param name="previousEstimate">previous estimate</param> /// <returns>new estimate value</returns> public static Estimate PI(Estimate previousEstimate) { if (previousEstimate == null) { return(new Estimate()); } int prevCount = previousEstimate.IterationCount; int fraction = (previousEstimate.IterationCount * 2) + 1; int count = prevCount + 1; double denominator = fraction; double newValue; if (count % 2 != 0) { newValue = previousEstimate.EstimatedValue + (LeibnizNumerator / denominator); } else { newValue = previousEstimate.EstimatedValue - (LeibnizNumerator / denominator); } Estimate e = new Estimate(); e.IterationCount = count; e.EstimatedValue = newValue; return(e); }
/// <summary> /// Follows the Liebniz formula for pi. ref http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 /// </summary> /// <param name="previousEstimate">previous estimate</param> /// <returns>new estimate value</returns> public static Estimate PI(Estimate previousEstimate) { if (previousEstimate == null) { return new Estimate(); } int prevCount = previousEstimate.IterationCount; int fraction = (previousEstimate.IterationCount*2) + 1; int count = prevCount + 1; double denominator = fraction; double newValue; if (count%2 != 0) { newValue = previousEstimate.EstimatedValue + (LeibnizNumerator/denominator); } else { newValue = previousEstimate.EstimatedValue - (LeibnizNumerator/denominator); } Estimate e = new Estimate(); e.IterationCount = count; e.EstimatedValue = newValue; return e; }
/// <summary> /// Compares two <see cref="Estimate"/> values /// </summary> /// <param name="x">First estimate</param> /// <param name="y">Second estimate</param> /// <returns>true if <paramref name="x"/> is exactly equal to <paramref name="y"/></returns> public static bool Equals(Estimate x, Estimate y) { if (x == y) { return(true); } if (x == null || y == null) { return(true); } return(x.IterationCount == y.IterationCount && x.EstimatedValue == y.EstimatedValue); }
/// <summary> /// compares two <see cref="Estimate"/> values /// </summary> /// <param name="x">First estimate</param> /// <param name="y">Second estimate</param> /// <returns>true if <paramref name="x"/> is exactly equal to <paramref name="y"/></returns> public static bool Equals(Estimate x, Estimate y) { if (x == y) { return true; } if (x == null || y == null) { return true; } return x.IterationCount == y.IterationCount && x.EstimatedValue == y.EstimatedValue; }
protected override async Task RunAsync(CancellationToken cancellationToken) { Trace.WriteLine("Starting Pi estimation."); Estimate next = Estimate.PI(null); while (true) { cancellationToken.ThrowIfCancellationRequested(); next = Estimate.PI(next); ServiceEventSource.Current.ServiceMessage(this, next.ToString()); await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); } }
protected override async Task RunAsync(CancellationToken cancellationToken) { Trace.WriteLine("Starting Pi estimation."); Estimate next = Estimate.PI(null); while (true) { cancellationToken.ThrowIfCancellationRequested(); next = Estimate.PI(next); Trace.WriteLine(next); await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); } }
/// <summary> /// Compares this instance to another <see cref="Estimate"/> /// </summary> /// <param name="x">the <see cref="Estimate"/> to compare with</param> /// <returns>true if this <see cref="Estimate"/> is exactly equal to <paramref name="x"/></returns> public bool Equals(Estimate x) { return(Equals(this, x)); }
/// <summary> /// compares this instance to another <see cref="Estimate"/> /// </summary> /// <param name="x">the <see cref="Estimate"/> to compare with</param> /// <returns>true if this <see cref="Estimate"/> is exactly equal to <paramref name="x"/></returns> public bool Equals(Estimate x) { return Equals(this, x); }