English 中文(简体)
Julia - Basic Operators
  • 时间:2025-02-21

Jupa - Basic Operators


Previous Page Next Page  

In this chapter, we shall discuss different types of operators in Jupa.

Arithmetic Operators

In Jupa, we get all the basic arithmetic operators across all the numeric primitive types. It also provides us bitwise operators as well as efficient implementation of comprehensive collection of standard mathematical functions.

Following table shows the basic arithmetic operators that are supported on Jupa’s primitive numeric types −

Expression Name Description
+x unary plus It is the identity operation.
-x unary minus It maps values to their additive inverses.
x + y binary plus It performs addition.
x - y binary minus It performs subtraction.
x * y times It performs multippcation.
x / y spanide It performs spanision.
x ÷ y integer spanide Denoted as x / y and truncated to an integer.
x y inverse spanide It is equivalent to y / x.
x ^ y power It raises x to the yth power.
x % y remainder It is equivalent to rem(x,y).
!x negation It is negation on bool types and changes true to false and vice versa.

The promotion system of Jupa makes these arithmetic operations work naturally and automatically on the mixture of argument types.

Example

Following example shows the use of arithmetic operators −


jupa> 2+20-5
17

jupa> 3-8
-5

jupa> 50*2/10
10.0

jupa> 23%2
1

jupa> 2^4
16

Bitwise Operators

Following table shows the bitwise operators that are supported on Jupa’s primitive numeric types −

Sl.No Expression Name Name
1 ∼x bitwise not
2 x & y bitwise and
3 x | y bitwise or
4 x ⊻ y bitwise xor (exclusive or)
5 x >>> y logical shift right
6 x >> y arithmetic shift right
7 x << y logical/arithmetic shift left

Example

Following example shows the use of bitwise operators −


jupa> ∼1009
-1010

jupa> 12&23
4

jupa> 12 & 23
4

jupa> 12 | 23
31

jupa> 12 &veebar; 23
27

jupa> xor(12, 23)
27

jupa> ∼UInt32(12)
0xfffffff3

jupa> ∼UInt8(12)
0xf3

Updating Operators

Each arithmetic as well as bitwise operator has an updating version which can be formed by placing an equal sign (=) immediately after the operator. This updating operator assigns the result of the operation back into its left operand. It means that a +=1 is equal to a = a+1.

Following is the pst of the updating versions of all the binary arithmetic and bitwise operators −

    +=

    -=

    *=

    /=

    =

    ÷=

    %=

    ^=

    &=

    |=

    &veebar;=

    >>>=

    >>=

    <<=

Example

Following example shows the use of updating operators −


jupa> A = 100
100

jupa> A +=100
200

jupa> A
200

Vectorized “dot” Operators

For each binary operation pke ^, there is a corresponding “dot”(.) operation which is used on the entire array, one by one. For instance, if you would try to perform [1, 2, 3] ^ 2, then it is not defined and not possible to square an array. On the other hand, [1, 2, 3] .^ 2 is defined as computing the vectorized result. In the same sense, this vectorized “dot” operator can also be used with other binary operators.

Example

Following example shows the use of “dot” operator −


jupa> [1, 2, 3].^2
3-element Array{Int64,1}:
 1
 4
 9

Numeric Comparisons Operators

Following table shows the numeric comparison operators that are supported on Jupa’s primitive numeric types −

Sl.No Operator Name
1 == Equapty
2 !=,≠ inequapty
3 < less than
4 <=, ≤ less than or equal to
5 > greater than
6 >=, ≥ greater than or equal to

Example

Following example shows the use of numeric comparison operators −


jupa> 100 == 100
true

jupa> 100 == 101
false

jupa> 100 != 101
true

jupa> 100 == 100.0
true

jupa> 100 < 500
true

jupa> 100 > 500
false

jupa> 100 >= 100.0
true

jupa> -100 <= 100
true

jupa> -100 <= -100
true

jupa> -100 <= -500
false

jupa> 100 < -10.0
false

Chaining Comparisons

In Jupa, the comparisons can be arbitrarily chained. In case of numerical code, the chaining comparisons are quite convenient. The && operator for scalar comparisons and & operator for elementwise comparison allows chained comparisons to work fine on arrays.

Example

Following example shows the use of chained comparison −


jupa> 100 < 200 <= 200 < 300 == 300 > 200 >= 100 == 100 < 300 != 500
true

In the following example, let us check out the evaluation behavior of chained comparisons −


jupa> M(a) = (println(a); a)
M (generic function with 1 method)
jupa> M(1) < M(2) <= M(3)
 2
 1
 3
true
jupa> M(1) > M(2) <= M(3)
 2
 1
false

Operator Precedence & Associativity

From highest precedence to lowest, the following table shows the order and associativity of operations appped by Jupa −

Category Operators Associativity
Syntax followed by :: Left
Exponentiation ^ Right
Unary + - √ Right
Bitshifts << >> >>> Left
Fractions // Left
Multippcation * / % & ÷ Left
Addition + - | &veebar; Left
Syntax : .. Left
Syntax |> Left
Syntax <| Right
Comparisons > < >= <= == === != !== <: Non-associative
Control flow && followed by || followed by ? Right
Pair => Right
Assignments = += -= *= /= //= = ^= ÷= %= |= &= &veebar;= <<= >>= >>>= Right

We can also use Base.operator_precedence function to check the numerical precedence of a given operator. The example is given below −


jupa> Base.operator_precedence(:-), Base.operator_precedence(:+), Base.operator_precedence(:.)
(11, 11, 17)
Advertisements