sqrt

Where can I inspect Python's math functions?

人盡茶涼 提交于 2019-12-28 06:41:36
问题 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() ? 回答1: It depends on the implementation. CPython is using math functions from the standard C

c++ negative square root

梦想的初衷 提交于 2019-12-24 13:11:30
问题 My goal is to print *** if the square root is negative. But I can't think of a way to change default nan text to *** for(int i=x1;i<=x2;i++){ double y = sqrt(pow(i,2)+3*i-500); if(y = ?){ outFile << "***"; } So, what should I write in the if statement to make it possible? Or maybe there is another way to check if the y is nan then print * 回答1: How about checking for a negative input to the square root function? for (int i = x1; i <= x2; ++i) { double x = pow(i, 2) + 3*i - 500; if (x < 0) {

Turbo C compiler issue, sqrt() function not working with variable arguments

时间秒杀一切 提交于 2019-12-23 17:22:37
问题 I searched the question similar to my problem Similar problem. But my problem is when using Turbo C compiler v3.0. Should I have to do some additional work for math.h file? please help. int main (void){ double result, a; clrscr(); printf("Enter a # for square root.\n"); scanf("%f",&a); printf("a = %f\n",a); result = sqrt(a); printf("a = %f and square root is %f\n",a, result); getch(); return 0; } The Output is like this: Enter a # for square root. 64 a = 0.000000 a = 0.000000 and square root

Negative square root

爱⌒轻易说出口 提交于 2019-12-22 08:09:09
问题 How do you take the square root of a negative number in C++? I know it should return a real and a complex part, I get a NaN? How do I take the real part? 回答1: #include <complex> int main() { std::complex<double> two_i = std::sqrt(std::complex<double>(-4)); } or just std::complex<double> sqrt_minus_x(0, std::sqrt(std::abs(x))); 回答2: sqrt(-x) where x is a positive number is simply 0 + sqrt(x)*i . The real part is just 0. In general, the real part is x > 0 ? sqrt(x) : 0 and the imaginary part is

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

你说的曾经没有我的故事 提交于 2019-12-22 04:51:39
问题 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" 回答1: probably xs**2 returns a number > 1 sqrt with negative number will return nan (not a number) >>> import numpy

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

大憨熊 提交于 2019-12-22 03:43:15
问题 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? 回答1: 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

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

自古美人都是妖i 提交于 2019-12-22 03:43:14
问题 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? 回答1: 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

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

≯℡__Kan透↙ 提交于 2019-12-21 09:20:29
问题 (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 machine, it's clearly the fastest integer square root function I've tested (including the functions in Hacker's Delight, this page, and floor(sqrt()) from the standard library). After cleaning up the formatting a bit, renaming a variable, and using fixed

How to use sqrt and ceil with Boost::multiprecision?

☆樱花仙子☆ 提交于 2019-12-19 22:01:06
问题 Do you know how to do this simple line of code without error using Boost::multiprecison ? boost::multiprecision::cpp_int v, uMax, candidate; //... v += 6 * ceil((sqrt(uMax * uMax - candidate) - v) / 6); Using MSVC there is an error for "sqrt" and it's possible to fix it with: v += 6 * ceil((sqrt(static_cast<boost::multiprecision::cpp_int>(uMax * uMax - candidate)) - v) / 6); Then there is an error for "ceil" and it's possible to fix it with: namespace bmp = boost::multiprecision; typedef bmp:

Why is multiplied many times faster than taking the square root?

天大地大妈咪最大 提交于 2019-12-19 04:56:20
问题 I have several questions with the following algorithms to tell if a number is prime, I also know that with the sieve of Eratosthenes can be faster response. Why is faster to compute i i * sqrt (n) times. than sqrt (n) just one time ? Why Math.sqrt() is faster than my sqrt() method ? What is the complexity of these algorithms O (n), O (sqrt (n)), O (n log (n))? public class Main { public static void main(String[] args) { // Case 1 comparing Algorithms long startTime = System.currentTimeMillis(