I am doing a calculation which frequently involves values like 3.47493E+17298. This is way beyond what a double can handle, and I don t need extra precision, just extra range of exponents, so I created my own little struct in C#.
我的 st用了很长的时间,可以用来表示和签署,也可以是旁观,因此我实际上已经:
1 sign bit 32 exponent bits (regular 2 s complement exponent) 63 significand bits
我奇怪的是,可以采取哪些步骤,使我的多彩工作更加日常有效。 我正在追随这些扩展范围价值的大量繁多之处,而且很快,但我正在寻找使这一范围更快的方面。
My multiplication routine:
public static BigFloat Multiply(BigFloat left, BigFloat right)
{
long shsign1;
long shsign2;
if (left.significand == 0)
{
return bigZero;
}
if (right.significand == 0)
{
return bigZero;
}
shsign1 = left.significand;
shsign2 = right.significand;
// scaling down significand to prevent overflow multiply
// s1 and s2 indicate how much the left and right
// significands need shifting.
// The multLimit is a long constant indicating the
// max value I want either significand to be
int s1 = qshift(shsign1, multLimit);
int s2 = qshift(shsign2, multLimit);
shsign1 >>= s1;
shsign2 >>= s2;
BigFloat r;
r.significand = shsign1 * shsign2;
r.exponent = left.exponent + right.exponent + s1 + s2;
return r;
}
And the qshift:
It just finds out how much to shift the val to make it smaller in absolute value than the limit.
public static int qshift(long val, long limit)
{
long q = val;
long c = limit;
long nc = -limit;
int counter = 0;
while (q > c || q < nc)
{
q >>= 1;
counter++;
}
return counter;
}