Skip to content

Length 0 checks should come after checking for mismatched sizes #336

Closed

Description

@syclik

Summary:

Bug in checking for length 0 vectorized arguments.

Description:

This looks like a bug in the vectorized prob functions (possibly just the cdf / ccdfs). It'll looks something like:

// Size checks
if ( !( stan::length(y) && stan::length(mu) && stan::length(sigma) ) )
  return 0.0;

This will not check that the dimensions are actually ok. It'll return 0 if any are length 0.

Bob writes in this pull request comment:

I'd also rather remove all those redundant spaces. I also
prefer tighter negations for less nesting and to make it clear
that if any of the lengths is zero, the result should be zero.
So I'd write that as:

if (!length(y) || !length(mu) || !length(sigma)) return 0;

And that test should go after the mismatched size tests, which
would indicate a bug, right?

Additional Information:

Provide any additional information here.

Current Version:

v2.11.0

Activity

  1. bob-carpenter commented on Aug 18, 2016

    @bob-carpenter
    Member

    I also think these are clearer as

    if (!stan::length(y) || !stan::length(mu) || !stan::length(sigma))
      return 0;
    

    because the nesting is less deep and it's clearer that if any of them are length zero, we return 0 (don't need that 0.0).

    We should also make sure not to allocate a variable if we're going to just return constant 0 because it can cause autodiff overhead.

  2. added this to the milestone on Aug 18, 2016
  3. syclik commented on Nov 28, 2016

    @syclik
    MemberAuthor

    This is easy, but it'll need to wait until #320 is finished or else we'll end up with lots of merge conflicts.

  4. modified the milestones: , on Apr 3, 2017
  5. self-assigned this
    on Apr 3, 2017
  6. modified the milestones: 2.16.0, on Jul 5, 2017
  7. modified the milestones: 2.17.0, on Sep 6, 2017
  8. modified the milestones: 2.18.0, on Feb 13, 2018
  9. modified the milestones: 2.19.0, 2.19.0++ on Mar 18, 2019
  10. rok-cesnovar commented on May 7, 2019

    @rok-cesnovar
    Member

    Closed via #1207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions