The problem: So, obviously 3.0 can be considered a float. But for some reason Kotlin sees it as a double. This makes no sense to me. Could somebody tell me why Kotlin sees the number 3.0 as a double, rather than a float? Thanks!
fun main(){
val num = 3.0
random(num)
}
fun random(num: Float){
print(num)
}
Note This is the online IDE that I was using. https://developer.android.com/training/kotlinplayground
CodePudding user response:
Because you did not type annotate it.
Double
is a double-precision floating point number. (64bit)Float
is a single-precision floating point number. (32bit)
The default is the first because you're likely to find less unexpected bugs due to insufficient precision.
You may specify 3.0F
, 3.0f
to make that a Float
or val number: Float = 3.0
should also hint the compiler that you want to deal with a 32bit floating-point number