I tried using std::copysign to set an int with the sign of another.
int translate = getTranslate();
int offset = getOffset();
translate = std::copysign(offset, translate)
But copysign<int, int> returns a double.
How should I use it better?
Or what should I use instead?
At first I was a bit confused about what std::copysign is actually good for, but the note on cppreference gives a reasonable motivation:
std::copysignis the only portable way to manipulate the sign of aNaNvalue (to examine the sign of aNaN,signbitmay also be used)
Take this plus the fact that integers cannot be NaN and it makes sense that there is no std::copysign for int. On the other hand, a std::copysign<int,int> returning an int would come in handy in generic code, where you want it to work for double as well as for int. For this I would perhaps write my own wrapper, starting from a
template <typename T>
T my_copy_sign(const T& t, const T& s);
and then specialize it accordingly.
PS: If you are only working on int, you probably dont need std::copysign in the first place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With