How do I compare two dimensions and determine which is longer?
← Operators and Expressions · Ref: Q1191
EK9 Dimension uses unit-suffixed literals and supports comparison operators for same-unit values.
DIMENSION LITERALS
width <- 2.5m height <- 1.8m
COMPARISON (SAME UNIT)
if width > height width is longer ordering <- width <=> height spaceship comparison longer <- width >? height coalescing for greater
IMPORTANT: Comparison only works between same-unit Dimensions. Comparing 2.5m with 10px returns unset — EK9 prevents unit mismatch errors at runtime.
Use convert() to change units before comparing across different unit systems.
See Q36 for Dimension basics. See Q238 for the complete operator set.
Example
defines module qa.operators.dimensioncompare defines function higherOf() as pure -> left as Dimension right as Dimension <- rtn as Dimension: left >? right lowerOf() as pure -> left as Dimension right as Dimension <- rtn as Dimension: left <? right defines program DimensionCompareDemo() stdout <- Stdout() // === DIMENSION LITERALS === width <- 2.5m height <- 1.8m sameWidth <- 2.5m // === COMPARISON === if width > height stdout.println("Width is longer than height") require width > height require height < width require width == sameWidth require width <> height // === SPACESHIP ORDERING === ordering <- width <=> height stdout.println(`Width <=> Height: ${ordering}`) require ordering > 0 // === COALESCING: FIND LONGER AND SHORTER === longer <- higherOf(width, height) shorter <- lowerOf(width, height) stdout.println(`Longer: ${longer}`) stdout.println(`Shorter: ${shorter}`) require longer == width require shorter == height // === CROSS-UNIT RETURNS UNSET === pixels <- 100px crossUnit <- width == pixels require ~crossUnit? stdout.println(`Cross-unit compare isSet: ${crossUnit?}`) // === STRING AND ISSET === dimStr <- $width stdout.println(`Width as string: ${dimStr}`) require width? unsetDim <- Dimension() require ~unsetDim?
Common mistakes
E50060 — Dimension has no getValue() method. EK9 uses comparison operators directly on Dimension values. Use #< to extract the numeric value if needed. See ek9 -h E50060 for details.
Incorrect:
if width.getValue() > height.getValue()
Correct:
if width > height
Other ways to ask this
- Compare two Dimension values to find the larger measurement.
- I need to check whether width exceeds height for a layout calculation.
- In CSS I compare pixel values as numbers — how does EK9 compare dimensions?
Coming from another language?
Java: plain double, manual unit tracking, no compile-time safety. F#: units of measure at compile time. CSS: string comparison. Python: no built-in dimension type. EK9: unit-suffixed literals (2.5m, 10px), same-unit comparison, cross-unit returns unset for safety.
Keywords: unit, height, shorter, dimension, longer, measurement, compare, operator, width, layout