# Bugs in isfinite(), isnan and isinf()?

**URL:** https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005
**Category:** New to Julia
**Created:** [February 10, 2021, 3:48pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005 "2021-02-10T15:48:42Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 10, 2021, 3:48pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/1 "2021-02-10T15:48:42Z")

</div>

the following seems to be bugs to me:

```julia
julia> isfinite.([NaN, Inf, 2.0, -Inf])
4-element BitArray{1}:
 1
 1
 1
 1

julia> isinf.([NaN, Inf, 2.0, -Inf])
4-element BitArray{1}:
 0
 0
 0
 0

julia> isnan.([NaN, Inf, 2.0, -Inf])
4-element BitArray{1}:
 0
 0
 0
 0

```

am I missing something? or are they really bugs? I’m using 1.5.3

---

<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 10, 2021, 3:50pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/2 "2021-02-10T15:50:47Z")

</div>

I think you’ve redefined something really fundamental. I can’t reproduce any of these.

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 10, 2021, 3:54pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/3 "2021-02-10T15:54:34Z")

</div>

> [@Oscar\_Smith](#):
>
> I think you’ve redefined something really fundamental

oh yes! they back to normal after I start with `--startup-file=no`

but I did **not** redefine them!!! maybe some packages did that??? need to check…

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 10, 2021, 3:59pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/4 "2021-02-10T15:59:21Z")

</div>

I found that it’s `--math-mode=fast` makes them crazy:

```julia
julia> @enter isfinite(NaN)
[ Info: tracking Base
In isfinite(x) at float.jl:554
>554 isfinite(x::AbstractFloat) = x - x == 0

About to run: (-)(0.0, 0.0)

```

from the above, I see that `NaN` is indeed passed as `0.0` into `isfinite()` !  
is it the expected behavior when `--math-mode=fast`? thanks.

---

<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 10, 2021, 4:01pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/5 "2021-02-10T16:01:07Z")

</div>

Yes. fastmath implies that all values are finite, and produces undefined behavior if that is not true.

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 10, 2021, 4:15pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/6 "2021-02-10T16:15:27Z")

</div>

could u please point to the appropriate documentation about `math-mode` behaviors? dunno why I can’t find the docs. thanks.

---

<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 10, 2021, 4:17pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/7 "2021-02-10T16:17:02Z")

</div>

Is this what you’re looking for? [Mathematics · The Julia Language](https://docs.julialang.org/en/v1/base/math/#Base.FastMath.@fastmath)

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 10, 2021, 4:18pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/8 "2021-02-10T16:18:56Z")

</div>

not exactly.

> [@Oscar\_Smith](#):
>
> fastmath implies that all values are finite, and produces undefined behavior if that is not true.

I want document on above ☝

I think it’s a statement that has huge implications. Wanna see it in official doc.

---

<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 10, 2021, 4:22pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/9 "2021-02-10T16:22:21Z")

</div>

" Use [`@fastmath`](https://docs.julialang.org/en/v1/base/math/#Base.FastMath.@fastmath) to allow floating point optimizations that are correct for real numbers, but lead to differences for IEEE numbers." [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-annotations). By “IEEE Numbers”, this is explicitly calling out things like `-0.0` `NaN` and `Inf`

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [February 10, 2021, 4:24pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/10 "2021-02-10T16:24:38Z")

</div>

See fast-math and finite-math-only on the gcc man pages: [https://linux.die.net/man/1/gcc](https://linux.die.net/man/1/gcc)

> [@](#):
>
> Allow optimizations for floating-point arithmetic that assume that arguments and results are not NaNs or ±Infs.

EDIT: the Julia documentation mentions that `@fastmath` corresponds to the `fast-math` option of clang which [in its documentation](https://clang.llvm.org/docs/UsersManual.html#controlling-floating-point-behavior) is pretty clear about `NaN` and `+-Inf` as well:

> Allow floating-point optimizations that assume arguments and results are not NaNs or ±Inf.

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 10, 2021, 4:25pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/11 "2021-02-10T16:25:09Z")

</div>

thanks.

but I think your earlier statement is much more concise and clear. It should be put in official documentation…

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [February 10, 2021, 4:39pm UTC](https://discourse.julialang.org/t/bugs-in-isfinite-isnan-and-isinf/55005/12 "2021-02-10T16:39:37Z")

</div>

I just noticed that if you click on [LLVM Fast-Math flags](http://llvm.org/docs/LangRef.html#fast-math-flags), which is prominently mentioned in the [`@fastmath` documentation](https://docs.julialang.org/en/v1/base/math/#Base.FastMath.@fastmath), the first two points are

> [@](#):
>
> `nnan`
> 
> No NaNs - Allow optimizations to assume the arguments and result are not NaN. If an argument is a nan, or the result would be a nan, it produces a [poison value](https://llvm.org/docs/LangRef.html#poisonvalues) instead.
> 
> `ninf`
> 
> No Infs - Allow optimizations to assume the arguments and result are not +/-Inf. If an argument is +/-Inf, or the result would be +/-Inf, it produces a [poison value](https://llvm.org/docs/LangRef.html#poisonvalues) instead.
