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

Bug: wrong calculate median function #4754

Open
tiendn opened this issue Oct 25, 2024 · 0 comments · May be fixed by #4753
Open

Bug: wrong calculate median function #4754

tiendn opened this issue Oct 25, 2024 · 0 comments · May be fixed by #4753

Comments

@tiendn
Copy link
Contributor

tiendn commented Oct 25, 2024

Problem

Calculate wrong value median

import {median} from '@hyperlane/utils'

const result = median([1,2,3,4])
return result;
// Expected: 2.5
// Actual: 3.5

Additional context
Add any other context or screenshots about the feature request here.

Solution

Reimplement median function at #4753

Related logics

Fetches the median prioritization fee for transfers of the collateralAddress token

github-merge-queue bot pushed a commit that referenced this issue Oct 27, 2024
…tion (#4753)

### Description
This pull request introduces the following changes to the math utility
functions:
#### Fix Median Calculation:
Corrected the median function to handle even-length arrays properly by
averaging the two middle numbers.

#### Add Unit Tests:
Implemented unit tests for the math utility functions using Chai to
ensure their correctness and reliability.
The functions tested include:
- `median`: Tests for both odd and even-length arrays.
- `sum`: Verifies the sum of an array of numbers.
- `mean`: Checks the calculation of the mean.
- `stdDev`: Validates the standard deviation calculation, including
cases with negative numbers.
- `randomInt`: Ensures the generated random integer falls within the
specified range.
<!--
What's included in this PR?
-->

### Drive-by changes
- Code: Updated the median function in math.ts to correctly calculate
the median for even-length arrays.
- Tests: Added a new test file math.test.ts with comprehensive test
cases for each function.
<!--
Are there any minor or drive-by changes also included?
-->

### Related issues

<!--
- Fixes [#[issue number
here]](#4754)
-->
Fixes
[#[4754]](#4754)

### Backward compatibility
Yes
<!--
Are these changes backward compatible? Are there any infrastructure
implications, e.g. changes that would prohibit deploying older commits
using this infra tooling?

Yes/No
-->

### Testing

- All tests have been executed and passed successfully, confirming the
correctness of the functions.
- Special attention was given to edge cases, such as arrays with
negative numbers and random integer generation.
<!--
What kind of testing have these changes undergone?

None/Manual/Unit Tests
-->
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 a pull request may close this issue.

1 participant