-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
86 additions
and
106 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
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
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,82 +1,48 @@ | ||
const greatestCommonDivisor = ( | ||
numerator: number, | ||
denominator: number, | ||
): number => { | ||
while (denominator !== 0) { | ||
const temp = denominator; | ||
// eslint-disable-next-line no-param-reassign | ||
denominator = numerator % denominator; | ||
// eslint-disable-next-line no-param-reassign | ||
numerator = temp; | ||
} | ||
return numerator; | ||
}; | ||
import Big from "big.js"; | ||
|
||
const scaleValue = (value: number, scale: number) => { | ||
return Math.round(value * scale); | ||
}; | ||
Big.DP = 20; | ||
Big.RM = Big.roundDown; | ||
|
||
const maxDivisionPrecision = 16; | ||
export const divide = (numerator: Big, denominator: Big): Big => { | ||
try { | ||
if (denominator.eq(0)) { | ||
return new Big(0); | ||
} | ||
|
||
export const divide = (numerator: number, denominator: number): number => { | ||
if (Number.isNaN(numerator) || Number.isNaN(denominator)) { | ||
return numerator.div(denominator); | ||
} catch (error: unknown) { | ||
throw new Error( | ||
`Arguments cannot be NaN, got numerator: ${numerator}, denominator: ${denominator}`, | ||
`Division error: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
|
||
if (denominator === 0) { | ||
return 0; | ||
} | ||
|
||
const commonDivisor = greatestCommonDivisor(numerator, denominator); | ||
const simplifiedNumerator = numerator / commonDivisor; | ||
const simplifiedDenominator = denominator / commonDivisor; | ||
|
||
const result = simplifiedNumerator / simplifiedDenominator; | ||
|
||
const scale = 10 ** maxDivisionPrecision; | ||
const truncatedResult = scaleValue(result, scale) / scale; | ||
|
||
return truncatedResult; | ||
}; | ||
|
||
const countDecimals = (number: number) => { | ||
if (Math.floor(number) === number) { | ||
return 0; | ||
export const add = (a: Big, b: Big): Big => { | ||
try { | ||
return a.plus(b); | ||
} catch (error: unknown) { | ||
throw new Error( | ||
`Addition error: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
return number.toString().split(".")[1]?.length ?? 0; | ||
}; | ||
|
||
export const add = (a: number, b: number) => { | ||
const precisionA = countDecimals(a); | ||
const precisionB = countDecimals(b); | ||
|
||
const maxPrecision = Math.max(precisionA, precisionB); | ||
|
||
const scale = 10 ** maxPrecision; | ||
|
||
return (scaleValue(a, scale) + scaleValue(b, scale)) / scale; | ||
}; | ||
|
||
export const subtract = (a: number, b: number) => { | ||
const precisionA = countDecimals(a); | ||
const precisionB = countDecimals(b); | ||
|
||
const maxPrecision = Math.max(precisionA, precisionB); | ||
|
||
const scale = 10 ** maxPrecision; | ||
|
||
return (scaleValue(a, scale) - scaleValue(b, scale)) / scale; | ||
export const subtract = (a: Big, b: Big): Big => { | ||
try { | ||
return a.minus(b); | ||
} catch (error: unknown) { | ||
throw new Error( | ||
`Subtraction error: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
}; | ||
|
||
export const multiply = (a: number, b: number) => { | ||
const precisionA = countDecimals(a); | ||
const precisionB = countDecimals(b); | ||
|
||
const totalPrecision = precisionA + precisionB; | ||
|
||
const scale = 10 ** totalPrecision; | ||
|
||
return scaleValue(a * b, scale) / scale; | ||
export const multiply = (a: Big, b: Big): Big => { | ||
try { | ||
return a.times(b); | ||
} catch (error: unknown) { | ||
throw new Error( | ||
`Multiplication error: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
}; |
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