本文整理汇总了C#中BigDecimal.Multiply方法的典型用法代码示例。如果您正苦于以下问题:C# BigDecimal.Multiply方法的具体用法?C# BigDecimal.Multiply怎么用?C# BigDecimal.Multiply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.Multiply方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MultiplyRound
public static BigDecimal MultiplyRound(BigDecimal x, BigDecimal y)
{
BigDecimal resul = x.Multiply(y);
// The estimation of the relative error in the result is the sum of the relative
// errors |err(y)/y|+|err(x)/x|
var mc = new MathContext(System.Math.Min(x.Precision, y.Precision));
return resul.Round(mc);
}
示例2: Zeta
public static BigDecimal Zeta(int n, MathContext mc)
{
if (n <= 0)
throw new NotSupportedException("Zeta at negative argument " + n + " not supported");
if (n == 1)
throw new ArithmeticException("Pole at zeta(1) ");
if (n%2 == 0) {
/* Even indices. Abramowitz-Stegun 23.2.16. Start with 2^(n-1)*B(n)/n!
*/
Rational b = Bernoulli.Default[n].Abs();
b = b.Divide(Factorial.Default[n]);
b = b.Multiply(BigInteger.One.ShiftLeft(n - 1));
/* to be multiplied by pi^n. Absolute error in the result of pi^n is n times
* error in pi times pi^(n-1). Relative error is n*error(pi)/pi, requested by mc.
* Need one more digit in pi if n=10, two digits if n=100 etc, and add one extra digit.
*/
var mcpi = new MathContext(mc.Precision + (int) (System.Math.Log10(10.0*n)));
BigDecimal piton = PiRound(mcpi).Pow(n, mc);
return MultiplyRound(piton, b);
}
if (n == 3) {
/* Broadhurst BBP <a href="http://arxiv.org/abs/math/9803067">arXiv:math/9803067</a>
* Error propagation: S31 is roughly 0.087, S33 roughly 0.131
*/
int[] a31 = {1, -7, -1, 10, -1, -7, 1, 0};
int[] a33 = {1, 1, -1, -2, -1, 1, 1, 0};
BigDecimal S31 = BroadhurstBbp(3, 1, a31, mc);
BigDecimal S33 = BroadhurstBbp(3, 3, a33, mc);
S31 = S31.Multiply(new BigDecimal(48));
S33 = S33.Multiply(new BigDecimal(32));
return S31.Add(S33).Divide(new BigDecimal(7), mc);
}
if (n == 5) {
/* Broadhurst BBP <a href=http://arxiv.org/abs/math/9803067">arXiv:math/9803067</a>
* Error propagation: S51 is roughly -11.15, S53 roughly 22.165, S55 is roughly 0.031
* 9*2048*S51/6265 = -3.28. 7*2038*S53/61651= 5.07. 738*2048*S55/61651= 0.747.
* The result is of the order 1.03, so we add 2 digits to S51 and S52 and one digit to S55.
*/
int[] a51 = {31, -1614, -31, -6212, -31, -1614, 31, 74552};
int[] a53 = {173, 284, -173, -457, -173, 284, 173, -111};
int[] a55 = {1, 0, -1, -1, -1, 0, 1, 1};
BigDecimal S51 = BroadhurstBbp(5, 1, a51, new MathContext(2 + mc.Precision));
BigDecimal S53 = BroadhurstBbp(5, 3, a53, new MathContext(2 + mc.Precision));
BigDecimal S55 = BroadhurstBbp(5, 5, a55, new MathContext(1 + mc.Precision));
S51 = S51.Multiply(new BigDecimal(18432));
S53 = S53.Multiply(new BigDecimal(14336));
S55 = S55.Multiply(new BigDecimal(1511424));
return S51.Add(S53).Subtract(S55).Divide(new BigDecimal(62651), mc);
}
/* Cohen et al Exp Math 1 (1) (1992) 25
*/
var betsum = new Rational();
var bern = new Bernoulli();
var fact = new Factorial();
for (int npr = 0; npr <= (n + 1)/2; npr++) {
Rational b = bern[2*npr].Multiply(bern[n + 1 - 2*npr]);
b = b.Divide(fact[2*npr]).Divide(fact[n + 1 - 2*npr]);
b = b.Multiply(1 - 2*npr);
if (npr%2 == 0)
betsum = betsum.Add(b);
else
betsum = betsum.Subtract(b);
}
betsum = betsum.Divide(n - 1);
/* The first term, including the facor (2pi)^n, is essentially most
* of the result, near one. The second term below is roughly in the range 0.003 to 0.009.
* So the precision here is matching the precisionn requested by mc, and the precision
* requested for 2*pi is in absolute terms adjusted.
*/
var mcloc = new MathContext(2 + mc.Precision + (int) (System.Math.Log10(n)));
BigDecimal ftrm = PiRound(mcloc).Multiply(new BigDecimal(2));
ftrm = ftrm.Pow(n);
ftrm = MultiplyRound(ftrm, betsum.ToBigDecimal(mcloc));
var exps = new BigDecimal(0);
/* the basic accuracy of the accumulated terms before multiplication with 2
*/
double eps = System.Math.Pow(10d, -mc.Precision);
if (n%4 == 3) {
/* since the argument n is at least 7 here, the drop
* of the terms is at rather constant pace at least 10^-3, for example
* 0.0018, 0.2e-7, 0.29e-11, 0.74e-15 etc for npr=1,2,3.... We want 2 times these terms
* fall below eps/10.
*/
int kmax = mc.Precision/3;
eps /= kmax;
/* need an error of eps for 2/(exp(2pi)-1) = 0.0037
* The absolute error is 4*exp(2pi)*err(pi)/(exp(2pi)-1)^2=0.0075*err(pi)
*/
BigDecimal exp2p = PiRound(new MathContext(3 + ErrorToPrecision(3.14, eps/0.0075)));
exp2p = Exp(exp2p.Multiply(new BigDecimal(2)));
BigDecimal c = exp2p.Subtract(BigDecimal.One);
exps = DivideRound(1, c);
for (int npr = 2; npr <= kmax; npr++) {
/* the error estimate above for npr=1 is the worst case of
* the absolute error created by an error in 2pi. So we can
* safely re-use the exp2p value computed above without
//.........这里部分代码省略.........
示例3: PowRound
public static BigDecimal PowRound(BigDecimal x, Rational q)
{
/** Special cases: x^1=x and x^0 = 1
*/
if (q.CompareTo(BigInteger.One) == 0)
return x;
if (q.Sign == 0)
return BigDecimal.One;
if (q.IsInteger) {
/* We are sure that the denominator is positive here, because normalize() has been
* called during constrution etc.
*/
return PowRound(x, q.Numerator);
}
/* Refuse to operate on the general negative basis. The integer q have already been handled above.
*/
if (x.CompareTo(BigDecimal.Zero) < 0)
throw new ArithmeticException("Cannot power negative " + x);
if (q.IsIntegerFraction) {
/* Newton method with first estimate in double precision.
* The disadvantage of this first line here is that the result must fit in the
* standard range of double precision numbers exponents.
*/
double estim = System.Math.Pow(x.ToDouble(), q.ToDouble());
var res = new BigDecimal(estim);
/* The error in x^q is q*x^(q-1)*Delta(x).
* The relative error is q*Delta(x)/x, q times the relative error of x.
*/
var reserr = new BigDecimal(0.5*q.Abs().ToDouble()
*x.Ulp().Divide(x.Abs(), MathContext.Decimal64).ToDouble());
/* The main point in branching the cases above is that this conversion
* will succeed for numerator and denominator of q.
*/
int qa = q.Numerator.ToInt32();
int qb = q.Denominator.ToInt32();
/* Newton iterations. */
BigDecimal xpowa = PowRound(x, qa);
for (;;) {
/* numerator and denominator of the Newton term. The major
* disadvantage of this implementation is that the updates of the powers
* of the new estimate are done in full precision calling BigDecimal.pow(),
* which becomes slow if the denominator of q is large.
*/
BigDecimal nu = res.Pow(qb).Subtract(xpowa);
BigDecimal de = MultiplyRound(res.Pow(qb - 1), q.Denominator);
/* estimated correction */
BigDecimal eps = nu.Divide(de, MathContext.Decimal64);
BigDecimal err = res.Multiply(reserr, MathContext.Decimal64);
int precDiv = 2 + ErrorToPrecision(eps, err);
if (precDiv <= 0) {
/* The case when the precision is already reached and any precision
* will do. */
eps = nu.Divide(de, MathContext.Decimal32);
} else {
eps = nu.Divide(de, new MathContext(precDiv));
}
res = SubtractRound(res, eps);
/* reached final precision if the relative error fell below reserr,
* |eps/res| < reserr
*/
if (eps.Divide(res, MathContext.Decimal64).Abs().CompareTo(reserr) < 0) {
/* delete the bits of extra precision kept in this
* working copy.
*/
return res.Round(new MathContext(ErrorToPrecision(reserr.ToDouble())));
}
}
}
/* The error in x^q is q*x^(q-1)*Delta(x) + Delta(q)*x^q*log(x).
* The relative error is q/x*Delta(x) + Delta(q)*log(x). Convert q to a floating point
* number such that its relative error becomes negligible: Delta(q)/q << Delta(x)/x/log(x) .
*/
int precq = 3 + ErrorToPrecision((x.Ulp().Divide(x, MathContext.Decimal64)).ToDouble()
/System.Math.Log(x.ToDouble()));
/* Perform the actual calculation as exponentiation of two floating point numbers.
*/
return Pow(x, q.ToBigDecimal(new MathContext(precq)));
}
示例4: Pow
public static BigDecimal Pow(BigDecimal x, BigDecimal y)
{
if (x.CompareTo(BigDecimal.Zero) < 0)
throw new ArithmeticException("Cannot power negative " + x);
if (x.CompareTo(BigDecimal.Zero) == 0)
return BigDecimal.Zero;
/* return x^y = exp(y*log(x)) ;
*/
BigDecimal logx = Log(x);
BigDecimal ylogx = y.Multiply(logx);
BigDecimal resul = Exp(ylogx);
/* The estimation of the relative error in the result is |log(x)*err(y)|+|y*err(x)/x|
*/
double errR = System.Math.Abs(logx.ToDouble())*y.Ulp().ToDouble()/2d
+ System.Math.Abs(y.ToDouble()*x.Ulp().ToDouble()/2d/x.ToDouble());
var mcR = new MathContext(ErrorToPrecision(1.0, errR));
return resul.Round(mcR);
}
示例5: MultiplyWithContext
public void MultiplyWithContext(string a, int aScale, string b, int bScale, string c, int cScale, int precision, RoundingMode roundingMode)
{
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
MathContext mc = new MathContext(precision, roundingMode);
BigDecimal result = aNumber.Multiply(bNumber, mc);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(cScale, result.Scale, "incorrect scale");
}
示例6: Multiply
public void Multiply(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.Multiply(bNumber);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(cScale, result.Scale, "incorrect scale");
}
示例7: MultiplyBigDecimal
public void MultiplyBigDecimal()
{
BigDecimal multi1 = new BigDecimal(value, 5);
BigDecimal multi2 = new BigDecimal(2.345D);
BigDecimal result = multi1.Multiply(multi2);
Assert.IsTrue(result.ToString().StartsWith("289.51154260") && result.Scale == multi1.Scale + multi2.Scale,
"123.45908 * 2.345 is not correct: " + result);
multi1 = BigDecimal.Parse("34656");
multi2 = BigDecimal.Parse("-2");
result = multi1.Multiply(multi2);
Assert.IsTrue(result.ToString().Equals("-69312") && result.Scale == 0, "34656 * 2 is not correct");
multi1 = new BigDecimal(-2.345E-02);
multi2 = new BigDecimal(-134E130);
result = multi1.Multiply(multi2);
Assert.IsTrue(result.ToDouble() == 3.1422999999999997E130 && result.Scale == multi1.Scale + multi2.Scale,
"-2.345E-02 * -134E130 is not correct " + result.ToDouble());
multi1 = BigDecimal.Parse("11235");
multi2 = BigDecimal.Parse("0");
result = multi1.Multiply(multi2);
Assert.IsTrue(result.ToDouble() == 0 && result.Scale == 0, "11235 * 0 is not correct");
multi1 = BigDecimal.Parse("-0.00234");
multi2 = new BigDecimal(13.4E10);
result = multi1.Multiply(multi2);
Assert.IsTrue(result.ToDouble() == -313560000 && result.Scale == multi1.Scale + multi2.Scale,
"-0.00234 * 13.4E10 is not correct");
}