+ 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 ответов
+ 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