Home > Enterprise >  Why does this convert Int to Double in Swift playground not work?
Why does this convert Int to Double in Swift playground not work?

Time:03-02

f = 802
var df = Double(f / 4)
print(df)

result is 200.0

I expected 200.5

CodePudding user response:

Your expression creates a Double from the division of the integers 820 by 4 which is 200.

If you want a floating point division you have to do the conversion before the division. Or simpler without a conversion declare f as Double

let f = 802.0
let df = f / 4 // 200.5

It's a good practice anyway to declare numeric literals as the actual type. I would even write

let df = f / 4.0

The benefit is that the compiler complains if the types don't match

CodePudding user response:

This is a common bug and easy mistake to make. When you want a floating point result, you need to ensure that the operands of your arithmetic expressions are floating point numbers instead of integers. This will produce the expected result of 200.5:

var df = Double(f) / 4.0

(Edit: if your variable f really is going to be a hard-coded constant 802, I actually recommend vadian's solution of declaring f itself as Double rather than Int.)

A more detailed explanation:

Looking at the order of operations of var df = Double(f / 4):

The innermost expression is f / 4. This is evaluated first. f and 4 are both integers, so this is calculated using integer division which rounds down, so 802/4 => 200.

Then the result 200 is used in the Double() conversion, thus the result of 200.0. Finally, the result is assigned to the newly-declared variable df, which Swift infers to have the type Double based on the expression to the right of the equals sign.

Compare this to var df = Double(f) / 4.0: the Double(f) is evaluated first, converting the integer 802 to a double value 802.0. Now the division is performed, and since both operands of the division sign are floating point, floating-point division is performed and you get the result 802.0 / 4.0 => 200.5. This result is a Double value, so the variable df is declared to be a Double and assigned the value 200.5.

Some other approaches that don't work:

  • var df = f / 4: f and 4 are both integers, integer division is performed automatically, and df is now a variable of type Int with value 200
  • var df: Double = f / 4: trying to explicitly declare df as Double will produce a compiler error. The right side of the equals sign is still an integer division operation, and Swift won't automatically cast from Integer to Double, it wants you to explicitly decide how to cast
  • var df = f / 4.0: in some languages, this type of expression would automatically convert f to a Double and thus perform floating-point division like you want. But again Swift will not automatically convert and wants you to be explicit…this leads to my recommended solution of Double(f)/4.0

CodePudding user response:

In your example you are dividing Integers and then casts to double. Fix:

f = 802
var df = Double(f) / 4
print(df)
  • Related