sqrt

Implement double sqrt(double x) in C++ [closed]

流过昼夜 提交于 2019-12-04 20:55:52
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Implement double sqrt(double x) in C++ without using std library. This is a facebook interview question I saw here. http://www.glassdoor.com/Interview/Implement-double-sqrt-double-x-in-C-QTN_87210.htm Any other good idea about this?... !

sqrt is only defined when argument is nonnegative

ぐ巨炮叔叔 提交于 2019-12-04 10:00:43
This compiles fine #include <math.h> int main(void) { double i = sqrt(9.0); } If I change 9.0 to -9.0, then my compiler (GNU C) gives an error about an undefined reference to 'sqrt'. I was expecting the sqrt function to return NaN or an exception. How does the C library only define sqrt for non-negative arguments? This is happening because gcc can use builtin functions during the optimization process to compute certain functions including sqrt at compile time in many but not all cases. If that is the case it will not need to emit a call to sqrt and therefore will not need to link against libm

Divide Array to 2 sub arrays and check if the multiplication are equal

时光怂恿深爱的人放手 提交于 2019-12-04 06:40:35
I practice for an exam in Java. One of the questions I faced today is: Given an array with n numbers, I need to check if there are 2 subarrays(doesn't have to be equal) that their multiplication equals - if there are, will return true, else false. for example : if the array is : {2,15,3,4,2,5} - will return True if the array is : {2,4,6,2,3,4} - will return False. the answer must be recursive, without any loops. so I thought that if there are 2 sub arrays that their multiplication equal it means that the total multiplication of the whole array must be a square root number. for example at the

Difference between **(1/2), math.sqrt and cmath.sqrt?

扶醉桌前 提交于 2019-12-04 04:02:39
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? 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 cmath module if you require support for complex numbers." The (**) operator maps to the pow function, with

Why sqrt in global scope is much slower than std::sqrt in MinGW?

余生颓废 提交于 2019-12-04 03:12:14
Consider the following code: #include <cmath> #include <cstdio> const int COUNT = 100000000; int main() { double sum = 0; for (int i = 1; i <= COUNT; ++i) sum += sqrt(i); printf("%f\n", sum); return 0; } It runs 5.5s on my computer. However, if I change sqrt into std::sqrt , It will run only 0.7s. I know that if I use sqrt , I'm using the function from C library, and if I use std::sqrt , I'm using the one in <cmath> . But <cmath> doesn't define one for int , and if I change the type of i into double , they will run for equal speed. So the compiler isn't optimizing for int . This seems to only

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

拟墨画扇 提交于 2019-12-04 03:09:14
(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-width types, it looks like this: static uint32_t mcrowne_isqrt(uint32_t val) { uint32_t temp, root = 0;

C# high precision calculations

匆匆过客 提交于 2019-12-03 21:03:33
问题 Consider this code: double result = Math.Sqrt(4746073226998689451); For result I get 2178548422 instead of 2178548421.999999854etc... How can I get more precise result? 回答1: For the particular problem, computing the square root, you can use Decimal type and Newton's algorithm: using System; class Program { public static void Main() { long x = 4746073226998689451; decimal sqrt_x = (decimal)Math.Sqrt(x); for (int i = 0; i < 10; ++i) sqrt_x = 0.5m * (sqrt_x + x / sqrt_x); Console.WriteLine("{0

Pentagon Project in c++

核能气质少年 提交于 2019-12-02 19:09:59
问题 Soo.. I'm writing a pentagon project for my c++ class, and to be honest I'm not really doing well right now due job and other classes. So.. we need to make a pentagon program that will have Pentagon class and Menu Class. I managed working Menu Class but I'm not sure how to work with Pentagon class. anyways, what I currently need is - to make a right equation with square roots. Equation for finding a Area of pentagon is: A = s^2 sqrt ( of 25 + 10 sqrt (5) ) / (over) 4 So how do I do that? This

Why do I get math domain error?

北慕城南 提交于 2019-12-02 16:00:41
问题 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 回答1: 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 +

Pentagon Project in c++

扶醉桌前 提交于 2019-12-02 13:03:09
Soo.. I'm writing a pentagon project for my c++ class, and to be honest I'm not really doing well right now due job and other classes. So.. we need to make a pentagon program that will have Pentagon class and Menu Class. I managed working Menu Class but I'm not sure how to work with Pentagon class. anyways, what I currently need is - to make a right equation with square roots. Equation for finding a Area of pentagon is: A = s^2 sqrt ( of 25 + 10 sqrt (5) ) / (over) 4 So how do I do that? This is what I currently have in my Menu Class: // ================== #include "StdAfx.h" #include <string>