1 文本格式
using System;
namespace Legalsoft.Truffer
{
public class Eulsum
{
private double[] wksp { get; set; }
private int n { get; set; }
private int ncv { get; set; }
public bool cnvgd { get; set; }
private double sum { get; set; }
private double eps { get; set; }
private double lastval { get; set; }
private double lasteps { get; set; }
public Eulsum(int nmax, double epss)
{
this.wksp = new double[nmax];
this.n = 0;
this.ncv = 0;
this.cnvgd = false;
this.sum = 0.0;
this.eps = epss;
this.lastval = 0.0;
}
public double next(double term)
{
if (n + 1 > wksp.Length)
{
throw new Exception("wksp too small in eulsum");
}
if (n == 0)
{
sum = 0.5 * (wksp[n++] = term);
}
else
{
double tmp = wksp[0];
wksp[0] = term;
for (int j = 1; j < n; j++)
{
double dum = wksp[j];
wksp[j] = 0.5 * (wksp[j - 1] + tmp);
tmp = dum;
}
wksp[n] = 0.5 * (wksp[n - 1] + tmp);
if (Math.Abs(wksp[n]) <= Math.Abs(wksp[n - 1]))
{
sum += (0.5 * wksp[n++]);
}
else
{
sum += wksp[n];
}
}
lasteps = Math.Abs(sum - lastval);
if (lasteps <= eps)
{
ncv++;
}
if (ncv >= 2)
{
cnvgd = true;
}
return (lastval = sum);
}
}
}
2 代码格式
using System;namespace Legalsoft.Truffer
{public class Eulsum{private double[] wksp { get; set; }private int n { get; set; }private int ncv { get; set; }public bool cnvgd { get; set; }private double sum { get; set; }private double eps { get; set; }private double lastval { get; set; }private double lasteps { get; set; }public Eulsum(int nmax, double epss){this.wksp = new double[nmax];this.n = 0;this.ncv = 0;this.cnvgd = false;this.sum = 0.0;this.eps = epss;this.lastval = 0.0;}public double next(double term){if (n + 1 > wksp.Length){throw new Exception("wksp too small in eulsum");}if (n == 0){sum = 0.5 * (wksp[n++] = term);}else{double tmp = wksp[0];wksp[0] = term;for (int j = 1; j < n; j++){double dum = wksp[j];wksp[j] = 0.5 * (wksp[j - 1] + tmp);tmp = dum;}wksp[n] = 0.5 * (wksp[n - 1] + tmp);if (Math.Abs(wksp[n]) <= Math.Abs(wksp[n - 1])){sum += (0.5 * wksp[n++]);}else{sum += wksp[n];}}lasteps = Math.Abs(sum - lastval);if (lasteps <= eps){ncv++;}if (ncv >= 2){cnvgd = true;}return (lastval = sum);}}
}