sqrt

How to fix 'ValueError: math domain error' in python?

╄→гoц情女王★ 提交于 2021-02-05 08:53:07
问题 I am trying to write a program which calculates formula y=√x*x + 3*x - 500, Interval is [x1;x2] and f.e x1=15, x2=25. I tried to use Exception Handling but it didn't help. And the code I try to use now gives me: ValueError: math domain error import math x1 = int(input("Enter first number:")) x2 = int(input("Enter second number:")) print(" x", " y") for x in range(x1, x2): formula = math.sqrt(x * x + 3 * x - 500) if formula < 0: print("square root cant be negative") print(x, round(formula, 2))

RuntimeWarning: invalid value encountered in sqrt

倾然丶 夕夏残阳落幕 提交于 2021-01-25 07:32:05
问题 I get the error in the title for my code below, but I do not have any negative values or NaNs in my array. Is there anything else I should check for? I don't get the error for small arrays that I could copy here. I only get it in my arrays that are approx 100,000 x 1,000 def norm2(A,B): """numpy.ndarray A shape (m1,d), B shape (m2,d) Returns ndarray of shape (m1, m2) dists_{i,j} = ||A_i - B_j|| """ print("A.shape: ", A.shape) print("B.shape: ", B.shape) sums = np.sum(np.square(A[:,None,:] - B

How to compute the digits of an irrational number one by one?

£可爱£侵袭症+ 提交于 2020-12-06 16:56:43
问题 I want to read digit by digit the decimals of the sqrt of 5 in C. The square root of 5 is 2,23606797749979..., so this'd be the expected output: 2 3 6 0 6 7 9 7 7 ... I've found the following code: #include<stdio.h> void main() { int number; float temp, sqrt; printf("Provide the number: \n"); scanf("%d", &number); // store the half of the given number e.g from 256 => 128 sqrt = number / 2; temp = 0; // Iterate until sqrt is different of temp, that is updated on the loop while(sqrt != temp){ /

How to compute the digits of an irrational number one by one?

家住魔仙堡 提交于 2020-12-06 16:55:23
问题 I want to read digit by digit the decimals of the sqrt of 5 in C. The square root of 5 is 2,23606797749979..., so this'd be the expected output: 2 3 6 0 6 7 9 7 7 ... I've found the following code: #include<stdio.h> void main() { int number; float temp, sqrt; printf("Provide the number: \n"); scanf("%d", &number); // store the half of the given number e.g from 256 => 128 sqrt = number / 2; temp = 0; // Iterate until sqrt is different of temp, that is updated on the loop while(sqrt != temp){ /

Very fast approximate Logarithm (natural log) function in C++?

痞子三分冷 提交于 2020-07-05 02:52:07
问题 We find various tricks to replace std::sqrt (Timing Square Root) and some for std::exp (Using Faster Exponential Approximation) , but I find nothing to replace std::log . It's part of loops in my program and its called multiple times and while exp and sqrt were optimized, Intel VTune now suggest me to optimize std::log , after that it seems that only my design choices will be limiting. For now I use a 3rd order taylor approximation of ln(1+x) with x between -0.5 and +0.5 (90% of the case for

Calculating a nested root in C

孤街醉人 提交于 2020-05-11 05:08:08
问题 I was asked to calculate the following nested root expression using recursion only. I wrote the code below that works, but they allowed us to use only one function and 1 input n for the purpose and not 2 like I used. Can someone help me transform this code into one function that will calculate the expression? cant use any library except functions from <math.h> . output for n=10: 1.757932 double rec_sqrt_series(int n, int m) { if (n <= 0) return 0; if (m > n) return 0; return sqrt(m + rec_sqrt

Is it possible to write Quake's fast InvSqrt() function in Rust?

爱⌒轻易说出口 提交于 2020-04-07 10:52:06
问题 This is just to satisfy my own curiosity. Is there an implementation of this: float InvSqrt (float x) { float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } in Rust? If it exists, post the code. I tried it and failed. I don't know how to encode the float number using integer format. Here is my attempt: fn main() { println!("Hello, world!"); println!("sqrt1: {}, ",sqrt2(100f64)); } fn sqrt1(x: f64) -> f64 { x.sqrt() } fn sqrt2

sqrt() - Why am I allowed to provide an argument of int and not only double and the output is also right?

久未见 提交于 2020-01-14 22:02:14
问题 I wondering why the compiler let this pass and is giving the right output, although sqrt() from its prototype normally should only get an double value as argument: In C99 the declaration of the prototype is: double sqrt (double x); #include <stdio.h> #include <math.h> int main (void) { int i = 9; printf("\t Number \t\t Square Root of Number\n\n"); printf("\t %d \t\t\t %f \n",i, sqrt(i)); } Output: Number Square Root of Number 9 3.000000 Why does the compiler not throw a warning at least and

sqrt() - Why am I allowed to provide an argument of int and not only double and the output is also right?

谁说胖子不能爱 提交于 2020-01-14 21:48:14
问题 I wondering why the compiler let this pass and is giving the right output, although sqrt() from its prototype normally should only get an double value as argument: In C99 the declaration of the prototype is: double sqrt (double x); #include <stdio.h> #include <math.h> int main (void) { int i = 9; printf("\t Number \t\t Square Root of Number\n\n"); printf("\t %d \t\t\t %f \n",i, sqrt(i)); } Output: Number Square Root of Number 9 3.000000 Why does the compiler not throw a warning at least and

Understanding why an ASM fsqrt implementation is faster than the standard sqrt function

前提是你 提交于 2020-01-14 10:26:29
问题 I have playing around with basic math function implementations in C++ for academic purposes. Today, I benchmarked the following code for Square Root: inline float sqrt_new(float n) { __asm { fld n fsqrt } } I was surprised to see that it is consistently faster than the standard sqrt function (it takes around 85% of the execution time of the standard function). I don't quite get why and would love to better understand it. Below I show the full code I am using to profile (in Visual Studio 2015,