Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (769) Games in Android Showcase (230) games submitted by our members Games in WIP (855) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Calculating a SquareRoot  (Read 8207 times) 0 Members and 1 Guest are viewing this topic.
Damocles
 « Posted 2013-11-15 17:29:33 »

Here an "Indy" way to calculate an (approximate) squareRoot

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 `public class CalcSquareRoot {   public static void main(String[] args)       {         //number to calc root of      double base=544.5;            double adder=1;      double sum=0;      double counter=0;            double comp = base*1000000;            while(sum

A similar method was used in the very first pocket calculators.
trollwarrior1
 « Reply #1 - Posted 2013-11-15 17:32:19 »

Dam that is cool!
Damocles
 « Reply #2 - Posted 2013-11-15 17:36:44 »

Here just using ints, adding, bitshifting and multiplication

(more close  to primitive processors)

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 `public class CalcSquareRoot {   public static void main(String[] args)       {         //number to calc root of      int base=544544;            int adder=1;      int sum=0;      int counter=0;            int comp = base<<8;            while(sum>4) + "\n actual root is: " + Math.sqrt(base) + "\n processing steps taken:" + counter);         }}`
 Games published by our own members! Check 'em out!
philfrei
 « Reply #3 - Posted 2013-11-15 19:06:11 »

Neat! Was disappointed it couldn't handle the decimals in the square root of 2.0 though.

Reminds me of SICP lectures online, from MIT (Abelson and Sussman, 1986). Early on there is a square root program using LISP and recursion.

Using Clojure, I wrote the following based on their LISP listing.
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `(defn sqrt [v]  (defn avg [v1 v2] ( / ( + v1 v2 ) 2 ) )  (defn sq [v] ( * v v ) )  (defn abs [v] ( if ( < v 0 ) ( * -1 v) v ) )  (defn good-enough? [v1 v2] ( < ( abs ( - v1 v2) ) 0.00001 ) )  (defn improve-guess [v1 v2] ( avg v1 ( / v2 v1 ) ) )  (defn try-guess [v1 v2]     ( if       (good-enough? (sq v1) v2)       v1       (try-guess (improve-guess v1 v2) v2))    )   (try-guess 1 v))(sqrt 2.0)`

I'm five videos in so far. It's a very interesting set of lectures. I suppose I could have written their examples in Java, but it seems like a good opportunity to get acquainted with a LISP-based language, and Clojure purports to have a lot to offer in terms of gaining functional programming chops.

music and music apps: http://adonax.com
Roquen

JGO Kernel

Medals: 518

 « Reply #4 - Posted 2013-11-15 19:21:56 »

Note: you can't the beat hardware these days.
vbrain
 « Reply #5 - Posted 2013-11-16 03:18:48 »

Christ, all those lines and CPU hugging.

 1  2  3  4  5  6  7  8  9 `   public final static float inverseSqrt(float x) {      final float xhalves = 0.5f * x;      x = (float)(Double.longBitsToDouble(0x5FE6EB50C7B537AAl - (Double.doubleToRawLongBits(x) >> 1)));      return x * (1.5f - xhalves * x * x);   }   public final static float sqrt(final float x) {      return x * inverseSqrt(x);   }`
StrideColossus
 « Reply #6 - Posted 2013-11-16 08:53:16 »

http://en.wikipedia.org/wiki/Fast_inverse_square_root