# Why does scientific notation break the range function?

**URL:** <https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014>\
**Category:** New to Julia\
**Created:** [April 25, 2022, 8:46pm UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014 "2022-04-25T20:46:57Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Post date:** [April 27, 2022, 3:43am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/64 "2022-04-27T03:43:42Z")

</div>

It’s not about whether a few experts on this forum _can_ understand the error message, it’s about what will be _best_ understood by _most_ users.

The current error message:

1. fails to identify which line the error occurs on,
2. fails to identify which function is causing the error,
3. poorly communicates what’s actually causing the error, and
4. does not offer the user any constructive solutions.

I encourage you to read the current error message aloud to a spouse or roommate, or get [Google to read it aloud to you](https://cloud.google.com/text-to-speech). Then compare against my suggested rewrite. Here’s the comparison:

```julia
ERROR: TypeError: in keyword argument length, expected Union{Nothing, Integer}, got a value of type Float64.
. . . 
ERROR in range(): Keyword argument length was entered as type Float64, but should be of type Integer.

```

I am of course very biased, but it’s hard to believe that any neutral party would say the current error message is better in any way, compared to my suggested rewrite. Are there some merits to the current error message that I’m not understanding?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 27, 2022, 4:13am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/65 "2022-04-27T04:13:06Z")

</div>

The current message has the advantage that it doesn’t lie about the expected type.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 27, 2022, 4:26am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/66 "2022-04-27T04:26:31Z")

</div>

> [@chadagreene](#):
>
> `ERROR in range(): Keyword argument length was entered as type Float64, but should be of type Integer.`

I would prefer:  
`TypeError in range(): Keyword argument length was entered as type Float64, but should be a subtype of Union{Nothing,Integer}.`  
starting with `TypeError` i already know that one function has an incorrect input. The ERROR: TypeError is superfluous. on the type suggestion, `Union{Nothing,Integer}` is not a concrete type, so the error message should explicitly say that the input is not a subtype, instead of is not an expected type.

---

<div class="post-metadata">

**Author:** ![suavesito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suavesito/32/34386_2.png) [@suavesito](https://discourse.julialang.org/u/suavesito)\
**Post date:** [April 27, 2022, 4:27am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/67 "2022-04-27T04:27:40Z")

</div>

> [@chadagreene](#):
>
> fails to identify which line the error occurs on,

This is a problem of the environment in which you’re coding. When you’re executing scripts, Julia _does_ point out the line that caused the error. This might be solved by the plugin for VS Code, but I don’t know if it is possible or feasible (hope it is).

> [@chadagreene](#):
>
> fails to identify which function is causing the error

Think this could be changed, as pointed out by @trahflow [here](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/54). I think is worth filling an issue. 🙂

> [@chadagreene](#):
>
> poorly communicates what’s actually causing the error

I disagree with this, as it _does_ say that you’re passing the wrong type to the argument. That the expected type `Union{Nothing, Integer}` is not trivial to understand coming from another languages is not a problem of Julia. I would argue that this is something almost every user will need to learn.

> [@chadagreene](#):
>
> does not offer the user any constructive solutions.

This is hard to address because of how the language was designed, as the error is not properly caused _inside_ the function, and we don’t have traits as a first class language feature. I don’t see how this could be done.

> [@chadagreene](#):
>
> `ERROR in range(): Keyword argument length was entered as type Float64, but should be of type Integer.`

I do like more the way you phrase the error, it looks clearer than the current one. 🙂

> [@chadagreene](#):
>
> I encourage you to read the current error message aloud to a spouse or roommate

Given my roommates are either computer scientist or from humanistic areas, I would get wildly different responses. 😜

Jokes aside, your criticism is very constructive, just that I don’t think your last point is easily solvable. At least not yet.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 27, 2022, 6:24am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/68 "2022-04-27T06:24:18Z")

</div>

> 1. Fails to identify which line the error occurs on

Works for me:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/b/8bba2b37635ed4c6464f67fd8101e58d6ed959c1.png)

The first line shows the error to be on line 5, which is indeed correct. You have to toggle line numbers in cells though, default is (as in regular Jupyter notebooks) that they’re switched off:

![image](https://global.discourse-cdn.com/julialang/original/3X/2/9/29cdea104f7a9ada929eb3bfa4352b55a5ff24a0.png)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 27, 2022, 7:57am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/69 "2022-04-27T07:57:52Z")

</div>

Doesn’t specify `range` though. Which is a source of confusion.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [April 27, 2022, 8:34am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/70 "2022-04-27T08:34:00Z")

</div>

`expected Union{Nothing, Integer}, got a value of type Float64`

It looks possible to modify Julia in order to change the error message like

`expected either Integer or nothing (Union{Nothing, Integer}), got a value of type Float64`

Would it be easier to read ?

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [April 27, 2022, 8:35am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/71 "2022-04-27T08:35:07Z")

</div>

```julia
julia> x = range(0,10,length=nothing)
0:10

```

A `length` of type `Nothing` is allowed, so the current error message is correct, while the suggested rewrite is not. One may discuss whether the `range` function should allow `length=nothing`, but given its current implementation the error message is fine.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 27, 2022, 8:50am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/72 "2022-04-27T08:50:28Z")

</div>

To be fair, while it doesn’t lie, it is hard to interpret for a beginner.

```julia
ERROR: TypeError: in keyword argument length, expected Union{Nothing, Integer}, got a value of type Float64

```

This makes it sound like the argument should be of type `Union{Nothing, Integer}`. Except, how does one create that type?

```julia
julia> Union{Nothing, Integer}(3)
ERROR: MethodError: no method matching Union{Nothing, Integer}(::Int64)

```

That doesn’t work, and it also doesn’t tell me what I should do. One can search for help on `Union`, but that’s probably a waste of their time for something so trivial.

Unless I know that `Union{Nothing, Integer}` means “either an `Integer`, or `nothing`”, it’s not obvious to a beginner. I would say that the error message might be improved here to make it easier to understand. A beginner shouldn’t be required to understand `Union` types to get their work done, that’s an unnecessarily steep learning curve when there’s a simpler solution.

Also, if someone specifies a numerical value for `length`, it’s probably reasonable to assume that they don’t intend that argument to be `nothing`. The fact that it may accept `nothing` is likely irrelevant in this context.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 27, 2022, 9:33am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/73 "2022-04-27T09:33:23Z")

</div>

Are you suggesting hardcoding it for the `range` function, or changing how `Union` is printed generally? Because to me reading about how `Union` types work isn’t _that_ high a requirement. It’s easily googleable (`Julia Union` gets you right there), and once you know what it means it’s a more compact description than natural language versions, especially for larger union types.

And we don’t “simplify” other abstract types.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 27, 2022, 9:38am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/74 "2022-04-27T09:38:08Z")

</div>

I’m suggesting changing how a `TypeError` is shown if the expected type is a `Union`. Instead of `Union{Nothing, Integer}` it may say “an `Integer` or `nothing`”, which conveys the same information.

I understand that `Union{Nothing, Integer}` is a more compact description that’s googleable, however, in this case, the bar to a user-friendly error message doesn’t appear to be high.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 27, 2022, 9:41am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/75 "2022-04-27T09:41:32Z")

</div>

That doesn’t seem to be too unreasonable, going from

```julia
expected Union{Nothing, Integer}, got a value of type Float64

```

to

```julia
expected value to be of type Integer or Nothing, got a value of type Float64

```

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [April 27, 2022, 9:44am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/76 "2022-04-27T09:44:17Z")

</div>

I think that @gustaphe’s concern is about scalability when the `Union` gets more complex (e.g. nested).  
Integration with a clickable `Union` link to a doc pane (à la RStudio) in vscode could be a good trade-off between current compact error message (which drives you toward a better understanding of Julia’s type sytem) and user-friendlyness.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [April 27, 2022, 10:22am UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/77 "2022-04-27T10:22:36Z")

</div>

I’d say, as a beginner, it is worth spending time learning the error message. Or in other words, beginners should learn how to read the basic error message. Here, `Union` is a thing to learn, whether you like it or not, you will encounter it sometime.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [April 27, 2022, 12:24pm UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/78 "2022-04-27T12:24:40Z")

</div>

How about changing the help text so it says `type of X` when `X` is a concrete type and `subtype of Y` when `Y` is an abstract type? I would say that trying to find an object that is an instance of a `Union{Integer, Nothing}` is indeed confusing because it can’t be done in that sense.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 27, 2022, 12:37pm UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/79 "2022-04-27T12:37:13Z")

</div>

Perhaps an error hint may be added to clarify this, instead of changing the error message if this is deemed to be difficult.

---

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [April 27, 2022, 1:22pm UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/80 "2022-04-27T13:22:09Z")

</div>

I agree so much with this. It was one of the first thing I tried to learn and it was immensely helpful (and it is always one of the first things I teach in classes).

I think the error message really just lacks the information that the `keyword argument length` directly refers to the `range()` function.

I honestly think something like (as suggested by @nilshg)

```julia
expected value to be of type Integer or Nothing, got a value of type Float64

```

is just more confusing, because this could also mean that 2 functions exist, one of which expects an `Integer`, one of which expects type `Nothing` (obviously nonsense with `Integer`, but not in other `Union` cases).

My hope would be that explicitly referring to the function is helpful to beginners e.g.

```julia
ERROR: TypeError: in keyword argument length of function range(), expected Union{Nothing, Integer}, got a value of type Float64

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 27, 2022, 1:49pm UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/81 "2022-04-27T13:49:40Z")

</div>

Definitely agree the wording could be improved. Would you be willing to open a GitHub issue about the wording of this message?

---

<div class="post-metadata">

**Author:** ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Post date:** [April 27, 2022, 1:55pm UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/82 "2022-04-27T13:55:08Z")

</div>

Absolutely. This’d be my first GitHub issue–can ya point me to where exactly I should do it?

---

<div class="post-metadata">

**Author:** ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Post date:** [April 27, 2022, 1:57pm UTC](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014/83 "2022-04-27T13:57:13Z")

</div>

Oh! I had no idea that tiny number `5` buried in the stacktrace is a line number! Would be good to label it “Line 5” and print it _first_.

Thanks for the tip on showing line numbers in notebook cells!

[Previous page](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014.md?page=3)

[Next page](https://discourse.julialang.org/t/why-does-scientific-notation-break-the-range-function/80014.md?page=5)
