Skip to content

Round sqrt correctly in the current rounding mode - #1463

Merged
mborland merged 1 commit into
boostorg:developfrom
ibmibmibm:worktree-sqrt-nearest
Sep 21, 2026
Merged

mborland merged 1 commit into
boostorg:developfrom
ibmibmibm:worktree-sqrt-nearest

Conversation

@ibmibmibm

Copy link
Copy Markdown
Contributor
  • IEEE 754-2019 4.3 and 5.4.1: sqrt gives the infinitely precise root
    rounded in the current mode. The 32 and 64 bit kernels truncated the
    integer root, an odd exponent multiplied the root by a rounded sqrt(10)
    for the three sizes, and no kernel read the mode.
  • For an odd exponent the three kernels take the root of 10 * gx, so the
    result is rounded once. The estimate is scaled by an integer sqrt(10),
    and the Newton steps correct it. The multiply by sqrt10_v goes away.
  • A new helper reads the mode, and the final step of each kernel keeps
    the floor, steps up on a remainder, or steps up past the half point.
  • The step down to the floor is a loop. One Newton step can land two
    above the floor when the estimate is off and the root ends near .999.
  • An odd power of ten takes the kernel path too. The old fast path gave
    sqrt(0.1) one step low, and test_sqrt.cpp expected that value.
  • A new test compares sqrt against the floor and the ceiling of the root
    for the six types in the five modes, with a sibling under the
    compile-time upward mode.

Fixes #1462

- IEEE 754-2019 4.3 and 5.4.1: sqrt gives the infinitely precise root
  rounded in the current mode. The 32 and 64 bit kernels truncated the
  integer root, an odd exponent multiplied the root by a rounded sqrt(10)
  for the three sizes, and no kernel read the mode.
- For an odd exponent the three kernels take the root of 10 * gx, so the
  result is rounded once. The estimate is scaled by an integer sqrt(10),
  and the Newton steps correct it. The multiply by sqrt10_v goes away.
- A new helper reads the mode, and the final step of each kernel keeps
  the floor, steps up on a remainder, or steps up past the half point.
- The step down to the floor is a loop. One Newton step can land two
  above the floor when the estimate is off and the root ends near .999.
- An odd power of ten takes the kernel path too. The old fast path gave
  sqrt(0.1) one step low, and test_sqrt.cpp expected that value.
- A new test compares sqrt against the floor and the ceiling of the root
  for the six types in the five modes, with a sibling under the
  compile-time upward mode.

Fixes boostorg#1462
@ibmibmibm
ibmibmibm marked this pull request as ready for review September 21, 2026 13:03

@mborland mborland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks as always

@mborland
mborland merged commit 380e799 into boostorg:develop Sep 21, 2026
65 of 82 checks passed
@ibmibmibm
ibmibmibm deleted the worktree-sqrt-nearest branch September 21, 2026 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqrt is not correctly rounded, in the default mode or in the directed modes

2 participants