Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ranges in Kotlin using data type Double

Tags:

    fun calcInterest(amount: Double, interest: Double): Double {
    return(amount *(interest/100.0))
}

fun main(args: Array<String>) {

    for (i in 1.0..2.0 step .5) {
        println("&10,000 at 5% interest is = ${calcInterest(10000.0,i)}")
    }

}

I get the error saying the For-loop range must have an 'Iterator()'Method. It underlines my doubles in the section (i in 1.0..2.0)

How can I use doubles in a range?? A website on Ranges Reloaded (https://blog.jetbrains.com/kotlin/2013/02/ranges-reloaded/ ) shows that using datatype Double is fine. I don't know what's wrong with mine. I need to use doubles for the fact that my interest rates are using decimals. Completely new to programming so hopefully someone can explain simply. Thanks!

Edit: added step .5

like image 608
Nate Watts Avatar asked Jun 01 '17 19:06

Nate Watts


People also ask

How do you set range on Kotlin?

To create a range for your class, call the rangeTo() function on the range start value and provide the end value as an argument.

How do you use the Range function in Kotlin?

Kotlin range is defined as an interval from start value to the end value. Range expressions are created with operator (. .) which is complemented by in and !in. The value which is equal or greater than start value and smaller or equal to end value comes inside the definedrange.

How do you compare two double values in Kotlin?

If we instead start with 2 same floating-point values without arithmetic, or if the arithmetic done is the same, equality comparison using == works.


2 Answers

As of Kotlin 1.1, a ClosedRange<Double> "cannot be used for iteration" (rangeTo() - Utility functions - Ranges - Kotlin Programming Language).

You can, however, define your own step extension function for this. e.g.:

infix fun ClosedRange<Double>.step(step: Double): Iterable<Double> {
    require(start.isFinite())
    require(endInclusive.isFinite())
    require(step > 0.0) { "Step must be positive, was: $step." }
    val sequence = generateSequence(start) { previous ->
        if (previous == Double.POSITIVE_INFINITY) return@generateSequence null
        val next = previous + step
        if (next > endInclusive) null else next
    }
    return sequence.asIterable()
}

Although you can do this if you are working with money you shouldn't really be using Double (or Float). See Java Practices -> Representing money.

like image 147
mfulton26 Avatar answered Oct 12 '22 08:10

mfulton26


According to the documentation for ranges:

Floating point numbers (Double, Float) do not define their rangeTo operator, and the one provided by the standard library for generic Comparable types is used instead:

public operator fun <T: Comparable<T>> T.rangeTo(that: T): ClosedRange<T>

The range returned by this function cannot be used for iteration.

You will have to use some other kind of loop since you can't use ranges.

like image 31
Todd Avatar answered Oct 12 '22 08:10

Todd