把一个BigIntegers返回双 [英] Dividing a BigIntegers to return double
问题描述
欲计算直线的斜率
public sealed class Point
{
public System.Numerics.BigInteger x = 0;
public System.Numerics.BigInteger y = 0;
public double CalculateSlope (Point point)
{
return ((point.Y - this.Y) / (point.X - this.X));
}
}
我知道的BigInteger具有DivRem函数,返回除法结果再加上余,但我不知道如何运用它来得到一个双。我处理的数字是远远超越Int64.MaxValue的范围,其余本身可能是超出范围,计算按常规划分。
I know that BigInteger has a DivRem function that returns the division result plus the remainder but am not sure how to apply it to get a double. The numbers I'm dealing with are far far beyond the range of Int64.MaxValue so the remainder itself could be out of range to calculate by conventional division.
修改: 不知道是否有帮助,但我只用正整数处理(> = 1)。
EDIT: Not sure if it helps but I'm dealing with only positive integers (>=1).
重要:我只需要precision几个小数点(5应该是对我来说已经足够了)
IMPORTANT: I only need a few decimal points of precision (5 should be good enough for my purpose).
推荐答案
The BigRational library has a conversion operator to double.
另外,要记住将无穷大作为特殊情况下的垂直线,你会得到一个被零除例外情况与当前的code。也许最好只计算X1 - X2第一,并返回无穷大,如果是零,然后做了分工,避免重复操作
Also, remember to return infinity as a special case for a vertical line, you'll get a divide by zero exception with your current code. Probably best to just calculate the X1 - X2 first, and return infinity if it's zero, then do the division, to avoid redundant operations.
这篇关于把一个BigIntegers返回双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!