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
126
votes
5 answers

Why does Python give the "wrong" answer for square root? What is integer division in Python 2?

x = 16 sqrt = x**(.5) #returns 4 sqrt = x**(1/2) #returns 1 I know I can import math and use sqrt, but I'm looking for an answer to the above. What is integer division in Python 2? This behavior is fixed in Python 3.
Merlin
  • 24,552
  • 41
  • 131
  • 206
113
votes
3 answers

Is it possible to write Quake's fast InvSqrt() function in Rust?

This is just to satisfy my own curiosity. Is there an implementation of this: float InvSqrt (float x) { float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } in…
Flyq
  • 1,246
  • 2
  • 13
  • 13
78
votes
14 answers

Integer square root in python

Is there an integer square root somewhere in python, or in standard libraries? I want it to be exact (i.e. return an integer), and raise an exception if the input isn't a perfect square. I tried using this code: def isqrt(n): i =…
wim
  • 338,267
  • 99
  • 616
  • 750
48
votes
8 answers

How do I calculate square root in Python?

I need to calculate the square root of some numbers, for example √9 = 3 and √2 = 1.4142. How can I do it in Python? The inputs will probably be all positive integers, and relatively small (say less than a billion), but just in case they're not, is…
wjandrea
  • 28,235
  • 9
  • 60
  • 81
45
votes
5 answers

Comparing two values in the form (a + sqrt(b)) as fast as possible?

As part of a program that I'm writing, I need to compare two values in the form a + sqrt(b) where a and b are unsigned integers. As this is part of a tight loop, I'd like this comparison to run as fast as possible. (If it matters, I'm running the…
Bernard
  • 5,209
  • 1
  • 34
  • 64
34
votes
3 answers

Get sqrt from Int in Haskell

How can I get sqrt from Int. I try so: sqrt . fromInteger x But get error with types compatibility.
0xAX
  • 20,957
  • 26
  • 117
  • 206
34
votes
6 answers

Fastest Integer Square Root in the least amount of instructions

I am in a need of fast integer square root that does not involve any explicit division. The target RISC architecture can do operations like add, mul, sub, shift in one cycle (well - the operation's result is written in third cycle, really - but…
3D Coder
  • 498
  • 1
  • 5
  • 10
34
votes
6 answers

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…
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
27
votes
3 answers

Where can I inspect Python's math functions?

I would like to look at the way Python does computes square roots, so I tried to find the definition for math.sqrt(), but I can't find it anywhere. I have looked in _math.c, mathmodule.c, and elsewhere. I know that python uses C's math functions,…
Tom Scrace
  • 1,606
  • 3
  • 13
  • 11
26
votes
2 answers

'sqrt' is not a member of 'std'

I compile my program in Linux - it has the following line: std::sqrt((double)num); On Windows, it is ok. However, on Linux, I get an error: sqrt is not a member of std I have already included math.h. What is the problem with that?
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
25
votes
9 answers

Finding square root without using sqrt function?

I was finding out the algorithm for finding out the square root without using sqrt function and then tried to put into programming. I end up with this working code in C++ #include using namespace std; double SqrtNumber(double…
Arun Pandey
  • 513
  • 3
  • 6
  • 13
24
votes
2 answers

Using fractional exponent with bc

bc, a Linux command-line calculator, is proficient enough to calculate 3^2 9 Even a negative exponent doesn't confuse it: 3^-2 0.11111 Yet it fails when it encounters 9^0.5 Runtime warning (func=(main), adr=8): non-zero scale in exponent How…
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
20
votes
8 answers

Very fast approximate Logarithm (natural log) function in C++?

We find various tricks to replace std::sqrt (Timing Square Root) and some for std::exp (Using Faster Exponential Approximation) , but I find nothing to replace std::log. It's part of loops in my program and its called multiple times and while exp…
qwark
  • 493
  • 1
  • 4
  • 15
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
16
votes
9 answers

Generating digits of square root of 2

I want to generate the digits of the square root of two to 3 million digits. I am aware of Newton-Raphson but I don't have much clue how to implement it in C or C++ due to lack of biginteger support. Can somebody point me in the right…
Quixotic
  • 2,424
  • 7
  • 36
  • 58
1
2 3
27 28