Division Not Working Properly in Swift

Division operator not working as expected (swift)

You're dividing two integers, so integer division is used. To avoid this, you could explicitly cast the operands to Doubles:

self.pairChance = Double(self.pairCount) / Double(self.rollCount) * 100.0

Is the Swift divide / operator not working or have I missed something?

Your problem is here: 1/36. Both 1 and 36 are Ints. Try this:

var probabilitiesX = Array(repeating: 1.0 / 36.0, count: 36)

Why can't I divide integers in swift?

The OP seems to know how the code has to look like but he is explicitly asking why it is not working the other way.

So, "explicitly" is part of the answer he is looking for: Apple writes inside the "Language Guide" in chapter "The Basics" -> "Integer and Floating-Point Conversion":

Conversions between integer and floating-point numeric types must be
made explicit

Math divison in Swift

Your code is performing integer division, taking the integer result and converting it to a double. Instead, you want to convert these individual integers to doubles and then do the division. So, instead of

let result = (Double(myInt! / lutning) * Double(pi))

You should

let result = Double(myInt!) / Double(lutning) * Double(pi)

Note, Double already has a .pi constant, so you can remove your pi constant, and simplify the above to:

let result = Double(myInt!) / Double(lutning) * .pi

Personally, I’d define myInt and lutning to be Double from the get go (and, while we’re at it, remove all of the forced unwrapping (with the !) of the optionals):

guard
let text = graderna.text,
let text2 = radien.text,
let value = Double(text),
let value2 = Double(text2)
else {
return
}

let lutning: Double = 360
let result = value / lutning * .pi

Or, you can use flatMap to safely unwrap those optional strings:

guard
let value = graderna.text.flatMap({ Double($0) }),
let value2 = radien.text.flatMap({ Double($0) })
else {
return
}

let lutning: Double = 360
let result = value / lutning * .pi

(By the way, if you’re converting between radians and degrees, it should be 2π/360, not π/360.)

Why can't I divide integers correctly within reduce in Swift?

The division does not yield a double; you're doing integer division.

You're not getting ((1 + 2) + 3 etc.) / 5.

In the first case, you're getting (((((0 + (1/5 = 0)) + (2/5 = 0)) + (3/5 = 0)) + (4/5 = 0)) + (5/5 = 1)) = 0 + 0 + 0 + 0 + 0 + 1 = 1.

In the second case, you're getting ((((((0 + 1) + 2) + 3) + 4) + 5) / 5) = 15 / 5 = 3.

In the third case, double precision loss is much smaller than the integer, and you get something like (((((0 + (1/5.0 = 0.2)) + (2/5.0 = 0.4)) + (3/5.0 = 0.6)) + (4/5.0 = 0.8)) + (5/5.0 = 1.0)).

Why is x / 100 == 0 when x != 0 in swift?

Since i and 100 are both integer values, / will do integer division and the result will be truncated to 0.

Even when you do let xValue: Float = Float(i/100), the result of division inside the parentheses is already truncated to 0 before the value can be converted to a Float.

Convert i to a floating-point value before dividing to prevent the result from being truncated.

for i in 1...100{
let xValue = Float(i)/100
print(xValue)
}

can't do division function in swift

There are two issues in the AVG function – btw. function names are supposed to start with a lowercase letter.

A playground displays clear error messages

  1. variable 'avg' used before being initialized

    Solution:

    var avg = 0.0
  2. cannot assign value of type 'Int' to type 'Double'

    Solution:

    avg = Double(sum) / Double(i)

PS: An alternative using the key-value coding operator @avg

func avg(numbers: Int...) -> Double {
return (numbers as NSArray).value(forKeyPath: "@avg.self") as! Double
}

Dividing two doubles gives wrong result in Xcode console

Well this is probably due to this issue: Why not use Double or Float to represent currency?. Were you thinking that Apple implemented floating point wrong? In the Java World, these questions came up quite often, and BigDecimal was the solution, you can read about that.



Related Topics



Leave a reply



Submit