F # 5: Operators

I intended to write 1/2 a decent blog post about F # statements, but then I thought, and honestly, I couldn’t see too many advantages in simply repeating what is already freely available on MSDN, which lists ALL F # statements.



You can find a complete list of all F # operators and characters on this MSDN page.



If you follow this link, the first thing you will understand is that F # has a lot of operators and symbols, too many for me to include in one blog post. With that in mind, I took on the intention of creating half a decent blog post on operators and decided to cut the scope a bit to include only a discussion of the most common operators, so I think you could say that this will be only 1/4 or 1/8 of a decent post, for which I humbly apologize.



Now, before we begin, I just wanted to say that I am going to go through only the most common operators; You will most likely have to study the MSDN link that I included at the beginning of this post when you really start using F #, but now I hope that the ones we dwell on will be enough for us to get started.



Arithmetic Operators



+ Summation

Out of control. A possible overflow condition is when numbers are summed and the sum exceeds the maximum absolute value supported by the type.



- Subtraction

Out of control. Possible underfill condition when unsigned types are subtracted or when floating point values ​​are too small to be represented by the type.



* Multiplication

Out of control. A possible overflow condition if the numbers are multiplied and the product exceeds the maximum absolute value supported by the type.



/ Division

Division by zero raises a DivideByZeroException for integer types. For floating point types, dividing by zero gives you special floating point values ​​+ Infinity or -Infinity. An underfill condition is also possible when the floating point number is too small to be represented by a type.



% Module

Returns the remainder of a division operation. The sign of the result is the same as the sign of the first operand.



** Exposure

Possible overflow condition when the result exceeds the maximum absolute value for the type. The exponentiation operator only works with floating point types.



Demo



//Arithmetic operators printfn "25 + 25 = %i" (25 + 25) printfn "75 - 25 = %i" (75 - 25) printfn "12 * 12 = %i" (12 * 12) printfn "100 / 4 = %i" (100 / 4) printfn "101 %% 10 = %i" (101 % 10) printfn "2 ** 3 = %f" (2.0 ** 3.0)
      
      





And here is the launch result:



image



Binary operators



The following table shows the binary comparison operators available for integer and floating-point types. These operators return values ​​of type bool.



= Equal

This is not an assignment operator. It is used only for comparison. This is a universal operator.



> more than

This is a universal operator.



<less than

This is a universal operator.



> = greater than or equal

This is a universal operator.



<= greater than or equal

This is a universal operator.



<> Not equal

This is a universal operator.



 //Binary operators printfn "25 = 25 = %b" (25 = 25) printfn "26 > 25 = %b" (26 > 25) printfn "26 < 25 = %b" (26 < 25) printfn "26 >= 25 = %b" (26 >= 25) printfn "26 <= 25 = %b" (26 <= 25) printfn "'a' <= 'b' = %b" ('a' <> 'b') //how about a more complex example, a tuple printfn "(1,'a') = (2,'a') = %b" ((1,'a') = (2,'a')) printfn "(1,'a') = (1,'a') = %b" ((1,'a') = (1,'a')) printfn "Some(1) = Some(2) = %b" (Some(1) = Some(2)) printfn "Some(2) = Some(2) = %b" (Some(2) = Some(2))
      
      





image



Boolean operators



The following table lists the logical operators available in F #. The only type supported by these operators is the bool type.



not

Boolean negation



||

Logical OR



&&

Logical and



Here is a small demonstration of the boolean operators listed above



 //Boolean operators printfn "not true = %b" (not true) printfn "true || false = %b" (true || false) printfn "true && true = %b" (true && true) printfn "true && false = %b" (true && false)
      
      





image



Bit operators



The following table describes the bitwise operators that are supported for unpacked integer types in F #.



&&&

The bitwise operator "AND". Bits as a result have a value of 1 if and only if the corresponding bits in both source operands are 1.



|||

The bitwise OR operator. The bits as a result have a value of 1 if any of the corresponding bits in the source operands is 1.



^^^

The bitwise exclusive operator "OR". Bits as a result have a value of 1 if and only if the bits in the original operands have unequal values.



~~~

Bitwise negation operator. This is a unary operator that produces a result in which all 0 bits in the original operand are converted to 1 bit, and all 1 bits are converted to 0 bits.



<<<

Bitwise left shift operator. The result is the first operand with the bits shifted to the left by the number of bits in the second operand. Bits shifted from the most significant position do not turn into the least significant position. The least significant bits are padded with zeros. The type of the second argument is int32.



>>>

Bitwise right shift operator. The result is a first operand with bits shifted to the right by the number of bits in the second operand. Bits shifted from the least significant position do not turn into the most significant position. For unsigned types, the most significant bits are padded with zeros. For signed types, the most significant bits are padded with ones. The type of the second argument is int32.



 //Bit shift operators //&&& and printfn "2 &&& 4 (which is 0010 &&& 0100, should be 0) = %X" (2 &&& 4) printfn "2 &&& 3 (which is 0010 &&& 0011, should be 2) = %X" (2 &&& 3) //||| or printfn "2 ||| 4 (which is 0010 ||| 0100, should be 6) = %X" (2 ||| 4) printfn "2 ||| 3 (which is 0010 ||| 0011, should be 3) = %X" (2 ||| 3) //^^^ xor printfn "2 ^^^ 4 (which is 0010 ^^^ 0100, should be 6) = %X" (2 ^^^ 4) printfn "2 ^^^ 3 (which is 0010 ^^^ 0011, should be 1) = %X" (2 ^^^ 3) //^^^ negate printfn "~~~4 (which is not 0100, should be 1011 (B hex), or 11 decimal) = %X" (~~~4) //<<< bit shift left printfn "4 <<< 1 (which is 0100 <<< by 1 place left , should be 1000 (8 hex), or 8 decimal) = %X" (4 <<< 1) //>>> bit shift right printfn "4 >>> 1 (which is 0100 >>> by 1 place right , should be 0010 (2 hex), or 2 decimal) = %X" (4 >>> 1)
      
      





image



Operator Overload



As I already mentioned, there will be times when you need to implement your own operator logic. In other .NET languages, this can be achieved by providing your own operator overloads. No wonder you need to do the same in F #. I didn’t want to go into classes and OO yet, but this seems to be in line with the current discussion, so let's see how you can implement your own operators in F #.



I stole it again from MSDN. The following code illustrates a vector class that has only two operators: one for unary minus and one for multiplication by scalar. In this example, two overloads are necessary for scalar multiplication, since the operator must work regardless of the order in which the vector and scalar appear.



 type Vector(x: float, y : float) = member this.x = x member this.y = y static member (~-) (v : Vector) = Vector(-1.0 * vx, -1.0 * vy) static member (*) (v : Vector, a) = Vector(a * vx, a * vy) static member (*) (a, v: Vector) = Vector(a * vx, a * vy) override this.ToString() = this.x.ToString() + " " + this.y.ToString()
      
      






All Articles