Questions tagged [square-root]

A square root of a number A is a number y such that y^2 = A, or, in other words, a number y whose square (the result of multiplying the number by itself, or y × y) is A.

A square root of a number A is a number y such that y2 = a, or, in other words, a number y whose square (the result of multiplying the number by itself, or y × y) is A.

398 questions
14
votes
3 answers

Numpy - square root of -1 leaves a small real part

Perhaps this is an algorithmic issue, but the following piece of code numpy.power((-1+0j),0.5) produces the following output (6.1230317691118863e-17+1j) Analogous expressions e.g. numpy.power(complex(-1),.5) yield the same result, however -…
crasic
  • 1,918
  • 2
  • 18
  • 29
14
votes
5 answers

How do I compute the square root of a number without using builtins?

how can I create a method that returns the sqrt of a given nunber? For example: sqrt(16) returns 4 and sqrt(5) returns 2.3 ... I am using Java and know the Math.sqrt() API function but I need the method itself.
user466534
13
votes
4 answers

How to improve fixed point square-root for small values

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…
Clifford
  • 88,407
  • 13
  • 85
  • 165
13
votes
3 answers

Given r^2, is there an efficient way to compute r^3?

double r2 = dx * dx + dy * dy; double r3 = r2 * sqrt(r2); Can the second line be replaced by something faster? Something that does not involve sqrt?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
13
votes
8 answers

Binary Search to Compute Square root (Java)

I need help writing a program that uses binary search to recursively compute a square root (rounded down to the nearest integer) of an input non-negative integer. This is what I have so far: import java.util.Scanner; public class Sqrt { public…
NuNu
  • 667
  • 2
  • 12
  • 21
13
votes
2 answers

Why is my call of the CUDA math library sqrt() function failing?

I am new to Cuda, I have the following function: __global__ void square(float *myArrayGPU) { myArrayGPU[threadIdx.x] = sqrt(threadIdx.x); } I want to use the cuda math library, I tried to #include "math.h" but I still get the error error:…
Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133
12
votes
7 answers

Generating continued fractions for square roots

I wrote this code for generating Continued Fraction of a square root N. But it fails when N = 139. The output should be {11,1,3,1,3,7,1,1,2,11,2,1,1,7,3,1,3,1,22} Whilst my code gives me a sequence of 394 terms... of which the first few terms are…
Loers Antario
  • 1,611
  • 6
  • 20
  • 24
11
votes
4 answers

How to Calculate the Square Root of a Float in C#

How can I calculate the square root of a Float in C#, similar to Core.Sqrt in XNA?
Chris
  • 2,340
  • 6
  • 40
  • 63
11
votes
4 answers

Difference between **(1/2), math.sqrt and cmath.sqrt?

What is the difference between x**(1/2) , math.sqrt() and cmath.sqrt()? Why does cmath.sqrt() get complex roots of a quadratic right alone? Should I use that for my square roots exclusively? What do they do in the background differently?
RhythmInk
  • 513
  • 1
  • 4
  • 18
10
votes
7 answers

fast integer square root approximation

I am currently searching for a very fast integer square root approximation, where floor(sqrt(x)) <= veryFastIntegerSquareRoot(x) <= x The square root routine is used for calculating prime numbers, which get considerably faster if only values below…
Morten
  • 143
  • 1
  • 1
  • 10
10
votes
2 answers

Why is square root such a slow operation?

I've been warned by numerous programmers not to use the square root function, and instead to raise numbers to the half power. My question is twofold: What is the perceived/real performance benefit to doing this? Why is it faster? If it really is…
Athena
  • 3,200
  • 3
  • 27
  • 35
9
votes
3 answers

Fast inverse square of double in C/C++

Recently I was profiling a program in which the hotspot is definitely this double d = somevalue(); double d2=d*d; double c = 1.0/d2 // HOT SPOT The value d2 is not used after because I only need value c. Some time ago I've read about the Carmack…
linello
  • 8,451
  • 18
  • 63
  • 109
8
votes
3 answers

square root of a number greater than 10^2000 in Python 3

I'd like to calculate the square root of a number bigger than 10^2000 in Python. If I treat this number like a normal integer, I will always get this result back: Traceback (most recent call last): File "...", line 3, in print(…
cubeAD
  • 83
  • 1
  • 1
  • 5
8
votes
2 answers

sqrt of uint64_t vs. int64_t

I noticed that calculating the integer part of square root of uint64_t is much more complicated than of int64_t. Please, does anybody have an explanation for this? Why is it seemingly much more difficult to deal with one extra bit? The…
Hinrik
  • 83
  • 4
8
votes
3 answers

What's the efficient algorithm to find the Integer square root of a very large number, digit by digit?

I need to write program to find the integer square root of a number which is thousands of digits long. I can't use Newton Raphson as I don't have data types to store and divide such large numbers. I am using a long array in C to store the number. …
Naman
  • 2,569
  • 4
  • 27
  • 44
1
2
3
26 27