Chapter 3
3.1 Convert the following base-2 numbers to base-10: (a) 1011001,
(b) 110.00101 and (c) 0.01011
I wrote a program to solve this:
public class Binary {
public static void main(String[] args) {
String binary = "1101.1";
String[] halfs = binary.split("\\.");
double answer = Integer.parseInt(halfs[0], 2);
if(halfs.length > 1)
for(int i = 0; i < halfs[1].length(); i++){
if(halfs[1].substring(i, i+1).equals("1")){
answer += (1 / Math.pow(2, (i + 1))) ;
}
}
System.out.println(answer);
}
}
a) 89.0
b) 6.15625
c) 0.34375
3.2 Convert the following base-8 numbers to base 10: 71,563 and
3.14.
I made a program to do this:
public class Octal {
public static void main(String[] args) {
String octal = "3.14";
String[] halfs = octal.split("\\.");
double answer = Integer.parseInt(halfs[0], 8);
if(halfs.length > 1)
for(int i = 0; i < halfs[1].length(); i++){
answer += Integer.parseInt(halfs[1].substring(i, i+1)) *(1 / Math.pow(8, (i + 1))) ;
}
System.out.println(answer);
}
}
a) 29555.0
b) 3.1875
3.3 Compose your own program based on Fig. 3.11 and use it to
determine your computer’s machine epsilon.
I had to convert the epsilon to a string, java couldn't tell that the number was <= 1
public class Epsilon {
public static void main(String[] args) {
double epsilon = 1.0;
while(!String.valueOf(epsilon + 1.0).equals("1.0")){
epsilon /= 2;
}
epsilon = 2 * epsilon;
System.out.println(epsilon);
}
}
2.220446049250313E-16
3.14.
I made a program to do this:
public class Octal {
public static void main(String[] args) {
String octal = "3.14";
String[] halfs = octal.split("\\.");
double answer = Integer.parseInt(halfs[0], 8);
if(halfs.length > 1)
for(int i = 0; i < halfs[1].length(); i++){
answer += Integer.parseInt(halfs[1].substring(i, i+1)) *(1 / Math.pow(8, (i + 1))) ;
}
System.out.println(answer);
}
}
a) 29555.0
b) 3.1875
3.3 Compose your own program based on Fig. 3.11 and use it to
determine your computer’s machine epsilon.
I had to convert the epsilon to a string, java couldn't tell that the number was <= 1
public class Epsilon {
public static void main(String[] args) {
double epsilon = 1.0;
while(!String.valueOf(epsilon + 1.0).equals("1.0")){
epsilon /= 2;
}
epsilon = 2 * epsilon;
System.out.println(epsilon);
}
}
2.220446049250313E-16
3.5)
The infinite series converges on a value of f (n) = π4/90 as n approaches infinity.
Write a program in single precision to calculate f (n) for
n = 10,000 by computing the sum from i = 1 to 10,000. Then
repeat the calculation but in reverse order—that is, from
i = 10,000 to 1 using increments of −1. In each case, compute the
true percent relative error. Explain the results.
public static void main(String[] args) {
double t = 0;
for(int i = 1; i <= 10000; i++)
t+=s(i);
System.out.println(t);
for(int i = 10000; i >0 ;i--)
t+=s(i);
System.out.println(t);
}
public static double s(int t){
return 1 / Math.pow(t,4);
}
public static void main(String[] args) {
double a = ex1(5);
double t = 0.006737947;
System.out.println(a + "\t" + ((t - a)/ t) * 100);
a = ex2(5);
System.out.println(a + "\t" + ((t - a)/ t) * 100);
}
public static double ex1(int e){
double rv = 1.0;
for(int i = 1; i < 20; i++)
if((i&1) == 1)
rv -= (Math.pow(e,i) / fact(i));
else
rv += (Math.pow(e,i) / fact(i));
return rv;
}
public static double ex2(int e){
double rv = 1.0;
for(int i = 1; i < 20; i++)
rv += (Math.pow(e,i) / fact(i));
return 1/rv;
}
public static double fact(int t){
double rv = 1;
for(int i = 2; i <= t; i++)
rv *= i;
return rv;
}
0.00670634105421557 0.46907382596554204
0.00673794932511709 -3.450779725904994E-5
public class Chop {
static int c = 3;
public static void main(String[] args) {
System.out.println(f(0.577));
c= 4;
System.out.println(f(0.577));
}
public static double chop(double t){
String rv = Double.toString(t);
if(rv.indexOf(".") < 0) return t;
return (new Double(rv.substring(0, rv.indexOf(".") + c + 1))).doubleValue();
}
public static double f(double x){
return chop(chop(6 * x) / chop(Math.pow(chop(1 - Math.pow(chop(3 * x), 2)), 2)));
}
}
0.872
0.8689
3.8 (a) Evaluate the polynomial
y = x3 − 7x2 + 8x − 0.35
at x = 1.37. Use 3-digit arithmetic with chopping. Evaluate the
percent relative error.
public class Chop2 {
static int c = 3;
public static void main(String[] args) {
System.out.println(yc(1.37));
System.out.println(((y(1.37) - yc(1.37)) / y(1.37)) * 100);
}
public static double yc(double x){
double rv = chop(Math.pow(x,3));
rv -= chop(7 * (Math.pow(x, 2)));
rv += chop(8 * x);
rv -= 0.35;
return rv;
}
public static double chop(double t){
String rv = Double.toString(t);
if(rv.indexOf(".") < 0) return t;
if((rv.length() - rv.indexOf(".")) <= c) return t;
return (new Double(rv.substring(0, rv.indexOf(".") + c + 1))).doubleValue();
}
public static double y(double x){
double rv = Math.pow(x,3);
rv -= (7 * (Math.pow(x, 2)));
rv += (8 * x);
rv -= 0.35;
return rv;
}
}
public class Chop2 {
static int c = 3;
public static void main(String[] args) {
System.out.println(yc(1.37));
System.out.println(((y(1.37) - yc(1.37)) / y(1.37)) * 100);
}
public static double yc(double x){
double rv = chop(Math.pow(x,3));
rv -= chop(7 * (Math.pow(x, 2)));
rv += chop(8 * x);
rv -= 0.35;
return rv;
}
public static double chop(double t){
String rv = Double.toString(t);
if(rv.indexOf(".") < 0) return t;
if((rv.length() - rv.indexOf(".")) <= c) return t;
return (new Double(rv.substring(0, rv.indexOf(".") + c + 1))).doubleValue();
}
public static double y(double x){
double rv = Math.pow(x,3);
rv -= (7 * (Math.pow(x, 2)));
rv += (8 * x);
rv -= 0.35;
return rv;
}
}
(b) Repeat (a) but express y as
y = ((x − 7)x + 8)x − 0.35
Evaluate the error and compare with part (a).
public class Chop2 {
static int c = 3;
public static void main(String[] args) {
System.out.println(yc(1.37));
System.out.println(((y(1.37) - yc(1.37)) / y(1.37)) * 100);
}
public static double yc(double x){
//y = ((x − 7)x + 8)x − 0.35
double rv = chop(x - 7);
rv = chop((rv * x) + 8);
rv = chop((rv * x) - 0.38);
return rv;
}
public static double chop(double t){
String rv = Double.toString(t);
if(rv.indexOf(".") < 0) return t;
if((rv.length() - rv.indexOf(".")) <= c) return t;
return (new Double(rv.substring(0, rv.indexOf(".") + c + 1))).doubleValue();
}
public static double y(double x){
//y = ((x − 7)x + 8)x − 0.35
double rv = x - 7;
rv = (rv * x) + 8;
rv = (rv * x) - 0.38;
return rv;
}
}
0.011
15.728185091543626
part a had less percent relative error
3.9 Calculate the random access memory (RAM) in megabytes
necessary to store a multidimensional array that is 20 × 40 × 120.
This array is double precision, and each value requires a 64-bit word.
Recall that a 64-bit word = 8 bytes and 1 kilobyte = 2^10 bytes.
Assume that the index starts at 1.
20 x 40 x 120 x 8 = 768000 bytes = 750K = about 0.75 M
public class Cos {
public static void main(String[] args) {
double x = .3 * Math.PI, cosx = Math.cos(x);
double answer = 1.0;
System.out.println("Approximation:\t\t%error");
for(int i = 1; Double.toString(answer).length() < 10; i++){
if((i & 1) == 1)
answer = answer - Math.pow(x, (i*2))/ fact(i * 2);
else
answer = answer + Math.pow(x, (i*2))/ fact(i * 2);
System.out.println(answer + "\t" + ((cosx - answer)/ cosx) * 100);
}
}
public static int fact(int f){
int returnVal = 1;
for(int i = f; i > 1; i--)
returnVal *= i;
return returnVal;
}
}
3.13) The “divide and average” method, an old-time method for approximating
the square root of any positive number a, can be formulated
as: x = (x + a/x) / 2
Write a well-structured function to implement this algorithm based
on the algorithm outlined in Fig. 3.3.
the square root of any positive number a, can be formulated
as: x = (x + a/x) / 2
Write a well-structured function to implement this algorithm based
on the algorithm outlined in Fig. 3.3.