How to generate a range of floating-point numbers in Kotlin

2 Answers

0 votes
/*
 * ============================================================
 * Title: Generating a Range of Floating-Point Numbers in Kotlin
 * ============================================================
 *
 * Kotlin's range operators (.., until, downTo) work with
 * integral types, but they do not directly support stepping
 * through Double or Float values.
 *
 * Best Practice:
 * Use an integer range and transform each element into the
 * desired floating-point value, or generate a Sequence when
 * working with large or potentially infinite ranges.
 */

fun main() {

    // --------------------------------------------------------
    // Example 1: Generate values from 0.0 to 1.0
    // --------------------------------------------------------
    //
    // Create an integer range from 0 to 10 and convert each
    // value to a Double by dividing by 10.0.
    //
    // Result:
    // 0.0, 0.1, 0.2, ..., 1.0
    //
    val range1: List<Double> =
        (0..10).map { it / 10.0 }

    println("Range 1:")
    println(range1.joinToString())


    // --------------------------------------------------------
    // Example 2: Generate a custom floating-point range
    // --------------------------------------------------------
    //
    // Goal:
    // Generate values from 1.5 to 3.0 using a step of 0.25.
    //
    val start = 1.5
    val end = 3.0
    val step = 0.25

    // Calculate the number of steps required.
    val numberOfSteps =
        ((end - start) / step).toInt()

    val range2: List<Double> =
        (0..numberOfSteps)
            .map { index -> start + index * step }

    println("\nRange 2:")
    println(range2.joinToString())


    // --------------------------------------------------------
    // Example 3: Lazy generation using Sequence
    // --------------------------------------------------------
    //
    // Sequences generate values on demand and are ideal for
    // very large or potentially infinite data streams.
    //
    val sequenceRange: Sequence<Double> =
        generateSequence(0.0) { previousValue ->
            previousValue + 0.5
        }

    println("\nSequence Range:")
    println(sequenceRange.take(10).joinToString())
}


/*
run:

Range 1:
0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0

Range 2:
1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0

Sequence Range:
0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5

*/

 



answered Oct 7 by avibootz
0 votes
/*
 * Title: Reusable Floating-Point Range Generator
 */
fun doubleRange(
    start: Double,
    endInclusive: Double,
    step: Double
): List<Double> {

    require(step > 0) {
        "Step must be greater than zero."
    }

    val numberOfSteps =
        ((endInclusive - start) / step).toInt()

    return (0..numberOfSteps)
        .map { start + it * step }
}

fun main() {
    val values = doubleRange(
        start = 1.0,
        endInclusive = 2.0,
        step = 0.2
    )

    println(values.joinToString())
}


/*
run:

1.0, 1.2, 1.4, 1.6, 1.8, 2.0

*/

 



answered Oct 7 by avibootz
...