Sass power function

in code


This was fun to write. Raise or lower a number to any power, although you are limited to five digits of precision. Any number of greater precision is rounded off to the nearest whole number, so you couldn’t have an rem font-size equal to the gravitational constant (not that I’d want to do this):

#gravitationally-awesome {
    // font-size: 0rem;
    font-size: #{6.674 * power(10,-11)}rem;
}
/**
 * Exponential Number
 * -----------------------------------------------------------------------------
 * Raise a number to a given power.
 *
 * @param   number      $number         Number to raise.
 * @param   number      $power          Power to which it should be raised.
 * @return  number      $exponent       Number raised to exponent.
 */

@function power($number, $power) {
    $exponent: 1;

    @if $power > 0 {
        @for $i from 1 through $power {
            $exponent: $exponent * $number;
        }
    } @else if $power < 0 {
        @for $i from 1 through $power {
            $exponent: $exponent / $number;
        }
    }

    @return $exponent;
}


March 20

in me


Your email address will not be published. Required fields are marked *