sqrt

Finding square root without using sqrt function?

核能气质少年 提交于 2019-11-26 22:40:11
问题 I was finding out the algorithm for finding out the square root without using sqrt function and then tried to put into programming. I end up with this working code in C++ #include <iostream> using namespace std; double SqrtNumber(double num) { double lower_bound=0; double upper_bound=num; double temp=0; /* ek edited this line */ int nCount = 50; while(nCount != 0) { temp=(lower_bound+upper_bound)/2; if(temp*temp==num) { return temp; } else if(temp*temp > num) { upper_bound = temp; } else {

How to improve fixed point square-root for small values

隐身守侯 提交于 2019-11-26 21:20:03
问题 I am using Anthony Williams' fixed point library described in the Dr Dobb's article " Optimizing Math-Intensive Applications with Fixed-Point Arithmetic " to calculate the distance between two geographical points using the Rhumb Line method. This works well enough when the distance between the points is significant (greater than a few kilometers), but is very poor at smaller distances. The worst case being when the two points are equal or near equal, the result is a distance of 194 meters,

How to get a square root for 32 bit input in one clock cycle only?

北城以北 提交于 2019-11-26 15:33:42
I want to design a synthesizable module in Verilog which will take only one cycle in calculating square root of given input of 32 bit. [Edit1] repaired code Recently found the results where off even if tests determine all was OK so I dig deeper and found out that I had a silly bug in my equation and due to name conflicts with my pgm environment the tests got false positives so I overlooked it before. Now it work in all cases as it should. The best thing I can think of (except approximation or large LUT ) is binary search without multiplication, here C++ code: //--------------------------------

sqrt() function not working with variable arguments [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-26 14:37:08
问题 This question already has an answer here: Why am I getting “undefined reference to sqrt” error even though I include math.h header? [duplicate] 5 answers I don't know if I'm missing something obvious, but it appears that I'm unable to compute square roots of a variable in C; the sqrt() function only seems to work on constants. This is my code: #include <math.h> #include <stdio.h> int main() { double a = 2.0; double b = sqrt(a); printf("%f", b); return 0; } When I run this program, I get the

Integer square root in python

徘徊边缘 提交于 2019-11-26 08:50:15
问题 Is there an integer square root somewhere in python, or in standard libraries? I want it to be exact (i.e. return an integer), and bark if there\'s no solution. At the moment I rolled my own naive one: def isqrt(n): i = int(math.sqrt(n) + 0.5) if i**2 == n: return i raise ValueError(\'input was not a perfect square\') But it\'s ugly and I don\'t really trust it for large integers. I could iterate through the squares and give up if I\'ve exceeded the value, but I assume it would be kinda slow

How to get a square root for 32 bit input in one clock cycle only?

Deadly 提交于 2019-11-26 04:02:51
问题 I want to design a synthesizable module in Verilog which will take only one cycle in calculating square root of given input of 32 bit. 回答1: [Edit1] repaired code Recently found the results where off even if tests determine all was OK so I dig deeper and found out that I had a silly bug in my equation and due to name conflicts with my pgm environment the tests got false positives so I overlooked it before. Now it work in all cases as it should. The best thing I can think of (except