Skip to content

Commit 0a95ae4

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Update pure python float checking.
Our C++ strtod was updated to handle text-format rounding about a month after it was ported to python. PiperOrigin-RevId: 706000143
1 parent 84ed87e commit 0a95ae4

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

conformance/text_format_failure_list_python.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,3 @@ Required.*.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
77
Required.*.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput # Output was not equivalent to reference message: modified: optional_bytes: "\007\010\014\n\r\t\013?\\\'\"" -> "\007\010\014\n\r\t
88
Required.*.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput # Output was not equivalent to reference message: modified: optional_string: "\007\010\014\n\r\t\013?\\\'\"" -> "\007\010\014\n\r\
99
Required.*.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput # Output was not equivalent to reference message: modified: optional_string: "\007\010\014\n\r\t\013?\\\'\"" -> "\007\010\014\n\r\
10-
11-
# Optional float interpreted as `inf`
12-
Required.*.TextFormatInput.FloatFieldMaxValue.ProtobufOutput # Output was not equivalent to reference message
13-
Required.*.TextFormatInput.FloatFieldMaxValue.TextFormatOutput # Output was not equivalent to reference message
14-
Required.*.TextFormatInput.FloatFieldMaxValue_f.ProtobufOutput # Output was not equivalent to reference message
15-
Required.*.TextFormatInput.FloatFieldMaxValue_f.TextFormatOutput # Output was not equivalent to reference message
16-
Required.*.TextFormatInput.FloatFieldMaxValue_F.ProtobufOutput # Output was not equivalent to reference message
17-
Required.*.TextFormatInput.FloatFieldMaxValue_F.TextFormatOutput # Output was not equivalent to reference message
18-

python/google/protobuf/internal/type_checkers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class Uint64ValueChecker(IntValueChecker):
231231
# The max 4 bytes float is about 3.4028234663852886e+38
232232
_FLOAT_MAX = float.fromhex('0x1.fffffep+127')
233233
_FLOAT_MIN = -_FLOAT_MAX
234+
_MAX_FLOAT_AS_DOUBLE_ROUNDED = 3.4028235677973366e38
234235
_INF = float('inf')
235236
_NEG_INF = float('-inf')
236237

@@ -269,8 +270,12 @@ def CheckValue(self, proposed_value):
269270
converted_value = super().CheckValue(proposed_value)
270271
# This inf rounding matches the C++ proto SafeDoubleToFloat logic.
271272
if converted_value > _FLOAT_MAX:
273+
if converted_value <= _MAX_FLOAT_AS_DOUBLE_ROUNDED:
274+
return _FLOAT_MAX
272275
return _INF
273276
if converted_value < _FLOAT_MIN:
277+
if converted_value >= -_MAX_FLOAT_AS_DOUBLE_ROUNDED:
278+
return _FLOAT_MIN
274279
return _NEG_INF
275280

276281
return TruncateToFourByteFloat(converted_value)

0 commit comments

Comments
 (0)