Skip to content

feat: add Sqrt and SqrtRound - #465

Merged
mwoss merged 1 commit into
masterfrom
feat/sqrt
Oct 16, 2026
Merged

mwoss merged 1 commit into
masterfrom
feat/sqrt

Conversation

@mwoss

@mwoss mwoss commented Oct 16, 2026

Copy link
Copy Markdown
Member

Adds Sqrt and SqrtRound.

  • SqrtRound(places) returns the square root rounded half away from zero to places digits after the decimal point, like DivRound. The result is always correctly rounded.
  • Sqrt() uses DivisionPrecision, like Div.
  • Both return an error for negative numbers instead of panicking.

It runs big.Int.Sqrt on the scaled coefficient and rounds once, so there are no loops in our code. That's about 4–12x faster than going through PowWithPrecision(0.5), for example 0.25 µs instead of 3.1 µs for sqrt(2).

Checked against big.Float on 30k cases, plus a property test in the repo. Builds on Go 1.10.

Replaces #393, #130 and #112. #130 and #112 give wrong digits, and #393 panics on negative numbers and is slower.

@mwoss
mwoss merged commit 2d97fa9 into master Oct 16, 2026
7 checks passed
@mwoss
mwoss deleted the feat/sqrt branch October 16, 2026 16:21
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.

1 participant