[common] Fix FLOAT precision checking. (#4656)#4658
Closed
hunshenshi wants to merge 1 commit into
Closed
Conversation
wwj6591812
reviewed
Dec 8, 2024
| throw new NumberFormatException( | ||
| s + " cannot be cast to float due to precision loss"); | ||
| } else { | ||
| // Validate precision by comparing the rounded value |
Contributor
There was a problem hiding this comment.
float f = Float.parseFloat(s);
| s + " cannot be cast to float due to precision loss"); | ||
| } else { | ||
| // Validate precision by comparing the rounded value | ||
| if ((double) f == d || Math.abs(f - d) < Math.abs(d * 1e-6)) { |
Contributor
There was a problem hiding this comment.
why use "d * 1e-6"?
what about Double#compare?
Author
There was a problem hiding this comment.
- The value 1e-6 (which is 0.000001) is typically a reasonable threshold for floating-point comparisons, it's a balance between precision and practical usability.
- This approach helps avoid rejecting values that are within the acceptable precision for FLOAT types in databases like MySQL, while still accounting for the limitations of floating-point precision.
| } else { | ||
| // Significant precision loss detected | ||
| throw new NumberFormatException( | ||
| s + " cannot be cast to FLOAT due to precision loss"); |
Contributor
There was a problem hiding this comment.
The exception message need improve. It needs to cover the comparative failure situation.
|
This pull request has had no activity for 90 days and is being marked stale. If you'd like to keep it open, please leave a comment or push a new commit. Thank you for your contribution to Apache Paimon! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Linked issue: close #4656
Tests
API and Format
Documentation