-
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.
- Loading branch information
kry008
committed
Apr 4, 2023
1 parent
3c9fa0c
commit 63eaa06
Showing
6 changed files
with
218 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# calc-new | ||
![GitHub pull requests](https://img.shields.io/github/issues-pr-raw/kry008/calc-new) ![GitHub package.json version](https://img.shields.io/github/package-json/v/kry008/calc-new) ![GitHub Repo stars](https://img.shields.io/github/stars/kry008/calc-new?style=social) | ||
A small library with many math operations |
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,162 @@ | ||
class BasicMath | ||
{ | ||
addition(a, b) | ||
{ | ||
return {a: a, b: b, result: a + b}; | ||
} | ||
addition(a, b, c) | ||
{ | ||
return {a: a, b: b, c: c, result: a + b + c}; | ||
} | ||
addition(a) | ||
{ | ||
var sum = 0; | ||
for (var i = 0; i < a.length; i++) | ||
{ | ||
sum += a[i]; | ||
} | ||
return {a: a, result: sum}; | ||
} | ||
subtraction(a, b) | ||
{ | ||
return {a: a, b: b, result: a - b}; | ||
} | ||
subtraction(a, b, c) | ||
{ | ||
return {a: a, b: b, c: c, result: a - b - c}; | ||
} | ||
subtraction(a) | ||
{ | ||
var sum = 0; | ||
for (var i = 0; i < a.length; i++) | ||
{ | ||
sum -= a[i]; | ||
} | ||
return {a: a, result: sum}; | ||
} | ||
multiplication(a, b) | ||
{ | ||
return {a: a, b: b, result: a * b}; | ||
} | ||
multiplication(a, b, c) | ||
{ | ||
return {a: a, b: b, c: c, result: a * b * c}; | ||
} | ||
multiplication(a) | ||
{ | ||
var sum = 1; | ||
for (var i = 0; i < a.length; i++) | ||
{ | ||
sum *= a[i]; | ||
} | ||
return {a: a, result: sum}; | ||
} | ||
division(a, b) | ||
{ | ||
if (b == 0) | ||
{ | ||
return {a: a, b: b, result: "Cannot divide by zero"}; | ||
} | ||
return {a: a, b: b, result: a / b}; | ||
} | ||
division(a, b, c) | ||
{ | ||
if (b == 0 || c == 0) | ||
{ | ||
return {a: a, b: b, c: c, result: "Cannot divide by zero"}; | ||
} | ||
return {a: a, b: b, c: c, result: a / b / c}; | ||
} | ||
division(a) | ||
{ | ||
var sum = 1; | ||
for (var i = 0; i < a.length; i++) | ||
{ | ||
//check for divide by zero | ||
if (a[i] == 0) | ||
{ | ||
return {a: a, result: "Cannot divide by zero"}; | ||
} | ||
sum /= a[i]; | ||
} | ||
return {a: a, result: sum}; | ||
} | ||
moduo(a, b) | ||
{ | ||
return {a: a, b: b, result: a % b}; | ||
} | ||
power(a, b) | ||
{ | ||
if(b == 0) | ||
{ | ||
return {a: a, b: b, result: 1}; | ||
} | ||
if(b < 0) | ||
{ | ||
// Example: 5^3 = 1 ÷ 5 ÷ 5 ÷ 5 = 0,008 | ||
let sum = 1; | ||
for (var i = 0; i < b; i++) | ||
{ | ||
sum /= a; | ||
} | ||
return {a: a, b: b, result: sum}; | ||
} | ||
if(b > 0) | ||
{ | ||
let sum = 1; | ||
for (var i = 0; i < b; i++) | ||
{ | ||
sum *= a; | ||
} | ||
return {a: a, b: b, result: sum}; | ||
} | ||
} | ||
absolute(a) | ||
{ | ||
if(a < 0) | ||
{ | ||
return {a: a, result: a * -1}; | ||
} | ||
return {a: a, result: a}; | ||
} | ||
squareRoot(a) | ||
{ | ||
return {a: a, result: a*a} | ||
} | ||
logarithm(a, b) | ||
{ | ||
if(a == 0) | ||
{ | ||
return {a: a, b: b, result: "Cannot divide by zero"}; | ||
} | ||
if(b == 0) | ||
{ | ||
return {a: a, b: b, result: "Cannot divide by zero"}; | ||
} | ||
if(a == 1) | ||
{ | ||
return {a: a, b: b, result: "Cannot divide by zero"}; | ||
} | ||
if(a == -1) | ||
{ | ||
return {a: a, b: b, result: "Cannot divide by zero"}; | ||
} | ||
if(a == b) | ||
{ | ||
return {a: a, b: b, result: 1}; | ||
} | ||
if(a == -b) | ||
{ | ||
return {a: a, b: b, result: 1}; | ||
} | ||
let sum = 1; | ||
for (var i = 0; i < b; i++) | ||
{ | ||
sum *= a; | ||
} | ||
return {a: a, b: b, result: sum}; | ||
} | ||
|
||
} | ||
|
||
module.exports = BasicMath; |
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 @@ | ||
class functionsMath | ||
{ | ||
|
||
} | ||
|
||
module.exports = functionsMath; |
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,15 @@ | ||
let basicMath = require('./class/basicMath.js'); | ||
let functionsMath = require('./class/functionsMath.js'); | ||
class allMath { | ||
constructor() { | ||
this.basicMath = new basicMath(); | ||
this.functionsMath = new functionsMath(); | ||
} | ||
|
||
} | ||
|
||
module.exports = { | ||
basicMath: basicMath, | ||
functionsMath: functionsMath, | ||
allMath: allMath | ||
} |
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
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,33 @@ | ||
let math_c = require('./index.js'); | ||
let math_basicMath = new math_c.basicMath(); | ||
|
||
console.log(math_basicMath.addition(1, 2)); | ||
console.log(math_basicMath.addition(1, 2).result); | ||
console.log(math_basicMath.addition(1, 2, 3)); | ||
console.log(math_basicMath.addition(1, 2, 3).result); | ||
console.log(math_basicMath.addition([1, 2, 3])); | ||
console.log(math_basicMath.addition([1, 2, 3]).result); | ||
console.log(math_basicMath.subtraction(1, 2)); | ||
console.log(math_basicMath.subtraction(1, 2).result); | ||
console.log(math_basicMath.subtraction(1, 2, 3)); | ||
console.log(math_basicMath.subtraction(1, 2, 3).result); | ||
console.log(math_basicMath.subtraction([1, 2, 3])); | ||
console.log(math_basicMath.subtraction([1, 2, 3]).result); | ||
console.log(math_basicMath.multiplication(1, 2)); | ||
console.log(math_basicMath.multiplication(1, 2).result); | ||
console.log(math_basicMath.multiplication(1, 2, 3)); | ||
console.log(math_basicMath.multiplication(1, 2, 3).result); | ||
console.log(math_basicMath.multiplication([1, 2, 3])); | ||
console.log(math_basicMath.multiplication([1, 2, 3]).result); | ||
console.log(math_basicMath.division(1, 2)); | ||
console.log(math_basicMath.division(1, 2).result); | ||
console.log(math_basicMath.division(1, 2, 3)); | ||
console.log(math_basicMath.division(1, 2, 3).result); | ||
console.log(math_basicMath.division([1, 2, 3])); | ||
console.log(math_basicMath.division([1, 2, 3]).result); | ||
console.log(math_basicMath.division(1, 0)); | ||
console.log(math_basicMath.division(1, 0).result); | ||
console.log(math_basicMath.division(1, 0, 3)); | ||
console.log(math_basicMath.division(1, 0, 3).result); | ||
console.log(math_basicMath.division([1, 2, 0])); | ||
console.log(math_basicMath.division([1, 2, 0]).result); |