square-root

Square root of BigDecimal in Java

两盒软妹~` 提交于 2019-11-26 11:47:08
Can we compute the square root of a BigDecimal in Java by using only the Java API and not a custom-made 100-line algorithm? I've used this and it works quite well. Here's an example of how the algorithm works at a high level. Edit: I was curious to see just how accurate this was as defined below. Here is the sqrt(2) from an official source : (first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147 and here it is using the approach

Trouble with float on C [duplicate]

时光怂恿深爱的人放手 提交于 2019-11-26 11:44:19
问题 This question already has answers here : C program to convert Fahrenheit to Celsius always prints zero (8 answers) Closed 5 years ago . I have this little program in C that calculates the square root x of a positive integer N using a recursive function (implemented using a while loop). If I calculate x using this: x = (1/2)*(x + N/x) //x0 = 1.0 Then x keeps growing to inf and then nan. However if I use this: x = (x + N/x)/2 //x0 = 1.0 It works fine, why? Thanks. 回答1: 1/2 does integer division

Writing your own square root function

橙三吉。 提交于 2019-11-26 11:04:14
How do you write your own function for finding the most accurate square root of an integer? After googling it, I found this (archived from its original link ), but first, I didn't get it completely, and second, it is approximate too. Assume square root as nearest integer (to the actual root) or a float. Fredrik Johansson The following computes floor(sqrt(N)) for N > 0: x = 2^ceil(numbits(N)/2) loop: y = floor((x + floor(N/x))/2) if y >= x return x x = y This is a version of Newton's method given in Crandall & Pomerance, "Prime Numbers: A Computational Perspective". The reason you should use

How is the square root function implemented?

◇◆丶佛笑我妖孽 提交于 2019-11-26 07:24:31
问题 How is the square root function implemented? 回答1: Simple implementation using Binary Search with C++ double root(double n){ double lo = 0, hi = n, mid; for(int i = 0 ; i < 1000 ; i++){ mid = (lo+hi)/2; if(mid*mid == n) return mid; if(mid*mid > n) hi = mid; else lo = mid; } return mid; } Note that while loop is most common with the binary search but Personally I prefer using for when dealing with decimal numbers, it saves some special cases handling and gets pretty accurate result from small

John Carmack&#39;s Unusual Fast Inverse Square Root (Quake III)

烈酒焚心 提交于 2019-11-26 05:48:29
John Carmack has a special function in the Quake III source code which calculates the inverse square root of a float, 4x faster than regular (float)(1.0/sqrt(x)) , including a strange 0x5f3759df constant. See the code below. Can someone explain line by line what exactly is going on here and why this works so much faster than the regular implementation? float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; i = 0x5f3759df - ( i >> 1 ); y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); #ifndef Q3_VM

Looking for an efficient integer square root algorithm for ARM Thumb2

折月煮酒 提交于 2019-11-26 05:21:45
问题 I am looking for a fast, integer only algorithm to find the square root (integer part thereof) of an unsigned integer. The code must have excellent performance on ARM Thumb 2 processors. It could be assembly language or C code. Any hints welcome. 回答1: Integer Square Roots by Jack W. Crenshaw could be useful as another reference. The C Snippets Archive also has an integer square root implementation. This one goes beyond just the integer result, and calculates extra fractional (fixed-point)

Square root of BigDecimal in Java

时光怂恿深爱的人放手 提交于 2019-11-26 02:35:41
问题 Can we compute the square root of a BigDecimal in Java by using only the Java API and not a custom-made 100-line algorithm? 回答1: I've used this and it works quite well. Here's an example of how the algorithm works at a high level. Edit: I was curious to see just how accurate this was as defined below. Here is the sqrt(2) from an official source: (first 200 digits) 1

Writing your own square root function

霸气de小男生 提交于 2019-11-26 02:17:05
问题 How do you write your own function for finding the most accurate square root of an integer? After googling it, I found this (archived from its original link), but first, I didn\'t get it completely, and second, it is approximate too. Assume square root as nearest integer (to the actual root) or a float. 回答1: The following computes floor(sqrt(N)) for N > 0: x = 2^ceil(numbits(N)/2) loop: y = floor((x + floor(N/x))/2) if y >= x return x x = y This is a version of Newton's method given in

John Carmack&#39;s Unusual Fast Inverse Square Root (Quake III)

人走茶凉 提交于 2019-11-26 01:56:02
问题 John Carmack has a special function in the Quake III source code which calculates the inverse square root of a float, 4x faster than regular (float)(1.0/sqrt(x)) , including a strange 0x5f3759df constant. See the code below. Can someone explain line by line what exactly is going on here and why this works so much faster than the regular implementation? float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; i =