Int power function in Swift 3

Moving from Swift 2 to Swift 3, I noticed that now the pow function works with Int : pow(2,3) == 8

In fact not as pow(Int(2),Int(3)) returns an error : cannot convert from Int to Decimal

Why pow does not simply work on Int ? What is the real use of Decimal ?


In fact, I wanted to add an integer power operator as :


infix operator ** { associativity right precedence 160 } /

func **(a: Int,b: Int) -> Int {

return pow(a,b) // Swift 2 : Int(pow(Double(a),Double(b))). Should I keep this in Swift 3 ????

}

print("4*3**2-1 = \(4*3**2-1)")


I get an error :

overloads for 'pow' exist with these result types: Decimal, Float, Double, CGFloat


I wonder why should I use Decimal (and how ?) instead of Int. Keep it simple :-)

Thanks.

This new overload of `pow` introduced in Swift 3:

public func pow(_ x: Decimal, _ y: Int) -> Decimal

is corresponding to the global function for old NSDecimal:

public func NSDecimalPower(_ result: UnsafeMutablePointer<Decimal>, _ number: UnsafePointer<Decimal>, _ power: Int, _ roundingMode: NSDecimalNumber.RoundingMode) -> NSDecimalNumber.CalculationError


With a new policy of importing Foundation into Swift 3, NSDecimal has become Decimal, and some global functions such as:

public func NSDecimalAdd(_ result: UnsafeMutablePointer<Decimal>, _ leftOperand: UnsafePointer<Decimal>, _ rightOperand: UnsafePointer<Decimal>, _ roundingMode: NSDecimalNumber.RoundingMode) -> NSDecimalNumber.CalculationError

has got an overload for operator:

public func +(lhs: Decimal, rhs: Decimal) -> Decimal


So, no functionality about `pow` is added in Swift3. You may need to keep your own `pow(Int, Int)`.

Thanks for your answer. I still have some uncertainty about the rationale of the Decimal class.


a) Apple API Ref says : "Decimal is used to describe a decimal number". Should explain when to use Decimal and when to stick with Int and Double ?

Is it only a solution for getting more precision under intentive floating point computations ?


b) When typing pow(2,3), it seems that 2 and 3 are read as Decimal, right ?


c) Why can't I convert an Int to Decimal to compute pow(a,b) when a and b are Int ?

a) Decimal calculation is needed for systems in which calculation details are specified based on decimal representation of number.


You know, in binary floating point system, 0.1 cannot be represented precisely.

For example, in finance or currency exchange, the result of calucation has a detailed definition including rounding mode -- based on decimal representation.

In such systems, 0.1 × 10 needs to be precisely 1.0, not just displaying 1.0 with actual value 0.9999999999999998 rounded.


Decimal calculation is sort of heavy operation, so, if you do not need such behaviours, you should use Int or Double. (Or sometimes Float.)


b) NO

In Swift 3, writting `pow(2,3)` is read as `pow(2 as Decimal, 3 as Int)`. As `pow` for Decimal has only one overload: `pow(Decimal, Int)`.


c) In Swift, no numeric types are automatically converted to other numeric types. But numeric literals are typeless.


So, integer literals are not always Int. They can be treated as any type which conforms to IntegerLiteralConvertible.

When you write `pow(2, 3.0)`, you are calling `pow(Double, Double)` and the literal `2` is treated as Double.

This is not the same as some other languages which automatically converts `int` to `double`.

So, you cannot call it with:

let iVal: Int = 2
let dVal: Double = 3 //`3` is treated as Double here.
let result = pow(iVal, dVal) //->causes compile time error


Decimal is also IntegerLiteralConvertible, so in `pow(2,3)`, 2 is treated as Decimal, to match one overload for `pow`.

Int power function in Swift 3
 
 
Q