I'm new to Swift and I was trying to make something which coverts any base 10 number to a different base. I started by making a function which finds the the greatest exponent value for a base and a number. For example, if the number was 26 and the base was 5, the greatest exponent would be 2. I created the program but it always gives me an error. I feel like it might have something to do with the Double(num/exponentedBase), but I'm not sure. Finally, is there a better way to do this. Please help.

- 1,670
- 5
- 20
- 39
1 Answers
The xor operator in the first line in combination with the division in the second line of your functions is causing the exception.
The xor operator returns a new number whose bits are set to 1 where the input bits are different and are set to 0 where the input bits are the same (see https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html, chapter Bitwise XOR operator).
Therefor your variable "exponentedbase" can be 0 and it is possible that you are trying to divide through 0, which causes the exception.
When you print the values of your greatestCommonExponent function with num 12 and base 2, you get the following result:
first call:
num: 12
base: 2
exponent: 1
exponentedbase: 3
second call:
num: 12
base: 2
exponent: 2
exponentedbase: 0
You should add a guard statement to make your code save. (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID120, chapter "early exit")
Edit: The ^ operator in Swift is the XOR function. The statement 2^2 will compare the numbers bitwise.
10 XOR 10 = 00
For further reference follow https://en.wikipedia.org/wiki/Exclusive_or or https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html
If you want to have a pow function, you should do something like this: How to get the Power of some Integer in Swift language?
This should work for you:
func greatestCommonExponent(num: Int, base: Int, exponent: Int = 1) -> Int {
let exponentedbase = base^^exponent
let value = Double(num/exponentedbase)
if value > 1 {
return greatestCommonExponent(num: num, base: base, exponent: exponent+1)
}
if value == 1 {
return exponent
} else {
return exponent-1
}
}
precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence }
infix operator ^^ : PowerPrecedence
func ^^ (radix: Int, power: Int) -> Int {
return Int(pow(Double(radix), Double(power)))
}
greatestCommonExponent(num: 12, base: 2)
The result of 12 base 2 is 3
-
I'm still confused. In the second call, how is the exponent base 0? The exponent base should be 2^2 which is equal to 4. – Nikhil Sridhar Sep 29 '16 at 04:55
-
^ is the _xor_ operator, not for the exponent. If you want to have a pow function, perhaps this can help: http://stackoverflow.com/questions/24196689/how-to-get-the-power-of-some-integer-in-swift-language?answertab=votes#tab-top – mbachm Sep 29 '16 at 05:08
-
I changed the operator, but it still only returns zero. I printed out all the values to check, and they were all correct. It seems to be something with the if statements. – Nikhil Sridhar Sep 29 '16 at 05:12
-
Thank you so very much. That really helped. I found that there were two problems. The first was the power function, which I fixed. The second was that I didn't return the greatestCommonExponent() when x > 1. That's why my function still returned 0. Again, thank you so very much. – Nikhil Sridhar Sep 29 '16 at 05:47
-
You are welcome. It would be nice of you if you could accept my answer. – mbachm Sep 29 '16 at 06:14