Skip to content
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

VIP: epsilon(decimal) builtin for decimal types #2992

Closed
charles-cooper opened this issue Jul 26, 2022 · 2 comments · Fixed by #3057
Closed

VIP: epsilon(decimal) builtin for decimal types #2992

charles-cooper opened this issue Jul 26, 2022 · 2 comments · Fixed by #3057
Labels
Easy Pickings Used to denote issues that should be easy to implement help wanted VIP: Approved VIP Approved

Comments

@charles-cooper
Copy link
Member

Simple Summary

Add an epsilon() builtin which returns the smallest non-zero value for a decimal type.

Motivation

Similar to #1923, it is nice to not have to type out these special constants (as is done for instance here: https://github.com/vyperlang/vyper/pull/2935/files#diff-86a0306fe1e101309bd0992f2780ec4f0c3dd319940131270904157845c9d9beR79)

Specification

Add an epsilon() builtin which takes a decimal typedef as its argument, and returns the smallest non-zero value for that type. Currently as we only have one decimal type, this would be epsilon(decimal) == 1e-10. In general as we add more decimal types, epsilon(Decimal[N, ...]) would return 1 / 10**N. This is already in the typeinfo for decimals, it's just a matter of exposing as a builtin

@property
def epsilon(self) -> Decimal:
return 1 / Decimal(self.divisor)

Copyright

Copyright and related rights waived via CC0

@charles-cooper charles-cooper changed the title VIP: epsilon(decimal) for decimal types VIP: epsilon(decimal) builtin for decimal types Jul 26, 2022
@charles-cooper charles-cooper added the VIP: Discussion Used to denote VIPs and more complex issues that are waiting discussion in a meeting label Jul 29, 2022
@charles-cooper
Copy link
Member Author

note: consider for future VIP to expose a scale(decimal) builtin which returns 1 / epsilon(decimal). This could be useful for instance for scaling raw integers for manual implementations of fixed point math.

@charles-cooper
Copy link
Member Author

meeting notes: approved

@charles-cooper charles-cooper added VIP: Approved VIP Approved help wanted Easy Pickings Used to denote issues that should be easy to implement and removed VIP: Discussion Used to denote VIPs and more complex issues that are waiting discussion in a meeting labels Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Easy Pickings Used to denote issues that should be easy to implement help wanted VIP: Approved VIP Approved
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant