+ 4
Exponentiation in Swift
I need to perform exponentiation in Swift (done with the ** in python and javascript) but there is no such operator. Google said I need to use âpow(a, b)â instead of âa ** bâ, but âerror: use of unresolved identifier powâ. More google searching said I need to do âimport Darwinâ to get access to pow and other math functions. Guess what: âerror: no module Darwinâ. Help!
7 Respostas
+ 4
Maybe you can define your own power function, like this:
func pow (base:Int, power:UInt) -> Int {
var answer : Int = 1
for _ in 0..power { answer *= base }
return answer
}
Copied from https://stackoverflow.com/questions/24196689/how-to-get-the-power-of-some-integer-in-swift-language
I donât know any Swift myself đđ
+ 5
import Foundation, then you can use pow(a,b).
+ 4
Sorry, I do not know. I have not tried Swift on Sololearn yet.
I tried Swift on my Mac some time ago.
+ 2
so SL has foundation but not darwin? thatâs confusing
+ 2
code: âimport Foundationâ
result:
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "CoreFoundation.h"
^
..\Playground\/CoreFoundation.h:25:10: error: 'assert.h' file not found
#include <assert.h>
^
<unknown>:0: error: could not build C module 'CoreFoundation'
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "../../..../Playground/-w64-mingw32/include/assert.h"
^
+ 2
yeah, I was thinking I might have to write it myself. All I really need is powers of two, so itâll be simple.
+ 1
Hello! To use the âpow(_:,_:)â Function, you need to import the Foundation module. Also, Swift enables you to define this functionality yourself; like in the following code:
https://code.sololearn.com/c4iMv66GY8Tp/?ref=app