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

Add test-case for floating-point-and-numerical-precision #37

Closed
aon opened this issue Jun 8, 2023 · 0 comments · Fixed by #46
Closed

Add test-case for floating-point-and-numerical-precision #37

aon opened this issue Jun 8, 2023 · 0 comments · Fixed by #46
Assignees
Labels
feature New feature or request
Milestone

Comments

@aon
Copy link
Contributor

aon commented Jun 8, 2023

Description

Due to the impossibility to represent floating point numbers in Ink, order of multiplications and divisions are important to ensure numerical precision.

Take for example the following function. It is intended to return the percentage of a total profit:

#[ink::message]
pub fn split_profit(uint64 percentage, uint64 total_profit) -> uint64 {
    (percentage / 100) * total_profit
}

The result however, will always be zero, given the integer division percentage / 100 yields zero.

Reordering operations to perform multiplications before divisions is a good way to avoid loss of precision:

#[ink::message]
pub fn split_profit(uint64 percentage, uint64 total_profit) -> uint64 {
    (percentage * total_profit) / 100
}

Acceptance Criteria

  • Add a test-case that shows the vulnerability of floating-point-and-numerical-precision
@aon aon added the feature New feature or request label Jun 8, 2023
@aon aon added this to the detectors milestone Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants