# The speed of light is an integer. Why should we care?

**URL:** https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108
**Category:** Internals & Design
**Tags:** integer-overflow
**Created:** [May 25, 2020, 5:16am UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108 "2020-05-25T05:16:30Z")
**Posts on this page:** 19
**Page:** 4

<div class="post-metadata">

### Author: ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)
#### Post date: [May 28, 2020, 7:37pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/61 "2020-05-28T19:37:33Z")

</div>

What if there was a tutorial mode in the REPL, kind of like how a lot of video games start of popping up info on the screen about what button do what to gradually introduce the information. The first time you define an integer from an integer literal it would pop up some info about what you just did, including a warning about integer overflow and link to the manual. Likewise, the first time you write a for loop in the REPL outside a function, maybe it links you to the performance tips about not working in global scope, and to info about scoping rules for julia. Are there enough items like this to make a tutorial mode useful? Maybe every-time you start up it picks some info from it’s library to put at the top of the REPL.

---

<div class="post-metadata">

### Author: ![gevis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gevis/32/11737_2.png) [@gevis](https://discourse.julialang.org/u/gevis)
#### Post date: [May 28, 2020, 8:03pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/62 "2020-05-28T20:03:10Z")

</div>

> [@orialb](#):
>
> I’m sure that if you write a nice tutorial emphasizing the things you find important, people will appreciate it.

Actually, everything has already been written in the my first post in this thread.

In my view, the subsection [Overflow Behavior](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Overflow-behavior-1) only lacks the following [link](https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic-1) and may include the following example:

> [@gevis](#):
>
> Since 1983, the speed of light is defined to be exactly equal to 299,792,458 meters per second. So, it seems natural to define it as an integer while doing some culculations with Julia, for example, to calculate a Planck’s length. This is how it can be done in Julia REPL:
> 
> ```julia
> c = 299_792_458; # the speed of light
> G = 6.67430e-11; # Gravitational constant
> h = 6.62607015e-34; # Planck's contant 
> h_bar = h / (2*pi); # reduced Planck's constant
> planks_length = sqrt(G*h_bar/c^3)
> 
> ```
> 
> As a result, we immediately get `3.5793584153084007e-32` which at first sight is what the Planck’s length should be.
> 
> However, Wikipedia says that the Planck’s length is equal to `1.616255e-35` .
> 
> You probably already realized that the reason for this is the integer overflow that happens when raising `c` to the power of `3` . Moreover, it is easy to fix this redefining `c` to be a float:
> 
> ```julia
> c = 2.99792458e8
> 
> ```
> 
> After doing so, we get the value of the Planck’s length to be equal to `1.6162550244237053e-35` , which coincides with the value stated in Wikipedia.

If the corresponding git pull request will be accepted, I can prepare it.

The link above can be introduced at the end of the said subsection as follows:  
“For further discussion of this topic see link.”

The example above, can be introduced with as follows:  
“If your previous experience make you think that integer overflow is quite rare and almost never happen in “real life”, concider the following example:”

---

<div class="post-metadata">

### Author: ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)
#### Post date: [May 28, 2020, 8:31pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/63 "2020-05-28T20:31:33Z")

</div>

Actually as a physicsit it does not seem natural to me to define c as an integer.  
Clearly it is define as a whole number of metres in the SI system since 1983  
Then again I started my undergrad degree in 1981…

I would first of all reach for PhysicalConstants.jl

I also saw mention of SaferIntegers.jl in this thread. Mind. Blown.  
Are there similar things in other modern languages?

---

<div class="post-metadata">

### Author: ![Ian\_Slagle](https://avatars.discourse-cdn.com/v4/letter/i/b5a626/32.png) [@Ian\_Slagle](https://discourse.julialang.org/u/Ian_Slagle)
#### Post date: [May 28, 2020, 8:35pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/64 "2020-05-28T20:35:09Z")

</div>

> [@johnh](#):
>
> Are there similar things in other modern languages?

IIRC, Rust (which was designed for very safe code) doesn’t have overflow in debug code, but allows it in release code

> <https://github.com/rust-lang/rfcs/pull/560>
>
> A simplified version of Gabhor's \["Scoped Attributes for Integer Overflow" RFC\](…https://github.com/rust-lang/rfcs/pull/146).
> 
> \[Merged \`text/\`\](https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md)
> 
> \[Rendered.\](https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md)
> 
> tracking issue: https://github.com/rust-lang/rust/issues/22020

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 28, 2020, 9:12pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/65 "2020-05-28T21:12:13Z")

</div>

> [@johnh](#):
>
> Actually as a physicsit it does not seem natural to me to define c as an integer.  
> Clearly it is define as a whole number of metres in the SI system since 1983  
> Then again I started my undergrad degree in 1981…

I’d strongly disagree. The speed of light in my view is the dimensionless integer `1` [GitHub - MasonProtter/NaturallyUnitful.jl: Facilities for using natural units with Unitful.jl](https://github.com/MasonProtter/NaturallyUnitful.jl) 😃

---

<div class="post-metadata">

### Author: ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)
#### Post date: [May 28, 2020, 9:40pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/66 "2020-05-28T21:40:53Z")

</div>

> [@Mason](#):
>
> I’d strongly disagree. The speed of light in my view is the dimensionless integer `1` [GitHub - MasonProtter/NaturallyUnitful.jl: Facilities for using natural units with Unitful.jl](https://github.com/MasonProtter/NaturallyUnitful.jl) 😃

This is the only acceptable solution. 😆  
Bonus: it saves you from integer overflows.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 28, 2020, 10:58pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/67 "2020-05-28T22:58:11Z")

</div>

@gevis, one of the core tenets of my interactions on this board is to assume the best of faith when interacting with others here. I don’t see folks shoving things in your face, nor do I see folks saying you can’t read, nor do I see folks saying that the documentation is in the best possible state and should not be changed. At the same time, I totally empathize with getting bit by a bug like this and feeling frustrated — and I get how the discussion here might feel like pushback. I’d just ask that you (and others here) focus on the potential improvements and avoid putting words in others mouths (or reacting to such). Remember that we’re all here because we enjoy the language and want to see it improve and succeed.

This discussion has already spurred [one concrete improvement to the docs](https://github.com/JuliaLang/julia/pull/36056), and I’d further support cross-linking the FAQ to the overflow behaviors. I’m not as certain about the speed-of-light example simply because it’s rather long and complicated for non-physicists. It could be as simple as:

```Julia
julia> c = 299_792_458
299792458

julia> 100c^2
8987551787368176400

julia> 200c^2
-471640498973198816

```

But I’m not sure it’d get accepted. There’s a tension in trying to keep things terse as too much detail can also get in the way; I’m not sure where this one falls. We’ve also seen molecular biologists hit this in defining Avogadro’s constant as `6.022*10^23` — should we also include that? What about other domains?

> [@gevis](#):
>
> Especially at the time when Python has already established the standard for dealing with integer overflow by a higher level languages.

I do want to emphasize that numpy has the same behavior — and also relegates its discussion of overflow to the [third major section](https://numpy.org/doc/stable/user/basics.types.html#overflow-errors) of their user guide, even though they’re violating the behavior of their own language.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [May 29, 2020, 1:30am UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/69 "2020-05-29T01:30:44Z")

</div>

> [@gevis](#):
>
> Did you take the trouble to view the video in the reference above explaining how documentation should be organized and what is the difference between tutorial and reference guide?
> 
> Currently the Julia tutorial is the mixture of them.

I’m really curious what gave you the impression that the Julia manual is a tutorial. It’s not, as @orialb pointed out. It’s a manual - primarily a technical reference (that’s primarily how I use it) with a fair amount of explanation sprinkled in. [Nowhere does it claim](https://docs.julialang.org/en/v1/search/?q=tutorial) to be a tutorial.

I 100% agree that more tutorials would be useful, and I think should be tailored to different domains. I don’t think that’s what the manual is for though.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 29, 2020, 5:01am UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/70 "2020-05-29T05:01:33Z")

</div>

To be fair, I believe that historically the manual did take on the role of a tutorial, and was partially written that way.

---

<div class="post-metadata">

### Author: ![gevis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gevis/32/11737_2.png) [@gevis](https://discourse.julialang.org/u/gevis)
#### Post date: [May 29, 2020, 5:47am UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/71 "2020-05-29T05:47:23Z")

</div>

> [@mbauman](#):
>
> I’d further support cross-linking the FAQ to the overflow behaviors.

It would be nice.

> [@mbauman](#):
>
> I’m not as certain about the speed-of-light example

I do not insist on that.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 29, 2020, 6:41am UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/72 "2020-05-29T06:41:03Z")

</div>

> [@dpsanders](#):
>
> historically the manual did take on the role of a tutorial, and was partially written that way

AFAICT the tutorial-style parts cover concepts that are more or less unique to Julia. A nice example is the sketch of [`Rational`](https://docs.julialang.org/en/v1/manual/constructors/#Case-Study:-Rational-1), which brings together various concepts (parametric types, inner and outer constructors, promotion, etc).

I think that these belong in the manual, since otherwise the intent of how basic constructs should be used would be difficult to decipher. This, of course, does not mean that the manual is a tutorial about all things that are necessary to use and understand Julia, especially if they are generic concepts of programming. For example, [the Control Flow](https://docs.julialang.org/en/v1/manual/control-flow/) section does not explain what a conditional is.

That said, just working through (most of) the manual is a very nice way to get started with Julia. But it does require covering about 70% of it, instead of just looking up certain sections.

---

<div class="post-metadata">

### Author: ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)
#### Post date: [May 30, 2020, 12:08pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/73 "2020-05-30T12:08:18Z")

</div>

It is a great shame that Avogadro’s constant cant be expressed cannot be expressed as a `Float64` with no loss in precision.

```julia
julia> convert(BigInt, big"6.02214076E23") |> Float64 |> BigInt
602214075999999987023872

julia> convert(BigInt, big"6.02214076E23") |> Float64 |> nextfloat |> BigInt
602214076000000054132736

```

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [June 4, 2020, 10:21pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/74 "2020-06-04T22:21:52Z")

</div>

I know this has been mentioned, but I did not see a link. The reason that Julia allows integer overflows is explained in great length here:  
[https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic-1](https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic-1)

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [August 17, 2020, 12:50am UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/75 "2020-08-17T00:50:11Z")

</div>

> [@tamasgal](#):
>
> I think the problem starts at the physics students not being taught some basic CS or the lack of the need of such during a physics study

I did a course in Numerical Computing and I was taught all sorts of things like catastrophic cancellation etc. I think if you do a lot of computation computers then learnign about Int and floats and how calculations can be messed up is super useful!

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [August 23, 2020, 2:22pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/76 "2020-08-23T14:22:21Z")

</div>

It might be nice if there was a tutorial package that would interactively walk people through the syntax and interfaces, a few key packages, even basic computer science knowledge like this.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [August 23, 2020, 2:28pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/77 "2020-08-23T14:28:28Z")

</div>

Not exactly what you are looking for but checking out exercism which has a julia track.

> **[Exercism](https://exercism.org/)**
>
> Learn, practice and get world-class mentoring in over 50 languages. 100% free.

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [September 5, 2020, 11:46pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/78 "2020-09-05T23:46:10Z")

</div>

At least this is false (for the REPL with alternative REPL modes):

> [@gevis](#):
>
> At the very least this means that Julia REPL cannot be used as a scientific calculator by an average student.

Your exact code can work with first doing:

```julia-auto
julia> using SafeREPL # note, exact installation instructions (since not yet registered) at: https://github.com/rfourquet/SafeREPL.jl

```

i.e. without doing:

```julia-auto
c = 299_792_458.0; 

```

what you otherwise could do, to get a floating point number, arguably better.

> [@oheil](#):
>
> should not be used as scientific calculator by an average student!

I’m not sure about that, I support Julia’s defaults, as it’s faster, while I can see both sides on the topic of the defaults, and thus I agree with you; and because of the danger of getting used to that non-default mode, maybe even it shouldn’t be used/installed by default?

My rule of thumb is that what can be measured, e.g. in meters (per sec), such as the speed of light, should use floating point, and what MUST (can?) be counted, e.g. currency (photons?), should use ~~floats~~ integers. [Those rules are sometimes broken, in banking integer cents or decimal (floating point), for dollars, should be used, while for a hedge fund floats can be appropriate for amounts.]

Since the Avogato constant was also brought up, since its SI redefinition in 2019, it’s exactly 6.02214076×10^23, and as atoms can be counted, its an argument for integers (BigInt then)…, while I guess floats (almost) always used.

> [@\[ANN\] SafeREPL: use BigInt by default at the REPL](https://discourse.julialang.org/t/ann-saferepl-use-bigint-by-default-at-the-repl/41271):
>
> With Julia 1.5 around the corner, I’m happy to present [SafeREPL](https://github.com/rfourquet/SafeREPL.jl), a little experimental package which by default interprets Int and Int128 REPL literals as BigInt, and Float64 literals as BigFloat: julia\> using SafeREPL julia\> factorial(40) 815915283247897734345611269596115894272000000000 julia\> sqrt(2.0) 1.414213562373095048801688724209698078569671875376948073176679737990732478462102 julia\> c = 299\_792\_458; c^3 # the cube of the speed of light can even be computed safely! 26944002417373989…

> **[GitHub - JeffreySarnoff/SaferIntegers.jl: These integer types use checked arithmetic,...](https://github.com/JeffreySarnoff/SaferIntegers.jl)**
>
> These integer types use checked arithmetic, otherwise they are as system types.

---

<div class="post-metadata">

### Author: ![gevis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gevis/32/11737_2.png) [@gevis](https://discourse.julialang.org/u/gevis)
#### Post date: [September 7, 2020, 5:02am UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/79 "2020-09-07T05:02:31Z")

</div>

> [@Palli](#):
>
> My rule of thumb is that what can be measured, e.g. in meters (per sec), such as the speed of light, should use floating point, and what MUST (can?) be counted, e.g. currency (photons?), should use floats.

So, no place for intergers? 🙂

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [September 7, 2020, 3:55pm UTC](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108/80 "2020-09-07T15:55:35Z")

</div>

Not in e.g. JavaScript…, yes, integers have a place (I edited that error in my post), and I’m very conflicted if that language has the right idea (Fraudian slip? 🙂 ), but it does simplify to only have floats (for the user, not the implementer of the language/runtime, or for speed, while JS/V8 is often very fast).

[Previous page](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108.md?page=3)
