8000 log compute - taylor series by hnb22 · Pull Request #957 · mpmath/mpmath · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

log compute - taylor series #957

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

log compute - taylor series #957

wants to merge 2 commits into from

Conversation

hnb22
Copy link
@hnb22 hnb22 commented May 13, 2025

Comments in libelefun.py, new tests.

-> Old PR accidently removed ctx_mp file, different refactor changes.

Copy link
Collaborator
@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

Old PR accidently removed ctx_mp file

Next time please just do "git revert" for bad commit.

I worry, that you are committed fix from a different GH account. Are you code author?

Comment on lines +212 to +214
# Taylor series
assert (log(0.99999)).ae(-1.0000050000287824e-5)
assert (log(1.00001)).ae(9.9999500003988414e-6)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see changes.

Did you realize what's doing your code?

Copy link
Author

Choose a reason for hiding this comment

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

With default precision of 53, I tested locally for log(0.99999) and log(1.00001) and it ran through to do taylor series compute. The code checks for tfixed (distance from 1) and compares to a threshold scaled to a wp that is 0.001 in floating point?

Copy link
Collaborator

Choose a reason for hiding this comment

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

With default precision of 53, I tested locally for log(0.99999) and log(1.00001) and it ran through to do taylor series compute.

Great, you have found values, that trigger you code path.

But results are same wrt the master (or not?). Could you argue why we need this?

The code checks for tfixed (distance from 1) and compares to a threshold scaled to a wp that is 0.001 in floating point?

Did you ask me?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# Taylor series
assert (log(0.99999)).ae(-1.0000050000287824e-5)
assert (log(1.00001)).ae(9.9999500003988414e-6)
# Taylor series
assert log(0.99999).ae(-1.0000050000287824e-5)
assert log(1.00001).ae(9.9999500003988414e-6)

Copy link
Author

Choose a reason for hiding this comment

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

We may not need it I guess. We wouldn't need to compute as many taylor series terms as the general case of code provided later.

Depending on the threshold, I wasn't sure what was close to 1 enough. I was looking for feedback.

if (tfixed <= threshold):
s = log_taylor(to_fixed(x, wp), wp)
sb = s.bit_length()
rman = s << (sb)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's happens here, for example? Why mantissa corrected this way?

Copy link
Author

Choose a reason for hiding this comment

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

Constructing the mantissa from the fixed point need to be normalize it with MSB. Which is used using bit length and shifted left by this amount to get no leading zeros. Also, then constructing the exponent to input into creating a mpf. rman is constructed using sb, and to get floating point representation, we also need the working precision wp, and by exponent laws of base 2 its wp+sb.

Still learning, this is new :)

Copy link
Collaborator
@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

Sorry, I still think you shouldn't post code on behalf of some other person.

BTW, please also don't use the master branch of your fork to create pr. Put your code in some branch, else you can't use your fork after merging.

Comment on lines +212 to +214
# Taylor series
assert (log(0.99999)).ae(-1.0000050000287824e-5)
assert (log(1.00001)).ae(9.9999500003988414e-6)
Copy link
Collaborator

Choose a reason for hiding this comment

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

With default precision of 53, I tested locally for log(0.99999) and log(1.00001) and it ran through to do taylor series compute.

Great, you have found values, that trigger you code path.

But results are same wrt the master (or not?). Could you argue why we need this?

The code checks for tfixed (distance from 1) and compares to a threshold scaled to a wp that is 0.001 in floating point?

Did you ask me?

Comment on lines +212 to +214
# Taylor series
assert (log(0.99999)).ae(-1.0000050000287824e-5)
assert (log(1.00001)).ae(9.9999500003988414e-6)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# Taylor series
assert (log(0.99999)).ae(-1.0000050000287824e-5)
assert (log(1.00001)).ae(9.9999500003988414e-6)
# Taylor series
assert log(0.99999).ae(-1.0000050000287824e-5)
assert log(1.00001).ae(9.9999500003988414e-6)

Comment on lines +698 to +699
# if close enough to 1, use Taylor series
# since Taylor series converges rapidly
Copy link
Collaborator

Choose a reason for hiding this comment

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

You removed AGM mention. Why? Are you know what's it about?

Copy link
Author

Choose a reason for hiding this comment

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

I know it's used for high precision due to its convergence properties which contrasts with taylor series depending on x.

Don't know how it would be applied to current code.

else:
tfixed = to_fixed(x, wp) - (MPZ_ONE << wp)
threshold = (1 << wp) // 1000
if (tfixed <= threshold):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (tfixed <= threshold):
if tfixed <= threshold:

tfixed = (MPZ_ONE << wp) - to_fixed(x,wp)
else:
tfixed = to_fixed(x, 8000 wp) - (MPZ_ONE << wp)
threshold = (1 << wp) // 1000
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why such value?

Copy link
Author

Choose a reason for hiding this comment

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

Not sure here, looking for feedback.

@hnb22
Copy link
Author
hnb22 commented May 15, 2025

Hi, it is my code. If your referring to borjha19, thats an old Github account of mine that I don't use and forgot about. Not sure why its showing up here. If your not referring to that, I don't know what you mean. I wrote this code and it wasn't behalf on anyone.

@skirpichev skirpichev added the pending The issue will be closed if no feedback is provided label Jun 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0