Sass math.log()

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass 1.25+

Definition and Usage

math.log($number, $base: null) returns the logarithm of $number with respect to $base. If $base is null (the default), Sass calculates the natural log (base e). Both $number and $base must be unitless. Load it with @use "sass:math" on Dart Sass 1.25+.

Official samples.

math.log(10) → 2.302585093 · math.log(10, 10) → 1.

Inverse of power.

Pair with math.pow: if y = math.log(x, b), then math.pow(b, y) recovers x (within floating-point precision).

Unitless only.

Do not pass px or %. Cancel units first with math.div, and guard with math.is-unitless.

Syntax

SCSS
@use "sass:math";

math.log($number, $base: null); //=> unitless number

math.log(10);     // 2.302585093  (natural log)
math.log(10, 10); // 1
math.log(8, 2);   // 3

math.log(math.$e); // 1  (ln(e))

// Module API — prefer math.log after @use "sass:math"

Pattern: ensure unitless input → choose natural log or a base → use the result as a scale exponent or step count. Related: math.pow, math.sqrt, math.div.

Bases and Inputs

How the second argument changes the result:

CallMeaningResult
math.log(10)Natural log (base e)2.302585093
math.log(10, 10)Log base 101
math.log(8, 2)Log base 23
math.log(math.$e)ln(e)1
Input with unitsNot allowedError
math.$e.

The sass:math module also exposes the constant math.$e (≈ 2.7182818285) when you want an explicit natural-log base.

SCSS Examples

Examples target Dart Sass 1.25+ with @use "sass:math". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — Official Docs Sample (Natural Log)

Omit the base to get ln(10).

SCSS
@use "sass:math";

@debug math.log(10); // 2.302585093

.basic {
  --ln: #{math.log(10)};
}

How It Works

  1. One argument means natural log.
  2. Input is unitless 10.
  3. Result is about 2.302585093.
  4. Matches the official Sass docs sample.

Example 2 — Official Docs Sample (Base 10)

Pass a second argument for a custom base.

SCSS
@use "sass:math";

@debug math.log(10, 10); // 1

.base {
  --log10: #{math.log(10, 10)};
}

How It Works

  1. Base is 10.
  2. log₁₀(10) is exactly 1.
  3. Both args stay unitless.
  4. Matches the official Sass docs sample.

Example 3 — Base 2 Steps

Count doublings with a binary logarithm.

SCSS
@use "sass:math";

.binary {
  --two: #{math.log(8, 2)};
  --e: #{math.log(math.$e)};
}

How It Works

  1. 8 = 2³ → math.log(8, 2) is 3.
  2. math.$e is the natural-log base constant.
  3. ln(e) is 1.
  4. Handy for scale steps and octave-style tokens.

Example 4 — Round-Trip with math.pow

Power undoes a same-base logarithm.

SCSS
@use "sass:math";

.roundtrip {
  --pow: #{math.pow(10, math.log(10, 10))};
}

How It Works

  1. math.log(10, 10) is 1.
  2. math.pow(10, 1) recovers 10.
  3. Same base on both sides.
  4. Use this pair for exponent math in design tokens.

Example 5 — Emit a Scale Token into CSS

Turn a unitless ratio into a compile-time step count.

SCSS
@use "sass:math";

$ratio: 100;

.scale {
  --steps: #{math.log($ratio, 10)};
  opacity: math.div(math.log(8, 2), 10);
}

How It Works

  1. log₁₀(100) is 2 steps.
  2. log₂(8) / 10 becomes opacity 0.3.
  3. Sass resolves everything before CSS sees it.
  4. Keep inputs unitless or cancel units first.

Edge Cases & Pitfalls

Most log bugs are feeding units, or forgetting that the default base is e (not 10).

units

Unitless input required

Cancel with math.div and check with math.is-unitless.

base

Default is natural log

math.log(10) is ln(10), not log base 10. Pass 10 as the second argument when you want log₁₀.

domain

Positive numbers only

Logarithm is undefined for zero and negatives in real numbers — avoid invalid inputs.

module

Module-only helper

Use math.log after @use "sass:math".

Sass Support

Prefer the module form with Dart Sass 1.25+.

APIStatus
math.log()Dart Sass 1.25+ via @use "sass:math"
InputUnitless $number; optional unitless $base
Default baseNatural log (e) when $base is null
LibSass / Ruby SassNo modern sass:math log

Compile with the Dart Sass CLI.

Key Takeaways

  • Natural log by default; pass a second arg for another base.
  • Unitless only for both number and base.
  • Inverse of math.pow for the same base.
  • Dart Sass 1.25+ via @use "sass:math".

One line: when a unitless Sass number needs a logarithm, call math.log($number) or math.log($number, $base).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Ensure $number (and $base) are unitless
3. Omit $base for natural log; pass a base for log_b
4. Pair with math.pow for round-trips
5. Use math.is-unitless before logging unknown tokens

Frequently Asked Questions

math.log($number, $base: null) returns the logarithm of $number. If $base is null (the default), it returns the natural logarithm (base e). Example: math.log(10) is about 2.302585093; math.log(10, 10) is 1.
Add @use "sass:math"; then call math.log($number) or math.log($number, $base). Requires Dart Sass 1.25+.
No. Official docs require $number and $base to be unitless. Strip units first, for example with math.div($a, $b) when both share a unit. Guard with math.is-unitless().
Official docs expose this as math.log() on sass:math. Prefer the module form with @use "sass:math".
They are inverse ideas: if y = math.log(x, b), then math.pow(b, y) recovers x (within floating-point precision). Pair them for scale exponents and growth math.
Natural log uses base e (about 2.718…). In Sass, math.log(10) with no second argument is ln(10). You can also write math.log($n, math.$e) for the same idea explicitly.

Next: Sass math.max()

Return the highest of one or more numbers.

math.max() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful