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

BUGFIX avoid silent overflow in lcm least common multiple function #33926

Merged
merged 7 commits into from
Dec 12, 2019
Merged

BUGFIX avoid silent overflow in lcm least common multiple function #33926

merged 7 commits into from
Dec 12, 2019

Conversation

KlausC
Copy link
Contributor

@KlausC KlausC commented Nov 23, 2019

Solves #33911 .
Define new functions checked_lcm and widelcm analogously to checked_mul and widemul.

The behaviour of lcm is not changed, and the user can switch to the appropriate variant at his own discretion.

@StefanKarpinski
Copy link
Member

The lcm function is so costly that making it checked by default seems reasonable to me. Similarly, I feel like explicitly widening before calling lcm would be more composable.

@KlausC
Copy link
Contributor Author

KlausC commented Nov 27, 2019

So it would be ok to change lcm by using checked_mul internally and neither introduce widelcm nor checked_lcm.
To be honest, I think that is the best fix, but I wanted to avoid the "checking is too expensive" - discussion.

@StefanKarpinski
Copy link
Member

It's all about context: checking is too expensive when you're doing i + j but when you're doing lcm(i, j) that's a totally different story—that's already a very expensive operation.

@KlausC
Copy link
Contributor Author

KlausC commented Nov 29, 2019

As you can see, I retired the new functions and added the checked_mul to lcm. The widelcm function is probably not worth the effort. So what remains is the fix of the silent error in lcm.

@KlausC KlausC changed the title new functions widelcm and checked_lcm for least common multiple BUGFIX avoid silent overflow in lcm least common multiple function Nov 29, 2019
test/checked.jl Outdated
@@ -166,6 +166,7 @@ import Base: checked_abs, checked_neg, checked_add, checked_sub, checked_mul,
@test checked_cld(typemin(T), T(1)) === typemin(T)
@test_throws DivideError checked_cld(typemin(T), T(0))
@test_throws DivideError checked_cld(typemin(T), T(-1))

Copy link
Member

Choose a reason for hiding this comment

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

I think you can revert the changes to this file now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@StefanKarpinski
Copy link
Member

Well, that's certainly a much simpler fix now 😁 👍 💯

@KlausC
Copy link
Contributor Author

KlausC commented Nov 30, 2019

The last change fixes a gotcha in twiceprecision.jl, where lcm is expected to produce incorrect results (and not to throw an overflow exception).
A test case failed for this reason on 32-bit machines.
No other such uses of lcm were found in base or stdlib sources.

@KlausC
Copy link
Contributor Author

KlausC commented Dec 12, 2019

is the being forgotten?

@StefanKarpinski
Copy link
Member

Thanks for the reminder!

@StefanKarpinski StefanKarpinski merged commit cbdb321 into JuliaLang:master Dec 12, 2019
KristofferC pushed a commit that referenced this pull request Dec 17, 2019
@KristofferC KristofferC mentioned this pull request Dec 17, 2019
18 tasks
KristofferC pushed a commit that referenced this pull request Feb 20, 2020
@KristofferC KristofferC mentioned this pull request Feb 20, 2020
56 tasks
KristofferC pushed a commit that referenced this pull request Feb 20, 2020
KristofferC pushed a commit that referenced this pull request Feb 20, 2020
KristofferC pushed a commit that referenced this pull request Feb 22, 2020
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.

2 participants