Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition 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
 
Old April 10th, 2013, 03:22 AM
Registered User
 
Join Date: Apr 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 2 Excercise- Q4 Confusion

I m getting two different output in the programs which are almost same.
just the difference is '.0'. In the first program I have not added 'x.0' after the digits(where x is the integral part). and this thing is giving me 2 different answers.

Please explain why am I getting answers like that???

Code for the first program

1.

public class Exercise4 {
public static void main(String[] args){
double diameterSun = 865000;
double diameterEarth= 7600;
double volumeSun = 0;
double volumeEarth=0;
double radiusSun= diameterSun/2;
double radiusEarth = diameterEarth/2;
double fourOverThree = (4/3);
volumeSun =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusSun,3)));
volumeEarth =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusEarth,3)) );
double ratioVolume = (volumeSun/volumeEarth);

System.out.println("Volume of Sun = "+volumeSun+"\nVolume of Earth = "+volumeEarth+
"\n Ratio = "+ratioVolume);

}
}




the code for the 2nd version.


2.

public class Exercise4 {
public static void main(String[] args){
double diameterSun = 865000.0;
double diameterEarth= 7600.0;
double volumeSun = 0.0;
double volumeEarth=0.0;
double radiusSun= diameterSun/2.0;
double radiusEarth = diameterEarth/2.0;
double fourOverThree = (4.0/3.0);
volumeSun =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusSun,3)));
volumeEarth =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusEarth,3)) );
double ratioVolume = (volumeSun/volumeEarth);

System.out.println("Volume of Sun = "+volumeSun+"\nVolume of Earth = "+volumeEarth+
"\n Ratio = "+ratioVolume);

}
}

Last edited by devanshj95; April 15th, 2013 at 08:00 AM..
 
Old April 26th, 2013, 10:53 PM
Registered User
 
Join Date: Apr 2013
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Default 32 bit precision vs 64 bit precision

Change the calculation of "fourOverThree" to the following:
double fourOverThree = (4.0/3.0);
Although the ratiovolume is the same for both programs, the values for volumeSun and volumeEarth are different because when you initialize fourOverThree using:
double fourOverThree = (4/3);
the 4 and 3 are 32 bit integer literals. After the calculation, the answer is widened to 64 bit precison and stored in the double variable fourOverThree. Using integers in the calculation only provides 32 bits of precision resulting in smaller values in the following calculations.

public class Exercise4 {
public static void main(String[] args){
double diameterSun = 865000;
double diameterEarth= 7600;
double volumeSun = 0;
double volumeEarth=0;
double radiusSun= diameterSun/2;
double radiusEarth = diameterEarth/2;
double fourOverThree = (4.0/3.0);
volumeSun =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusSun,3)));
volumeEarth =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusEarth,3)) );
double ratioVolume = (volumeSun/volumeEarth);

System.out.println("Volume of Sun = "+volumeSun+"\nVolume of Earth = "+volumeEarth+
"\n Ratio = "+ratioVolume);

}
}
The Following User Says Thank You to bkuipers For This Useful Post:
devanshj95 (May 20th, 2013)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 11 - Excercise 1 (ClientIDMode) Tulsi BOOK: Beginning ASP.NET 4 : in C# and VB 6 May 1st, 2012 12:58 PM
Chapter 11 - Excercise 1 Tulsi BOOK: Beginning ASP.NET 4 : in C# and VB 3 April 29th, 2012 06:32 AM
Chapter 4 Excercise 2 [email protected] BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 8 August 19th, 2010 11:24 PM
C#,chapter 8 excercise akbard BOOK: Beginning ASP.NET 1.0 2 October 7th, 2003 03:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.