-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqrtClassTester.java
More file actions
executable file
·112 lines (94 loc) · 2.71 KB
/
Copy pathSqrtClassTester.java
File metadata and controls
executable file
·112 lines (94 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public class SqrtClassTester
{
public static double[] arrange(double start, double stop, double step)
{
int N = 0;
if (start >= stop)
{
return new double[0];
}
else
{
N = (int) ((stop - start) / step) + 1;
}
double[] result = new double[N];
for (int i = 0; i < N; i++)
{
result[i] = start + i * step;
}
return result;
}
public static boolean areWithinEpsilon(double a, double b, double epsilon)
{
if ( a > b )
{
if ( a - b >= epsilon ) return false;
}
else if ( a < b )
{
if ( b - a >= epsilon ) return false;
}
return true;
}
public static boolean testGood(double[] testArray, double epsilon)
{
for (int i = 0; i < testArray.length; i++)
{
if (areWithinEpsilon(GoodSquareRoot.sqrt(testArray[i]), Math.sqrt(testArray[i]), epsilon) == false)
{
System.out.print("GoodSqrt - Not valid within: ");
if (GoodSquareRoot.sqrt(testArray[i]) > Math.sqrt(testArray[i]))
{
System.out.print(GoodSquareRoot.sqrt(testArray[i]) - Math.sqrt(testArray[i]));
}
else System.out.print(Math.sqrt(testArray[i]) - GoodSquareRoot.sqrt(testArray[i]));
return false;
}
}
return true;
}
public static boolean testBad(double[] testArray, double epsilon)
{
for (int i = 0; i < testArray.length; i++)
{
if (areWithinEpsilon(BadSquareRoot.sqrt(testArray[i]), Math.sqrt(testArray[i]), epsilon) == false)
{
System.out.print("BadSqrt - Not valid within: ");
if (BadSquareRoot.sqrt(testArray[i]) > Math.sqrt(testArray[i]))
{
System.out.print(BadSquareRoot.sqrt(testArray[i]) - Math.sqrt(testArray[i]));
}
else System.out.print(Math.sqrt(testArray[i]) - BadSquareRoot.sqrt(testArray[i]));
return false;
}
}
return true;
}
public static boolean testUgly(double[] testArray, double epsilon)
{
for (int i = 0; i < testArray.length; i++)
{
if (areWithinEpsilon(UglySquareRoot.sqrt(testArray[i]), Math.sqrt(testArray[i]), epsilon) == false)
{
System.out.print("UglySqrt - Not valid within: ");
if (BadSquareRoot.sqrt(testArray[i]) > Math.sqrt(testArray[i]))
{
System.out.print(UglySquareRoot.sqrt(testArray[i]) - Math.sqrt(testArray[i]));
}
else System.out.print(Math.sqrt(testArray[i]) - UglySquareRoot.sqrt(testArray[i]));
return false;
}
}
return true;
}
public static void main(String[] args)
{
double start = 1.0;
double stop = 5.0;
double inc = 0.1;
double epsilon = 0.001;
testGood(arrange(start,stop,inc), epsilon );
testBad(arrange(start,stop,inc), epsilon );
testUgly(arrange(start,stop,inc), epsilon );
}
}