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

fix[lang]: define rounding mode for sqrt #4486

Merged
merged 7 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/built-in-functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ Math

.. py:function:: sqrt(d: decimal) -> decimal

Return the square root of the provided decimal number, using the Babylonian square root algorithm.
Return the square root of the provided decimal number, using the Babylonian square root algorithm. The rounding mode is to round down to the nearest epsilon. For instance, ``sqrt(0.9999999998) == 0.9999999998``.

.. code-block:: vyper

Expand Down
4 changes: 4 additions & 0 deletions tests/functional/codegen/types/numbers/test_sqrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ def test_sqrt_bounds(sqrt_contract, value):
)
@hypothesis.example(value=Decimal(SizeLimits.MAX_INT128))
@hypothesis.example(value=Decimal(0))
# cf. GHSA-2p94-8669-xg86 for the following three examples:
@hypothesis.example(value=Decimal("0.9999999998"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please leave a link to the advisory to explain the values GHSA-2p94-8669-xg86

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hypothesis.example(value=Decimal("0.9999999997"))
@hypothesis.example(value=Decimal("1.1000000000"))
def test_sqrt_valid_range(sqrt_contract, value):
vyper_sqrt = sqrt_contract.test(decimal_to_int(value))
actual_sqrt = decimal_sqrt(value)
Expand Down
3 changes: 3 additions & 0 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,9 @@ def build_IR(self, expr, args, kwargs, context):
break
y = z
z = (x / z + z) / 2.0

if y < z:
z = y
"""

x_type = DecimalT()
Expand Down