- #1
PrudensOptimus
- 646
- 0
Excerpt of my selfwritten math functions, enjoy.
Code:
/*+++++++++++++++++++++++++
/*Project III
/*Object Oriented Programming
/*Dr. Fani Milanova
/*Tom Zhang
/*
/*Note: No java package was
/* imported. Everything
/* was done by selfmade
/* functions.
/* This demonstrates
/* great math and coding
/* abilities without use
/* of predefined math
/* functions.
/*++++++++++++++++++++++++*/
public class Project3 {
public static void main (String[] args) {
displayCalcOutput();
}
static void displayCalcOutput(){
int var1 = 12;
int var2 = 20;
int var3 = 37;
double var4 = 45;
double var5 = 55;
double var6 = -16;
float fltVar1 = (float)25.3;
float fltVar2 = (float)13.5;
float fltVar3 = (float)25.5;
/* MAIN */
System.out.println("+++++++++++++++++++++++++++++++++++++++" + '\n' +
"+ Project III by Tom Zhang " + '\n' +
"+++++++++++++++++++++++++++++++++++++++" + '\n' +
" ");
//a: Difference of var 3 and var2
int var2_1 = var3 - var2;
System.out.println(" (var3 - var2) = (" + var3 + " - " + var2 + ") = " + var2_1);
//b: Quotient of var4 and var5
double var4_1 = var4/var5;
System.out.println(" (var4 / var5) = (" + var4 + " / " + var5 + ") = " + var4_1);
//c: Abs of var6
if(var6 <0)
var6 *= -1;
System.out.println(" |var6| = |" + (-var6) + "| = " + var6);
//d: 4th power of var4
System.out.println(" (var4*var4*var4*var4) = (var4^4) = " + Power(var4,4));
//e: Sin(var4)
System.out.println(" Sin(" + var4 + ") = " + Sin(var4,'d'));
//f: Sqrt(var5)
System.out.println(" Sqrt(" + var5 + ") = " + Sqrt(var5));
//g: Floor(fltVr3)
System.out.println(" Floor(" + fltVar3 + ") = " + Floor(fltVar3));
//h: Ceil(fltVr3)
System.out.println(" Ceil(" + fltVar3 + ") = " + Ceil(fltVar3));
//i: Log(var3)
System.out.println(" Log(" + var3 + ") = " + Log(var3));
//j: Result
double result = ((var1 + var2 - var5)/fltVar1) + ((fltVar3*Power(var1,var2))-var1);
System.out.println(" -------------------------------------------- " + '\n' +
" Result: " + result );
System.out.println('\n');
}
static double Power(double a, int b) {
// Power(a,b) = a^b, where b is integer.
double j = 1.0;
for (int i = 1; i <= b; i++){
j *= a;
}
return j;
}
static double Sin(double a, int b) {
//Sine is an infinite series composed of both negative and positive "odd" terms.
//My approach is to split into "odd" positive and negative parts and sum up.
//But first let us do some conversions necessary.
switch (b) {
case 'd':
//Convert to radians
a *= (3.14/180);
break;
case 'r':
break;
}
//Declare v for pos ones, sign for the sign.
double v = 0.0;
int sign = -1;
for(int i = 1; i <= 10; i+=2){
sign = -sign;
v += sign*(Power(a, i)/Factorial(i));
}
//Sum
return v;
}
static double Factorial(int a) {
//Factorial a! is defined to be a(a-1)(a-2)...(a-i) or j(j+1)(j+2)...(a)
//Example: 7! = 7(7-1)(7-2)...(7-6) = 1(2)(3)(4)...(7).
double j = 1.0;
for (int i = 1; i < a; i++) {
j *= i;
}
return j*a;
}
static double Sqrt(double a) {
//There is really no easy way to find sqrts out of scratch.
//You can expand it, but as numbers vary, inaccurcy varies greatly.
//Thus, my intention is to loop until b*b is about a.
double b = 0;
while((b*b) <= a) {
b += 0.0001;
}
return b;
}
static double Log(double a) {
//Log(a) is also known as 10^b = a, where b = Log(a).
//Since the expansion for Log(a) is very complex and is not needed at this level,
//I will approach Log(a) the same way I approached Sqrt(a).
//Except this time b could be a decimal... Power() wouldn't work, so I have to
//write a new PowerDec Function.
double b = 0;
while(PowerDec(10,b) <= a) {
b += 0.11;
}
return b;
}
static double PowerDec(double x, double a){
//Evaluates decimal powers from i+0.01 -> i+0.99
double b = 0;//value of the root.
int i = 0;
//Check special cases
if (a == 1)
return x;
//Check power if > 1.
while (a > 1) {
a -= 1;
i += 1;
}
//MAIN
if( (a>=0.01) && (a<0.125) ){ //0.11 1/9
while ((b*b*b*b*b*b*b*b*b) <= x) {
b += 0.0001;
}
}
if( (a>=0.125) && (a<0.14) ){ //0.125 1/8
while ((b*b*b*b*b*b*b*b) <= x) {
b += 0.0001;
}
}
if( (a>=0.16) && (a<=0.17) ){ //0.16 1/6
while ((b*b*b*b*b*b) <= x) {
b += 0.0001;
}
}
if( (a>=0.14) && (a<0.17) ){ //0.14 1/7
while ((b*b*b*b*b*b*b) <= x) {
b += 0.0001;
}
}
if( (a>0.17) && (a<0.25) ){ //0.2
while ((b*b*b*b*b) <= x) {
b += 0.0001;
}
}
if( (a>=0.25) && (a<0.3) ) {//0.25
while ((b*b*b*b) <= x) {
b += 0.0001;
}
}
if( (a>=0.3) && (a<0.45) ) {//0.3
while ((b*b*b) <= x) {
b += 0.0001;
}
}
if( (a>=0.45) && (a<0.55) ) {//0.5, just call sqrt
b=Sqrt(x);
}
if( (a>=0.55) && (a<0.65) ) {//0.6, 3/5
while ((b*b*b*b*b) <= (x*x*x)) {
b += 0.0001;
}
}
if( (a>=0.65) && (a<0.75) ) {//0.7, 7/10
while ((b*b*b*b*b*b*b*b*b*b) <= (x*x*x*x*x*x*x)) {
b += 0.0001;
}
}
if( (a>=0.75) && (a<0.85) ) {//0.8, 4/5
while ((b*b*b*b*b) <= (x*x*x*x)) {
b += 0.0001;
}
}
if( (a>=0.85) && (a<=0.99) ) {//0.9, 9/10
while ((b*b*b*b*b*b*b*b*b*b) <= (x*x*x*x*x*x*x*x*x)) {
b += 0.0001;
}
}
//Check if i>=1, multiply b by x i times until i becomes less than 1.
//IE x^(1.2) = x^1 * x^0.2
while (i>=1) {
b *= x;
i -= 1;
}
return b;
}
static int Floor(float z) {
return (int)z;
}
static int Ceil(float z) {
return (int)(z+1);
}
}
Last edited: