Is the Swift Divide "/" Operator Not Working or Have I Missed Something

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

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)
}

Strange Swift numbers type casting

Yes, I also found this quite surprising. Double conforms to both FloatLiteralConvertible and IntegerLiteralConvertible (ExpressibleByFloatLiteral and ExpressibleByIntegerLiteral in Swift 3). Therefore a
Double can be initialized with floating point literal

let a = 3.0

or with an integer literal:

let b : Double = 10

(The same is true for other floating point types like Float and
CGFloat.)

Now it might be unexpected for all of us with an (Objective-)C background
that both statements

let x : Double = 10/4     // x = 2.5 .  Really? Yes!
let y = 10/4 as Double // Same here ...

assign the value 0.25 to the variable. From the context, the result of the
division must be a Double and Swift does not implicitly convert types.
Therefore / must be the floating point division operator

func /(lhs: Double, rhs: Double) -> Double

so the compiler creates both arguments as Doubles from the literals
"10" and "4". (If 10/4 were treated as the division of two integers
then the result would also be an integer, and that cannot be assigned
to a Double.)

Note that this is different from

let z = Double(10/4)   // z = 2.0 . (I just thought that I understood it &%$!?)

which does an integer division and converts the result to Double.
Double has an init(_ v: Int) constructor, and therefore 10/4
can be treated as the division of two integers here.

It really looks a bit strange if we summarize these results:

let x : Double = 10/4     // x = 2.5 
let y = 10/4 as Double // y = 2.5
let z = Double(10/4) // z = 2.0

Now we can apply these results to your expression

(10 / 3.0) - (10 / 3)

The first part (10 / 3.0) can only be a Double, therefore -
must be the floating point subtraction operator

func -(lhs: Double, rhs: Double) -> Double

and thus (10 / 3) must also be a Double. Again, / must be the floating point division operator, so 10 and 3 are treated as Double constants.

Therefore the expression is equivalent to

(Double(10) / 3.0) - (Double(10) / Double(3))

and evaluates to 0.0. If you change the expression to

(10 / 3.0) - Double(10 / 3)

then the result is 0.333... because in this context, 10 / 3
is the division of two integer constants, as explained above.

ios how to check if division remainder is integer

You should use the modulo operator like this

// a,b are ints
if ( a % b == 0) {
// remainder 0
} else
{
// b does not divide a evenly
}

Split string in a foreach-object loop

The error means that the ManagedBy is null when you try to split on it.

Just add if else for ManagedBy in this code block $Output = $DISK | ForEach-Object{},

like below:

$Output = $DISK | ForEach-Object {
[PSCustomObject]@{
"Name" = $_.Name
"Resource Group Name" =$_.ResourceGroupName
"Disk Tier" = $_.Sku.Tier
"Disk Type" = $_.Sku.Name
"Managed By" = if($_.ManagedBy){$_.ManagedBy.Remove(0,$_.ManagedBy.LastIndexOf('/')+1)}else{"none"}
"Time Created" = $_.TimeCreated
"Disk Size (in GB)" = $_.DiskSizeGB
"I/O per second" = $_.DiskIOPSReadWrite
"MBps per second" = $_.DiskMBpsReadWrite
"Location" = $_.Location
}
}

I write a sample code and test result as blow, you can just make some changes to meet your need:

Sample Image

Determine whether number is a multiple of 5

Swift 5 UPDATE

According to newly released language version you can determine this using isMultiple(of:) method

let num = 75
if num.isMultiple(of: 5) {
// multiple of 5
} else {
// not a multiple of 5
}

Use the modulus operator to check the remainder of integer division.

if (num % 5 == 0) {
// multiple of 5.
}
else {
// not a multiple of 5.
}


Related Topics



Leave a reply



Submit