Keno / SIUnits.jl

Efficient unit-checked computation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How should `NaN` values be treated?

tomasaschan opened this issue · comments

Does it make sense to preserve unit information for NaN values?

I would assume that NaN values "poison" any other numbers in calculations as usual, but it turns out I'm wrong - e.g. multiplication by NaN does preserve units. Currently,

julia> NaN * 1kg
NaN kg

I think it would make more sense if

julia> NaN * 1kg
NaN

Is this possible without violating type stability?

I don't think it is possible to do this in a type stable way. The other consideration is that the unit computation should really be independent of whatever the underlying type does (and ideally happen in the type system), so I think the first version is better.

Yeah, you're probably right.

However, it sort of bugs me that it's shown as NaN kg rather than NaN - even if the calculations of units are done independently of value, maybe it's possible to show NaN values differently? Basically, I'm suggesting changing show(::SIQuantity) to

function show{T,m,kg,s,A,K,mol,cd}(io::IO,x::SIQuantity{T,m,kg,s,A,K,mol,cd})
    show(io,x.val)
    if !isnan(x.val)
        print(io," ")
        show(io,unit(x))
    end
end

-1 – NaN is just a floating-point value and special-casing how you show it seems confusing.

Oh well - I guess my opinion is just the odd one in this case. I'll have to live with it =)