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
136
votes
6 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
  • 75,813
  • 86
  • 255
  • 348
86
votes
15 answers

How is the square root function implemented?

How is the square root function implemented?
pp7
  • 885
  • 1
  • 7
  • 3
79
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…
Ravindra S
  • 6,302
  • 12
  • 70
  • 108
72
votes
9 answers

Is there a short-hand for nth root of x in Python?

In maths, if I wish to calculate 3 to the power of 2 then no symbol is required, but I write the 2 small: 3². In Python this operation seems to be represented by the ** syntax. >>> 3**2 9 If I want to go the other direction and calculate the 2nd…
whytheq
  • 34,466
  • 65
  • 172
  • 267
66
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
  • 675
  • 1
  • 5
  • 4
58
votes
20 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
48
votes
14 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
  • 40,356
  • 16
  • 72
  • 88
38
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,611
  • 6
  • 20
  • 24
31
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
  • 15,024
  • 26
  • 81
  • 118
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
  • 47,861
  • 24
  • 143
  • 221
24
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
  • 911
  • 2
  • 11
  • 25
19
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,901
  • 2
  • 38
  • 67
19
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,389
  • 1
  • 24
  • 36
16
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,631
  • 8
  • 69
  • 131
14
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
  • 7,100
  • 3
  • 28
  • 58
1
2 3
26 27