本文整理汇总了C#中Rational.ToDouble方法的典型用法代码示例。如果您正苦于以下问题:C# Rational.ToDouble方法的具体用法?C# Rational.ToDouble怎么用?C# Rational.ToDouble使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Rational
的用法示例。
在下文中一共展示了Rational.ToDouble方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCreateRational
public void TestCreateRational()
{
var rational = new Rational(1, 3);
Assert.Equal(1, rational.Numerator);
Assert.Equal(3, rational.Denominator);
Assert.Equal(1d / 3d, rational.ToDouble(), 4);
}
示例2: Test_ToDouble
public void Test_ToDouble()
{
Rational rational = new Rational(0, 0);
Assert.AreEqual(double.NaN, rational.ToDouble());
rational = new Rational(2, 0);
Assert.AreEqual(double.PositiveInfinity, rational.ToDouble());
}
示例3: CalculateMaxSize
/// <summary>
/// Returns the maximium dimensions of an image w/ a specific aspect
/// </summary>
public static Size CalculateMaxSize(Size sourceSize, Rational aspect)
{
var targetAspect = aspect.ToDouble();
var currentAspect = sourceSize.ToRational().ToDouble();
if (currentAspect > targetAspect) // Shrink the width
{
int newWidth = (int)(sourceSize.Height * targetAspect);
return new Size(newWidth, sourceSize.Height);
}
else if (currentAspect < targetAspect) // Shrink the height
{
int newHeight = (int)(sourceSize.Width / targetAspect);
return new Size(sourceSize.Width, newHeight);
}
else // The source and target aspect are the same
{
return sourceSize;
}
}
示例4: buildDB
//.........这里部分代码省略.........
case 7: v = "Strobe return light detected"; break;
default: v = "reserved"; break;
}
}
break;
default:
v = convertToInt16U(p.Value).ToString();
break;
}
}
//4 = LONG A 32-bit (4 -byte) unsigned integer,
else if( p.Type == 0x4 )
{
// orientation // lookup table
v = convertToInt32U(p.Value).ToString();
}
//5 = RATIONAL Two LONGs. The first LONG is the numerator and the second LONG expresses the//denominator.,
else if( p.Type == 0x5 )
{
// rational
byte []n = new byte[p.Len/2];
byte []d = new byte[p.Len/2];
Array.Copy(p.Value,0,n,0,p.Len/2);
Array.Copy(p.Value,p.Len/2,d,0,p.Len/2);
uint a = convertToInt32U(n);
uint b = convertToInt32U(d);
Rational r = new Rational(a,b);
//
//convert here
//
switch( p.Id )
{
case 0x9202: // aperture
v = "F/" + Math.Round(Math.Pow(Math.Sqrt(2),r.ToDouble()),2).ToString();
break;
case 0x920A:
v = r.ToDouble().ToString();
break;
case 0x829A:
v = r.ToDouble().ToString();
break;
case 0x829D: // F-number
v = "F/" + r.ToDouble().ToString();
break;
default:
v= r.ToString("/");
break;
}
}
//7 = UNDEFINED An 8-bit byte that can take any value depending on the field definition,
else if( p.Type == 0x7 )
{
switch (p.Id )
{
case 0xA300:
{
if( p.Value[0] == 3 )
{
v = "DSC";
}
else
{
v = "reserved";
}
break;
示例5: Read
//.........这里部分代码省略.........
{
case 0: propertyValue = "Flash did not fire"; break;
case 1: propertyValue = "Flash fired"; break;
case 5: propertyValue = "Strobe return light not detected"; break;
case 7: propertyValue = "Strobe return light detected"; break;
default: propertyValue = "reserved"; break;
}
}
break;
default:
propertyValue = ConvertToUInt16(property.Value).ToString();
break;
}
}
else if (property.Type == 0x4)
{
// 4 = unsigned int
propertyValue = ConvertToUInt32(property.Value).ToString();
}
else if (property.Type == 0x5)
{
// 5 = rational of two unsigned ints
byte[] n = new byte[property.Len / 2];
byte[] d = new byte[property.Len / 2];
Array.Copy(property.Value, 0, n, 0, property.Len / 2);
Array.Copy(property.Value, property.Len / 2, d, 0, property.Len / 2);
uint a = ConvertToUInt32(n);
uint b = ConvertToUInt32(d);
Rational r = new Rational(a, b);
switch (property.Id)
{
case 0x9202: // aperture
propertyValue = "F/" + Math.Round(Math.Pow(Math.Sqrt(2), r.ToDouble()), 2).ToString();
break;
case 0x920A:
propertyValue = r.ToDouble().ToString();
break;
case 0x829A:
propertyValue = r.ToDouble().ToString();
break;
case 0x829D: // F-number
propertyValue = "F/" + r.ToDouble().ToString();
break;
default:
propertyValue = r.ToString("/");
break;
}
}
else if (property.Type == 0x7)
{
// 7 = undefined
// A byte that can take any value depending on the field
switch (property.Id)
{
case 0xA300:
{
if (property.Value[0] == 3)
{
propertyValue = "DSC";
}
else
{
propertyValue = "reserved";
}
示例6: Log
public static BigDecimal Log(Rational r, MathContext mc)
{
/* the value is undefined if x is negative.
*/
if (r.CompareTo(Rational.Zero) <= 0)
throw new ArithmeticException("Cannot take log of negative " + r);
if (r.CompareTo(Rational.One) == 0)
return BigDecimal.Zero;
/* log(r+epsr) = log(r)+epsr/r. Convert the precision to an absolute error in the result.
* eps contains the required absolute error of the result, epsr/r.
*/
double eps = PrecisionToError(System.Math.Log(r.ToDouble()), mc.Precision);
/* Convert this further into a requirement of the relative precision in r, given that
* epsr/r is also the relative precision of r. Add one safety digit.
*/
var mcloc = new MathContext(1 + ErrorToPrecision(eps));
BigDecimal resul = Log(r.ToBigDecimal(mcloc));
return resul.Round(mc);
}
示例7: BroadhurstBbp
private static BigDecimal BroadhurstBbp(int n, int p, int[] a, MathContext mc)
{
/* Explore the actual magnitude of the result first with a quick estimate.
*/
double x = 0.0;
for (int k = 1; k < 10; k++)
x += a[(k - 1)%8]/System.Math.Pow(2d, p*(k + 1)/2d)/System.Math.Pow(k, n);
/* Convert the relative precision and estimate of the result into an absolute precision.
*/
double eps = PrecisionToError(x, mc.Precision);
/* Divide this through the number of terms in the sum to account for error accumulation
* The divisor 2^(p(k+1)/2) means that on the average each 8th term in k has shrunk by
* relative to the 8th predecessor by 1/2^(4p). 1/2^(4pc) = 10^(-precision) with c the 8term
* cycles yields c=log_2( 10^precision)/4p = 3.3*precision/4p with k=8c
*/
var kmax = (int) (6.6*mc.Precision/p);
/* Now eps is the absolute error in each term */
eps /= kmax;
BigDecimal res = BigDecimal.Zero;
for (int c = 0;; c++) {
var r = new Rational();
for (int k = 0; k < 8; k++) {
var tmp = new Rational(BigInteger.ValueOf(a[k]), (BigInteger.ValueOf((1 + 8*c + k))).Pow(n));
/* floor( (pk+p)/2)
*/
int pk1h = p*(2 + 8*c + k)/2;
tmp = tmp.Divide(BigInteger.One.ShiftLeft(pk1h));
r = r.Add(tmp);
}
if (System.Math.Abs(r.ToDouble()) < eps)
break;
var mcloc = new MathContext(1 + ErrorToPrecision(r.ToDouble(), eps));
res = res.Add(r.ToBigDecimal(mcloc));
}
return res.Round(mc);
}
示例8: 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)));
}