@use "sass:math";

@function pow($base, $exponent, $precision: 12) {
    @if (floor($exponent) !=$exponent) {
        $precision2: pow(10, $precision);
        $exponent: round($exponent * $precision2);
        $denominator: greatest-common-divisor($exponent, $precision2);
        @return nthRoot(pow($base, math.div($exponent, $denominator)), math.div($precision2, $denominator), $precision);
    }

    $value: $base;

    @if $exponent>1 {
        @for $i from 2 through $exponent {
            $value: $value * $base;
        }
    }

    @else if $exponent < 1 {
        @for $i from 0 through -$exponent {
            $value: math.div($value, $base);
        }
    }

    @return $value;
}

@function greatest-common-divisor($a, $b) {
    @if ($b !=0) {
        @return greatest-common-divisor($b, $a % $b);
    }

    @else {
        @return abs($a);
    }
}

@function nthRoot($num, $n: 2, $precision: 12) {
    $x: 1;

    @for $i from 0 through $precision {
        $x: math.div(1, $n) * (($n - 1) * $x + math.div($num, pow($x, $n - 1)));
    }

    @return $x;
}
