Java Programming Create a program to create a program that adds the numbers 1/2
ID: 639634 • Letter: J
Question
Java Programming
Create a program to create a program that adds the numbers 1/2 to itself and 1/3 to itself a large number of times using both types float and double. It is to then compare the values of adding the numbers to multiplying 1/2 and 1/3 times the number of times they were added.
The program must first add 1/2 and 1/3 using type float, and compare to the value obtain using multiplication instead of addition. It is to do this for the following values:
1.000
10,000
100,000
1,000,000
10,000,000
100,000,000
1,000,000,000
The program should do this using two nested loops. The outer loop determines the number of times to add and starts at a 1000 and increments by multiples of 10 up to a 1,000,000,000. The inner loop does the addition of the numbers 1/2 and 1/3. When the inner loop completes, output the two sums and the two products, and the differences between the sums and the products to check for rounding errors. Then continue the outer loop. Remember to clear your variables for the sums before the start of the inner loop.
Next the program repeats the above except that it uses type double instead of type float for the variables. This will require a second set of nested loops and different variables.
As always, have your name, the assignment number, and a brief description of the program in comments at the top, have comments in the program, use proper blocking, and make certain to test your program to verify that it is working properly.
Lastly, notice the values produced, do they make sense to you?
Explanation / Answer
public class ErrorCheck {
public static void main(String args[]){
float float_sum_1=0, float_sum_2=0;
float float_mul_1=0, float_mul_2=0;
double double_sum_1=0, double_sum_2=0;
double double_mul_1=0, double_mul_2=0;
System.out.println("FOR FLOATS:- ");
for(long i=1000; i<=1000000000; i=i*10)
{
float_sum_1=0;
float_sum_2=0;
float_mul_1=0;
float_mul_2=0;
for(long j=1; j<=1000; j++)
{
float_sum_1 += (1/2.0);
float_sum_2 += (1/3.0);
}
float_mul_1 = (float) ((1/2.0)*i);
float_mul_2 = (float) ((1/3.0)*i);
System.out.println("For "+i +"times:");
System.out.println("Two sums are: "+float_sum_1+" and "+float_sum_2);
System.out.println("Two muls are: "+float_mul_1+" and "+float_mul_2);
System.out.println("Error as difference are: "+(float_mul_1 - float_sum_1)+ " and "+(float_mul_2 - float_sum_2));
System.out.println(" ");
}
System.out.println(" FOR DOUBLES:- ");
for(long i=1000; i<=1000000000; i=i*10)
{
double_sum_1=0;
double_sum_2=0;
double_mul_1=0;
double_mul_2=0;
for(long j=1; j<=1000; j++)
{
double_sum_1 += (1/2.0);
double_sum_2 += (1/3.0);
}
double_mul_1 = (float) ((1/2.0)*i);
double_mul_2 = (float) ((1/3.0)*i);
System.out.println("For "+i +"times:");
System.out.println("Two sums are: "+double_sum_1+" and "+double_sum_2);
System.out.println("Two muls are: "+double_mul_1+" and "+double_mul_2);
System.out.println("Error as difference are: "+(double_mul_1 - double_sum_1)+ " and "+(double_mul_2 - double_sum_2));
System.out.println(" ");
}
}
}
---------------------------------------------------------------------------
OUTPUT
FOR FLOATS:-
For 1000times:
Two sums are: 500.0 and 333.3341
Two muls are: 500.0 and 333.33334
Error as difference are: 0.0 and -7.6293945E-4
For 10000times:
Two sums are: 500.0 and 333.3341
Two muls are: 5000.0 and 3333.3333
Error as difference are: 4500.0 and 2999.999
For 100000times:
Two sums are: 500.0 and 333.3341
Two muls are: 50000.0 and 33333.332
Error as difference are: 49500.0 and 32999.996
For 1000000times:
Two sums are: 500.0 and 333.3341
Two muls are: 500000.0 and 333333.34
Error as difference are: 499500.0 and 333000.0
For 10000000times:
Two sums are: 500.0 and 333.3341
Two muls are: 5000000.0 and 3333333.2
Error as difference are: 4999500.0 and 3333000.0
For 100000000times:
Two sums are: 500.0 and 333.3341
Two muls are: 5.0E7 and 3.3333334E7
Error as difference are: 4.99995E7 and 3.3333E7
For 1000000000times:
Two sums are: 500.0 and 333.3341
Two muls are: 5.0E8 and 3.33333344E8
Error as difference are: 4.99999488E8 and 3.33333024E8
FOR DOUBLES:-
For 1000times:
Two sums are: 500.0 and 333.33333333333184
Two muls are: 500.0 and 333.3333435058594
Error as difference are: 0.0 and 1.0172527538543363E-5
For 10000times:
Two sums are: 500.0 and 333.33333333333184
Two muls are: 5000.0 and 3333.333251953125
Error as difference are: 4500.0 and 2999.9999186197933
For 100000times:
Two sums are: 500.0 and 333.33333333333184
Two muls are: 50000.0 and 33333.33203125
Error as difference are: 49500.0 and 32999.99869791667
For 1000000times:
Two sums are: 500.0 and 333.33333333333184
Two muls are: 500000.0 and 333333.34375
Error as difference are: 499500.0 and 333000.0104166667
For 10000000times:
Two sums are: 500.0 and 333.33333333333184
Two muls are: 5000000.0 and 3333333.25
Error as difference are: 4999500.0 and 3332999.9166666665
For 100000000times:
Two sums are: 500.0 and 333.33333333333184
Two muls are: 5.0E7 and 3.3333334E7
Error as difference are: 4.99995E7 and 3.3333000666666668E7
For 1000000000times:
Two sums are: 500.0 and 333.33333333333184
Two muls are: 5.0E8 and 3.33333344E8
Error as difference are: 4.999995E8 and 3.333330106666667E8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.