如何在不使用C#中内置数学函数的情况下计算以2为底的对数? [英] How can I compute a base 2 logarithm without using the built-in math functions in C#?

查看:64
本文介绍了如何在不使用C#中内置数学函数的情况下计算以2为底的对数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用C#中内置数学函数的情况下计算以2为底的对数?

How can I compute a base 2 logarithm without using the built-in math functions in C#?

我在应用程序中反复使用Math.Log和BigInteger.Log数百万次,并且速度变得非常慢.

I use Math.Log and BigInteger.Log repeatedly in an application millions of times and it becomes painfully slow.

我对使用二进制操作来达到相同目的的替代方案很感兴趣.请记住,我可以使用Log近似值,以防加速执行时间.

I am interested in alternatives that use binary manipulation to achieve the same. Please bear in mind that I can make do with Log approximations in case that helps speed up execution times.

推荐答案

对于BigInteger,您可以使用toByteArray()方法,然后手动找到最高有效的1,然后计算零的数量.这将为您提供以2为底的对数,并具有整数精度.

For the BigInteger you could use the toByteArray() method and then manually find the most significant 1 and count the number of zeroes afterward. This would give you the base-2 logarithm with integer precision.

这篇关于如何在不使用C#中内置数学函数的情况下计算以2为底的对数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆