-
Notifications
You must be signed in to change notification settings - Fork 635
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
Fix comparison operators for double values in DS #9735
Conversation
hmm, I'm not sure why this ever had a tolerance like this? We should definitely consider though what this might break - it seems to me that it's worth it though because some of these results very confusing to users who use a > 5 digits of precision - this doesn't seem like a very uncommon use case to me regarding optimization or when dealing with parameter spaces over a curve etc. |
Jira task for this? We need a math epic/story to centralize the issues. |
For the time being I have adjusted the tolerance to make it smaller so that floating point comparisons will be possible to higher decimal places in DS however I feel we should make it settable by the user via a UI to give more flexibility and control. Thoughts? |
@aparajit-pratap - have you considered adding an overload like: It does feel odd to have an arbitrary tolerance when considering large scale projects (kilometers) or non geometrical comparisons - fitness functions etc - I am curious what @RyaPorter was trying to do when they ran into this issue? |
One thing to possibly consider; one of the intentions of the Dynamo Primitives package was to be able to "grid" up a double space (in 1D, 2D, and 3D), and once the space is gridded then integers can be used instead of doubles which gets rid of the need for epsilons. Using epsilons is necessary in double comparisons, but will inevitably lead to tolerance creep. Just something to consider if it could be useful. |
|
@gregmarr do you have any inputs on what should be the best way to implement equality comparisons for floating point values in languages? |
A useful reference for anyone who isn't already familiar with it: This might also be relevant: Doing equality tests on IEEE floating point numbers pretty much never results in what the user intends. The But the complaint in #9733 is about the |
@Dewb in this case it appears the |
@mjkkirschner where is that in the code? |
Ah, I see:
The For In C-like languages, the convention is that a method named |
@aparajit-pratap This is the behavior that I would expect in an environment that uses IEEE floats. You'll observe the same results in C#, Javascript, Go, and Python. |
@Dewb so in that case are you suggesting that it's okay to implement the Equals method in:
lhs.Equals(rhs) ?
|
No, any floating-point equality method that uses a tolerance value should be implemented that way (comparing the absolute value of the difference to the tolerance value.) I'm saying that any methods implementing less than or greater than on floating point values should use either the built-in operators or the built-in |
@Dewb I got your point on removing the Equals check from the From your comment about languages like C# I understood that for the equality check it is okay to use the underlying equality of the language we're implementing with (C# in this case) and we could leave it up to users to do "comparing the absolute value of the difference to the tolerance value" for floating point values in their graphs themselves OR have a separate node that expects an epsilon input additionally. On the other hand if we wish to continue to have the tolerance check implicit in the node, the question is on what basis do we select the tolerance. The current tolerance value of Also in TS the equality operator simply uses the equality check of the language (Go): https://git.autodesk.com/aecgd/thustrm/blob/c015d689975b9e9a99711ca2f955eb82962d8c93/intrinsics/intrinsics.go#L845 |
It looks like the only difference between Double.CompareTo vs. the builtin |
I have left the |
@aparajit-pratap lets just change the name of this PR (since it address comparison operators only) and then LGTM. |
one more request - is there a test which checks the operators work for higher precision doubles (over e^-5)? |
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.
My comments earlier were a little meandering. It distills down to one rule: we shouldn't be in the business of re-implementing the logic of any floating-point operations. We should always use the standard operators (or .CompareTo
when we need the combined comparison operation.)
The only thing we should offer to augment that featureset is an explicitly toleranced "sufficiently close" or "sufficiently different" method which should be distinct from the ==
operator.
What we do in the node library is a slightly different question, but script should definitely follow the expectations and behavior for IEEE doubles.
Sorry, I didn't mean to imply that we should prefer CompareTo over operators, but that we should use CompareTo when we wanted the <0 / 0 / >0 combined comparison operation. |
I have now removed any custom implementation for comparison operators except for
I would therefore propose that we do not have a custom implementation for
In addition we could deprecate the existing |
so to sum up the changes so far:
your proposal in the last comment sounds reasonable, but like we talked about earlier I'm not sure how to gauge the number of graphs this would break. I'm still leaning towards adding the Can we think of some way of estimating if the impact of this change will break many existing graphs? |
@mjkkirschner yes, you have summarized these changes correctly.
I would therefore recommend either deferring this whole PR for later or adding the change for equality as well, which is to simply use the platform |
@gregmarr we have a meeting tomorrow on this to discuss pros/cons / breaking changes - do you want to attend? |
* fix for equality for double values in DS * adjust tolerance value, add tests * preserve access to prevent API break * fix < and > operators * use Double.CompareTo, more refactoring * update tests * address review comments * add tests, restore deleted methods as obsolete
Purpose
Fixes equality and comparison operators in DesignScript for double type.
Github issue: #9733
Main points addressed in this PR:
<
,>
,<=
,>=
use the underlying platform C# operators==
and!=
are left unchanged and continue to use a DS level tolerance of 1e-5 ni order to prevent breaking changesDeclarations
Check these if you believe they are true
*.resx
filesReviewers
@mjkkirschner
FYIs
@DynamoDS/dynamo