Questions tagged [math.sqrt]

Using the Math package (in different programming languages) method sqrt to calculate the square root of a value.

77 questions
20
votes
4 answers

Where can I find the source code for Java's Square Root function?

I know that Math.sqrt calls StrictMath.sqrt(double a). Method signature in StrictMath class: public static native double sqrt(double a); I was wanting to look at the actual implementation code used to calculate it.
Merk
18
votes
2 answers

Time complexity of Math.Sqrt()?

How can I find the complexity of this function? private double EuclideanDistance(MFCC.MFCCFrame vec1, MFCC.MFCCFrame vec2) { double Distance = 0.0; for (int K = 0; K < 13; K++) Distance += (vec1.Features[K] - vec2.Features[K]) *…
Bathant Hegazy
  • 549
  • 4
  • 16
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
7
votes
7 answers

Faster Alternative to Math.sqrt()

Are there any alternatives to using Math.sqrt() to get the square root of an unknown value? For example: var random = (Math.random() * (999 - 1)) + 1; var sqrt = Math.sqrt(random); I've heard that using Math.sqrt() to get the square root of a…
m-byte
  • 115
  • 1
  • 1
  • 6
6
votes
3 answers

Why use hex values instead of normal base 10 numbers?

I was looking over this code to calculate math.sqrt in Java. Why did they use hex values in some of the loops and normal values for variables? What benefits are there to use hex?
Nope
  • 34,682
  • 42
  • 94
  • 119
5
votes
3 answers

Worst case time complexity of Math.sqrt in java

We have a test exercise where you need to find out whether a given N number is a square of another number or no, with the smallest time complexity. I wrote: public static boolean what2(int n) { double newN = (double)n; double x =…
roony
  • 155
  • 2
  • 8
5
votes
1 answer

Exactness of integer square root in Python

I would like to understand why something is happening. I needed to implement the integer square root in Python (isqrt(64) = 8 = isqrt(80)). I was convinced that the naive approach: def isqrt(n): return int(math.sqrt(n)) was bound to fail…
zeycus
  • 860
  • 1
  • 8
  • 20
4
votes
3 answers

Why is there a Math.SQRT2 property in JavaScript?

Why is there a a property in Javascript that returns the approximate square root of 2. Isn't Math.sqrt(2) enough? There is also a property that returns the square root of 0.5. I'm wondering out of pure curiosity.
u840903
  • 2,151
  • 2
  • 16
  • 27
4
votes
1 answer

It's really strange that sometimes gcc can't find reference of sqrt but sometimes gcc can

I tried this code /*main.c*/ #include /* printf */ #include /* sqrt */ int frequency_of_primes (int n) { int i, j; int freq = n - 1; for (i = 2; i <= n; ++i) for (j = sqrt(i); j > 1; --j) if (i%j==0)…
TorosFanny
  • 1,702
  • 1
  • 16
  • 25
3
votes
2 answers

Why is println in a for loop printing just once and the wrong value?

I have to create a code that, given the polygon name and its vertex coordinates, prints the perimeter. Even if I change the input values, it always print 5.0 . What am I doing wrong? I tried using a for loop and print the length of every side of a…
3
votes
2 answers

Finding values in a substring within a string?

I am creating a simple Python calculator that uses the order of operations to do simple math, and adds in a square root and integer division feature. The concept is that the user could enclose the value they want to be square rooted within a sqr()…
miike3459
  • 1,431
  • 2
  • 16
  • 32
3
votes
1 answer

Performing math.sqrt on numpy structured array column

I have a numpy structured array. The final column needs to contain the result of a simple math equation based on the other values in the row. Problem is I get the following error when trying to calculate the square root portion of the…
Carl
  • 1,346
  • 15
  • 35
2
votes
1 answer

Get ceiling of sqrt without float point. Python 3.7.3

I need to get the ceiling of a square root, such as 10**10001 (10^10001). If I do: from math import sqrt print(int(sqrt(10**10001))+1) I look, and it gives me an OverflowError. More precisely, a Traceback (most recent call last) File ,…
Lucas Urban
  • 627
  • 4
  • 15
2
votes
3 answers

List contains method returns false when using Math.sqrt() as a parameter

I am refreshing my knowledge in Java and working on an exercise from codewars. The problem is to compare two arrays if the elements are the "same". The meaning of "same" is that the elements in 'b' are the elements in 'a' squared, regardless of the…
iamLinker
  • 68
  • 7
2
votes
2 answers

Python calculator with the math.sqrt function

I am very new to Python but I wanted to code an calculator. It works fine exept for the sqrt function. Everytime I try to calculate the square root of a number I get the error message. I know that there are probably thousand ways to code a better…
Emma
  • 61
  • 6
1
2 3 4 5 6