Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (554)
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 2446 times)
0 Members and 1 Guest are viewing this topic.
Offline 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<comp) {counter++;sum+=adder;adder+=2;}
     
      System.out.println("for "+ base + "\n root is about: "+(counter/1000) + "\n actual root is: " + Math.sqrt(base));
     
   }

}



A similar method was used in the very first pocket calculators.

Online trollwarrior1
« Reply #1 - Posted 2013-11-15 17:32:19 »

Dam that is cool! Cheesy
Offline 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<comp) {counter++;sum+=adder;adder+=2;}
     
      System.out.println("for "+ base + "\n root is about: "+(counter>>4) + "\n actual root is: " + Math.sqrt(base) + "\n processing steps taken:" + counter);
     
   }

}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Roquen
« Reply #4 - Posted 2013-11-15 19:21:56 »

Note: you can't the beat hardware these days.
Offline 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);
   }
Offline StrideColossus
« Reply #6 - Posted 2013-11-16 08:53:16 »

http://en.wikipedia.org/wiki/Fast_inverse_square_root
Interesting read.
Offline Damocles
« Reply #7 - Posted 2013-11-16 10:27:58 »

This is like Nerd heaven Wink

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (39 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!