square-root

Simplify radical expression in java

霸气de小男生 提交于 2019-12-11 12:01:51
问题 Here is my method so far: public static int[] simplifyRadical(int number) { int[] result = new int[2]; for (int i = 1; i < number / 2; i++) { if ((i % number == 0)) { //IS a factor of the number in the radical } } return result; } The format I'm using is result[0] = number outside radical and result[1] = number inside radical . So far, my method gets all the factors of number (which is the initial UNSIMPLFIED number in the radical). So how can I divide the initial number by the perfect square

Rounding ** 0.5 and math.sqrt

佐手、 提交于 2019-12-11 01:17:57
问题 In Python, are either n**0.5 # or math.sqrt(n) recognized when a number is a perfect square? Specifically, should I worry that when I use int(n**0.5) # instead of int(n**0.5 + 0.000000001) I might accidentally end up with the number one less than the actual square root due to precision error? 回答1: Yes, you should worry: In [11]: int((100000000000000000000000000000000000**2) ** 0.5) Out[11]: 99999999999999996863366107917975552L In [12]: int(math.sqrt(100000000000000000000000000000000000**2))

How to take the square root of quad_form output in CVXPY?

旧街凉风 提交于 2019-12-11 00:55:34
问题 I am trying to solve a problem that involves \sqrt{w^t \Sigma w} in the objective function. To compute w^t \Sigma w , I use the quad_form function. How do I take its square root? When in the code I try to write risk = sqrt(quad_form(w, E)) I am getting a DCP rule error but I am pretty sure it is convex given the other constraints I have. So the question is not really about maths but the actual implementation of the convex program. The problem I am trying to solve is ret = mu.T*w risk = sqrt

Square root of negative numbers

假装没事ソ 提交于 2019-12-10 18:26:13
问题 Just wondering, is it possible to compute the square roots of negative numbers in C#? For example sqrt(-1) = i. I wrote this piece of code: using System; public static class sqrts { public static void Main() { string x; double sqrtofx; Console.Write("Enter a number: "); x = Console.ReadLine(); sqrtofx = Math.Sqrt(Convert.ToInt32(x)); Console.WriteLine("\nSqrt({0}): {1}", x, sqrtofx); Console.ReadKey(); } } If I enter 25, it gives 5. However, if I put in -5, it gives NaN instead of 5i. 回答1:

OverflowError: long int too large to convert to float [duplicate]

我是研究僧i 提交于 2019-12-10 16:37:47
问题 This question already has answers here : Integer square root in python (11 answers) Closed 5 years ago . I am trying to get the square root of a really large number yet I get the error: deltaSqrt = pow(delta,0.5) OverflowError: long int too large to convert to float In my case delta is equal to:

Clojure: Complex Iteration Across a List?

孤街醉人 提交于 2019-12-10 16:29:44
问题 I'd like to take a number, 20, and a list. '(1 2 3 4 5 6 7 8 9 10) , and return a collection containing two values for each value in the original list: the original value paired with the remainder when diving 20 by that value. It would be nice if the original values were somehow keyed to the remainders, so that I could easily retrieve each number that produced a particular remainder. Basically I want some function func : user=> (func 20 '(1 2 3 4 5 6 7 8 9 10)) '(:0 1, :0 2, :2 3,... :20 0) I

IEEE double such that sqrt(x*x) ≠ x

空扰寡人 提交于 2019-12-09 02:33:00
问题 Does there exist an IEEE double x>0 such that sqrt(x*x) ≠ x , under the condition that the computation x*x does not overflow or underflow to Inf , 0 , or a denormal number? This is given that sqrt returns the nearest representable result, and so does x*x (both as mandated by the IEEE standard, "square root operation be calculated as if in infinite precision, and then rounded to one of the two nearest floating-point numbers of the specified precision that surround the infinitely precise result

Square root of s15.16 fixed point number in Java

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 13:48:12
问题 I want to write a function to calculate the square root of a s15.16 fixed point number. I know its a signed number with 15 digit int and 16 digit fraction. Is there anyway to do it without any libraries? Any other languages is fine too. 回答1: I assume you are asking this question because the platform you are on does not provide floating-point, otherwise you can implement 15.16 fixed-point square root via the floating-point square root as follows (this is C code, I assume Java code will look

Infinite Recursion in Meta Integer Square Root

爱⌒轻易说出口 提交于 2019-12-07 02:47:02
问题 Good day, A friend of mine is asking about transforming an integer square root function into a meta-function. Here is the original function: unsigned isqrt(unsigned value) { unsigned sq = 1, dlt = 3; while(sq<=value) { sq += dlt; dlt += 2; } return (dlt>>1) - 1; } I wrote a meta version using constexpr , but he said he can't use the new feature for some reason: constexpr std::size_t isqrt_impl (std::size_t sq, std::size_t dlt, std::size_t value){ return sq <= value ? isqrt_impl(sq+dlt, dlt+2,

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