Job Board
Consulting

Spark Scala E and Pi Constants

e() returns Euler's number (≈ 2.71828) and pi() returns π (≈ 3.14159). They're handy when a Spark expression needs one of these mathematical constants — for circle math, exponential growth, trigonometry, and so on — without you having to hard-code the literal value.

Both are Spark SQL functions. They aren't exposed in the org.apache.spark.sql.functions object, so you call them through expr():

def e(): Column — via expr()

def pi(): Column — via expr()

Each function takes no arguments and returns a double-precision value. The simplest way to see them in action is to project both as new columns:

val df = spark.range(1).toDF("id")

val df2 = df
  .withColumn("e",  expr("e()"))
  .withColumn("pi", expr("pi()"))

df2.show(false)
// +---+-----------------+-----------------+
// |id |e                |pi               |
// +---+-----------------+-----------------+
// |0  |2.718281828459045|3.141592653589793|
// +---+-----------------+-----------------+

The returned values match the java.lang.Math.E and java.lang.Math.PI constants from the JVM.

Using pi() for Circle Math

The most common use of pi() is computing the circumference or area of a circle from a radius column:

val df = Seq(
  ("small",  1.0),
  ("medium", 2.5),
  ("large",  5.0),
  ("xl",     10.0),
).toDF("size", "radius")

val df2 = df
  .withColumn("circumference", expr("2 * pi() * radius"))
  .withColumn("area",          expr("pi() * radius * radius"))

df2.show(false)
// +------+------+------------------+------------------+
// |size  |radius|circumference     |area              |
// +------+------+------------------+------------------+
// |small |1.0   |6.283185307179586 |3.141592653589793 |
// |medium|2.5   |15.707963267948966|19.634954084936208|
// |large |5.0   |31.41592653589793 |78.53981633974483 |
// |xl    |10.0  |62.83185307179586 |314.1592653589793 |
// +------+------+------------------+------------------+

If you're working with angles in degrees and need to convert to radians (or vice versa), reach for degrees and radians instead of multiplying by pi() / 180 by hand.

Using e() for Exponential Growth

e() shows up wherever continuous compounding or exponential decay appears. Here's the continuous-compounding formula A = P * e^(rt) applied to a few accounts:

val df = Seq(
  ("account_a", 1000.0, 0.05, 1),
  ("account_b", 1000.0, 0.05, 5),
  ("account_c", 1000.0, 0.05, 10),
  ("account_d", 1000.0, 0.07, 10),
).toDF("account", "principal", "rate", "years")

val df2 = df
  .withColumn("balance", expr("principal * power(e(), rate * years)"))

df2.show(false)
// +---------+---------+----+-----+------------------+
// |account  |principal|rate|years|balance           |
// +---------+---------+----+-----+------------------+
// |account_a|1000.0   |0.05|1    |1051.2710963760242|
// |account_b|1000.0   |0.05|5    |1284.0254166877414|
// |account_c|1000.0   |0.05|10   |1648.7212707001281|
// |account_d|1000.0   |0.07|10   |2013.7527074704767|
// +---------+---------+----+-----+------------------+

If e() is only ever the base of an exponentiation, the exp function is a cleaner choice — exp(x) is equivalent to power(e(), x) and reads more naturally. Use e() when you actually need the constant on its own (for example, when comparing it to another value or scaling it inside a larger expression).

Example Details

Created: 2026-06-04 10:06:55 PM

Last Updated: 2026-06-04 10:06:55 PM