How do I compare screen resolutions and find the higher one in EK9?

← Operators and Expressions · Ref: Q1193

EK9 Resolution uses DPI/PPI literals and supports all comparison and coalescing operators.

RESOLUTION LITERALS

  screenDpi <- 96dpi
  printDpi <- 300dpi

COMPARISON

  if printDpi > screenDpi       higher resolution
  ordering <- screenDpi <=> printDpi  spaceship comparison

COALESCING

  higherDpi <- screenDpi >? printDpi   returns the higher resolution
  lowerDpi <- screenDpi <? printDpi    returns the lower resolution

Resolution works with the same operators as Integer, Date, Money, Dimension — EK9 operators are universal across types.

See Q23 for basic types. See Q238 for the complete operator set.

Example

defines module qa.operators.resolutioncompare

  defines function

    higherOf() as pure
      ->
        left as Resolution
        right as Resolution
      <- rtn as Resolution: left >? right

    lowerOf() as pure
      ->
        left as Resolution
        right as Resolution
      <- rtn as Resolution: left <? right

  defines program

    ResolutionCompareDemo()
      stdout <- Stdout()

      // === RESOLUTION LITERALS ===

      screenDpi <- 96dpi
      printDpi <- 300dpi
      retinaDisplay <- 220dpi

      // === COMPARISON ===

      if printDpi > screenDpi
        stdout.println("Print has higher resolution than screen")

      require printDpi > screenDpi
      require screenDpi < printDpi
      require screenDpi <> printDpi

      // === SPACESHIP ORDERING ===

      ordering <- screenDpi <=> printDpi
      stdout.println(`Screen <=> Print: ${ordering}`)
      require ordering < 0

      // === COALESCING: FIND HIGHER AND LOWER ===

      higherDpi <- higherOf(screenDpi, printDpi)
      lowerDpi <- lowerOf(screenDpi, printDpi)
      stdout.println(`Higher DPI: ${higherDpi}`)
      stdout.println(`Lower DPI: ${lowerDpi}`)
      require higherDpi == printDpi
      require lowerDpi == screenDpi

      // === RETINA COMPARISON ===

      require retinaDisplay > screenDpi
      require retinaDisplay < printDpi
      stdout.println(`Retina: ${retinaDisplay}`)

      // === STRING AND ISSET ===

      dpiStr <- $printDpi
      stdout.println(`Print DPI as string: ${dpiStr}`)
      require printDpi?

      unsetRes <- Resolution()
      require ~unsetRes?

Common mistakes

E50001 — There is no 'Math' type in EK9 so 'Math.max' does not resolve; use the '>?' coalescing operator to select the greater value. See ek9 -h E50001 for details.

Incorrect:

      higherDpi <- Math.max(screenDpi, printDpi)

Correct:

      higherDpi <- higherOf(screenDpi, printDpi)
Other ways to ask this
  • Compare two Resolution values and pick the greater using >? in EK9.
  • I need to select the higher DPI for print output from two options.
  • How do I work with Resolution literals like 96dpi and 300dpi?

Coming from another language?

Java: no built-in resolution type, use int with manual DPI tracking. Python: no built-in. CSS: dpi in media queries but not programmable. Go: no built-in. EK9: 96dpi literals, direct comparison operators, >? coalescing for safe maximum selection.

Keywords: screen, ppi, resolution, lower, higher, operator, compare, print, dpi, display