Skip to content

Commit

Permalink
[common] Fix FLOAT precision checking. (apache#4656)
Browse files Browse the repository at this point in the history
  • Loading branch information
hunshenshi committed Dec 7, 2024
1 parent 8484bb4 commit 386c983
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
return f;
} else {
// Significant precision loss detected
throw new NumberFormatException(
s + " cannot be cast to FLOAT due to precision loss");
}
}
case DOUBLE:
Expand Down

0 comments on commit 386c983

Please sign in to comment.