How to resolve the algorithm Map range step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Map range step by step in the Nim programming language

Table of Contents

Problem Statement

Given two ranges:   where:

Write a function/subroutine/... that takes two ranges and a real number, and returns the mapping of the real number from the first to the second range. Use this function to map values from the range   [0, 10]   to the range   [-1, 0].

Show additional idiomatic ways of performing the mapping, using tools available to the language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Map range step by step in the Nim programming language

Source code in the nim programming language

import strformat

type FloatRange = tuple[s, e: float]

proc mapRange(a, b: FloatRange; s: float): float =
  b.s + (s - a.s) * (b.e - b.s) / (a.e - a.s)

for i in 0..10:
  let m = mapRange((0.0,10.0), (-1.0, 0.0), float(i))
  echo &"{i:>2} maps to {m:4.1f}"


  

You may also check:How to resolve the algorithm Shell one-liner step by step in the S-lang programming language
You may also check:How to resolve the algorithm Brace expansion step by step in the Python programming language
You may also check:How to resolve the algorithm Quaternion type step by step in the Crystal programming language
You may also check:How to resolve the algorithm Enumerations step by step in the Rust programming language
You may also check:How to resolve the algorithm Comments step by step in the Gambas programming language