Skip to content

Generate better start values for nth-root#598

Open
czurnieden wants to merge 2 commits intolibtom:developfrom
czurnieden:faster_nroot
Open

Generate better start values for nth-root#598
czurnieden wants to merge 2 commits intolibtom:developfrom
czurnieden:faster_nroot

Conversation

@czurnieden
Copy link
Copy Markdown
Contributor

This is the implementation I talked about in DCIT/perl-CryptX#123 with @danaj
The runtime is up to standard now.
Needs some work (there is a TODO list at the top of mp_root_n.c, but mostly chores).

@danaj
Copy link
Copy Markdown

danaj commented Apr 24, 2026

I'm getting a failure with a = 2147483645^23 b = 23. The start value t2 = 2147483634 is too low.
[MacOS 26, M1, 64-bit, gcc-15]

@czurnieden
Copy link
Copy Markdown
Contributor Author

I had the angst-allowance at the wrong place. Should be added to the log(a).
Worst thing: had it right before ;-)

The number 2147483645^23 is well chosen: log2(2147483645^23) ~ 712.99999995364 so we got 712.99999995364/23 ~ 30.99999999798, which gets got caught in all of the rounding errors. Adding 1 (one) to o the log gives 713.99999995364/23 ~ 31.043478258852, resulting in a roughly 10% higher start-value in theory and is also larger than the actual root. 1/2should do it, too

I also found an nasty little bug if root^base == input, see #599

Thanks for taking a look!

@danaj
Copy link
Copy Markdown

danaj commented Apr 24, 2026

9999999999900000000000000 ^ 1/53 is coming into the loop with t2 = 2. We used to have the comment "Start value must be larger than root" and this isn't true since 2 is the result we're looking for. It doesn't seem to be working right, but with t2=3 it's fine. Sorry for not triaging past that.

@czurnieden
Copy link
Copy Markdown
Contributor Author

@danaj

Sorry for not triaging past that

Nah, no reason, I wrote most of it, so it's all on me ;-)

That seems to have been fixed by #599. Will put it in this PR now.

@czurnieden
Copy link
Copy Markdown
Contributor Author

Should work now. (famous last words)
Which leads to the question: worth it?

 - Implementation of a fixed point exp2 function to
   get better start-values
 - fixed treatment of perfect powers
 - fixed 0^(1/x) == 0 with x != 0
 - added test for b == 0 (division by zero)
@czurnieden
Copy link
Copy Markdown
Contributor Author

Could it be that I was a little bit behind? ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants