Questions tagged [sqrt]

The `sqrt` function returns the positive square root of a number in several programming languages.

programming language:
The sqrt(), sqrtf() and sqrtl() functions return the nonnegative square root of x. With the GCC compiler they resid in the math library (libm), which is linked using -lm. They can be found in the <math.h> header.

programming language:
The std::sqrt() function returns the nonnegative square root of x. It can be found in the <cmath> header.

:
A built-in function, can (and should) be used in a vectorived manner on numerc matrices.
Handles negative and complex numbers as well.
See official doc for more details.

:
Return the positive square-root of an array, element-wise.
See doc for more details.

:
Returns the correctly rounded positive square root of a double value.
See the java.lang.Math class for more details.

408 questions
16
votes
3 answers

c++ sqrt guaranteed precision, upper/lower bound

I have to check an inequality containing square roots. To avoid incorrect results due to floating point inaccuracy and rounding, I use std::nextafter() to get an upper/lower bound: #include // DBL_MAX #include // std::nextafter,…
Lix
  • 171
  • 6
16
votes
3 answers

Difference between sqrt(x) and pow(x,0.5)

I was wondering why there is sqrt() function in C/c++ as we can achieve the same using pow(x,0.5); how is sqrt(x) different for pow(x,0.5) . Is there a specific reason of having sqrt function?
Alex
  • 1,178
  • 3
  • 9
  • 24
16
votes
3 answers

Why is fast inverse square root so odd and slow on Java?

I'm trying to implement Fast Inverse Square Root on java in order to speed up vector normalization. However, when I implement the single-precision version in Java, I get speeds about the same as 1F / (float)Math.sqrt() at first, then quickly drops…
Leo Izen
  • 4,165
  • 7
  • 37
  • 56
15
votes
9 answers

What's the way to determine if an Int is 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 $…
Valentin Golev
  • 9,965
  • 10
  • 60
  • 84
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
12
votes
2 answers

I don't understand number conversions in Haskell

Here is what I'm trying to do: isPrime :: Int -> Bool isPrime x = all (\y -> x `mod` y /= 0) [3, 5..floor(sqrt x)] (I know I'm not checking for division by two--please ignore that.) Here's what I get: No instance for (Floating Int) arising from a…
David Matuszek
  • 1,263
  • 1
  • 16
  • 30
12
votes
2 answers

Where are the inaccuracies in math.sqrt() and math.pow() coming from for large numbers?

If you take a number, take its square root, drop the decimal, and then raise it to the second power, the result should always be less than or equal to the original number. This seems to hold true in python until you try it on 99999999999999975425…
Evan
  • 1,348
  • 2
  • 10
  • 20
12
votes
7 answers

Is there a fast C or C++ standard library function for double precision inverse square root?

I find myself typing double foo=1.0/sqrt(...); a lot, and I've heard that modern processors have built-in inverse square root opcodes. Is there a C or C++ standard library inverse square root function that uses double precision floating point? is…
Dan
  • 12,157
  • 12
  • 50
  • 84
11
votes
3 answers

c++ practical computational complexity of SQRT()

What is the difference in CPU cycles (or, in essence, in 'speed') between x /= y; and #include x = sqrt(y); EDIT: I know the operations aren't equivalent, I'm just arbitrarily proposing x /= y as a benchmark for x = sqrt(y)
Matt Munson
  • 2,903
  • 5
  • 33
  • 52
11
votes
2 answers

How can I set the level of precision for Raku's sqrt?

With Perl, one could use bignum to set the level of precision for all operators. As in: use bignum ( p => -50 ); print sqrt(20); # 4.47213595499957939281834733746255247088123671922305 With Raku I have no problems with rationals since I can use…
Julio
  • 5,208
  • 1
  • 13
  • 42
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
11
votes
3 answers

Fast sqrt in Java at the expense of accuracy

I am looking for a fast square root implementation in Java for double values in the input range of [0, 2*10^12]. For any value in this range, the precision should be upto 5 decimal places. In other words, the result can differ from the Math.sqrt()…
Paresh
  • 542
  • 2
  • 6
  • 15
10
votes
1 answer

Can I change this macro to an inline function without a performance hit?

(EDIT: Let's title this, "Lessons in how measurements can go wrong." I still haven't figured out exactly what's causing the discrepancy though.) I found a very fast integer square root function here by Mark Crowne. At least with GCC on my…
Mike S
  • 531
  • 4
  • 14
10
votes
2 answers

Why sqrt become much faster without -O2 in g++ on my computer?

Consider the following code: #include #include const int COUNT = 1000000000; int main() { double sum = 0; for (int i = 1; i <= COUNT; ++i) { sum += sqrt(i); } printf("%f\n", sum); return 0; } Without…
debug18
  • 143
  • 5
10
votes
1 answer

Why sqrt in global scope is much slower than std::sqrt in MinGW?

Consider the following code: #include #include const int COUNT = 100000000; int main() { double sum = 0; for (int i = 1; i <= COUNT; ++i) sum += sqrt(i); printf("%f\n", sum); return 0; } It runs 5.5s on my…
infmagic2047
  • 209
  • 1
  • 8
1
2
3
27 28