nimble-dev / nimble

The base NIMBLE package for R
http://R-nimble.org
BSD 3-Clause "New" or "Revised" License
160 stars 24 forks source link

unexpected behavior with `nimNumeric` when passed `value` but not `length` #1508

Closed paciorek closed 2 days ago

paciorek commented 3 weeks ago

If we don't provide length, then the default of 0 seems to override the fact that we are providing value:

nf <- nimbleFunction(
    run = function(x=double(1)) {
        out = nimNumeric(value=x)                          # `out` is empty
        out2 = nimNumeric(value=x, length= 4)    # `out2` has the values
        print("out: ", out)
        print("out2: ", out2)
        returnType(double(1))
        return(out)
    })
cnf <- compileNimble(nf)
cnf(rnorm(4))
out: 
out2:   1.36455
-0.524717
 0.113249
  2.03788
numeric(0)

I'm guessing this is not what we want and in fact it's the cause of the floating point exception in issue #1504.

I haven't dived into the details of what is going on.

@perrydv thoughts on what you expect to happen in this case?

perrydv commented 2 weeks ago

@paciorek I get your point. The documentation states that the default value of length is 0, so by not providing it, the length is set to 0. Hence, I think the behavior matches what is documented. It should work to say length = length(x). My inclination is to not change this and consider generalizing it in nCompiler with a toggle to maintain all "classic nimble" behavior such as this.