Job Board
Consulting

Spark Scala degrees and radians

The degrees and radians functions convert DataFrame columns between the two ways of measuring angles. radians turns degrees into radians; degrees does the reverse. They're the unit-conversion helpers you reach for whenever your data is in degrees but you need to feed it into Spark's trig functions, which all expect radians.

radians: Degrees to Radians

def radians(e: Column): Column

def radians(columnName: String): Column

The radians function multiplies its input by π/180 to convert from degrees to radians. Use it when your source data records angles in degrees (latitudes, headings, rotations from a UI) but you need to call sin, cos, tan, or any of the other trig functions, which all expect radians.

val df = Seq(
  0.0,
  30.0,
  45.0,
  60.0,
  90.0,
  180.0,
  360.0,
).toDF("degrees")

val df2 = df
  .withColumn("radians", radians(col("degrees")))

df2.show(false)
// +-------+------------------+
// |degrees|radians           |
// +-------+------------------+
// |0.0    |0.0               |
// |30.0   |0.5235987755982988|
// |45.0   |0.7853981633974483|
// |60.0   |1.0471975511965976|
// |90.0   |1.5707963267948966|
// |180.0  |3.141592653589793 |
// |360.0  |6.283185307179586 |
// +-------+------------------+

The output of radians(180) is 3.141592653589793 — that's π. radians(360) is , and radians(90) is π/2. These are the values Spark's trig functions expect.

degrees: Radians to Degrees

def degrees(e: Column): Column

def degrees(columnName: String): Column

degrees multiplies by 180/π, going the other direction. It's most useful when you've computed an angle with atan, atan2, asin, or acos — all of which return radians — and you want to present the result in the more human-readable degrees.

val df = Seq(
  0.0,
  math.Pi / 6,
  math.Pi / 4,
  math.Pi / 3,
  math.Pi / 2,
  math.Pi,
  2 * math.Pi,
).toDF("radians")

val df2 = df
  .withColumn("degrees", degrees(col("radians")))

df2.show(false)
// +------------------+------------------+
// |radians           |degrees           |
// +------------------+------------------+
// |0.0               |0.0               |
// |0.5235987755982988|29.999999999999996|
// |0.7853981633974483|45.0              |
// |1.0471975511965976|59.99999999999999 |
// |1.5707963267948966|90.0              |
// |3.141592653589793 |180.0             |
// |6.283185307179586 |360.0             |
// +------------------+------------------+

Notice 29.999999999999996 instead of 30.0 and 59.99999999999999 instead of 60.0. That's not a bug in degrees — it's just that π/6 and π/3 aren't representable exactly in Double precision, so multiplying them by 180/π can't land exactly on the integer either. If you need a clean display value, wrap with round to the precision you actually care about.

Composing with the Trig Functions

The most common reason to use radians is to bridge the gap between source data in degrees and the trig functions. Wrap radians around your column before passing it to sin, cos, or tan:

val df = Seq(
  (0.0, 0.0),
  (30.0, 0.5),
  (45.0, math.sqrt(2) / 2),
  (60.0, math.sqrt(3) / 2),
  (90.0, 1.0),
).toDF("angle_degrees", "expected_sin")

val df2 = df
  .withColumn("sin", sin(radians(col("angle_degrees"))))

df2.show(false)
// +-------------+------------------+-------------------+
// |angle_degrees|expected_sin      |sin                |
// +-------------+------------------+-------------------+
// |0.0          |0.0               |0.0                |
// |30.0         |0.5               |0.49999999999999994|
// |45.0         |0.7071067811865476|0.7071067811865475 |
// |60.0         |0.8660254037844386|0.8660254037844386 |
// |90.0         |1.0               |1.0                |
// +-------------+------------------+-------------------+

The computed sin column matches the textbook values to within IEEE 754 rounding error (0.49999999999999994 vs the exact 0.5). This pattern — sin(radians(col(...))) — is the standard way to apply trig to degree-valued columns in Spark, and it's worth committing to muscle memory. Going the other way, an inverse like degrees(atan2(col("dy"), col("dx"))) gives you a compass-style bearing in degrees from coordinate deltas.

For the trig functions that consume the radians these helpers produce — sin, cos, tan, their inverses, reciprocals, and hyperbolic variants — see Trigonometric Functions.

Example Details

Created: 2026-05-28 10:28:24 PM

Last Updated: 2026-05-28 10:28:24 PM