// https://gist.github.com/voxpelli/6304812
@use "sass:math"

@function pi()
  @return 3.14159265359

@function gcd($a, $b)
  // From: http://rosettacode.org/wiki/Greatest_common_divisor#JavaScript
  @if ($b != 0)
    @return gcd($b, $a % $b)
  @else
    @return abs($a)

@function pow($base, $exponent, $prec: 12)
  // Handles decimal exponents by trying to convert them into a fraction and then use a nthRoot-algorithm for parts of the calculation
  @if (floor($exponent) != $exponent)
    $prec2 : pow(10, $prec)
    $exponent: round($exponent * $prec2)
    $denominator: gcd($exponent, $prec2)
    @return nthRoot(pow($base, math.div($exponent, $denominator)), math.div($prec2, $denominator), $prec)
  $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 nthRoot($num, $n: 2, $prec: 12)
  // From: http://rosettacode.org/wiki/Nth_root#JavaScript
  $x: 1
  @for $i from 0 through $prec
    $x: math.div(1, $n) * (($n - 1) * $x + math.div($num, pow($x, $n - 1)))
  @return $x

@function ratio_calculation($n1, $n2, $n3)
  @return math.div($n2 * $n3, $n1)

@function second_decimal_place_floor($num)
  @return floor($num * 10) * 0.1

@function second_decimal_place_ceil($num)
  @return ceil($num * 10) * 0.1

@function second_decimal_place_round($num)
  @return round($num * 10) * 0.1
