# About NaN, 0/0 and false

**URL:** <https://discourse.julialang.org/t/about-nan-0-0-and-false/34272>\
**Category:** Numerics\
**Tags:** question\
**Created:** [February 6, 2020, 8:33pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272 "2020-02-06T20:33:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerdely](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aerdely/32/37506_2.png) [@aerdely](https://discourse.julialang.org/u/aerdely)\
**Post date:** [February 6, 2020, 8:33pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/1 "2020-02-06T20:33:51Z")

</div>

Why…

`NaN*(0/0)*false` gives `0.0`,

`(0/0)*NaN*false` gives `-0.0`,

and:

`false*(0/0)*NaN`  
`false*NaN*(0/0)`  
`NaN*false*(0/0)`  
`(0/0)*false*NaN` all give `NaN`? 🤔

Because changing `false` by `true` we get always the same result: `NaN`

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [February 6, 2020, 9:12pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/2 "2020-02-06T21:12:47Z")

</div>

> [@aerdely](#):
>
> `NaN*(0/0)*false` gives `0.0` ,

Interesting, it seems to be intentional.

```julia
# make `false` a "strong zero": false*NaN == 0.0
function *(x::Bool, y::T)::promote_type(Bool,T) where T<:AbstractFloat
    return ifelse(x, y, copysign(zero(y), y))
end

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 6, 2020, 9:31pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/3 "2020-02-06T21:31:14Z")

</div>

Cf [Inconsistency of NaN](https://discourse.julialang.org/t/inconsistency-of-nan/28671).

Regarding false as strong zero, see Knuth’s writings at [[math/9205211] Two notes on notation](https://arxiv.org/abs/math/9205211).

---

<div class="post-metadata">

**Author:** ![aerdely](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aerdely/32/37506_2.png) [@aerdely](https://discourse.julialang.org/u/aerdely)\
**Post date:** [February 6, 2020, 9:32pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/4 "2020-02-06T21:32:59Z")

</div>

Ok, but how would you explain `-0.0` instead of `0.0` and the cases when we get `NaN`?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 6, 2020, 9:34pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/5 "2020-02-06T21:34:50Z")

</div>

Follow my first link and read the explanations from last time you asked that question.

---

<div class="post-metadata">

**Author:** ![aerdely](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aerdely/32/37506_2.png) [@aerdely](https://discourse.julialang.org/u/aerdely)\
**Post date:** [February 6, 2020, 9:37pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/6 "2020-02-06T21:37:23Z")

</div>

But how does that explain the cases when we get `NaN`?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 6, 2020, 9:41pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/7 "2020-02-06T21:41:37Z")

</div>

Your four last cases all boil down to `false` (strong zero) times anything becoming an ordinary 0, and ordinary 0 times `NaN` becoming `NaN`. Well, that and left-associativity of multiplication.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 6, 2020, 9:42pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/8 "2020-02-06T21:42:10Z")

</div>

Multiplication happens left to right. False\* Nan is 0 (or negative 0). Thus multiplying that by a Nan yields a Nan.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 6, 2020, 11:05pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/9 "2020-02-06T23:05:42Z")

</div>

```julia
julia> false * NaN
0.0

julia> NaN * false
0.0

```

Saying I’m surprising with this is to say the least.

For me ANY operation involving a NaN must be a NaN

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [February 6, 2020, 11:29pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/10 "2020-02-06T23:29:18Z")

</div>

> [@joa-quim](#):
>
> For me ANY operation involving a NaN must be a NaN

See Knuth’s paper linked by @GunnarFarneback , for me after thinking it up it makes sense to treat `false` as super strong `0` to suport Iverson’s bracket convention.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 7, 2020, 12:50pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/11 "2020-02-07T12:50:58Z")

</div>

> [@joa-quim](#):
>
> For me ANY operation involving a NaN must be a NaN

Pop-quiz, in C++ what would the following result in:

```julia
std::max(Nan,x) 
std::max(x,NaN) 
fmax(Nan,x)
fmax(x,NaN)

```

And what about in Julia?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 7, 2020, 2:26pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/12 "2020-02-07T14:26:41Z")

</div>

I’ve not present what they do in C++ (I deal quite a bit with NaNs in C but use isnan() to detect and deal with them).

In Julia they do the right thing

```julia
julia> max(NaN,8.0)
NaN

julia> max(NaN,8.0,NaN)
NaN

julia> maximum([1.1 5 7 9 NaN])
NaN

```

but the right thing is not always what we want. For example, for the `maximum` I would like to have a way to skip the NaNs. If they were `missing` one could use the `skipmissing` function but did not find an equivalent for NaNs.

---

<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:** [February 7, 2020, 3:12pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/13 "2020-02-07T15:12:23Z")

</div>

> [@joa-quim](#):
>
> the right thing is not always what we want

I wonder if I had to little or too much coffee to parse this.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 7, 2020, 3:31pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/14 "2020-02-07T15:31:09Z")

</div>

OK, I’ll give some sugar. More often than not one wants that `maximum([1.1 5 7 9 NaN])` returns `9` (which, BTW, is what Matlab does). But given the definition of `NaN` that, like Midas, where it touches transforms every number into a a NaN, returning NaN is the right thing.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [February 7, 2020, 3:55pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/15 "2020-02-07T15:55:04Z")

</div>

> > max([1.1 5 7 9 NaN],,‘includenan’)

ans =

NaN

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 7, 2020, 4:11pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/16 "2020-02-07T16:11:08Z")

</div>

There have been a hundred questions about how Julia handles (or, rather, does not handle) `NaN` and `missing` with regard to functions like `sum`, `max`, etc.

Every single time the conclusion is that it is up to the programmer to deal with them, and that functions like `max` should in-fact return `NaN` if one or more of its inputs have that value.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 7, 2020, 4:34pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/17 "2020-02-07T16:34:56Z")

</div>

Discussion derived but that was not the point before. I just manifested my surprise that

```julia
false * NaN
0.0

```

It’s only me?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 7, 2020, 4:40pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/18 "2020-02-07T16:40:02Z")

</div>

What do you think it should be the 3 semi-plausible outcomes are false, 0, and Nan. It can’t be false since true_float should be a float. Making it Nan would mean that A+false_B is not always equal to A, which would break sparse matrices. Thus 0.0 is the best answer.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 7, 2020, 4:49pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/19 "2020-02-07T16:49:08Z")

</div>

> [@](#):
>
> it is up to the programmer to deal with them

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 7, 2020, 5:00pm UTC](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272/20 "2020-02-07T17:00:28Z")

</div>

Once you accept the multiplication between a boolean and a floating point number, maybe you can also accept that it gives a particular, documented, result

[Next page](https://discourse.julialang.org/t/about-nan-0-0-and-false/34272.md?page=2)
