// Arup Guha
// 9/9/2026
// Little Math Library

public class MathLib {

	public static void main(String[] args) {
	
		// Test e to the power...
		double mine = exp(2.7);
		double javaans = Math.exp(2.7);
		System.out.println(mine);
		System.out.println(javaans);
		
		// cosine 
		System.out.println(cos(1.35));
		System.out.println(Math.cos(1.35));
	
		// and sine!
		System.out.println(sin(1.35));
		System.out.println(Math.sin(1.35));
	}


	// Returns base raised to the power exp.
	public static double mypow(double base, int exp) {
	
		// Negative exponent rule.
		if (exp < 0) return 1.0/mypow(base, -exp);
		
		// Accumulate result here.
		double res = 1;
		for (int i=0; i<exp; i++)
			res *= base;
			
		// Return answer.
		return res;
	}
	
	// Returns the absolute value of x.
	public static double abs(double x) {
		if (x < 0) 
			return -x;
		return x;
	}
	
	// Returns an approximation of n factorial.
	public static double factorial(int n) {
		
		// Accumulate result here.
		double res = 1;
		
		// Just multiply in each integer from 1 to n, inclusive.
		for (int i=1; i<=n; i++)
			res *= i;
		
		// Ta da!
		return res;
	}
	
	// Returns e to the x.
	public static double exp(double x) {
	
		// Store answer here.
		double res = 0;
		
		// i is the term we're on.
		for (int i=0;; i++) {
		
			// Numerator and denominator from series expansion.
			double num = mypow(x, i);
			double den = factorial(i);
			
			// Get this term.
			double term = num/den;
			
			// Too small to matter.
			if (abs(term) < 1e-9) break;
			
			// Add to result.
			res += term;
		}
		
		return res;
	}
	
	public static double cos(double x) {
		
		// Store answer here.
		double res = 0;
		int sign = 1;
		
		// i is the term we're on.
		for (int i=0;; i+=2) {
		
			// Get numerator and denominator (unsigned)
			double num = mypow(x, i);
			double den = factorial(i);
			
			// Generate signed term.
			double term = sign*num/den;
			
			// Too small to matter.
			if (abs(term) < 1e-9) break;
			
			// Add and flip sign.
			res += term;
			sign = -sign;
		}
		
		return res;
	}
	
	public static double sin(double x) {
		
		// Store answer here.
		double res = 0;
		int sign = 1;
		
		// i is the term we're on.
		for (int i=1;; i+=2) {
		
			// Same idea as cos...
			double num = mypow(x, i);
			double den = factorial(i);
			double term = sign*num/den;
			
			// Too small to matter.
			if (abs(term) < 1e-9) break;
			
			// Add to sum update sign.
			res += term;
			sign = -sign;
		}
		
		return res;
	}
}