0

I was solving a problem and noticed that conversion of Float value to Int gives wrong result. Below is peice of code which involves the conversion

while(height >= 0.0){
        height = (v * t) - (0.5*g*t*t)
        if(height >= maxHeight) {
            d_time = t
            maxHeight = height
            print(d_time)
        }
        t += 0.1
    }
    let time = d_time * 10.0
    print(d_time, Int(time)) // it prints 2.4, 23
    return time

However if I tried converting literals it works correct. For example

let time = 2.4 * 10.0
print(time) // prints 24 as expected.

I tried to figure out the issue but not able to get it. Please help in understanding this behavior.

Thanks.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382

0 Answers0