-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SolveNormalizedCubic fix to return proper real root #224
SolveNormalizedCubic fix to return proper real root #224
Conversation
|
Signed-off-by: Piotr Barejko <piotrbarejko@protonmail.com>
9e487eb
to
f912b0c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for figuring this out. One small suggestion on copysign
src/Imath/ImathRoots.h
Outdated
if (D > 0) | ||
{ | ||
auto real_root = [] (T a, T x) -> T { | ||
T sign = a < T (0) ? T (-1) : T (1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
T sign = a < T (0) ? T (-1) : T (1); | |
T sign = std::copysign(T(1), a); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion @meshula.
Also, I have a question, I didn't notice but in the declaration of the function here:
https://github.com/AcademySoftwareFoundation/Imath/blob/master/src/Imath/ImathRoots.h#L67
IMATH_HOSTDEVICE
is used. Does it mean we can't use std::
features? I guess code has to compile as cuda code too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the delay, and thank you for the fix! I'm not entirely sure about the complete details of compatibility between cuda and std, but I'm pretty sure simple inline std:: functions are acceptable.
use copy sign Co-authored-by: Nick Porcino <meshula@hotmail.com> Signed-off-by: Piotr Barejko <piotrbarejko@protonmail.com>
f3a6075
to
e2fe4b7
Compare
…oundation#224) * SolveNormalizedCubic fix to return proper real root Signed-off-by: Piotr Barejko <piotrbarejko@protonmail.com> * Update src/Imath/ImathRoots.h use copy sign Co-authored-by: Nick Porcino <meshula@hotmail.com> Signed-off-by: Piotr Barejko <piotrbarejko@protonmail.com> Co-authored-by: Nick Porcino <meshula@hotmail.com>
* SolveNormalizedCubic fix to return proper real root Signed-off-by: Piotr Barejko <piotrbarejko@protonmail.com> * Update src/Imath/ImathRoots.h use copy sign Co-authored-by: Nick Porcino <meshula@hotmail.com> Signed-off-by: Piotr Barejko <piotrbarejko@protonmail.com> Co-authored-by: Nick Porcino <meshula@hotmail.com>
In some cases
solveNormalizedCubic
can return a real value from principled root that is a complex root. This happens in #86 for example.In case of discriminant
D > 0
there must be one real and two complex roots. Given formula:-q/2 + sqrt(D)
might be a negative number. Instead of searching thoughy0
,y1
,y2
to find the real root, we can solve it as follows: