Java底层处理平方根考虑到效率问题是采用的c源码编写的而不是java源码。
相关c源码都是网上公开的一些数学算法都被优化的很好,效率很高。
以下是网上找来的一个实现算法,具体可以查看java源文件:
/* Code written by Sanchit Karve
A.K.A born2c0de
Contact Me at born2c0de AT hotmail.com
20 August, 2005
*/
From: http://www.dreamincode.net/code/snippet244.htm
#include <iostream>
#include <math.h>
using namespace std;
float sqroot(float m)
{
float i=0;
float x1,x2;
while( (i*i) <= m )
i+=0.1;
x1=i;
for(int j=0;j<10;j++)
{
x2=m;
x2/=x1;
x2+=x1;
x2/=2;
x1=x2;
}
return x2;
}
int main()
{
cout<<"Enter a Number:";
int no;
cin>>no;
cout<<"Square Root using sqroot()= "<<sqroot(no)<<endl
<<"Square Root using sqrt() = "<<sqrt(no);
return 0;
}
本文深入探讨了Java底层实现平方根运算的优化C源码,对比使用sqrt()函数,展示了通过迭代算法实现高效计算的过程。
704

被折叠的 条评论
为什么被折叠?



