cj wyett
cj wyett

Reputation: 152

What is the syntax for floor division in Julia?

In Python 5//2 is a floor division.

In Julia:

5//2

Returns

5//2

How can I do floor division in Julia?

Upvotes: 1

Views: 3058

Answers (3)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42244

Try:

julia> 5 ÷ 2
2

The character ÷ can be entered by typing \div and pressing Tab.

On the other hand the operator // is used to create rational numbers.

The ÷ sign represents a div operator. If the number is negative you need to use fld to the the actual floor division. You could assign it to one of unused operators for comfortable use:

julia> ∺ = fld
fld (generic function with 10 methods)

julia> -5 ∺  2
-3

Upvotes: 10

Arch D. Robison
Arch D. Robison

Reputation: 4059

In Julia, use fld:

julia> fld(5,2)
2

julia> fld(-5,2)
-3

julia> fld(5,-2)
-3

julia> fld(-5,-2)
2

Upvotes: 3

JD Pavek
JD Pavek

Reputation: 154

You can also try:

julia> floor(5/2)
2.0

julia> floor(Int64, 5/2)
2

At the julia> prompt type '?' and then 'floor()' and again '? div()' to see what has already been mentioned.

Upvotes: 3

Related Questions