I am doing some calculation using Swift. I understand that in Swift, 0/0 gives NAN (not a number) instead of 0. Is there anyway for it to return 0 instead?
for x in 0..<n {
for y in 0..<n {
if(B[0,y,x]==NAN) {B[0,y,x]=0 } //use of undeclared identifier 'NAN'
println("\((Float)B[0,y,x])")
}
}
NaN is defined in FloatingPointType protocol.
Which is the Swift equivalent of isnan()?
Then, if you want zero, how about using Overflow Operators?
let x = 1
let y = x &/ 0
// y is equal to 0
[UPDATED]
You can define custom overflow operator like this.
func &/(lhs: Float, rhs: Float) -> Float {
if rhs == 0 {
return 0
}
return lhs/rhs
}
var a: Float = 1.0
var b: Float = 0
// this is 0
a &/ b
good answer from #bluedome, in case you want this as an extension and\or have any errors there is an answer
infix operator &/ {}
extension CGFloat {
public static func &/(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
if rhs == 0 {
return 0
}
return lhs/rhs
}
}
then you can divide by zero
let a = 5
let b = 0
print(a &/ b) // outputs 0
Related
Can someone explain me how to write this function, which should return the factorial of x.
The way I tried to do it gives me an error.
This line is inside a dictionary and refers to Operation.UnaryOperation that is (Double) -> Double
I tried to write the function I need extensively, It should look like this:
private func factorial(n: Double) -> Double {
if (n<=1) {
return 1
}
return n * factorial(n-1)
}
Now I need to convert it to a single-line function, how do I do it? would it look something like this?, why am I getting an error?
"x!" : Operation.UnaryOperation({if ($0<=1) {return 1} else {return $0 * factorial($0-1)}}),
Just embed the named function in the closure - like this
enum Operation {
case UnaryOperation( (Double) -> Double)
case BinaryOperation( (Double, Double) -> Double)
}
let dictionary: [String: Operation] = [
"+" : Operation.BinaryOperation({ return $0 + $1 }),
"!" : Operation.UnaryOperation({
arg: Double in
func factorial(x: Double) -> Double {
if x <= 1 {
return 1
} else {
return x * factorial(x - 1)
}
}
return factorial(arg)
})
]
let x = Operation.UnaryOperation(factorial{if ($0<=1) {return 1} else {return $0 * factorial($0-1)}})
func factorial(_ x: UInt) -> UInt {
return x == 0 ? 1 : x * factorial(x - 1)
}
// Example:
print(factorial(6))
// 720
I want to make the computer generate predefined set of numbers one-by-one. How is it possible in Swift? Is it possible using arrays?
For example: I want to generate [1,2,3,4] in that same order one-by-one. By generate I mean to show up like how random numbers show up when you type in arcrandom_uniform().
Example:
var randomNumber = [1,2,3,4,5]
var guessInt = guess.text.toInt()
Then when I type ..
if Int(randomNumber) == guessInt {
//conditions
}
The error shows as : Cannot assgn to the result of ths expresson
Sometimes I get the error as Int' is not convertible to '[Int]' swift
The code won't work! Help ?
Here's the code for #David Skrundz :
` #IBAction func guessButton(sender: AnyObject) {
var randomNumber = [1,2,3,4,7,8,9,5]
var guessInt = guess.text.toInt()
func generator() -> () -> Int {
var currentIndex = 0
let array = Array(1...5)
func generate() -> Int {
let value = array[currentIndex++]
if currentIndex >= array.count {
currentIndex = 0
}
return value
}
return generate
}
let gen = generator()
gen() // Returns 1
gen() // Returns 2
gen() // Returns 3
gen() // Returns 4
gen() // Returns 1
let answer = gen()
if answer == guessInt {
resultLabel.text = "Bingoo..! "}
else {
resultLabel.text = "Oh such a bad guesser! It was a \(answer)"
}
println(answer)
}
`
Any idea how to fix this code the problem mentioned below ! ? Please !
You may want the range operator:
for index in 1...5 {
println("\(index)")
}
If you want to generate the next element in a repeating sequence, you could use
func generator() -> () -> Int {
var currentIndex = 0
let array = Array(1...4)
func generate() -> Int {
let value = array[currentIndex++]
if currentIndex >= array.count {
currentIndex = 0
}
return value
}
return generate
}
And to use it:
let gen = generator()
gen() // Returns 1
gen() // Returns 2
gen() // Returns 3
gen() // Returns 4
gen() // Returns 1
I am using swift and want to have a number of duplicatable patterns throughout my game.
Ideally I would have some sort of shared class that worked sort of like this (this is sort of pseudo-Swift code):
class RandomNumberUtility {
static var sharedInstance = RandomNumberUtility()
var random1 : Random()
var random2 : Random()
func seedRandom1(seed : Int){
random1 = Random(seed)
}
func seedRandom2(seed : Int){
random2 = Random(seed)
}
func getRandom1() -> Int {
return random1.next(1,10)
}
func getRandom2() -> Int {
return random2.next(1,100)
}
}
Then, to begin the series, anywhere in my program I could go like this:
RandomNumberUtility.sharedInstance.seedNumber1(7)
RandomNumberUtility.sharedInstance.seedNumber2(12)
And then I would know that (for example) the first 4 times I called
RandomNumberUtility.sharedInstance.getRandom1()
I would always get the same values (for example: 6, 1, 2, 6)
This would continue until at some point I seeded the number again, and then I would either get the exact same series back (if I used the same seed), or a different series (if I used a different seed).
And I want to have multiple series of numbers (random1 & random2) at the same time.
I am not sure how to begin to turn this into an actual Swift class.
Here is a possible implementation. It uses the jrand48 pseudo random number generator,
which produces 32-bit numbers.
This PRNG is not as good as arc4random(), but has the advantage
that all its state is stored in a user-supplied array, so that multiple
instances can run independently.
struct RandomNumberGenerator {
// 48 bit internal state for jrand48()
private var state : [UInt16] = [0, 0, 0]
// Return pseudo-random number in the range 0 ... upper_bound-1:
mutating func next(upper_bound: UInt32) -> UInt32 {
// Implementation avoiding the "module bias" problem,
// taken from: http://stackoverflow.com/a/10989061/1187415,
// Swift translation here: http://stackoverflow.com/a/26550169/1187415
let range = UInt32.max - UInt32.max % upper_bound
var rnd : UInt32
do {
rnd = UInt32(truncatingBitPattern: jrand48(&state))
} while rnd >= range
return rnd % upper_bound
}
mutating func seed(newSeed : Int) {
state[0] = UInt16(truncatingBitPattern: newSeed)
state[1] = UInt16(truncatingBitPattern: (newSeed >> 16))
state[2] = UInt16(truncatingBitPattern: (newSeed >> 32))
}
}
Example:
var rnd1 = RandomNumberGenerator()
rnd1.seed(7)
var rnd2 = RandomNumberGenerator()
rnd2.seed(12)
println(rnd1.next(10)) // 2
println(rnd1.next(10)) // 8
println(rnd1.next(10)) // 1
println(rnd2.next(10)) // 6
println(rnd2.next(10)) // 0
println(rnd2.next(10)) // 5
If rnd1 is seeded with the same value as above then it
produces the same numbers again:
rnd1.seed(7)
println(rnd1.next(10)) // 2
println(rnd1.next(10)) // 8
println(rnd1.next(10)) // 1
What you need is a singleton that generates pseudo-random numbers and make sure all your code that need a random number call via this class. The trick is to reset the seed for each run of your code. Here is a simple RandomGenerator class that will do the trick for you (it's optimized for speed which is a good thing when writing games):
import Foundation
// This random number generator comes from: Klimov, A. and Shamir, A.,
// "A New Class of Invertible Mappings", Cryptographic Hardware and Embedded
// Systems 2002, http://dl.acm.org/citation.cfm?id=752741
//
// Very fast, very simple, and passes Diehard and other good statistical
// tests as strongly as cryptographically-secure random number generators (but
// is not itself cryptographically-secure).
class RandomNumberGenerator {
static let sharedInstance = RandomNumberGenerator()
private init(seed: UInt64 = 12347) {
self.seed = seed
}
func nextInt() -> Int {
return next(32)
}
private func isPowerOfTwo(x: Int) -> Bool { return x != 0 && ((x & (x - 1)) == 0) }
func nextInt(max: Int) -> Int {
assert(!(max < 0))
// Fast path if max is a power of 2.
if isPowerOfTwo(max) {
return Int((Int64(max) * Int64(next(31))) >> 31)
}
while (true) {
var rnd = next(31)
var val = rnd % max
if rnd - val + (max - 1) >= 0 {
return val
}
}
}
func nextBool() -> Bool {
return next(1) != 0
}
func nextDouble() -> Double {
return Double((Int64(next(26)) << 27) + Int64(next(27))) /
Double(Int64(1) << 53)
}
func nextInt64() -> Int64 {
let lo = UInt(next(32))
let hi = UInt(next(32))
return Int64(UInt64(lo) | UInt64(hi << 32))
}
func nextBytes(inout buffer: [UInt8]) {
for n in 0..<buffer.count {
buffer[n] = UInt8(next(8))
}
}
var seed: UInt64 {
get {
return _seed
}
set(seed) {
_initialSeed = seed
_seed = seed
}
}
var initialSeed: UInt64 {
return _initialSeed!
}
private func randomNumber() -> UInt32 {
_seed = _seed &+ ((_seed &* _seed) | 5)
return UInt32(_seed >> 32)
}
private func next(bits: Int) -> Int {
assert(bits > 0)
assert(!(bits > 32))
return Int(randomNumber() >> UInt32(32 - bits))
}
private var _initialSeed: UInt64?
private var _seed: UInt64 = 0
}
I have been working on a function for reducing fractions in Swift, and came across the Euclidean algorithm for finding the greatest common factor (http://en.wikipedia.org/wiki/Euclidean_algorithm)
I converted the pseudo code into swift, but yet I am confused how this is going to give me the greatest common factor if it is returning a which I thought was supposed to be the numerator of the fraction. Any help on this would be greatly appreciated. Thanks!
Pseudocode:
function gcd(a, b)
while b ≠ 0
t := b
b := a mod b
a := t
return a
Swift:
var a = 2
var b = 4
func gcd(a: Int, b: Int) -> Int {
var t = 0
while b != 0 {
t = b
let b = a % b
let a = t
}
return a
}
println("\(a)/\(b)")
Console output: 2/4
When you do this
let b = a % b
you are creating another readonly variable b, which has nothing to do with the variable b from the outside scope. You need to remove both lets inside the loop, and make parameters modifiable by declaring them with var, like this:
func gcd(var a: Int, var b: Int) -> Int {
var t = 0
while b != 0 {
t = b
b = a % b
a = t
}
return a
}
You can call your function like this:
let a = 111
let b = 259
println("a=\(a), b=\(b), gcd=\(gcd(a,b))")
This prints a=111, b=259, gcd=37
Taking #dasblinkenlight's answer and getting rid of t by using tuples for parallel assignment yields:
Swift 2.1:
func gcd(var a: Int, var _ b: Int) -> Int {
while b != 0 {
(a, b) = (b, a % b)
}
return a
}
gcd(1001, 39) // 13
var parameters are deprecated in Swift 2.2 and will be removed in Swift 3. So now it becomes necessary to declare a and b as var within the function:
func gcd(a: Int, _ b: Int) -> Int {
var (a, b) = (a, b)
while b != 0 {
(a, b) = (b, a % b)
}
return a
}
Swift 3 version of answer given by Christopher Larsen
func gcd(a: Int, b: Int) -> Int {
if b == 0 { return a }
return gcd(a: b, b: a % b)
}
Can use a recursive method that just keeps calling itself until the GCD is found.
func gcd(a: Int, b: Int) -> Int {
if b == 0 {
return a
}
let remainder: Int = a % b
return gcd(b, b: remainder)
}
and use like so
let gcdOfSomeNums = gcd(28851538, b: 1183019)
//gcdOfSomeNums is 17657
Let say I have something like this:
var x: Int = 6
var y: Float = 11.5
so, the result has to be written like this
var result = Float(x) * y
or
var result = x * Int(y)
This makes sense, right ? However, I think that a little clumsy, so I'm trying to make some custom operators for this:
infix operator *~ { associativity left precedence 150 } //floating
func *~ (lhs: Float, rhs: Int) -> Float {
return lhs * Float(rhs)
}
func *~ (lhs: Int, rhs: Float) -> Float {
return rhs * Float(lhs)
}
infix operator *| { associativity left precedence 150 } //Integer
func *| (lhs: Float, rhs: Int) -> Int {
return Int(lhs) * rhs
}
func *| (lhs: Int, rhs: Float) -> Int {
return Int(rhs) * lhs
}
It works but there are too many of them, so I'm trying to make a generic version for these functions. My attempt so far:
func *~ <T: FloatingPointType, V:IntegerType>(lhs: T, rhs: V) -> T {
return lhs * T(rhs)
// error:
// Could not find an overload for 'init' that accepts the supplied arguments
}
Can somebody help please ? Thank you.
protocol Fraction {
init(_ value: Double)
var asDouble: Double { get }
}
extension Int : Fraction { var asDouble : Double { return Double(self) } }
extension Double : Fraction { var asDouble : Double { return self } }
extension Float : Fraction { var asDouble : Double { return Double(self) } }
extension CGFloat : Fraction { var asDouble : Double { return Double(self) } }
infix operator ** { associativity left precedence 170 }
func **(lhs:Int, rhs:Int) -> Int {
return lhs * rhs
}
func **<T:Fraction, U:Fraction>(lhs:T, rhs:U) -> Double {
return lhs.asDouble * rhs.asDouble
}
2 ** 2 // 4
2 ** 2.2 // 4.4
2.5 ** 2 // 5.0
2.2 ** 2.2 // 4.84
What you are doing is more than counter productive.
What you are calling clumsy is preventing one of the biggest sources of errors. So you want to invest lots of time and work to undo this, and make your source code unreadable in the process.
Anybody ever getting hold of your source code will first spend hours undoing your work and turning everything into normal Swift code.