问题描述
使用Math.sqrt()
获取未知值的平方根是否有其他选择?
Are there any alternatives to using Math.sqrt()
to get the square root of an unknown value?
例如:
var random = (Math.random() * (999 - 1)) + 1;
var sqrt = Math.sqrt(random);
我听说使用Math.sqrt()
来获取数字的平方根是一个非常慢的操作,我只是想知道是否有更快的方法可以获取随机数的平方根.任何帮助,将不胜感激.
I've heard that using Math.sqrt()
to get the square root of a number is a very slow operation, I'm just wondering if there are any faster ways I can get the square root of a random number. Any help with this would be greatly appreciated.
推荐答案
您可以确定,如果不是更好的话,您将在Math.sqrt中实现将要编写自己的最快算法.
You can be sure that the fastest algorithm you will write your self is already implemented within Math.sqrt if not better .
有一种算法可以遍历数字直到中间(通过一些简单的计算即可):
There is an algorithm to go through the numbers till the middle (with some simply calculation) : Writing your own square root function
但是正如我所说,如果不是更好的话,它可能会实现.
but as I said, it's probably implemented if not better.
您可以尝试寻找一些特定的业务/域逻辑,以减小数字范围.
You can try to look for some specific business/domain logic in order to reduce numbers range .
这篇关于替代Math.sqrt()的更快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!