as_bool? anyone have a spec on this function?

8 views
Skip to first unread message

Daniel Lee

unread,
Aug 11, 2014, 1:38:04 PM8/11/14
to stan...@googlegroups.com
Bob, I think you're probably best suited to answer this one.

I'm confused as to what stan::math::as_bool() does.
- For doubles, it returns a boolean true if the value is not zero, false otherwise. There's a test in there that checks that an NaN input returns true.
- For integers, it returns an integer that's equal to the input.

Was this an implementation of some external function? If not, are we ok with changing the spec so NaN input either returns NaN or (preferably) throws an exception? I'm a little uncomfortable with our functions swallowing NaNs.



Daniel

Bob Carpenter

unread,
Aug 11, 2014, 2:08:04 PM8/11/14
to stan...@googlegroups.com


On Aug 11, 2014, at 1:38 PM, Daniel Lee <bea...@alum.mit.edu> wrote:

> Bob, I think you're probably best suited to answer this one.
>
> I'm confused as to what stan::math::as_bool() does.

It's only used in the while loops and if loop. It's needed
to convert var to boolean, and the behavior for non-bool
should match static_cast<bool>().

There are two fixes we need for that.

1. correct behavior for int inputs (0 or 1)

2. return type bool rather than int


> - For doubles, it returns a boolean true if the value is not zero, false otherwise. There's a test in there that checks that an NaN input returns true.

That's the correct behavior. This code

double nand = std::numeric_limits<double>::quiet_NaN();

std::cout << "nand=" << nand << std::endl;
std::cout << "static_cast<bool>(nand)=" << static_cast<bool>(nand) << std::endl;

if (nand)
std::cout << "true" << std::endl;
else
std::cout << "false" << std::endl;

std::cout << "1 > 4 = " << (1 > 4) << std::endl;

std::cout << "static_cast<bool>(15) = " << static_cast<bool>(15) << std::endl;


prints out:


nand=nan
static_cast<bool>(nand)=1
true
1 > 4 = 0
static_cast<bool>(15) = 1


Now it'd be possible to have it throw, but that's not the way C++
works as is, and I was trying to follow C++ behavior as much as
possible for both ease of understanding and simplicity in coding.

Should this throw? I can't imagine passing in nan being anything
other than an error.

> - For integers, it returns an integer that's equal to the input.

That'll work for conditionals, but it should return 0 or 1 for integers!
If you replace it with static_cast<bool>(), that's what it'll do.

> Was this an implementation of some external function? If not, are we ok with changing the spec so NaN input either returns NaN or (preferably) throws an exception? I'm a little uncomfortable with our functions swallowing NaNs.

No, there's no external version of this anywhere.

It could throw an exception if you want --- that'd be a reason for
keeping it.

Sorry I was so sloppy doing this the first time? Can I blame
the code reviewer? :-)

- Bob

Daniel Lee

unread,
Aug 11, 2014, 2:19:02 PM8/11/14
to stan...@googlegroups.com
Thanks for the explanation! That clears up a lot of my confusion.


I think it should throw. I really don't want to be swallowing NaNs. C++'s bool type doesn't have an NaN, so we can't return one.


> - For integers, it returns an integer that's equal to the input.

That'll work for conditionals, but it should return 0 or 1 for integers!
If you replace it with static_cast<bool>(), that's what it'll do.

Ok, we could reimplement it to return bool.
 

> Was this an implementation of some external function? If not, are we ok with changing the spec so NaN input either returns NaN or (preferably) throws an exception? I'm a little uncomfortable with our functions swallowing NaNs.

No, there's no external version of this anywhere.

It could throw an exception if you want --- that'd be a reason for
keeping it.

Sorry I was so sloppy doing this the first time?  Can I blame
the code reviewer? :-)

Ha. No blaming anyone, unless it makes you sleep better at night. Then blame whomever you want.
 
Reply all
Reply to author
Forward
0 new messages