# It is so nice to program in a sane language!

**URL:** <https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227>\
**Category:** Offtopic\
**Created:** [December 13, 2019, 10:41am UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227 "2019-12-13T10:41:25Z")\
**Posts on this page:** 11\
**Page:** 3

<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:** [December 16, 2019, 7:32am UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/41 "2019-12-16T07:32:07Z")

</div>

It think this would be best handled by a [literal macro](https://docs.julialang.org/en/latest/manual/metaprogramming/#Non-Standard-String-Literals-1), eg

```julia
literal"1.0*10^30"

```

Perhaps a package is the best place to experiment with the semantics.

---

<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:** [December 16, 2019, 8:21am UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/42 "2019-12-16T08:21:59Z")

</div>

With some help

```julia
Base.:(+)(::typeof([]), ::typeof([])) = nothing
Base.:(+)(::typeof([]), ::typeof(())) = ["object" "Object"]
Base.:(+)(::typeof(()), ::typeof([])) = 0
Base.:(+)(::typeof(()), ::typeof(())) = NaN

```

we can get:

```julia
julia> [] + []

julia> [] + ()
1×2 Array{String,2}:
 "object" "Object"

julia> () + []
0

julia> () + ()
NaN

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 16, 2019, 8:49am UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/43 "2019-12-16T08:49:02Z")

</div>

> [@Tamas\_Papp](#):
>
> ```julia
> literal"1.0*10^30"
> 
> ```

This is good for experimenting. But this issue is something you are likely to stumble into. If you are aware enough to use a literal macro, it’s probably not an problem anyway.

Could a linter catch this?

---

<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:** [December 16, 2019, 8:57am UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/44 "2019-12-16T08:57:20Z")

</div>

Yes, a linter would be great for this too.

You are right that this is one of the problems people newbies stumble into, but [Julia made a decision very early not to be clever about this](https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic-1).

It is tempting to expect the parser to figure out the “right” type of numeric values behind the scenes, but I imagine that would (1) either have no effect (for non-broadened values), (2) make code slower beause the parser decided that `BigFloat` or `BigInt` is the right type and propagate that.

A lot of examples in this topic demonstrate gotchas that even relatively experienced users stumble into. I don’t think that integer arithmetic is one of those though, it is just part of the language design.

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [December 16, 2019, 3:25pm UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/45 "2019-12-16T15:25:10Z")

</div>

I’m too curious now given the above examples. Can someone explain in the following:

```julia
f2=3
2f2

```

produces `200.0f0`?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 16, 2019, 3:27pm UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/46 "2019-12-16T15:27:38Z")

</div>

`2f2` is parsed as a Float32 literal for `2 * 10^2`, which equals `200.0`. This happens before any variables are checked for values.

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [December 16, 2019, 3:30pm UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/47 "2019-12-16T15:30:56Z")

</div>

ah, of course. thanks.

---

<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:** [December 17, 2019, 11:40am UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/48 "2019-12-17T11:40:10Z")

</div>

> [@Tamas\_Papp](#):
>
> How would you suggest the above is parsed instead? Why not just use `1e30` ?

I ran into this recently - my reason for not doing the latter was that my exponent was stored in a variable (and `x=30; 1.0ex`) doesn’t work.

Edit: I don’t have a good suggestion to your first question though 🤷‍♀️

---

<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:** [December 17, 2019, 11:50am UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/49 "2019-12-17T11:50:55Z")

</div>

I think that’s an actual calculation, not a question of type widening for literals. See eg `exp10`.

---

<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:** [December 17, 2019, 6:07pm UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/50 "2019-12-17T18:07:49Z")

</div>

Yes, I ended up going with `exp10`, just offering another reason one might try eg `1.0*10^x` and get bit. I’m not arguing for a change to the language though - all the design choices around this seem reasonable to me.

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [February 6, 2020, 10:20am UTC](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227/51 "2020-02-06T10:20:13Z")

</div>

I run in a similar issue, took a while to isolate this MEW from my code:  
Seems like one has to copy all varaibles first before using them like this.

```julia
function ou(au::Number;logspaced::Bool=false)
    if logspaced
        transform = (x)->(10.0^x*sign(au))
        au = log10(abs(au)) # commenting this line makes the example work as expected,.
    else
        transform = (x) ->identity(x)
    end
    return transform
end

uu(21,logspaced=false)(42) # ->42 ✓
ou(23,logspaced=true)(42) # ->1e42 ✓
ou(-21,logspaced=false)(42) # 42 ✓
ou(-21,logspaced=true)(42) # 1e42 𝙓 -1e42 expected
ou(1,logspaced=true)(42) # 0 𝙓 1e42 expected

```

[Previous page](https://discourse.julialang.org/t/it-is-so-nice-to-program-in-a-sane-language/32227.md?page=2)
