 |
BOOK: Beginning Java 2  | This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 16th, 2003, 01:17 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Question from page 128: Why the double cast?
On page 128 in the class MorePrimes, there is a line as follows:
long limit = (long)Math.ceil(Math.sqrt((double)number));
What I want to know is: Why is it necessary to explicitely cast the long variable "number" as double? According to the text, the method Math.sqrt will return a double, so I don't understand the necessity of this cast. Also, when I run this code with "(double)" deleted, it runs fine.
Any explanation would be appreciated.
Bobby
|
|

June 17th, 2003, 06:35 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You are right - the expllicit cast isn't necessary.The compiler will insert a cast to type double for you. However, since there is a cast whether you put it in or not, it does no harm to put it in, and it has the positive effect of demonstrating that it was intentional and not accidental.
Ivor
|
|

June 20th, 2003, 09:28 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks, Ivor. That clears things up.
Bobby
|
|

August 14th, 2003, 06:52 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
what is the Math.ceil just before (math.sqrt?
I had mistakenly left this out of the program the first time I compiled it and ran it. Once I noticed that I had missed it I edited the program file and added it back in. the program ran exactly the same with and without it, so I naturally I wander what it's purpose is in this program.
Thanks,
Endamist
|
|

August 14th, 2003, 07:45 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I realize I need to clearify how I had miss typed the code the first time I had typed it in and ran it. so here is how I originally typed that line of code before I had corrected it:
long limit = (long)Math.sqrt((double)number);
of course I had two end brackets as indicated in the book at first and the compiler came up with an error. at first I though perhapse is was an error in the code example so I removed one of the end brackets and the program compiled not errors and ran exactly like the code listed in the bood does.
Is this perhaps another way of writing this particular line of code? Any thing you can tell me to help me understand the need for Math.ceil to be in this line of code would be appricated.
Thanks,
Endamist
|
|

August 15th, 2003, 04:28 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
With Math.ceil numbers are always rounded up to the next integer. I bet you can't get what Math.floor does ;)
|
|

August 15th, 2003, 12:09 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
 , hummmmm guessing....... rounds down........ but at the moment I do not know because I have not come acrossed it anywhere and I am, after all, learning from written matterials on my own. However, I tend to be persistant enough and stuborn enough that, until I'm satisfied with what I know, I'll keep looking until I find an answer. Even if that answer is slow in appearing or becomeing obvious:D. Stuborness can be a curse and a blessing all wrapped up in one  .
Thanks for responding, but what is the purpose of it in this prime program. If Math.sqrt is there to calculate wether or not a number multiplies out even telling the program that it does not have a prime number and continuing the loop going to the next number until the test is false and the number does not multiply out even. In which case it uses that number that could not be multiplied out evenly, and storing it to be printed out as a prime number along with so many more prime numbers based on a count int given in the the program.... then it would seem that Math.ceil is not necessary in this program. Of course I already know that the program works without it because I ran it with out it. I do want to understand, however why someone would feel that it was needed in this type of program because perhapse I am overlooking the reasoning for it in this code, or would overlook it's need in some other program if I can not understand why it may have been needed in this code. I want to understand the reasoning behind placing it in this code.
Endamist
|
|

August 18th, 2003, 10:19 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I obviously have an earlier version of the book because my page numbers don't match yours, but what I think is catching you out is the fact that Ivor is casting the double to a long, which will lose the decimal part of the number. To help you understand a little bit better run this code, which I accept is ridiculously simple, but it will help illustrate what is going on:
System.out.println(Math.sqrt(7)); // outputs 2.6457513110645907
System.out.println(Math.ceil(Math.sqrt(7))); // outputs 3.0
System.out.println((long)Math.sqrt(7));// outputs 2
System.out.println((long)Math.ceil(Math.sqrt(7))); // outputs 3
|
|

August 19th, 2003, 03:43 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Okay I think I understand this now.
long limit = (long)Math.ceil(Math.sqrt((double)number));
This is passing a number to limit for the purpose of produceing a maximum number to use to try to calculate into the number being tested as a prime. All that Math.ceil is doing is rounding the decimal number to the next whole number still keeping the decimal point. And the cast of long does not include the decimal point.
This would set the maximum number to divide into 7 as 3. without Math.ceil the maximum number would be 2.64575 (the squar root of 7)and leaveing in the cast of long sets the limit to 2.
since no matter which is used to divide 7 by it will come out not dividing evenly which will produce a prime results in either case.
Okay, I just wanted to understand why the results was the same with or without Math.ceil and why it was put into the code. obviously it makes more sence to round up than it does to round down even though the results is the same.
Thanks.
endamist
|
|
 |