Using `**` as exponentiation operator in swift working incorrectly with the `-` operator - swift

How can I make the exponentiation operator ** in Swift behave the same as in other programming languages.
The question Exponentiation operator in Swift has the following answer which has the highest number of up votes,
infix operator ** { associativity left precedence 170 }
func ** (num: Double, power: Double) -> Double{
return pow(num, power)
}
However, y = -x**2,
is interpreted as (-x)**2 = 4.0 (Swift)
usually it should be interpreted as -(x**2) = -4.0 (Expected!)

I believe your problem is:
Unary operators in Swift always take precedence over binary operators.
Here's the source: https://medium.com/swift-programming/facets-of-swift-part-5-custom-operators-1080bc78ccc
Therefore, the expression is always evaluated as (-x)**2 instead of -(x**2), since - is a unary operator and ** is a binary operator.

Related

precedence operator in java 8 - postfix operator first

Following the precedence operator in java 8 it is clear that the postfix operator (expr++ expr--) has a higher precedence that the unary operator, pre-unary operator (++expr --expr).
However when executing this code:
x = 3; y = ++x - x++;
The value of y is 0
But to me, following the above table, the result should be
y = (5 - 3) as x++ should be evaluated first.
Can anyone explain why this is y = 0 and not y = 2?
When do I use the Operator precedence on the same line in an expression? or why there is an operator precedence order and when is used?
Operator precedence decides which one of several operators is associated with an operand. In the expression ++x - x++ there are two places where precedence comes into play:
++x - … - The two operators ++ and (binary) - could be used on x; ++ has precedence, so this is equivalent to (++x) - …, not to ++(x - …).
… - x++ - The two operators (binary) - and ++ could be used on x; ++ has precedence, so this is equivalent to … - (x++), not to (… - x)++.

Is it possible to divide two numerics in swift?

func divtwoval<T: Numeric>(_a: T,_b: T){
let c = _a / _b
print(c)
}
I tried to divide two numeric generics but it doesent work.
I get this error message: error: binary operator '/' cannot be applied to two 'T' operands.
How do I divide this generics?
If you check the documentation of Numeric, it clearly shows its values only need to support multiplication.
The division operator (/) is defined on the BinaryInteger and FloatingPoint protocols separately, since they have different semantics, so you cannot divide any numeric types by each other.

Swift type inference and basic addition

Pretty new to Swift and learning about data types.
let partNumber = 3.2
let wholeNumber = 2
partNumber + wholeNumber //Binary operator '+' cannot be applied to operands of type 'Double' and 'Int'
3.2 + 2 // outputs 5.2
I understand that partNumber is a Double type and wholeNumber is an Int. What I don't understand is why playground errors out when I attempt to add both constants together. To add confusion the addition works when not assigned as a constant.
The + operator does not support adding a Double and and Integer together in this way
If you change up your code to make sure wholeNumber is a Double type, then it'll work
let partNumber = 3.2
let wholeNumber: Double = 2
let result = partNumber + wholeNumber
This is all covered in the Swift book under Numeric Type Conversion.
Some relevant quotes from the subsection titled "Integer and Floating-Point Conversion":
Conversions between integer and floating-point numeric types must be made explicit
This is followed by an example similar to your code. Your code needs a cast:
let partNumber = 3.2
let wholeNumber = 2
partNumber + Double(wholeNumber)
and:
The rules for combining numeric constants and variables are different from the rules for numeric literals. The literal value 3 can be added directly to the literal value 0.14159, because number literals don’t have an explicit type in and of themselves. Their type is inferred only at the point that they’re evaluated by the compiler.
Which covers the second part of your question.
To add confusion the addition works when not assigned as a constant.
That doesn't "add to the confusion" at all. It's the answer. There is implicit coercion between numeric types for literals (what you call a "constant") but not for variables. It's as simple as that.

Is there a reason why the shift operators (>> and <<) don't work on BitwiseOperationsType?

I was thinking of making an integer power function in Swift based on this StackOverflow answer:
func **<T : IntegerType>(var base: T, var exponent: T) -> T {
var result: T = 1
assert(exponent >= 0, "Exponent cannot be negative")
while exponent > 0 {
if exponent & 1 != 0 {
result *= base
}
exponent = exponent >> 1
base *= base
}
return result
}
I figured I could use generics to implement the function so that it would work with any integer type.
Unfortunately, I get an error when I attempt to use exponent >> 1:
Binary operator '>>' cannot be applied to two 'T' operands
Checking the function definitions for >>, I see that there is one for each of the ten integer types, but no other ones are defined. I was surprised therefore that all the other operators were working, such as &, but I noticed that & was actually defined to work on all types which conform to BitwiseOperationsType, which IntegerType appears to conform to.
Is there a reason why the >> and << operators are not implemented for BitwiseOperationsType?

Swift syntax issue: var a:Int64 = -7

I'm playing with Apple's new Swift language. Below snippet can successfully produce a result:
var a:Int64 = -7
println(a)
However, if I change the code to this:
var a:Int64 =-7
println(a)
I'll get the error:
Error:(12, 12) consecutive statements on a line must be separated by ';'
It seems that Swift thinks =- is an operator, which does not exist in Swift. If so, why generate that error?
Swift is very strict to avoid ambiguity in operators =- in var a:Int64 =-7 basically means unary prefix operator, that is undefined in this case. The =- cannot be split in two as there is no separator and =- operator can be defined any time. To avoid any ambiguity use spaces. var a:Int64 = -7 has clear separation between assignment and unary prefix operator.