Floating-Point Approximation in Kotlin


The challenge

Given

  • a semi-inclusive interval I = [l, u) (l is in interval I but u is not) l and u being floating numbers (0 <= l < u),
  • an integer n (n > 0)
  • a function f: x (float number) -> f(x) (float number)

we want to return as a list the n values:

f(l), f(l + d), ..., f(u -d) where d = (u - l) / n

or as a string (Bash, Nim):

"f(l), f(l + d), ..., f(u -d)" where d = (u - l) / n

Call this function interp:

interp(f, l, u, n) -> [f(l), f(l + d), ..., f(u -d)]

The n resulting values f(l), f(l + d), ..., f(u -d) will be **floored** to two decimals (except Shell and Nim: see below).

For that you can use: floor(y * 100.0) / 100.0.

Examples

interp(x -> x, 0.0, 0.9, 3) -> [0.; 0.3; 0.6]
interp(x -> x, 0.0, 0.9, 4) -> [0.; 0.22; 0.45; 0.67]
interp(x -> x, 0.0, 1.0, 4) -> [0.; 0.25; 0.5; 0.75]
interp(x -> sin x, 0.0, 0.9, 3)  -> [0.; 0.29; 0.56]

The solution in Kotlin

Option 1:

package approxfloat

fun interp(f: (Double) -> Double, l:Double, u:Double, n:Int):List<Double> {
    val d = (u - l) / n

    return (0 until n).map { Math.floor(f(it * d) * 100.0) / 100.0 }.toList()
}

Option 2:

package approxfloat

fun interp(f: (Double) -> Double, l: Double, u: Double, n: Int): List<Double> =
    MutableList<Double>(n){ l + it * (u - l) / n }
        .map{ f(it) }
        .map{ Math.floor(it * 100) / 100 }

Option 3:

package approxfloat

import kotlin.math.floor

fun interp(f: (Double) -> Double, l:Double, u:Double, n:Int):List<Double> {
    val d = (u - l) / n
    val sequence = generateSequence(l) { it + d }
    return sequence
        .map { floor(f(it) * 100) / 100}
        .take(n).toList()
}

Test cases to validate our solution

package approxfloat

import kotlin.test.assertEquals
import org.junit.Test
import java.util.Random
import org.junit.Assert.*
import java.util.ArrayList

class ApproxFloatTest {
  fun testing(actual:String, expected:String) {
    assertEquals(expected, actual)
  }
  @Test
  fun test1() {
    println("{ x-> x }")
    testing(interp({ x -> x }, 0.0, 9.0, 4).toString(),
            "[0.0, 2.25, 4.5, 6.75]")
    
  }
  @Test
  fun test2() {
    println("{ x-> sin(x) }")
    testing(interp({ x -> Math.sin(x.toDouble()) }, 0.0, 18.0, 12).toString(),
            "[0.0, 0.99, 0.14, -0.98, -0.28, 0.93, 0.41, -0.88, -0.54, 0.8, 0.65, -0.72]")
    
  }
  @Test
  fun test3() {
    println("{ x-> cos(x) }")
    testing(interp({ x -> Math.cos(x.toDouble()) }, 0.0, 21.0, 7).toString(),
            "[1.0, -0.99, 0.96, -0.92, 0.84, -0.76, 0.66]")
    
  }
  
}