我要疯了吗还是Math.Pow坏了? [英] Am I going crazy or is Math.Pow broken?

查看:220
本文介绍了我要疯了吗还是Math.Pow坏了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了基本转换器,从这里,并将其更改为使用ulong值,但是当转换大数字时,具体高于16677181699666568的数字返回不正确的值。我开始研究这一点,发现Math.Pow(3,34)返回值16677181699666568,实际上3 ^ 34是16677181699666569.因此,在我的作品中抛出一个扳手。我认为这是Pow方法中双重精度的问题?是我最简单的解决方案只是为了创造自己的Pow,需要消费的价值?

I used the base converter from here and changed it to work with ulong values, but when converting large numbers, specifically numbers higher than 16677181699666568 it was returning incorrect values. I started looking into this and discovered that Math.Pow(3, 34) returns the value 16677181699666568, when actually 3^34 is 16677181699666569. This therefore throws a spanner in the works for me. I assume this is just an issue with double precision within the Pow method? Is my easiest fix just to create my own Pow that takes ulong values?

如果是这样,最快的方式是做Pow?我假设每次都有比一个for循环更快的东西。

If so, what's the quickest way to do Pow? I assume there's something faster than a for loop with multiplication each time.

推荐答案

你可以使用 BigInteger.Pow 。或使用我的电源方法 long

You can use BigInteger.Pow. Or use my power method for long.

这篇关于我要疯了吗还是Math.Pow坏了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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