-
-
Notifications
You must be signed in to change notification settings - Fork 793
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
feat: add epsilon builtin #3057
Conversation
vyper/builtin_functions/functions.py
Outdated
# this check seems redundant, but sets a pattern to be followed | ||
# when new decimal types are created | ||
if isinstance(input_type, DecimalDefinition): | ||
val = self._eval_decimal(input_type) |
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.
i think you can just inline _eval_decimal
here. the use of that pattern elsewhere is to deal with some kind of inheritance, not needed here.
vyper/builtin_functions/functions.py
Outdated
@@ -120,6 +121,22 @@ | |||
SHA256_BASE_GAS = 60 | |||
SHA256_PER_WORD_GAS = 12 | |||
|
|||
class FoldedFunction(BuiltinFunction): | |||
# Base class for nodes which should always be folded | |||
_id = "" |
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.
probably can leave this out -- that way if the subclass does not supply _id
, accessing the attribute will fail explicitly rather than silently
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.
Good point!
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.
very nice work! left a couple comments
Codecov Report
@@ Coverage Diff @@
## master #3057 +/- ##
==========================================
- Coverage 88.34% 86.88% -1.46%
==========================================
Files 97 97
Lines 10963 10965 +2
Branches 2590 2528 -62
==========================================
- Hits 9685 9527 -158
- Misses 830 964 +134
- Partials 448 474 +26
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
What I did
Added an epsilon() builtin which returns the smallest non-zero value for a decimal type.
Closes #2992
How I did it
In
vyper/builtin_functions/functions.py
I implemented a new classEpsilon
. The function that this class represents should always be folded.Instead of adding to the duplication between MinMaxValue and MethodID, I created a new base class
FoldedFunction
which contains all the logic for enforcing a function should always be folded.In
Epsilon
'sevaluate
function, we check the type that was passed in, ensure it is aFixedAbstractType
(which is the parent class for Decimal and any other decimal classes to be supported later).Then we call the appropriate
_eval_<type>
function after checking which type was passed in. This currently only supports "decimal" but sets up a pattern for adding support for other decimal types in the future.How to verify it
Run the new test, which checks a compiled contract's return value against a folded node.
Commit message
Implement
epsilon
for decimal typesDescription for the changelog
Cute Animal Picture