NaN definition for Integer types?

1,334 views
Skip to first unread message

Júlio Hoffimann

unread,
Jul 24, 2015, 1:09:42 PM7/24/15
to julia-users
Hi,

Is there any definition of NaN for Integer types? Let's say I want to create a matrix with some unspecified entries, that is entries marked special. I am using NaN for Float64 but would like to make it work with other types too. Suggestions?

-Júlio

Tamas Papp

unread,
Jul 24, 2015, 1:56:54 PM7/24/15
to julia...@googlegroups.com
Every bit pattern in a Signed or Unsigned represents an integer,
there is no representation for a NaN like in eg Float64 etc. You
would have to use Nullable types, see
https://julia.readthedocs.org/en/latest/manual/types/#nullable-types-representing-missing-values

Best,

Tamas

On Fri, Jul 24 2015, Júlio Hoffimann <julio.h...@gmail.com>
wrote:

Erik Schnetter

unread,
Jul 24, 2015, 1:59:24 PM7/24/15
to julia...@googlegroups.com
On Fri, Jul 24, 2015 at 1:09 PM, Júlio Hoffimann <julio.h...@gmail.com> wrote:
Hi,

Is there any definition of NaN for Integer types? Let's say I want to create a matrix with some unspecified entries, that is entries marked special. I am using NaN for Float64 but would like to make it work with other types too. Suggestions?

There is no pre-defined integer nan equivalent.

Option 1 (fast but unsafe): Pick a special int value that is rarely used, e.g. typemin(Int).
Option 2 (works with all types, including Float64, and is safer): Use a nullable type. This might complicate your code and may slightly increase your memory usage.

-erik

--

Júlio Hoffimann

unread,
Jul 24, 2015, 2:04:33 PM7/24/15
to julia...@googlegroups.com
Thank you all, that is what I thought, I will stick with plain Float64 for now.

-Júlio

Scott Jones

unread,
Jul 24, 2015, 2:42:52 PM7/24/15
to julia-users, julio.h...@gmail.com
Maybe you could make your own 64-bit bitstype, which restricted values to integer values in the range that will fit in a Float64 (around -2^53 to 2^53).  Then you could be sure you didn't lose any information (give an InexactError if a value doesn't fit), and still allow for a NaN value, and fairly efficient computation.

Stefan Karpinski

unread,
Jul 24, 2015, 3:36:03 PM7/24/15
to Julia Users, julio.h...@gmail.com
This is definitely possible, but integer operations will be a bit slower since you'll have to do something like this:

bitstype 64 NullableInt

+(x::NullableInt, y::NullableInt) = ifelse(
        (reinterpret(Int,x) !== typemin(Int)) & (reinterpret(Int,y) !== typemin(Int)),
        reinterpret(NullableInt, reinterpret(Int,x) + reinterpret(Int,y)),
        reinterpret(NullableInt, typemin(Int)),
    )

Fortunately, this is not as bad as it looks, but it's still a lot more work than just doing an integer add instruction.

Júlio Hoffimann

unread,
Jul 24, 2015, 3:39:56 PM7/24/15
to Stefan Karpinski, Julia Users

Thank you all, I'll try to figure out a good solution for my use case.

-Júlio

Sisyphuss

unread,
Jul 24, 2015, 5:54:28 PM7/24/15
to julia-users, tkp...@gmail.com
By the way, is it a v0.4 feature?

Stefan Karpinski

unread,
Jul 24, 2015, 6:31:45 PM7/24/15
to julia...@googlegroups.com
Yes, but available with Compat too.

David Gold

unread,
Jul 24, 2015, 6:40:26 PM7/24/15
to julia-users, ste...@karpinski.org, julio.h...@gmail.com
If you end up trying the `Nullable` route, you might find our in-progress `NullableArrays` package helpful: https://github.com/johnmyleswhite/NullableArrays.jl. Its designed to extend the functionality of the `AbstractArray` interface to a container type specialized to handle `Nullable` entries. I'm currently working on this for the JSoC, so there is a point person for issues and functionality requests.

Note, though, that it's being developed for 0.4.
Reply all
Reply to author
Forward
0 new messages