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.

354 questions
116
votes
5 answers

John Carmack's Unusual Fast Inverse Square Root (Quake III)

John Carmack has a special function in the Quake III source code which calculates the inverse square root of a float, 4x faster than regular (float)(1.0/sqrt(x)), including a strange 0x5f3759df constant. See the code below. Can someone explain line…
Alex
  • 71,233
  • 79
  • 245
  • 337
81
votes
15 answers

How is the square root function implemented?

How is the square root function implemented?
pp7
  • 835
  • 1
  • 7
  • 3
74
votes
19 answers

Writing your own square root function

How do you write your own function for finding the most accurate square root of an integer? After googling it, I found this (archived from its original link), but first, I didn't get it completely, and second, it is approximate too. Assume square…
Pale Blue Dot
  • 5,611
  • 10
  • 66
  • 100
63
votes
12 answers

Square root of BigDecimal in Java

Can we compute the square root of a BigDecimal in Java by using only the Java API and not a custom-made 100-line algorithm?
user1853200
  • 645
  • 1
  • 5
  • 4
57
votes
19 answers

How can I find the Square Root of a Java BigInteger?

Is there a library that will find the square root of a BigInteger? I want it computed offline - only once, and not inside any loop. So even computationally expensive solution is okay. I don't want to find some algorithm and implement. A readily…
user529141
46
votes
13 answers

Looking for an efficient integer square root algorithm for ARM Thumb2

I am looking for a fast, integer only algorithm to find the square root (integer part thereof) of an unsigned integer. The code must have excellent performance on ARM Thumb 2 processors. It could be assembly language or C code. Any hints welcome.
Ber
  • 34,859
  • 15
  • 60
  • 79
32
votes
4 answers

How does the computer calculate Square roots?

How does the computer calculate Square roots ? I mean what is going on there! How does it process it!! Does it use some mathematical ways like Newton's method? What about Trigonometric Functions? And almost all those Mathematical Functions . In the…
Loers Antario
  • 1,341
  • 4
  • 15
  • 23
30
votes
4 answers

Shortest way to check perfect Square?

Possible Duplicate: What's a good algorithm to determine if an input is a perfect square? I want Shortest and Simplest way to Check a number is perfect square in C# Some of Perfect Squares: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, ......
Javed Akram
  • 14,220
  • 23
  • 77
  • 113
27
votes
8 answers

Does a range of integers contain at least one perfect square?

Given two integers a and b, is there an efficient way to test whether there is another integer n such that a ≤ n2 < b? I do not need to know n, only whether at least one such n exists or not, so I hope to avoid computing square roots of any numbers…
finnw
  • 45,253
  • 22
  • 134
  • 212
19
votes
6 answers

Easiest way to find Square Root in Swift?

I have been trying to figure out how to programmatically find a square root of a number in Swift. I am looking for the simplest possible way to accomplish with as little code needed. I now this is probably fairly easy to accomplish, but can't figure…
Bigfoot11
  • 821
  • 2
  • 9
  • 25
18
votes
1 answer

IEEE double such that sqrt(x*x) ≠ x

Does there exist an IEEE double x>0 such that sqrt(x*x) ≠ x, under the condition that the computation x*x does not overflow or underflow to Inf, 0, or a denormal number? This is given that sqrt returns the nearest representable result, and so does…
Fengyang Wang
  • 11,061
  • 1
  • 33
  • 63
15
votes
6 answers

Calculate Nth root with integer arithmetic

There are a couple of ways to find integer square roots using only integer arithmetic. For example this one. It makes for interesting reading and also a very interesting theory, particularly for my generation where such techniques aren't so useful…
Matt
  • 6,892
  • 2
  • 26
  • 53
15
votes
12 answers

Why do most programming languages only give one answer to square root of 4?

Most programming languages give 2 as the answer to square root of 4. However, there are two answers: 2 and -2. Is there any particular reason, historical or otherwise, why only one answer is usually given?
bugmagnet
  • 7,233
  • 7
  • 57
  • 121
15
votes
2 answers

How to implement the "fast inverse square root" in Java?

I've heard of the "fast inverse square root", discussed here, and I wanted to put it in my Java program (just for research purposes, so ignore anything about the native libraries being faster). I was looking at the code, and the C code directly…
Riking
  • 2,149
  • 19
  • 33
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,628
  • 2
  • 15
  • 27
1
2 3
23 24