Skip to content

Add portable formulas for fenwick calculations #1298

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

Closed
wants to merge 2 commits into from

Conversation

JJCUBER
Copy link
Contributor

@JJCUBER JJCUBER commented Jun 27, 2024

Alongside the equations provided to calculate g(i) and h(i), I provided some alternative ones which are more portable.

(Prior to C++20, it wasn't guaranteed that signed integers would be represented as two's complement, and the formulas I added do not rely on this, unlike the formulas previously mentioned in this Fenwick tree article. One exception would be when i = 0, but 0 & x, where x is any integer, is always 0.)

I believe that in languages such as C, it is still not guaranteed to be two's complement, so including this in the article makes it more language/compiler-agnostic.

Alongside the equations provided to calculate g(i) and h(i), I provided some alternative ones which are more portable.

(Prior to C++20, it wasn't guaranteed that signed integers would be represented as two's complement, and the formulas I added do not rely on this, unlike the formulas previously mentioned in this Fenwick tree article.  One exception would be when i = 0, but 0 & x, where x is any integer, is always 0.)
Copy link
Contributor

Visit the preview URL for this PR (for commit b1e3837):

https://cp-algorithms--preview-1298-i1gs0veh.web.app

(expires 2024-07-04T01:12:53.941446079Z)

@adamant-pwn
Copy link
Member

Hi there! Thanks for the pull request, but I think the article already contains some portable version? Specifically g(i) = i & (i + 1) and h(j) = j | (j + 1), as defined in upper sections. Do we really want to introduce a third way to do this?

@JJCUBER
Copy link
Contributor Author

JJCUBER commented Jun 28, 2024

That's with a zero-based implementation, but I see your point. Personally, I feel that it should still at least be mentioned how the one-based implementation provided isn't portable (whether it's inline, as its own section, as a footnote, etc), though that's just my opinion. You can close the pr if you don't think it's necessary.

@jxu
Copy link
Contributor

jxu commented Oct 14, 2024

Alongside the equations provided to calculate g(i) and h(i), I provided some alternative ones which are more portable.

(Prior to C++20, it wasn't guaranteed that signed integers would be represented as two's complement, and the formulas I added do not rely on this, unlike the formulas previously mentioned in this Fenwick tree article. One exception would be when i = 0, but 0 & x, where x is any integer, is always 0.)

I believe that in languages such as C, it is still not guaranteed to be two's complement, so including this in the article makes it more language/compiler-agnostic.

Any computer from this century uses 2s complement, and every code judge platform does. We can mention that negation works because of 2s complement, but providing alternatives for whatever ancient microprocessor still not using 2s complement is just too pedantic.

@adamant-pwn adamant-pwn deleted the branch cp-algorithms:main October 14, 2024 18:52
@adamant-pwn adamant-pwn reopened this Oct 14, 2024
@adamant-pwn adamant-pwn changed the base branch from master to main October 14, 2024 19:19
Copy link
Contributor

Preview the changes for PR #1298 at https://gh.cp-algorithms.com/1298/ (current version: 3d0d52e).

@jxu jxu closed this Jan 6, 2025
github-actions bot added a commit that referenced this pull request Jan 6, 2025
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.

3 participants