Keno / SIUnits.jl

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

Unit'd zero? #85

Closed ChrisRackauckas closed 8 years ago

ChrisRackauckas commented 8 years ago

Hey, I am updating DifferentialEquations.jl to have full compatibility with SIUnits.jl, and there's just one little part left to do. The offending code is here:

    for i = 1:stages
      utilde = 0
      for j = 1:i-1
        utilde += A[i,j]*ks[j]
      end
      ks[i] = f(u+Δt*utilde,t+c[i]*Δt)
    end

The problem is utilde doesn't get the appropriate units because it's initialized as utilde =0. I want to set the units of that 0 to the units of u (provided by the user), but I don't want to make an array (and doing something like similar(u)[1] seems like it could cause performance issues).

Any idea on how to do this?

timholy commented 8 years ago
u = A[1,1]*ks[1]
utilde = zero(u+u)

The u+u part is a little pedantic, since all types I'm aware of do not promote with +, but better safe than sorry.

This runs into trouble if A or ks is empty---see the enormous discussions in core julia about promote_op, inference, map, etc.

ChrisRackauckas commented 8 years ago

Works great, thanks. I always knew of zeros but never zero. Made more robust other places in my codes :+1: