I'm going through the Kotlin Apprentice book and the exercise is: Print 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0.
Below is my original code and the output. I tried using Float and got a similar problem. What am I doing wrong?
var count = 0.0
print(count)
while (count < 1) {
count += 0.1
print(", $count")
}
prints out: 0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, 0.8999999999999999, 0.9999999999999999, 1.0999999999999999
var count: Float = 0.0F
print(count)
while (count < 1) {
count += 0.1F
print(", $count")
}
prints out: 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.70000005, 0.8000001, 0.9000001, 1.0000001
Solution after reviewing reference in comments of answer:
var count = 0.0
print(count)
while (count < 10) {
count += 1
print(", ${count/10}")
}
prints out: 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
Thank you for the reference, very interesting.