## 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

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

Your problem is here: `1/36`

. Both **1** and **36** are `Int`

s. Try this:

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

## 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))`

.

## Divide two numbers and return fraction in swift

To get the quotient and remainder of a division, you can use the `quotientAndRemainder(dividingBy:)`

function.

`3.quotientAndRemainder(dividingBy: 6) // (quotient 0, remainder 3)`

If you want to get the floating point result of a division, use the `/`

operator on two floating point numbers.

Either do

`let result = 3.0 / 6.0 // 0.5`

or if your integers are coming from variables, do

`let result = Double(3.0) / Double(6.0) // 0.5`

## 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.)

## Dividing an integer variable is not working

Dividing `Int`

types always returns `Int`

types. Try:

`round(variable * 100) / 100.0`

It's not clear what `variable`

is, if it's not a double, do this:

`round(Double(variable) * 100) / 100.0`

## 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 `Double`

s:

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

## How to divide two numbers in swift using two variables?

Division operator is

`/`

, not`\`

.`sum\numbers`

- um, what? Did you mean`sum / numbers.count`

?

## Integer Division ('Int' is not convertible to 'Double')

In Swift you cannot do math with different types (in this case `Int`

and `Double`

). Even `Int`

and `UInt`

are not interchangeable.

You have to create new instances of the matching type

`var myNumber : Int = 2340`

var doubleDiv : Double = Double(myNumber) / 100.0

the decimal places in literals are not necessary in most cases, but it is clearer to use the same signatures.

### Related Topics

Can Swift Playgrounds See Other Source Files in the Same Project

Autofocus Textfield Programmatically in Swiftui

How to Hide the Navigationbar When Embedding Swiftui in Uikit

Check for Nil with Guard Instead of If

How to Edit the Uiblureffect Intensity

Define MACos Window Size Using Swiftui

How to Change "Return" Key to "Done" on Keyboard with Swiftui

Class-Only Generic Constraints in Swift

Correct Handling/Cleanup/Etc of Cadisplaylink in Swift Custom Animation

In Swift, Does Resetting the Property Inside Didset Trigger Another Didset

Clearing Uiwebview's Cache in Swift

What Is the Property Block Declaration Equivalent in Swift of the Following Block Property

Unsafemutablepointer in Swift as Replacement for Properly Sized C Array in Obj-C

Understanding the Userdefaults Register Method

App Crashes When Playing Audio on iOS13.1

Fbsdkapplicationdelegate Application Openurl:Sourceapplication:Annotation Deprecated