Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculating a SquareRoot  (Read 2798 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);         }}`

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.

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
Roquen
 « 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
Damocles
 « Reply #7 - Posted 2013-11-16 10:27:58 »

This is like Nerd heaven

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 theagentd (11 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (46 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (37 views) 2015-03-13 17:50:16 Olo (43 views) 2015-03-13 17:47:07 ClaasJG (56 views) 2015-03-10 11:36:42 ClaasJG (41 views) 2015-03-10 11:33:01 Pippogeek (47 views) 2015-03-05 14:36:23
 basil_ 22x LiquidNitrogen 21x BurntPizza 21x KevinWorkman 20x EgonOlsen 17x theagentd 16x Roquen 16x princec 11x Varkas 10x 65K 10x Riven 10x SHC 9x Ashedragon 8x Ecumene 7x nsigma 7x phu004 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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