/// <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> /// 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> /// 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."); try { IReliableDictionary <int, Estimate> estimateDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <int, Estimate> >("estimateDictionary"); while (true) { cancellationToken.ThrowIfCancellationRequested(); using (ITransaction tx = this.StateManager.CreateTransaction()) { await estimateDictionary.AddOrUpdateAsync( tx, 1, Estimate.PI(null), (key, value) => { Estimate next = Estimate.PI(value); ServiceEventSource.Current.ServiceMessage(this, next.ToString()); return(next); }); await tx.CommitAsync(); } await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); } } catch (Exception ex) { Trace.WriteLine(ex); throw; } }
/// <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); }