# 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

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```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]```Code language: CSS (css)```

## 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()
}
```Code language: Kotlin (kotlin)```

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 }
```Code language: Kotlin (kotlin)```

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()
}
```Code language: Kotlin (kotlin)```

## 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]")

}

}
```Code language: Kotlin (kotlin)```
Tags:
Subscribe
Notify of