Skip to content

fix: use big.Rat for precise calculation #504

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

Merged
merged 1 commit into from
May 23, 2025
Merged

Conversation

k1LoW
Copy link
Owner

@k1LoW k1LoW commented May 23, 2025

Fix: #503

Summary

This pull request introduces significant changes to improve precision in calculations for coverage, code-to-test ratio, and test execution time by replacing float64 with *big.Rat (arbitrary-precision rational numbers). It also updates the corresponding test cases to accommodate the new precision-based logic. Below is a summary of the most important changes:

Precision Improvements in Calculations:

  • Replaced float64 with *big.Rat for calculations: Updated functions coverageAcceptable, codeToTestRatioAcceptable, and testExecutionTimeAcceptable to use *big.Rat instead of float64 for improved precision in comparisons and calculations. (config/config.go, [1] [2] [3] [4]
  • Introduced big.Rat operations: Added logic to compute differences and convert *big.Rat values to float64 for compatibility in error messages and expressions. (config/config.go, [1] [2] [3]

Test Enhancements:

  • Updated test cases to support *big.Rat: Modified test cases for coverage, code-to-test ratio, and test execution time to include both float64 and *big.Rat inputs, ensuring precision-sensitive scenarios are tested. (config/config_test.go, [1] [2] [3]
  • Added pre-calculated big.Rat values for edge cases: Introduced special big.Rat values (e.g., almostSixty, oneThirdPlusSmall, almostOneMinute) to test scenarios that cannot be represented accurately with float64. (config/config_test.go, [1] [2] [3]

Code Maintenance:

  • Imported math/big: Added the math/big package to support arbitrary-precision rational number operations. (config/config.go, [1]; config/config_test.go, [2]

These changes ensure greater accuracy in calculations and robustness in handling edge cases, particularly for conditions that require high precision.

@k1LoW k1LoW self-assigned this May 23, 2025
@k1LoW k1LoW added the bug Something isn't working label May 23, 2025
@k1LoW k1LoW requested a review from Copilot May 23, 2025 04:26
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR aims to enhance the accuracy of coverage, code-to-test ratio, and test execution time computations by switching from float64 to arbitrary-precision *big.Rat calculations. Key changes include:

  • Replacing float64 values with *big.Rat in evaluation functions.
  • Updating test cases to support both float64 and *big.Rat inputs.
  • Introducing pre-calculated *big.Rat edge-case values and adjusting error messages accordingly.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
config/config_test.go Revised test cases to convert float64 to *big.Rat and added new tests leveraging big.Rat values.
config/config.go Updated functions to use *big.Rat instead of float64, including conversion logic for error messages.
Comments suppressed due to low confidence (1)

config/config.go:365

  • Verify that converting currentF to an int64 when creating a time.Duration does not cause unintended precision loss; consider adding a comment to document that this conversion is acceptable for error messaging.
return fmt.Errorf("test execution time is %v. the condition in the `testExecutionTime.acceptable:` section is not met (`%s`)", time.Duration(int64(currentF)), org)

Comment on lines +226 to +227
covRat = big.NewRat(int64(tt.cov*10000), 10000)
prevRat = big.NewRat(int64(tt.prev*10000), 10000)
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

Consider extracting the repeated conversion logic from float64 to *big.Rat into a helper function to reduce duplication across the test cases.

Suggested change
covRat = big.NewRat(int64(tt.cov*10000), 10000)
prevRat = big.NewRat(int64(tt.prev*10000), 10000)
covRat = float64ToBigRat(tt.cov)
prevRat = float64ToBigRat(tt.prev)

Copilot uses AI. Check for mistakes.

Comment on lines +266 to +268
diffF, _ := diff.Float64()
currentF, _ := current.Float64()
prevF, _ := prev.Float64()
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

The conversion of *big.Rat values to float64 for use in expression evaluation is repeated; consider abstracting this conversion into a helper function to improve readability and reduce duplication.

Suggested change
diffF, _ := diff.Float64()
currentF, _ := current.Float64()
prevF, _ := prev.Float64()
diffF := ratToFloat64(diff)
currentF := ratToFloat64(current)
prevF := ratToFloat64(prev)

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Code Metrics Report

main (ca82aca) #504 (8658084) +/-
Coverage 50.3% 50.4% +0.1%
Code to Test Ratio 1:0.5 1:0.5 +0.0
Test Execution Time 30s 30s 0s
Details
  |                     | main (ca82aca) | #504 (8658084) |  +/-  |
  |---------------------|----------------|----------------|-------|
+ | Coverage            |          50.3% |          50.4% | +0.1% |
  |   Files             |             53 |             53 |     0 |
  |   Lines             |           3825 |           3841 |   +16 |
+ |   Covered           |           1925 |           1938 |   +13 |
+ | Code to Test Ratio  |          1:0.5 |          1:0.5 |  +0.0 |
  |   Code              |           7494 |           7511 |   +17 |
+ |   Test              |           4415 |           4475 |   +60 |
  | Test Execution Time |            30s |            30s |    0s |

Code coverage of files in pull request scope (61.8% → 63.4%)

Files Coverage +/- Status
config/config.go 63.4% +1.6% modified

Benchmark-0 (this is custom metrics test)

main (ca82aca) #504 (8658084) +/-
Number of iterations 1000 1000 0
Nanoseconds per iteration 676.5 ns/op 676.5 ns/op 0 ns/op
Metadata
main (ca82aca) #504 (8658084)
GOOS darwin darwin
GOARCH amd64 amd64

Benchmark-1 (this is custom metrics test)

main (ca82aca) #504 (8658084) +/-
Number of iterations 1500 1500 0
Nanoseconds per iteration 1345 ns/op 1345 ns/op 0 ns/op
Metadata
main (ca82aca) #504 (8658084)
GOOS darwin darwin
GOARCH amd64 amd64

Reported by octocov

@k1LoW k1LoW merged commit 2652431 into main May 23, 2025
7 checks passed
@k1LoW k1LoW deleted the fix/coverage-big-rat branch May 23, 2025 10:27
@github-actions github-actions bot mentioned this pull request May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change bug Something isn't working minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Diff is reported as 0%
1 participant