sqrt

Weird behaviour of np.sqrt for very large integers

↘锁芯ラ 提交于 2019-12-08 20:29:23
问题 >>> np.__version__ '1.7.0' >>> np.sqrt(10000000000000000000) 3162277660.1683793 >>> np.sqrt(100000000000000000000.) 10000000000.0 >>> np.sqrt(100000000000000000000) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: sqrt Huh... AttributeError: sqrt what's going on here then? math.sqrt doesn't seem to have the same problem. 回答1: The final number is a long (Python's name for an arbitrary precision integer), which NumPy apparently can't deal with: >>> type

Difference between sqrt(x) and pow(x,0.5) [closed]

馋奶兔 提交于 2019-12-08 17:11:10
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . 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? 回答1: I ran a test for you to check the performance of sqrt(x) and pow(x,0.5) 1. for

Attempted to use awk sqrt but only returns 0

本秂侑毒 提交于 2019-12-08 07:16:44
问题 I am attempting to use the sqrt function from awk command in my script, but all it returns is 0. Is there anything wrong with my script below? echo "enter number" read root awk 'BEGIN{ print sqrt($root) }' This is my first time using the awk command, are there any mistakes that I am not understanding here? 回答1: Maybe you can try this. echo "enter number" read root echo "$root" | awk '{print sqrt($0)}' You have to give a data input to awk. So, you can pipe 'echo'. The BEGIN statement is to do

Is there a C++ function that returns exactly the value of the built-in CPU operation RSQRTSS for inverse square root?

纵饮孤独 提交于 2019-12-08 05:52:26
问题 I am looking for a C++ function that returns the inverse sqrt of a float: rsqrt(x) = 1/sqrt(x) by using the exact method like the built-in XMM operation RSQRTSS (cf. https://www.felixcloutier.com/x86/rsqrtss). (I.e., I want the built-in approximation rather than the more precise 1/sqrtf , and I don't care about speed (a lot).) According to this question: Is there a fast C or C++ standard library function for double precision inverse square root? ...there is at least no "fast way with double

sqrt for element-wise sparse matrix

我是研究僧i 提交于 2019-12-08 04:24:26
问题 I have a sparse matrix: from scipy import sparse a = sparse.diags([1,4,9],[-1,0,1],shape =(10,10),format ="csr") I want to take the square root of each of the elements in the sparse matrix I look up on the internet and it says I can use numpy.sqrt() to implement this. But error occurs: b = numpy.sqrt(a) AttributeError: sqrt How can I do it? 回答1: Caveat, this will create a resulting numpy ndarray instead of a sparse csr array. from scipy import sparse a = sparse.diags([1,4,9],[-1,0,1],shape =

sqrt is only defined when argument is nonnegative

元气小坏坏 提交于 2019-12-06 04:53:20
问题 This compiles fine #include <math.h> int main(void) { double i = sqrt(9.0); } If I change 9.0 to -9.0, then my compiler (GNU C) gives an error about an undefined reference to 'sqrt'. I was expecting the sqrt function to return NaN or an exception. How does the C library only define sqrt for non-negative arguments? 回答1: This is happening because gcc can use builtin functions during the optimization process to compute certain functions including sqrt at compile time in many but not all cases.

Difference between **(1/2), math.sqrt and cmath.sqrt?

ⅰ亾dé卋堺 提交于 2019-12-05 23:27:19
问题 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? 回答1: If you look at the documentation for cmath and math respectively, you will find that: cmath "provides access to mathematical functions for complex numbers" math "functions cannot be used with complex numbers; use the functions of the same name from the

python math domain error - sqrt

二次信任 提交于 2019-12-05 12:20:41
What causes the problem? from math import sqrt print "a : " a = float(raw_input()) print "b : " b = float(raw_input()) print "c : " c = float(raw_input()) d = (a + b + c)/2 s = sqrt(d*(d-a)*(d-b)*(d-c)) print "a+b+c =", a, b, c print "Distr. =", d*2, "Area =", s Error: Traceback (most recent call last): File "C:/Python27/fájlok/háromszög terület2.py", line 11, in <module> s = sqrt(d*(d-a)*(d-b)*(d-c)) ValueError: math domain error The problem is that the Heron's formula holds good only when the sum of the two numbers are greater than the third. You need to check that explicitly. A better way

I'm getting an error <string>:149: RuntimeWarning: invalid value encountered in sqrt while generating a list

拥有回忆 提交于 2019-12-05 03:00:59
def ellipse(numPoints, genX=np.linspace, HALF_WIDTH=10, HALF_HEIGHT=6.5): xs = 10.*genX(-1,1,numPoints) ys = 6.5*np.sqrt(1-(xs**2)) return(xs, ys, "-") I am getting an error that states that an invalid value was encountered in a squareroot. I can't see what it is. sqrt(0) = 0 6.5*sqrt(1- (-1**2)) = 0 They should work, but the y values are having problems, they are returning "nan" probably xs**2 returns a number > 1 sqrt with negative number will return nan (not a number) >>> import numpy as np >>> np.sqrt(-1) nan If i am right numpy provides complex numbers functionality which i think is the

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

丶灬走出姿态 提交于 2019-12-05 01:17:40
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 implement such a predicate? Think of it this way, if you have a positive int n , then you're basically doing a binary search on the range of numbers from 1 .. n to find the first number n' where n' * n' = n . I don't know Haskell, but this F# should be easy to convert: let is