operator bool() and set_zero(): test for zero and set to zero respectively.#39
Conversation
Checking for zero or non-zero and setting to zero are common operations, so fast implementations
| } | ||
|
|
||
| // Useful for fast test of equality to zero. | ||
| operator bool() const |
There was a problem hiding this comment.
This should be explicit when BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS is not set, otherwise use an implicit conversion to an "unmentionable" type, for example multiprecision::number uses:
typedef bool (self_type::*unmentionable_type)()const;
BOOST_MP_FORCEINLINE operator unmentionable_type()const
{
return is_zero() ? 0 : &self_type::is_zero;
}
Which prevents accidental conversion to an arithmetic type.
There was a problem hiding this comment.
Ahh, wow, that's a new one on me. What about naming a function that sets the polynomial to zero? I have used the verb clear but maybe set_zero is more appropriate? This is on the assumption that it is more efficient than assignment from zero, which I assume the compiler is not quite smart enough to optimize away for a class such as polynomial.
There was a problem hiding this comment.
On 12/05/2016 12:08, Jeremy W. Murphy wrote:
In include/boost/math/tools/polynomial.hpp
#39 (comment):@@ -455,6 +454,18 @@ class polynomial :
normalize();
return *this;
}
+
- // Useful for fast test of equality to zero.
- operator bool() const
Ahh, wow, that's a new one on me. What about naming a function that
sets the polynomial to zero? I have used the verb |clear| but maybe
|set_zero| is more appropriate? This is on the assumption that it is
more efficient than assignment from zero, which I assume the compiler
is not quite smart enough to optimize away for a class such as polynomial.
clear() works for me.
|
I still need to update the docs after we settle on the names. |
|
And what about the horrible workaround for gcc 4.6.x? |
|
On 12/05/2016 12:10, Jeremy W. Murphy wrote:
I don't believe that's required in this case - it was the presence of |
# Conflicts: # test/test_polynomial.cpp
|
John, if you're happy with my choice of |
|
Can you mark operator bool in the docs as explicit to match the code? Re the name "set_zero", I confess I'm not overly keen on set_ and get_ prefixes, so I would be inclined to go with either just "zero" or even "clear". Does assigning from a literal zero produce the same result BTW? Thanks for this, John. |
|
I'm definitely not in favour of the object-oriented habit of 'getters and setters' on a per-member basis, but I thought it was OK in this case. I thought "clear" was too tied to the underlying implementation and is not meaningful to a polynomial per se (although that's not a huge sin). "zero" seems ambiguous as to whether it is setting or querying? I don't love "set_zero" but the alternatives all seem more problematic. |
|
OK lets go with what we have and move on, merging to develop... |
Checking for zero or non-zero and setting to zero are common operations, so fast implementations are useful.