本文整理汇总了C#中BigDecimal.Remainder方法的典型用法代码示例。如果您正苦于以下问题:C# BigDecimal.Remainder方法的具体用法?C# BigDecimal.Remainder怎么用?C# BigDecimal.Remainder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.Remainder方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ModPi
public static BigDecimal ModPi(BigDecimal x)
{
/* write x= pi*k+r with the precision in r defined by the precision of x and not
* compromised by the precision of pi, so the ulp of pi*k should match the ulp of x.
* First get a guess of k to figure out how many digits of pi are needed.
*/
var k = (int) (x.ToDouble()/System.Math.PI);
/* want to have err(pi*k)< err(x)=x.ulp/2, so err(pi) = err(x)/(2k) with two safety digits
*/
double errpi;
if (k != 0)
errpi = 0.5*System.Math.Abs(x.Ulp().ToDouble()/k);
else
errpi = 0.5*System.Math.Abs(x.Ulp().ToDouble());
var mc = new MathContext(2 + ErrorToPrecision(3.1416, errpi));
BigDecimal onepi = PiRound(mc);
BigDecimal pihalf = onepi.Divide(new BigDecimal(2));
/* Delegate the actual operation to the BigDecimal class, which may return
* a negative value of x was negative .
*/
BigDecimal res = x.Remainder(onepi);
if (res.CompareTo(pihalf) > 0)
res = res.Subtract(onepi);
else if (res.CompareTo(pihalf.Negate()) < 0)
res = res.Add(onepi);
/* The actual precision is set by the input value, its absolute value of x.ulp()/2.
*/
mc = new MathContext(ErrorToPrecision(res.ToDouble(), x.Ulp().ToDouble()/2d));
return res.Round(mc);
}
示例2: Mod2Pi
public static BigDecimal Mod2Pi(BigDecimal x)
{
/* write x= 2*pi*k+r with the precision in r defined by the precision of x and not
* compromised by the precision of 2*pi, so the ulp of 2*pi*k should match the ulp of x.
* First get a guess of k to figure out how many digits of 2*pi are needed.
*/
var k = (int) (0.5*x.ToDouble()/System.Math.PI);
/* want to have err(2*pi*k)< err(x)=0.5*x.ulp, so err(pi) = err(x)/(4k) with two safety digits
*/
double err2pi;
if (k != 0)
err2pi = 0.25*System.Math.Abs(x.Ulp().ToDouble()/k);
else
err2pi = 0.5*System.Math.Abs(x.Ulp().ToDouble());
var mc = new MathContext(2 + ErrorToPrecision(6.283, err2pi));
BigDecimal twopi = PiRound(mc).Multiply(new BigDecimal(2));
/* Delegate the actual operation to the BigDecimal class, which may return
* a negative value of x was negative .
*/
BigDecimal res = x.Remainder(twopi);
if (res.CompareTo(BigDecimal.Zero) < 0)
res = res.Add(twopi);
/* The actual precision is set by the input value, its absolute value of x.ulp()/2.
*/
mc = new MathContext(ErrorToPrecision(res.ToDouble(), x.Ulp().ToDouble()/2d));
return res.Round(mc);
}
示例3: RemainderMathContextHALF_DOWN
public void RemainderMathContextHALF_DOWN()
{
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = -45;
String b = "134432345432345748766876876723342238476237823787879183470";
int bScale = 10;
int precision = 75;
RoundingMode rm = RoundingMode.HalfDown;
MathContext mc = new MathContext(precision, rm);
String res = "1149310942946292909508821656680979993738625937.2065885780";
int resScale = 10;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Remainder(bNumber, mc);
Assert.AreEqual(res, result.ToString(), "incorrect quotient value");
Assert.AreEqual(resScale, result.Scale, "incorrect quotient scale");
}
示例4: RemainderMathContextHALF_UP
public void RemainderMathContextHALF_UP()
{
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = 45;
String b = "134432345432345748766876876723342238476237823787879183470";
int bScale = 10;
int precision = 15;
RoundingMode rm = RoundingMode.HalfUp;
MathContext mc = new MathContext(precision, rm);
String res = "3736186567876.876578956958765675671119238118911893939591735";
int resScale = 45;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Remainder(bNumber, mc);
Assert.AreEqual(res, result.ToString(), "incorrect quotient value");
Assert.AreEqual(resScale, result.Scale, "incorrect quotient scale");
}
示例5: Remainder2
public void Remainder2()
{
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = -45;
String b = "134432345432345748766876876723342238476237823787879183470";
int bScale = 10;
String res = "1149310942946292909508821656680979993738625937.2065885780";
int resScale = 10;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Remainder(bNumber);
Assert.AreEqual(res, result.ToString(), "incorrect quotient value");
Assert.AreEqual(resScale, result.Scale, "incorrect quotient scale");
}