Skip to content

Commit

Permalink
Completed Trig functions
Browse files Browse the repository at this point in the history
- Bug fixes
- Optimizations added
  • Loading branch information
Jsoto22 committed Apr 24, 2024
1 parent 53de852 commit fd49fdc
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 44 deletions.
147 changes: 125 additions & 22 deletions src/big-decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { roundOff } from "./round";
import { multiply } from "./multiply";
import { divide } from "./divide";
import { modulus, modulusE } from "./modulus";
import { compareTo, lessThan } from "./compareTo";
import { compareTo, equals, greaterThan, lessThan } from "./compareTo";
import { subtract, negate } from "./subtract";
import { RoundingModes as Modes, RoundingModes } from "./roundingModes";
import { stripTrailingZero } from "./stripTrailingZero";
import { cbRoot, pow, sqRoot } from "./pow";
import { factorial } from "./utils";
import { cos, sin, tan } from "./trig";
import { ln, log, ln2, log10, exp, LN2, LN10, LN2E, LN10E, Euler, expm1 } from "./logarithm";
import { factorial, sign } from "./utils";
import { acos, asin, atan, atan2, cos, cosh, hypot, sin, sinh, tan, tanh } from "./trig";
import { ln, log, ln2, log10, exp, LN2, LN10, LOG2E, LOG10E, Euler, expm1 } from "./logarithm";

class bigDecimal {
private value: string;
Expand Down Expand Up @@ -209,16 +209,6 @@ class bigDecimal {
return new bigDecimal(modulusE(this.value, number.getValue()));
}

static compareTo(number1, number2) {
number1 = bigDecimal.validate(number1);
number2 = bigDecimal.validate(number2);
return compareTo(number1, number2);
}

compareTo(number: bigDecimal) {
return compareTo(this.value, number.getValue());
}

static negate(number) {
number = bigDecimal.validate(number);
return negate(number);
Expand All @@ -230,15 +220,14 @@ class bigDecimal {

// Powers

static pow(base: number|string, exponent: string) {
static pow(base: number|string, exponent: number|string) {
base = bigDecimal.validate(base);
exponent = bigDecimal.validate(exponent);
return pow(base, exponent);
}

pow(exponent: number|string) {
exponent = bigDecimal.validate(exponent);
return new bigDecimal(pow(this.value, exponent, 32));
pow(exponent: bigDecimal) {
return new bigDecimal(pow(this.value, exponent.getValue(), 32));
}

// Roots
Expand Down Expand Up @@ -283,12 +272,12 @@ class bigDecimal {
return LN10
}

static get LN2E(){
return LN2E
static get LOG2E(){
return LOG2E
}

static get LN10E(){
return LN10E
static get LOG10E(){
return LOG10E
}

static log2(number: number|string){
Expand Down Expand Up @@ -322,21 +311,135 @@ class bigDecimal {
}

// Trig
static hypot(a: number|string, b: number|string){
a = bigDecimal.validate(a);
b = bigDecimal.validate(b);

return hypot(a,b);

}

static sin(number: number|string): string {
number = bigDecimal.validate(number);
return sin(number);
}

static sinh(number: number|string): string {
number = bigDecimal.validate(number);
return sinh(number);
}

static asin(number: number|string): string {
number = bigDecimal.validate(number);
return asin(number);
}

static cos(number: number|string): string {
number = bigDecimal.validate(number);
return cos(number);
}

static cosh(number: number|string): string {
number = bigDecimal.validate(number);
return cosh(number);
}

static acos(number: number|string): string {
number = bigDecimal.validate(number);
return acos(number);
}

static tan(number: number|string): string {
number = bigDecimal.validate(number);
return tan(number);
}

static tanh(number: number|string): string {
number = bigDecimal.validate(number);
return tanh(number);
}

static atan(number: number|string): string {
number = bigDecimal.validate(number);
return atan(number);
}

static atan2(y: number|string, x: number|string): string {
x = bigDecimal.validate(x);
y = bigDecimal.validate(y);
return atan2(y, x);
}

// Comparisons
static compareTo(number1: number|string, number2: number|string) {
number1 = bigDecimal.validate(number1);
number2 = bigDecimal.validate(number2);
return compareTo(number1, number2);
}

compareTo(number: bigDecimal) {
return compareTo(this.value, number.getValue());
}

static equals(number1: number|string, number2: number|string){
number1 = bigDecimal.validate(number1);
number2 = bigDecimal.validate(number2);
return equals(number1, number2);
}

equals(number: bigDecimal){
return equals(this.value, number.getValue());
}

static lt(number1: number|string, number2: number|string){
number1 = bigDecimal.validate(number1);
number2 = bigDecimal.validate(number2);
return lessThan(number1, number2);
}

lt(number: bigDecimal){
return lessThan(this.value, number.getValue());
}

static leq(number1: number|string, number2: number|string){
number1 = bigDecimal.validate(number1);
number2 = bigDecimal.validate(number2);
return lessThan(number1, number2, true);
}

leq(number: bigDecimal){
return lessThan(this.value, number.getValue(), true);
}

static gt(number1: number|string, number2: number|string){
number1 = bigDecimal.validate(number1);
number2 = bigDecimal.validate(number2);
return greaterThan(number1, number2);
}

gt(number: bigDecimal){
return greaterThan(this.value, number.getValue());
}

static geq(number1: number|string, number2: number|string){
number1 = bigDecimal.validate(number1);
number2 = bigDecimal.validate(number2);
return greaterThan(number1, number2, true);
}

geq(number: bigDecimal){
return greaterThan(this.value, number.getValue(), true);
}

static sign(number: number|string){
number = bigDecimal.validate(number);
return sign(number);
}

sign(){
return sign(this.value);
}

// Misc.

static factorial(number: number|string): string {
Expand Down
18 changes: 8 additions & 10 deletions src/logarithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { roundOff } from "./round";
import { subtract } from "./subtract";
import { factorial, sigma, tolerance } from "./utils";

export const LN2 = ln('2');
export const LN2E = ln2(Euler(32));
export const LN10 = ln('10');
export const LN10E = log10(Euler(32));
export const LN2 = '0.69314718055994530941723212145818';
export const LOG2E = '1.44269504088896340735992468100188';
export const LN10 = '2.30258509299404568392825848336901';
export const LOG10E = '0.43429448190325182766805360691429';

export function Euler(precision: number = 32) {
precision = Math.max(16, precision)
Expand All @@ -24,7 +24,7 @@ export function exp(exponent: number | string) {
}

export function expm1(exponent: number | string) {
return subtract('1', pow(Euler(32), exponent))
return subtract(pow(Euler(32), exponent), '1')
}

export function ln(x: string | number = 2) {
Expand All @@ -44,13 +44,12 @@ export function ln(x: string | number = 2) {
i++
let iteration = subtract(multiply('2', i), '1');
let next = multiply(divide('1', iteration, 33), pow(term, iteration))
if (lessThan(next, tolerance(32)) || i == 100) {
if (lessThan(next, tolerance(33))) {
return roundOff(multiply('2', add(result, next)), 32)
}
result = add(result, next);
}

// return multiply('2', result)
}

export function ln2(x: string | number = 2) {
Expand All @@ -63,12 +62,11 @@ export function ln2(x: string | number = 2) {
x = divide(x, 2, 33);
result = add(result, '1');
}
var fractionalPart = ln(x);
return roundOff(add(result, divide(fractionalPart, LN2,33)), 32);
return roundOff(add(result, divide(ln(x), LN2,33)), 32);
}

export function log(base: string | number) {
return roundOff(multiply(ln2(base), LN10), 32)
return roundOff(multiply(ln2(base), LN2), 32)
}

export function log10(base: string | number) {
Expand Down
Loading

0 comments on commit fd49fdc

Please sign in to comment.