sqrt

How do I calculate square root in Python?

对着背影说爱祢 提交于 2019-11-28 15:26:59
问题 Why does Python give the "wrong" answer? x = 16 sqrt = x**(.5) returns 4 sqrt = x**(1/2) returns 1 Yes, I know import math and use sqrt . But I'm looking for an answer to the above. 回答1: sqrt=x**(1/2) is doing integer division. 1/2 == 0 . So you're computing x (1/2) in the first instance, x (0) in the second. So it's not wrong, it's the right answer to a different question. 回答2: You have to write: sqrt = x**(1/2.0) , otherwise an integer division is performed and the expression 1/2 returns 0

Where can I inspect Python's math functions?

只谈情不闲聊 提交于 2019-11-28 11:54:54
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, but are these somewhere in the Python distribution, or are they linked to code elsewhere? I am using Mac OS X. Where is the algorithm in math.sqrt() ? Alexei Sholik It depends on the implementation. CPython is using math functions from the standard C library. Jython is most likely using Java's math methods. And so on. In fact, Python has nothing to

How to improve fixed point square-root for small values

笑着哭i 提交于 2019-11-27 22:59:49
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 works well enough when the distance between the points is significant (greater than a few kilometers), but is very poor at smaller distances. The worst case being when the two points are equal or near equal, the result is a distance of 194 meters, while I need precision of at least 1 metre at distances >= 1 metre. By comparison with a double

Finding square root without using sqrt function?

泄露秘密 提交于 2019-11-27 18:12:24
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 <iostream> using namespace std; double SqrtNumber(double num) { double lower_bound=0; double upper_bound=num; double temp=0; /* ek edited this line */ int nCount = 50; while(nCount != 0) { temp=(lower_bound+upper_bound)/2; if(temp*temp==num) { return temp; } else if(temp*temp > num) { upper_bound = temp; } else { lower_bound = temp; } nCount--; } return temp; } int main() { double num; cout<<"Enter the number\n"; cin

'sqrt' is not a member of 'std'

坚强是说给别人听的谎言 提交于 2019-11-27 17:35:57
问题 I compile my program in linux - it has the following line : std::sqrt((double)num); On windows it is ok,but on linux I get 'sqrt' is not a member of 'std' I have an include for math.h what is a problem with it? 回答1: Change the directive to #include <cmath> . C++ headers of the form <cxxxxxxx> are guaranteed to have the standard names in std namespace (and may optionaly provide them in global namespace). <xxxxxx.h> are not. 回答2: it is simply because <math.h> does not declare the functions in

Fast sqrt in Java at the expense of accuracy

China☆狼群 提交于 2019-11-27 15:03:57
问题 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() method after 5 decimal places. However, this method needs to be much faster than Math.sqrt() . Any ideas? Thanks! 回答1: I don't believe (without a benchmark to prove this wrong) that a pure Java implementation could me much faster than Math.sqrt() .

Get sqrt from Int in Haskell

a 夏天 提交于 2019-11-27 14:27:07
问题 How can I get sqrt from Int . I try so: sqrt . fromInteger x But get error with types compatibility. 回答1: Using fromIntegral : Prelude> let x = 5::Int Prelude> sqrt (fromIntegral x) 2.23606797749979 both Int and Integer are instances of Integral : fromIntegral :: (Integral a, Num b) => a -> b takes your Int (which is an instance of Integral ) and "makes" it a Num . sqrt :: (Floating a) => a -> a expects a Floating , and Floating inherit from Fractional , which inherits from Num , so you can

Is it possible to roll a significantly faster version of sqrt

匆匆过客 提交于 2019-11-27 12:07:00
问题 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... though I'd assume sqrt is not going to add much overhead though. See also here for similar discussion on modf function. EDIT: for reference, this is one

sqrt() function not working with variable arguments [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-27 09:17:18
This question already has an answer here: Why am I getting “undefined reference to sqrt” error even though I include math.h header? [duplicate] 6 answers 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 this program, I get the following error: gcc -Wall -o "test2" "test2.c" (in directory: /home/eddy/Code/euler) /tmp/ccVfxkNh.o:

sqrt() of int type in C

早过忘川 提交于 2019-11-27 08:09:39
问题 I am programming in the c language on mac os x. I am using sqrt, from math.h, function like this: int start = Data -> start_number; double localSum; for (start; start <= end; start++) { localSum += sqrt(start); } This works, but why? and why am I getting no warning? On the man page for sqrt, it takes a double as parameter, but I give it an int - how can it work? Thanks 回答1: Type conversions which do not cause a loss in precision might not throw warnings. They are cast implicitly. int -->