Ray Toal
Ray Toal

Reputation: 88378

How to specify the type of irrational numbers in Julia?

This was a surprise:

$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.1.1 (2019-05-16)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> typeof((Base.MathConstants).e)
Irrational{:ℯ}

julia> typeof((Base.MathConstants).e) == Irrational{:e}
false

How do I write the type Irrational{:e} in Julia 1.1.1?

This expression returned true in Julia 0.5, but only because e was a top-level identifier. Something changed in the language between 0.5 and now. I found that e was moved to Base.MathConstants, but I have not figured out how to write its type. The REPL says one thing, but what it says cannot be used in a == expression.

Upvotes: 3

Views: 298

Answers (1)

Ray Toal
Ray Toal

Reputation: 88378

Note carefully that the response from

typeof((Base.MathConstants).e)

is

Irrational{:ℯ}

with an "italicized" e. If you copy-paste the response into the expression

typeof((Base.MathConstants).e) == Irrational{:ℯ}

and evaluate it, you will get the value

true

Here's the reason. In old versions of Julia, the constant e was used for the famous number 2.718281828... but apparently people liked using e for exceptions, so e was moved to Base.MathConstants.

However a new constant was introduced into Base, namely . This is character U+212F, a "script small e."

You can use this identifier directly in your Julia code.

Upvotes: 4

Related Questions