-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add math functions (cos, sin, sqrt, pi, round, factorial), UI and tes…
…t classes TODO: implement alpha function, fix cos(1)
- Loading branch information
KmZ mac
committed
Jul 20, 2016
1 parent
b2f8fa5
commit aa5ebad
Showing
8 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Taken from /~https://github.com/github/gitignore/blob/master/Java.gitignore | ||
*.class | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Cheers</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.concordia.soen6441.src; | ||
|
||
public class CheersException extends Exception | ||
{ | ||
|
||
public CheersException() | ||
{ | ||
} | ||
|
||
public CheersException(String message) | ||
{ | ||
super(message); | ||
} | ||
|
||
public CheersException(String message, Throwable cause) | ||
{ | ||
super(message, cause); | ||
} | ||
|
||
public CheersException(Throwable cause) | ||
{ | ||
super(cause); | ||
} | ||
|
||
public CheersException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) | ||
{ | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.concordia.soen6441.src; | ||
|
||
public class CheersHelper { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.concordia.soen6441.src; | ||
|
||
public class CheersMath { | ||
|
||
private int precision; | ||
private double pi; | ||
|
||
public CheersMath(int precision){ | ||
this.precision = precision; // TODO: cast precision to long only, no double, string or float allowed | ||
this.pi = pi(); | ||
} | ||
|
||
// Computing square root using Bablonian | ||
protected double sqrt(double number) { | ||
double x = number; | ||
double y = 1; | ||
double e = 0.000000000001; /* for accuracy level*/ | ||
while (x - y > e) { | ||
x = (x + y) / 2; | ||
y = number / x; | ||
} | ||
return x; | ||
} | ||
|
||
protected int factorial(int n){ | ||
if(n == 0) | ||
return 1; | ||
else | ||
return n * factorial(n-1); | ||
} | ||
|
||
protected double degToRad(double deg) { | ||
return deg / 180.0 * pi; | ||
} | ||
|
||
protected double round(double nb){ | ||
double prec = 10^this.precision; | ||
nb *= prec; | ||
nb = (int) nb; | ||
nb /= prec; | ||
return nb; | ||
} | ||
|
||
// 1 - x^2/2! + x^4/4! - x^6/6! + ... | ||
// Computing cos(x) using Taylor Series | ||
protected double cos(double x) { | ||
double cosValue = 1.0, power = 1.0; | ||
int n = 2, factorial = 1; | ||
while (n <= this.precision) { | ||
// while (power/factorial != 0) { | ||
power = power * x * x * -1; | ||
// factorial = factorial(n); | ||
factorial = factorial * n * (n-1); | ||
// System.out.println(power/factorial); | ||
cosValue = cosValue + (power / factorial); | ||
n = n + 2; | ||
} | ||
return cosValue; | ||
} | ||
|
||
// 4(1 - 1/3 + 1/5 - 1/7 + ...) | ||
// Gregory–Leibniz | ||
// http://functions.wolfram.com/Constants/Pi/02/ | ||
protected double pi() { | ||
double piValue = 0, flip = -1, prec = 1000000000; | ||
int n = 1; | ||
while (n <= prec) { | ||
flip = -1 * flip; | ||
piValue = piValue + (flip / n); | ||
n = n + 2; | ||
} | ||
return 4 * piValue; | ||
} | ||
|
||
protected double alpha(){ | ||
// for(double alpha = 0; alpha < 10000; alpha+=1){ | ||
double alpha = 0.0; | ||
double piOverTwo = pi/2.0; | ||
double prec = 0.00001; | ||
// double rad = degToRad(alpha); | ||
while(alpha - sin(alpha) - piOverTwo != prec){ | ||
System.out.println(alpha+" - sin("+alpha+")="+ (alpha - sin(alpha)) +", pi/2="+piOverTwo); | ||
if(alpha!= 0.0 && alpha - sin(alpha) - piOverTwo != prec){ | ||
return alpha; | ||
} | ||
alpha += prec; | ||
} | ||
return 0; | ||
} | ||
|
||
// Computing sin(x) using Taylor Series | ||
protected double sin(double x){ | ||
double term = 1.0; | ||
double sum = 0.0; | ||
x = x % (2 * pi); //converts value to an angle between negative 2 pi to positive 2 pi | ||
|
||
for (int i = 1; term != 0.0; i++) { | ||
term *= (x / i); | ||
if (i % 4 == 1) sum += term; | ||
if (i % 4 == 3) sum -= term; | ||
} | ||
return round(sum); | ||
} | ||
|
||
public double length(double radius){ | ||
// L = 2R(1 – cos(α/2)) | ||
// α – sin(α) = π/2. | ||
|
||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.concordia.soen6441.test; | ||
|
||
import java.lang.Math; | ||
|
||
import com.concordia.soen6441.src.*; | ||
|
||
public class CheersTest extends CheersMath{ | ||
|
||
private static final int PRECISION = 20; | ||
private static final boolean TESTCOS = false; | ||
private static final boolean TESTSIN = false; | ||
private static final boolean TESTSQRT = false; | ||
private static final boolean TESTPI = false; | ||
private static final boolean TESTALPHA = true; | ||
static CheersTest test = new CheersTest(PRECISION); | ||
|
||
public CheersTest(int precision) { | ||
super(precision); | ||
} | ||
|
||
private static void compareCos(double angdeg) { | ||
|
||
System.out.println("Test - Comparing Cos("+angdeg+") in Cheers and Native\n"); | ||
double rad = test.degToRad(angdeg); | ||
System.out.println("rad = " + rad); | ||
System.out.println("deg = " + angdeg); | ||
System.out.println("javaCos = " + Math.cos(rad)); | ||
System.out.println("cheersCos = " + test.cos(rad)); | ||
System.out.println("=======================================\n"); | ||
} | ||
|
||
private static void compareSin(double angdeg) { | ||
|
||
System.out.println("Test - Comparing Sin("+angdeg+") in Cheers and Native\n"); | ||
double rad = test.degToRad(angdeg); | ||
System.out.println("rad = " + rad); | ||
System.out.println("deg = " + angdeg); | ||
System.out.println("javaSin = " + Math.sin(rad)); | ||
System.out.println("cheersSin = " + test.sin(rad)); | ||
System.out.println("=======================================\n"); | ||
} | ||
|
||
private static void comparePi() { | ||
System.out.println("Test - Comparing PI in Cheers and Native\n"); | ||
System.out.println("javaPi = " + Math.PI); | ||
System.out.println("cheersPi = " + test.pi()); | ||
System.out.println("=======================================\n"); | ||
} | ||
|
||
private static void compareSqrt(double nb) { | ||
System.out.println("Test - Comparing Sqrt in Cheers and Native\n"); | ||
System.out.println("javaPi = " + Math.sqrt(nb)); | ||
System.out.println("cheersPi = " + test.sqrt(nb)); | ||
System.out.println("=======================================\n"); | ||
} | ||
|
||
private static void showAlpha() { | ||
System.out.println("Test - Compute alpha value in Cheers\n"); | ||
System.out.println("cheersAlpha = " + test.alpha()); | ||
System.out.println("=======================================\n"); | ||
} | ||
|
||
public static void main(String args[]) throws CheersException { | ||
|
||
try { | ||
if(TESTCOS) | ||
for(int angle=0; angle<=90; angle+=30){ | ||
compareCos(angle); | ||
} | ||
if(TESTSIN) | ||
for(int angle=0; angle<=90; angle+=30){ | ||
compareSin(angle); | ||
} | ||
if(TESTSQRT) | ||
compareSqrt(16); | ||
|
||
if(TESTPI) | ||
comparePi(); | ||
|
||
if(TESTALPHA) | ||
showAlpha(); | ||
} | ||
catch (Exception e) { | ||
System.out.println("Cheers Test Exception: " + e); | ||
} | ||
finally{ | ||
System.out.println("Test Ended Successfully!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.concordia.soen6441.ui; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
|
||
import com.concordia.soen6441.src.*; | ||
|
||
public class CheersUi { | ||
|
||
public static void main(String args[]) throws CheersException { | ||
try { | ||
float radius = 0; | ||
int precision = 0; | ||
// Get user input | ||
while (true) { | ||
InputStreamReader is = new InputStreamReader(System. in ); | ||
BufferedReader br = new BufferedReader(is); | ||
System.out.println("Enter the Radius and Precision:"); | ||
try{ | ||
radius = Float.parseFloat(br.readLine()); // TODO: make parseFloat native | ||
// precision = Integer.parseInt(br.readLine()); | ||
precision = 100; | ||
CheersMath cheersMath = new CheersMath(precision); | ||
System.out.println("length = " + cheersMath.length(radius)); | ||
} | ||
catch(NumberFormatException e){ | ||
System.out.println("Wrong Number Format"); | ||
} | ||
} | ||
} | ||
catch (Exception e) { | ||
System.out.println("Cheers UI Exception: " + e); | ||
} | ||
finally{ | ||
System.out.println("Program ended"); | ||
} | ||
} | ||
} |