Keno / SIUnits.jl

Efficient unit-checked computation
Other
70 stars 26 forks source link

How should `NaN` values be treated? #31

Closed tomasaschan closed 10 years ago

tomasaschan commented 10 years ago

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?

Keno commented 10 years ago

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.

tomasaschan commented 10 years ago

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
StefanKarpinski commented 10 years ago

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

tomasaschan commented 10 years ago

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