sqrt

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? ...

finding objects within radius.

looking for a light weight way to find objects within a radius. so far the answer that is obvious to me is go through each object, comparing its x and y position, with the center of the radius. example: Turret - looking for targets in radius. TargetArray - array of possible targets. WithinRangeArray - array we push applicable targets...

Square root in PHP

Why is the output of sqrt not an integer for "16" in PHP? Example php > $fig = 16; php > $sq = sqrt($fig); //should be 4 php > echo $sq; 4 php > echo is_int($sq); // should give 1, but gives false php > I feel that the problem is in the internal presentation which PHP hides similarly as Python. How can you then know when t...

Java: Performance SQRT Calculations

Hi, I have this code: package math; import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { System.out.println("Hi, I will beat Java's Math.sqrt(double) method"); System.out.println("Both ways of calculation will be done"); S...

Doesn't the Visual Studio 2008 compiler autocast to double with sqrt in C++?

Shouldn't the compiler automatically cast to a double in the following? At least according to Walter Savitch. #include <iostream> #include <cmath> using namespace std; int main(){ int k; for(k = 1; k <= 10; k++) cout << "The square root of k is: " << sqrt(k) << endl; return 0; }//error C2668: 'sqrt' : ambiguous call...

[C++] Is it possible to roll a significantly faster version of sqrt

In an app I'm profiling, I found that in some scenarios this function is able to take over 10% of total execution time. I've seen discussion over the years of faster sqrt implementations using sneaky floating-point trickery, but I don't know if such things are outdated on modern CPUs. MSVC++ 2008 compiler is being used, for reference.....

what's the way to determine if an Int a perfect square in Haskell?

I need a simple function is_square :: Int -> Bool which determines if an Int N a perfect square (is there an integer x such that x*x = N). Of course I can just write something like is_square n = sq * sq == n where sq = floor $ sqrt $ (fromIntegral n::Double) but it looks terrible! Maybe there is a common simple way to implemen...

sqrt turn out to be 4.9999 , instead of 5

Possible Duplicate: Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273? sqrt(25.0) turns out to be 4.99999 ,instead of 5 how would I fix this problem? ...

sqrt() function not working with variable arguments

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 t...

Estimating the square root

Hello- I am writing an iPhone app that needs to calculate the square root of a number about 2000 times every 1/30th of a second. sqrt() works fine on a computer, but the frame rate drops to around 10 FPS on an iPhone or iPad, and I have already optimized the rest of the code. I have heard that this can be sped up dramatically by estimati...