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

fix(p/int256): "-0" Output When Calculating Basic Arithmetics #2750

Merged
merged 7 commits into from
Sep 13, 2024

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Sep 2, 2024

Description

  • Added a check to prevent appending a minus sign to zero values. This ensures that "0" always returned for zero values.
  • Fix negative number handling in the Arithmetic function
  • Add divide by zero panic in Div function
Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

@notJoon notJoon requested review from a team as code owners September 2, 2024 05:14
@notJoon notJoon requested review from jaekwon and zivkovicmilos and removed request for a team September 2, 2024 05:14
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Sep 2, 2024
Copy link

codecov bot commented Sep 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.79%. Comparing base (701ac17) to head (4e84f64).
Report is 19 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2750      +/-   ##
==========================================
+ Coverage   60.21%   60.79%   +0.57%     
==========================================
  Files         562      563       +1     
  Lines       75038    75985     +947     
==========================================
+ Hits        45185    46192    +1007     
+ Misses      26476    26398      -78     
- Partials     3377     3395      +18     
Flag Coverage Δ
contribs/gnodev 61.46% <ø> (+0.06%) ⬆️
contribs/gnofaucet 15.31% <ø> (+0.85%) ⬆️
gno.land 67.21% <ø> (+2.64%) ⬆️
gnovm 65.59% <ø> (+1.26%) ⬆️
misc/genstd 80.54% <ø> (ø)
misc/logos 20.23% <ø> (ø)
tm2 62.17% <ø> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@notJoon notJoon changed the title fix(p/int256): "-0" Output in ToString method fix(p/int256): "-0" Output When Adding Two Numbers Sep 2, 2024
@notJoon notJoon changed the title fix(p/int256): "-0" Output When Adding Two Numbers fix(p/int256): "-0" Output When Calculating Basic Arithmetics Sep 2, 2024
Copy link
Contributor

@deelawn deelawn left a comment

Choose a reason for hiding this comment

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

Just one comment. Looks good to me other than that.

examples/gno.land/p/demo/int256/conversion.gno Outdated Show resolved Hide resolved
Copy link
Member

@thehowl thehowl left a comment

Choose a reason for hiding this comment

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

I think there's a problem at the root of this package; which is why is this not using two's complement?

The correct name for this package would actually be int257, because each Int is actually storing 257 bits of information, seeing as we separate the sign from the actual number.

If you were using two's complement, you wouldn't have problems like -0 to begin with, and addition/subtraction would Just Work without using any conditionals. So, if you're looking for improvements on this package, that's one place to look.

Aside from that, approach looks good. I made a suggestion to organize/optimize the result of Cmp.

examples/gno.land/p/demo/int256/arithmetic.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/int256/arithmetic.gno Outdated Show resolved Hide resolved
@notJoon
Copy link
Member Author

notJoon commented Sep 13, 2024

I think there's a problem at the root of this package; which is why is this not using two's complement?

If my recall correctly, this topic came up during the implementation, but we decided to follow another implementation that we were referencing, so that we eneded up setting the neg field instead of two's complement.

I definitely agree that we can eliminate a bunch of codes in current implementation. I'm currently preparing for a improvement of the 256-types, and I'll add this change as part of those modifications.

@thehowl thehowl merged commit 1239f1d into gnolang:master Sep 13, 2024
119 checks passed
@notJoon notJoon deleted the fix-tostring branch September 13, 2024 08:29
ltzmaxwell pushed a commit that referenced this pull request Nov 21, 2024
#2846)

# Description

This PR optimizes the implementation of `int256` type. Key changes
include:

- Changed from storing sign and value separately in the Int256 struct to
an implementation using two's complement method.
- This reduces unnecessary operations and improves overall performance.

## Performance Result

- Basic arithmetic operations (addition, subtraction, etc.): About 3x
performance improvement (based on Go benchmarks, may differ slightly in
gno)
- Division operations: Up to 5x performance decrease compared to the
previous implementation (can be improved by directly manipulating array
fields, but not applied to avoid duplication with p/demo/uint256)

## Additional improvements:
- Increased test coverage to 95%.

**This change is expected to improve performance for most int256
operations. However, please note the performance degradation in division
operations.**

## See Also

#2750 (review)

<details><summary>Contributors' checklist...</summary>

- [X] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Co-authored-by: Morgan <[email protected]>
r3v4s pushed a commit to gnoswap-labs/gno that referenced this pull request Dec 10, 2024
gnolang#2846)

# Description

This PR optimizes the implementation of `int256` type. Key changes
include:

- Changed from storing sign and value separately in the Int256 struct to
an implementation using two's complement method.
- This reduces unnecessary operations and improves overall performance.

## Performance Result

- Basic arithmetic operations (addition, subtraction, etc.): About 3x
performance improvement (based on Go benchmarks, may differ slightly in
gno)
- Division operations: Up to 5x performance decrease compared to the
previous implementation (can be improved by directly manipulating array
fields, but not applied to avoid duplication with p/demo/uint256)

## Additional improvements:
- Increased test coverage to 95%.

**This change is expected to improve performance for most int256
operations. However, please note the performance degradation in division
operations.**

## See Also

gnolang#2750 (review)

<details><summary>Contributors' checklist...</summary>

- [X] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Co-authored-by: Morgan <[email protected]>
albttx pushed a commit that referenced this pull request Jan 10, 2025
#2846)

# Description

This PR optimizes the implementation of `int256` type. Key changes
include:

- Changed from storing sign and value separately in the Int256 struct to
an implementation using two's complement method.
- This reduces unnecessary operations and improves overall performance.

## Performance Result

- Basic arithmetic operations (addition, subtraction, etc.): About 3x
performance improvement (based on Go benchmarks, may differ slightly in
gno)
- Division operations: Up to 5x performance decrease compared to the
previous implementation (can be improved by directly manipulating array
fields, but not applied to avoid duplication with p/demo/uint256)

## Additional improvements:
- Increased test coverage to 95%.

**This change is expected to improve performance for most int256
operations. However, please note the performance degradation in division
operations.**

## See Also

#2750 (review)

<details><summary>Contributors' checklist...</summary>

- [X] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Co-authored-by: Morgan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants