Swift Tutorial Swift References

Swift - remquo() Function



The Swift remquo() function returns the floating-point remainder of x/y (rounded to nearest integer). The function additionally also returns the quotient of the division operation. This can be mathematically expressed as below:

remainder = x - quotient * y

Where quotient is the result of x/y rounded towards nearest integer (with halfway cases rounded toward the even number). The returned value by this function is (remainder, quotient).

Note: The remainder function is similar to the remquo function except it only returns remainder of the division operation.

Syntax

In Foundation framework, it is defined as follows:

public func remquo(_ x: CGFloat, _ y: CGFloat) -> (CGFloat, Int)
public func remquo(_ x: Float, _ y: Float) -> (Float, Int)
public func remquo(_ x: Double, _ y: Double) -> (Double, Int)
public func remquo(_ x: Float80, _ y: Float80) -> (Float80, Int)

Parameters

x Specify the value of numerator.
y Specify the value of denominator.

Return Value

Returns (remainder, quotient) of x/y division operation. If the remainder is zero, its sign will be same as that of x. If y is zero, the function returns (-nan, 0).

Example:

In the example below, remquo() function is used to find out the remainder and quotient of a given division.

import Foundation

var x:Double = 25
var y:Double = 4

var rem:Double
var quo:Int

print("remquo(25.0, 4.0) = \(remquo(25.0, 4.0))")

(rem, quo) = remquo(x, y)
print("Remainder = \(rem)")
print("Quotient = \(quo)")

The output of the above code will be:

remquo(25.0, 4.0) = (1.0, 6)
Remainder = 1.0
Quotient = 6

❮ Swift Math Functions

5