3
votes

say I have this code:

a = 4//2

This returns me "2//1"

b = 4//3

This returns me "4//3"

I understand that it is returning the value of a and b in simplest form. But what then? I thought that this operator returns the value of division in integer form, taking away the reminder. But it does not seem this is what it is doing.

I actually have this code:

x=Fun(identity,0..4π)
d=domain(x)
B=[ldirichlet(d),lneumann(d),rneumann(d)]
D=Derivative(d)
κ = 0.33205733621519630   
u0 = (1//2) * κ * x^2 

I wanted to know what (1//2) here is. From what I had thought earlier, this should have been equal to zero, but that is not what is required here. Can please someone clarify what is happening here and how does the // operator works?

1
2//3 is a rational number, I suppose you might call the // operator a constructor. - High Performance Mark
This is correct, you should post it as an answer. - fredrikekre
You can see it's not just echoing back the input by trying values such as 5//10, which will yield 1//2 as the result. It reduces the ratio by eliminating common divisors of the numerator and denominator. - pjs
This is the second question this week about // where the original poster thought it was the "integer division" operator. There may be a tutorial out there miswording something. OP, do you mind sharing if you saw this operator in some tutorial, and if so in which one? - aramirezreyes
This looks like translated Sympy code somehow? // in Python and Julia is quite different. - phipsgabler

1 Answers

3
votes

In Julia when you do not know something the first thing to do is to press ? to go to the help REPL mode represented by help?> prompt. After pressing ? type the command you are curious about:

help?> //
search: //

  //(num, den)

  Divide two integers or rational numbers, giving a Rational result.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> 3 // 5
  3//5

  julia> (3 // 5) // (2 // 1)
  3//10

One more additional usefull way to check what is going on in Julia is to use dump:

julia> dump(2//4)
Rational{Int64}
  num: Int64 1
  den: Int64 2

Finally, following the comment by @DNF it is worth noting that there is the integer division operator ÷ (and a corresponding function div) that computes x/y, truncated to an integer.:

julia> 13 ÷ 4
3