本文整理汇总了C#中BigDecimal.Subtract方法的典型用法代码示例。如果您正苦于以下问题:C# BigDecimal.Subtract方法的具体用法?C# BigDecimal.Subtract怎么用?C# BigDecimal.Subtract使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.Subtract方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Log
public static BigDecimal Log(BigDecimal x)
{
/* the value is undefined if x is negative.
*/
if (x.CompareTo(BigDecimal.Zero) < 0)
throw new ArithmeticException("Cannot take log of negative " + x);
if (x.CompareTo(BigDecimal.One) == 0) {
/* log 1. = 0. */
return ScalePrecision(BigDecimal.Zero, x.Precision - 1);
}
if (System.Math.Abs(x.ToDouble() - 1.0) <= 0.3) {
/* The standard Taylor series around x=1, z=0, z=x-1. Abramowitz-Stegun 4.124.
* The absolute error is err(z)/(1+z) = err(x)/x.
*/
BigDecimal z = ScalePrecision(x.Subtract(BigDecimal.One), 2);
BigDecimal zpown = z;
double eps = 0.5*x.Ulp().ToDouble()/System.Math.Abs(x.ToDouble());
BigDecimal resul = z;
for (int k = 2;; k++) {
zpown = MultiplyRound(zpown, z);
BigDecimal c = DivideRound(zpown, k);
if (k%2 == 0)
resul = resul.Subtract(c);
else
resul = resul.Add(c);
if (System.Math.Abs(c.ToDouble()) < eps)
break;
}
var mc = new MathContext(ErrorToPrecision(resul.ToDouble(), eps));
return resul.Round(mc);
} else {
double xDbl = x.ToDouble();
double xUlpDbl = x.Ulp().ToDouble();
/* Map log(x) = log root[r](x)^r = r*log( root[r](x)) with the aim
* to move roor[r](x) near to 1.2 (that is, below the 0.3 appearing above), where log(1.2) is roughly 0.2.
*/
var r = (int) (System.Math.Log(xDbl)/0.2);
/* Since the actual requirement is a function of the value 0.3 appearing above,
* we avoid the hypothetical case of endless recurrence by ensuring that r >= 2.
*/
r = System.Math.Max(2, r);
/* Compute r-th root with 2 additional digits of precision
*/
BigDecimal xhighpr = ScalePrecision(x, 2);
BigDecimal resul = Root(r, xhighpr);
resul = Log(resul).Multiply(new BigDecimal(r));
/* error propagation: log(x+errx) = log(x)+errx/x, so the absolute error
* in the result equals the relative error in the input, xUlpDbl/xDbl .
*/
var mc = new MathContext(ErrorToPrecision(resul.ToDouble(), xUlpDbl/xDbl));
return resul.Round(mc);
}
}
示例2: Calculate
public override Number Calculate(BigDecimal bigDecimal1, BigDecimal bigDecimal2)
{
if (bigDecimal1 == null || bigDecimal2 == null)
{
return 0;
}
return bigDecimal1.Subtract(bigDecimal2);
}
示例3: SubtractRound
public static BigDecimal SubtractRound(BigDecimal x, BigDecimal y)
{
BigDecimal resul = x.Subtract(y);
// The estimation of the absolute error in the result is |err(y)|+|err(x)|
double errR = System.Math.Abs(y.Ulp().ToDouble()/2d) + System.Math.Abs(x.Ulp().ToDouble()/2d);
var mc = new MathContext(ErrorToPrecision(resul.ToDouble(), errR));
return resul.Round(mc);
}
示例4: Root
public static BigDecimal Root(int n, BigDecimal x)
{
if (x.CompareTo(BigDecimal.Zero) < 0)
throw new ArithmeticException("negative argument " + x + " of root");
if (n <= 0)
throw new ArithmeticException("negative power " + n + " of root");
if (n == 1)
return x;
/* start the computation from a double precision estimate */
var s = new BigDecimal(System.Math.Pow(x.ToDouble(), 1.0/n));
/* this creates nth with nominal precision of 1 digit
*/
var nth = new BigDecimal(n);
/* Specify an internal accuracy within the loop which is
* slightly larger than what is demanded by 'eps' below.
*/
BigDecimal xhighpr = ScalePrecision(x, 2);
var mc = new MathContext(2 + x.Precision);
/* Relative accuracy of the result is eps.
*/
double eps = x.Ulp().ToDouble()/(2*n*x.ToDouble());
for (;;) {
/* s = s -(s/n-x/n/s^(n-1)) = s-(s-x/s^(n-1))/n; test correction s/n-x/s for being
* smaller than the precision requested. The relative correction is (1-x/s^n)/n,
*/
BigDecimal c = xhighpr.Divide(s.Pow(n - 1), mc);
c = s.Subtract(c);
var locmc = new MathContext(c.Precision);
c = c.Divide(nth, locmc);
s = s.Subtract(c);
if (System.Math.Abs(c.ToDouble()/s.ToDouble()) < eps)
break;
}
return s.Round(new MathContext(ErrorToPrecision(eps)));
}
示例5: SubtractWithContext
public void SubtractWithContext(string a, int aScale, string b, int bScale, string c, int cScale, int precision, RoundingMode mode)
{
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
MathContext mc = new MathContext(precision, RoundingMode.Ceiling);
BigDecimal result = aNumber.Subtract(bNumber, mc);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(cScale, result.Scale, "incorrect scale");
}
示例6: Subtract
public void Subtract(string a, int aScale, string b, int bScale, string c, int cScale)
{
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Subtract(bNumber);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(cScale, result.Scale, "incorrect scale");
}
示例7: SubtractBigDecimal
public void SubtractBigDecimal()
{
BigDecimal sub1 = BigDecimal.Parse("13948");
BigDecimal sub2 = BigDecimal.Parse("2839.489");
BigDecimal result = sub1.Subtract(sub2);
Assert.IsTrue(result.ToString().Equals("11108.511") && result.Scale == 3, "13948 - 2839.489 is wrong: " + result);
BigDecimal result2 = sub2.Subtract(sub1);
Assert.IsTrue(result2.ToString().Equals("-11108.511") && result2.Scale == 3, "2839.489 - 13948 is wrong");
Assert.IsTrue(result.Equals(result2.Negate()), "13948 - 2839.489 is not the negative of 2839.489 - 13948");
sub1 = new BigDecimal(value, 1);
sub2 = BigDecimal.Parse("0");
result = sub1.Subtract(sub2);
Assert.IsTrue(result.Equals(sub1), "1234590.8 - 0 is wrong");
sub1 = new BigDecimal(1.234E-03);
sub2 = new BigDecimal(3.423E-10);
result = sub1.Subtract(sub2);
Assert.IsTrue(result.ToDouble() == 0.0012339996577, "1.234E-03 - 3.423E-10 is wrong, " + result.ToDouble());
sub1 = new BigDecimal(1234.0123);
sub2 = new BigDecimal(1234.0123000);
result = sub1.Subtract(sub2);
Assert.IsTrue(result.ToDouble() == 0.0, "1234.0123 - 1234.0123000 is wrong, " + result.ToDouble());
}