GPWiki.org
GPWiki.org
It is currently Sat May 25, 2013 9:56 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: I need the answer!
PostPosted: Wed Apr 11, 2012 11:39 am 
Prolific Poster

Joined: Sun Jan 08, 2012 10:26 pm
Posts: 22
I was writing in Java a little program that serves no use and the answer is 0.

Here is the code.

Code:
public static byte midget;
   public static long daniel;
   public static int x;
   public static int y;
   public static int jesus = 1337;
   public static int answer;
   
   public static void main(String[] args) {
      x = (int) (Math.PI/13);
      y = (int) (Math.PI / x);
      
      midget = (byte) (x*y);
      daniel = (long) (jesus/Math.PI);
      
      answer = (int) (midget * daniel);
      
      System.out.println(answer);
      
   }


I originally had this:

Code:
public static byte midget;
   public static long daniel;
   public static int x;
   public static int y;
   public static int jesus = 1337;
   public static int answer;
   
   public static void main(String[] args) {
      x = (int) ([b]daniel/midget[/b]);
      y = (int) (Math.PI / x);
      
      midget = (byte) (x*y);
      daniel = (long) (jesus/Math.PI);
      
      answer = (int) (midget * daniel);
      
      System.out.println(answer);
      
   }


But obvoiusly this happened:


Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(Main.java:13)


So, I'm no mathemetician, so can someone go step by step on how it gets to zero?

Thanks

_________________
Ghost of John Trailer:

http://www.youtube.com/watch?v=W1Ddgec36Jk&feature=youtu.be


Top
 Profile  
 
 Post subject: Re: I need the answer!
PostPosted: Wed Apr 11, 2012 11:58 am 
You divided by Midget before you assigned a value to it.. it will start with an unnasigned value of 0. I could be wrong but thats my Take.

M


Top
  
 
 Post subject: Re: I need the answer!
PostPosted: Wed Apr 11, 2012 12:01 pm 
..that was for the 2nd codeblock. Does the first code bock give you the error? if it does, i might see why.

PI/13 will evaluate to a decimal, less than 1.. you explicitly typecast the result into an int.. not sure if it rounds up or down.. but if it rounds down, youl get zero.. and you tried to divide by that result.

just my 0.02

M


Top
  
 
 Post subject: Re: I need the answer!
PostPosted: Wed Apr 11, 2012 2:09 pm 
Mumbo Jumbo
User avatar

Joined: Fri Aug 20, 2004 1:15 pm
Posts: 804
Location: Michigan, USA
the answer, of course, is 42

also, (int) (Math.PI / 13) = 0

_________________
The path to hell is paved with clever code.


Top
 Profile  
 
 Post subject: Re: I need the answer!
PostPosted: Wed Apr 11, 2012 3:32 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3810
Location: Ferriday, LA, US
Floating-point numbers cast into integers are invariably rounded down (technically, not "rounded down", but anything after the decimal point is rather truncated from the value), unless you explicitly use the Math.round() function (or, if you want to round up anything after the decimal, you can use Math.ceil() instead).

And, of course, division by zero is always problematic -- some languages will throw errors/exceptions... but some will simply return a special value (such as NaN -- "Not A Number", or "Infinity"). Further, although Java is nice to you by automatically initializing numeric variables to zero, not all languages are so courteous, and such practices can actually cause your code to segfault, rather than give you a nice and helpful exception and a clean exit. :)

Moral of the story: Don't mess with variables directly until you know they are what you expect. ;)

Another issue that commonly gives people grief is Java's automatic type casting. For example:

Code:
val = 1.0 * 4.5678; // -- returns 4.5678


Versus:

Code:
val = 1 * 4.5678; // -- returns 4


Java sees an integer (1 with no decimal point) and handles the formula as an integer formula -- it multiplies the integer by the float, but the result is automatically cast to an integer.

Java is a silly language. :P

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject: Re: I need the answer!
PostPosted: Thu Apr 12, 2012 12:12 am 
Prolific Poster

Joined: Sun Jan 08, 2012 10:26 pm
Posts: 22
I changed a few of the signs to this and I got a real number that doesn't = zero.

Code:
      x = (int) (Math.PI+13);
      y = (int) (Math.PI * x);
      
      midget = (byte) (x*y);
      daniel = (long) (jesus/Math.PI);
      
      answer = (int) (midget * daniel);
      
      System.out.println("Original Answer: " + answer);


I'm gonna see if I can change the signs to equal 42. It would take ages though.

Of course I could always just right System.out.println("42"); but that's cheating.

Edit: I changed it to this:

Code:
x = (int) (Math.PI+13);
      y = (int) (Math.PI * x);
      
      midget = (byte) (x/y);
      daniel = (long) (jesus*Math.PI);
      
      answer = (int) (midget * daniel);
      
      System.out.println("Original Answer: " + answer);


and got 0 but then on the x/y for midget I changed it to x/y+42, thinking I would get 42, and I got this. 176400. Then I added 42 to the answer and bam! I got 42 :D But that's still kinda cheating.

So if anyone can figure out how to get 42 by just changing the signs you are a maths ninja.

Edit2: I've got this now:

Code:
   public static byte midget;
   public static long daniel;
   public static int x;
   public static int y;
   public static int jesus;
   public static int god;
   public static int answer;
   public static int workOut;
   
   public static void main(String[] args) {
      x = (int) (Math.PI+13);
      y = (int) (Math.PI - x);
      
      jesus = (int) (Math.cos(daniel));
      
      midget = (byte) (x+y);
      daniel = (long) (jesus*Math.PI);
      
      answer = (int) (midget * daniel);
      
      workOut = 12*4-6;
      
      System.out.println("Original Answer: " + answer);
      System.out.println("Working Out: " + workOut);
      
   }


and it equals 12. All I need to do now is add in 12*4-6 somewhere. But I dont want it to be those numbers just added in, so I'm adding a few methods to get the numbers I need.

Edit3: I'm finally done! Here is the full code :D

Spoiler: show
Code:
public class Main {
   
   public static byte midget;
   public static long daniel;
   public static int x;
   public static int y;
   public static int jesus;
   public static int god;
   public static int answer;
   public static byte midget2;
   public static long daniel2;
   public static int x2;
   public static int y2;
   public static int jesus2;
   public static int god2;
   public static int answer2;
   public static byte midget3;
   public static long daniel3;
   public static int x3;
   public static int y3;
   public static int jesus3;
   public static int god3;
   public static int answer3;
   public static int otherAnswer;
   
    public static void six() {
      
      x2 = (int) (Math.PI+13);
      y2 = (int) (Math.PI - x2);
      
      jesus2 = (int) (Math.cos(daniel2));
      
      midget2 = (byte) (x2+y2);
      daniel2 = (long) (jesus2*Math.PI);
      
      answer2 = (int) (midget2 * daniel2) / 2;
      
   }
   
    public static void four() {
       
       x3 = (int) (Math.PI+13);
      y3 = (int) (Math.PI - x3);
      
      jesus3 = (int) (Math.cos(daniel3));
      
      midget3 = (byte) (x3+y3);
      daniel3 = (long) ((jesus3-Math.PI));
      
      answer3 = (int) (midget3 + daniel3) * 2;
       
    }
   
   public static void main(String[] args) {
      
      six();
      four();
      
      x = (int) (Math.PI+13);
      y = (int) (Math.PI - x);
      
      jesus = (int) (Math.cos(daniel));
      
      midget = (byte) (x+y);
      daniel = (long) (jesus*Math.PI);
      
      answer = (int) (midget * daniel * answer3 - answer2);
      
      System.out.println("Original Answer: " + answer);
      
   }
}


It equals 42! I'm so proud :D

_________________
Ghost of John Trailer:

http://www.youtube.com/watch?v=W1Ddgec36Jk&feature=youtu.be


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group