相关疑难解决方法(0)

记录非常大的数字

我正在处理BigInteger类,其数字大约为2,上升到10,000,000.

BigInteger Log函数现在是我算法中最昂贵的函数,我正在拼命寻找替代方案.

因为我只需要日志的组成部分,所以我遇到了这个答案,这在速度方面看起来很棒,但由于某些原因我没有得到准确的值.我不关心小数部分,但我确实需要得到一个准确的积分部分,无论该值是浮动还是上限,只要我知道哪个.

这是我实现的功能:

public static double LogBase2 (System.Numerics.BigInteger number)
{
    return (LogBase2(number.ToByteArray()));
}

public static double LogBase2 (byte [] bytes)
{
    // Corrected based on [ronalchn's] answer.
    return (System.Math.Log(bytes [bytes.Length - 1], 2) + ((bytes.Length - 1) * 8));
}
Run Code Online (Sandbox Code Playgroud)

除角落情况外,这些值现在非常准确.值7到7.99999,15到15.9999,23到23.9999 31到31.9999等返回-Infinity.数字似乎围绕字节边界.知道这里发生了什么吗?

例:

LogBase2(                    1081210289) = 30.009999999993600 != 30.000000000000000
LogBase2(                    1088730701) = 30.019999999613300 != 30.000000000000000
LogBase2(                    2132649894) = 30.989999999389400 != 30.988684686772200
LogBase2(                    2147483648) = 31.000000000000000 != -Infinity
LogBase2(                    2162420578) = 31.009999999993600 != -Infinity
LogBase2(                    4235837212) = …
Run Code Online (Sandbox Code Playgroud)

.net c# logarithm biginteger

4
推荐指数
1
解决办法
1481
查看次数

标签 统计

.net ×1

biginteger ×1

c# ×1

logarithm ×1