import type { BigNumber as EthersBigNumber } from "@ethersproject/bignumber" ; import { Decimal } from "decimal.js" ; import type { BigNumber as MathjsBigNumber } from "mathjs" ; import math from "./math" ; import { SCALE } from "./constants" ; import { toMbn , toEbn , solidityMod } from "./helpers" ; export function avg ( x , y ) { const result = math . mean ! ( toMbn ( x ) , toMbn ( y ) ) as MathjsBigNumber ; return toEbn ( result ) ; } export function ceil ( x ) { const result = toMbn ( x ) . ceil ( ) ; return toEbn ( result ) ; } export function div ( x , y ) { if ( y . isZero ( ) ) { throw new Error ( "Cannot divide by zero" ) ; } const result = toMbn ( x ) . div ( toMbn ( y ) ) ; return toEbn ( result ) ; } export function exp ( x ) { const result = toMbn ( x ) . exp ( ) ; return toEbn ( result ) ; } export function exp2 ( x ) { const two = math . bignumber ! ( "2" ) ; const result = < MathjsBigNumber > math . pow ! ( two , toMbn ( x ) ) ; return toEbn ( result ) ; } export function floor ( x ) { const result = toMbn ( x ) . floor ( ) ; return toEbn ( result ) ; } export function frac ( x ) { return solidityMod ( x , SCALE ) ; } export function gm ( x , y ) { const xy = toMbn ( x ) . mul ( toMbn ( y ) ) ; if ( xy . isNegative ( ) ) { throw new Error ( "PRBMath cannot calculate the geometric mean of a negative number" ) ; } const result = math . sqrt ! ( xy ) as MathjsBigNumber ; return toEbn ( result ) ; } export function inv ( x ) { if ( x . isZero ( ) ) { throw new Error ( "Cannot calculate the inverse of zero" ) ; } const one = math . bignumber ! ( "1" ) ; const result = one . div ( toMbn ( x ) ) ; return toEbn ( result ) ; } export function ln ( x ) { if ( x . isZero ( ) ) { throw new Error ( "Cannot calculate the natural logarithm of zero" ) ; } else if ( x . isNegative ( ) ) { throw new Error ( "Cannot calculate the natural logarithm of a negative number" ) ; } const result = math . log ! ( toMbn ( x ) ) as MathjsBigNumber ; return toEbn ( result ) ; } export function log10 ( x ) { if ( x . isZero ( ) ) { throw new Error ( "Cannot calculate the common logarithm of zero" ) ; } else if ( x . isNegative ( ) ) { throw new Error ( "Cannot calculate the common logarithm of a negative number" ) ; } const result = math . log10 ! ( toMbn ( x ) ) as MathjsBigNumber ; return toEbn ( result ) ; } export function log2 ( x ) { if ( x . isZero ( ) ) { throw new Error ( "Cannot calculate the binary logarithm of zero" ) ; } else if ( x . isNegative ( ) ) { throw new Error ( "Cannot calculate the binary logarithm of a negative number" ) ; } const result = math . log2 ! ( toMbn ( x ) ) as MathjsBigNumber ; return toEbn ( result ) ; } export function mul ( x , y ) { const result = toMbn ( x ) . mul ( toMbn ( y ) ) ; return toEbn ( result , Decimal . ROUND_HALF_UP ) ; } export function pow ( x , y ) { if ( x . isNegative ( ) ) { throw new Error ( "PRBMath cannot raise a negative base to a power" ) ; } const result = math . pow ! ( toMbn ( x ) , toMbn ( y ) ) as MathjsBigNumber ; return toEbn ( result ) ; } export function powu ( x , y ) { const exponent = math . bignumber ! ( String ( y ) ) ; const result = math . pow ! ( toMbn ( x ) , exponent ) as MathjsBigNumber ; return toEbn ( result ) ; } export function sqrt ( x ) { if ( x . isNegative ( ) ) { throw new Error ( "Cannot calculate the square root of a negative number" ) ; } const result = math . sqrt ! ( toMbn ( x ) ) as MathjsBigNumber ; return toEbn ( result ) ; }