What is the output of the following code?
var weight = 154 var height = 66.9 var BMI = (weight / pow(height,2)) * 703.06957964 print(BMI)
The compiler generates an error. weight is inferred to be of type Int. It will cause the error when using it to multiply other Double values.
The first way to fix this is to ensure that you assign a floating-point value to weight so that the compiler can infer it to be of type Double :
var weight = 154.0 var height = 66.9 var BMI = (weight / pow(height,2)) * 703.06957964 print(BMI)// ww w.ja v a2s. co m
The second approach is to explicitly declare weight as a Double:
var weight:Double = 154 var height = 66.9 var BMI = (weight / pow(height,2)) * 703.06957964 print(BMI)//from ww w .ja v a2 s . co m
The third approach is to explicitly perform a cast on weight and height when performing the calculations:
var weight = 154 var height = 66.9 var BMI = (Double (weight) / pow(Double (height),2)) * 703.06957964 print(BMI)/*from w ww . j a v a2 s . com*/