sqrt

Why do I get math domain error?

时光毁灭记忆、已成空白 提交于 2019-12-02 08:12:26
Why do I get this error? I'm trying to solve an equation like this: ax^2 + bx + c Traceback (most recent call last): File "I:/Taller de Programacion I/Clase 5/11.py", line 6, in <module> x1 = (-b + sqrt(b ** 2 - a * c)) / (2 * a) ValueError: math domain error The math module (which I suppose you are using) doesn't support complex numbers. Either use cmath (python2 and python3) or the power operator ** (python3). This should always work, no matter the sign of the discriminant: x1 = (-b + (b ** 2 - 4 * a * c) ** .5) / 2 / a Example: >>> b = 1 >>> a = 2 >>> c = 3 >>> (-b + (b ** 2 - 4 * a * c) **

Square root Python 2.7.12

笑着哭i 提交于 2019-12-02 01:46:05
问题 Why does the math module return the wrong result? First test A = 12345678917 print 'A =',A B = sqrt(A**2) print 'B =',int(B) Result A = 12345678917 B = 12345678917 Here, the result is correct. Second test A = 123456758365483459347856 print 'A =',A B = sqrt(A**2) print 'B =',int(B) Result A = 123456758365483459347856 B = 123456758365483467538432 Here the result is incorrect. Why is that the case? 回答1: Because math.sqrt(..) first casts the number to a floating point and floating points have a

sqrt() function link error

浪尽此生 提交于 2019-12-01 22:00:48
The following code is throwing undefined symbol error on Linux. $ cat rms.c /* sqrt example */ #include <stdio.h> #include <math.h> int main () { double param, result; param = 1024.0; result = sqrt (param); printf ("sqrt(%lf) = %lf\n", param, result ); return 0; } $ gcc rms.c /tmp/ccaWecFP.o(.text+0x24): In function `main': : undefined reference to `sqrt' collect2: ld returned 1 exit status If I replace argument to sqrt() with (double)16 then program is compiling and executing. Why is this throwing error in first case. This is a linker error. The linker is missing the implementation of sqrt()

find as many digits of the square root of 2 as possible

房东的猫 提交于 2019-12-01 21:43:40
问题 #include <iostream> #include <cmath> using namespace std; int main() { double a = sqrt(2); cout << a << endl; } hi this is the program to find sqrt of 2 it prints just 1.41421 in the output how to implement it in way such that it will print 200000 digits after decimal point 1.41421..........upto 200 000 digits Is there any approach to print like this? 回答1: Here is the code for your question which using GNU GMP library. The code will print 1000 digits of sqrt(2), increase the number in the

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

北城以北 提交于 2019-12-01 19:35:42
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::number<bmp::cpp_dec_float<0>> float_bmp; v += 6 * ceil(static_cast<float_bmp>((sqrt(static_cast<bmp:

find as many digits of the square root of 2 as possible

心已入冬 提交于 2019-12-01 18:27:50
#include <iostream> #include <cmath> using namespace std; int main() { double a = sqrt(2); cout << a << endl; } hi this is the program to find sqrt of 2 it prints just 1.41421 in the output how to implement it in way such that it will print 200000 digits after decimal point 1.41421..........upto 200 000 digits Is there any approach to print like this? Here is the code for your question which using GNU GMP library. The code will print 1000 digits of sqrt(2), increase the number in the lines with comment to satisfy your request. #include <stdio.h> #include <gmp.h> int main(int argc, char *argv[]

Is there a fast C or C++ standard library function for double precision inverse square root?

巧了我就是萌 提交于 2019-12-01 16:37:18
问题 I find myself typing double foo=1.0/sqrt(...); a lot, and I've heard that modern processors have built-in inverse square root opcodes. Is there a C or C++ standard library inverse square root function that uses double precision floating point? is as accurate as 1.0/sqrt(...) ? is just as fast or faster than the result of 1.0/sqrt(...) ? 回答1: No. No, there isn't. Not in C++. Nope. 回答2: You can use this function for faster inverse square root computing There's an article on wikipedia on how it

How to compute the square root of negative numbers?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 11:21:25
I'm trying to create a complex number from the square root of a negative number using the following code: include Math z = Complex(sqrt(-9)) But it produces this error: Math::DomainError: Numerical argument is out of domain - "sqrt" from kata2.rb:20:in `sqrt' from kata2.rb:20:in `polinomio' from kata2.rb:34 from /home/howarto/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>' How can I build a complex number from the square root of a negative number? The Math.sqrt function can't compute the square root of negative numbers: irb> Math.sqrt(-1) Math::DomainError: Numerical argument is out of

Guaranteed precision of sqrt function in C/C++

让人想犯罪 __ 提交于 2019-12-01 07:37:28
Everyone knows sqrt function from math.h / cmath in C/C++ - it returns square root of its argument. Of course, it has to do it with some error, because not every number can be stored precisely. But am I guaranteed that the result has some precision? For example, 'it's the best approximation of square root that can be represented in the floating point type used or if you calculate square of the result, it will be as close to initial argument as possible using the floating point type given`? Does C/C++ standard have something about it? For C99, there are no specific requirements. But most

How to compute the square root of negative numbers?

牧云@^-^@ 提交于 2019-12-01 07:26:39
问题 I'm trying to create a complex number from the square root of a negative number using the following code: include Math z = Complex(sqrt(-9)) But it produces this error: Math::DomainError: Numerical argument is out of domain - "sqrt" from kata2.rb:20:in `sqrt' from kata2.rb:20:in `polinomio' from kata2.rb:34 from /home/howarto/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>' How can I build a complex number from the square root of a negative number? 回答1: The Math.sqrt function can't compute