-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[common] Fix FLOAT precision checking. (#4656) #4658
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,11 +144,13 @@ public static Object castFromStringInternal(String s, DataType type, boolean isC | |
} else { | ||
// Compatible canal-cdc | ||
Float f = Float.valueOf(s); | ||
if (f.toString().length() != s.length()) { | ||
throw new NumberFormatException( | ||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why use "d * 1e-6"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return f; | ||
} else { | ||
// Significant precision loss detected | ||
throw new NumberFormatException( | ||
s + " cannot be cast to FLOAT due to precision loss"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception message need improve. It needs to cover the comparative failure situation. |
||
} | ||
} | ||
case DOUBLE: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
float f = Float.parseFloat(s);