sqrt

Square root metafunction?

风流意气都作罢 提交于 2020-01-14 08:54:52
问题 Is it possible to compute the square root of an integer with a metafunction with the following signature : template<unsigned int N> inline double sqrt(); (or maybe using the constexpr keyword, I don't know what is the best). With that, sqrt<2>() would be replaced by 1.414... at compile-time. What would be the best implementation for a such function ? 回答1: This may not be what you are looking for, but I wanted to make sure you realized that typically with optimization the compiler will

How can I account for round-off errors in floating-point arithmetic for inverse trig (and sqrt) functions (in C)?

南楼画角 提交于 2020-01-06 02:46:09
问题 I have a fairly complicated function that takes several double values that represent two vectors in 3-space of the form (magnitude, latitude, longitude) where latitude and longitude are in radians, and an angle. The purpose of the function is to rotate the first vector around the second by the angle specified and return the resultant vector. I have already verified that the code is logically correct and works. The expected purpose of the function is for graphics, so double precision is not

How can I account for round-off errors in floating-point arithmetic for inverse trig (and sqrt) functions (in C)?

不羁的心 提交于 2020-01-06 02:46:04
问题 I have a fairly complicated function that takes several double values that represent two vectors in 3-space of the form (magnitude, latitude, longitude) where latitude and longitude are in radians, and an angle. The purpose of the function is to rotate the first vector around the second by the angle specified and return the resultant vector. I have already verified that the code is logically correct and works. The expected purpose of the function is for graphics, so double precision is not

Prime factors in C

走远了吗. 提交于 2020-01-05 07:11:20
问题 I came across this effective program for printing all the prime factors of a given number: # include <stdio.h> # include <math.h> // A function to print all prime factors of a given number n void primeFactors(int n) { // Print the number of 2s that divide n while (n%2 == 0) { printf("%d ", 2); n = n/2; } // n must be odd at this point. So we can skip // one element (Note i = i +2) for (int i = 3; i <= sqrt(n); i = i+2) { // While i divides n, print i and divide n while (n%i == 0) { printf("%d

Issue with Square Root in C Algorithm

故事扮演 提交于 2020-01-04 15:58:56
问题 I have a bit of code that finds a point on a unit sphere. Recall, for a unit sphere: 1 = sqrt( x^2 + y^2 + z^2 ) The algorithm picks two random points (the x and y coordinates) between zero and one. Provided their magnitude is less than one we have room to define a third coordinate by solving the above equation for z. void pointOnSphere(double *point){ double x, y; do { x = 2*randf() - 1; y = 2*randf() - 1; } while (x*x + y*y > 1); double mag = sqrt(fabs(1 - x*x - y*y)); point[0] = 2*(x*mag);

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

谁都会走 提交于 2020-01-01 20:02:11
问题 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

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

我只是一个虾纸丫 提交于 2020-01-01 20:00:41
问题 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

sqrt() function link error

霸气de小男生 提交于 2019-12-31 02:34:04
问题 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

different behaviour or sqrt when compiled with 64 or 32 bits

帅比萌擦擦* 提交于 2019-12-30 18:30:09
问题 I'm using sqrt() function from math library, when I build for 64 bit using -m64 I'm getting correct result but when I build for 32 bit I have very inconsistent behaviour. For example on 64bit double dx = 0x1.fffffffffffffp+1023; sqrt(dx); // => 0x1.fffffffffffffp+511 sqrt(0x1.fffffffffffffp+1023);// => 0x1.fffffffffffffp+511 (which I believe is the correctly rounded result, verified with mpfr) But on 32 bit same input value it behaves differently. double dx = 0x1.fffffffffffffp+1023; sqrt(dx)

Where can I inspect Python's math functions?

泄露秘密 提交于 2019-12-28 06:41:44
问题 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