Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (788) Games in Android Showcase (234) games submitted by our members Games in WIP (860) 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 8469 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.
http://www.youtube.com/playlist?list=PLE18841CABEA24090

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
Interesting read.
Damocles
 « Reply #7 - Posted 2013-11-16 10:27:58 »

This is like Nerd heaven
Pages: [1]
 ignore  |  Print

 hadezbladez (2127 views) 2018-11-16 13:46:03 hadezbladez (808 views) 2018-11-16 13:41:33 hadezbladez (2099 views) 2018-11-16 13:35:35 hadezbladez (416 views) 2018-11-16 13:32:03 EgonOlsen (3451 views) 2018-06-10 19:43:48 EgonOlsen (3680 views) 2018-06-10 19:43:44 EgonOlsen (2272 views) 2018-06-10 19:43:20 DesertCoockie (3002 views) 2018-05-13 18:23:11 nelsongames (3072 views) 2018-04-24 18:15:36 nelsongames (3852 views) 2018-04-24 18:14:32
 Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org