Skip to content
Tactiverge

○ Sensors · linear interpolation

Thermistor lookup table

Read between the rows of a datasheet table with linear interpolation and clamp outside it.

  • Level 3 · Innovator
  • 6 tests
  • technically checked

Your task

Thermistor datasheets give a table of resistance (kΩ) at a few temperatures (°C). To estimate a value between rows, draw a straight line between the two neighbouring rows.

Write interpolate(table, x) where table is an array of [x, y] pairs sorted by x:

  • if x is at or below the first row, return the first y; at or above the last row, return the last y (clamp);
  • otherwise find the pair with x₀ ≤ x ≤ x₁ and return y₀ + (y₁ − y₀) × (x − x₀) / (x₁ − x₀).

Hints (0 of 3 shown)

    What gets checked

    • interpolate([[0, 32.6], [25, 10], [50, 3.6], [100, 0.68]], 25) → 10
    • interpolate([[0, 32.6], [25, 10], [50, 3.6], [100, 0.68]], 37.5) → ≈ 6.8 (± 0.000001)

    Plus 4 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.