-
Notifications
You must be signed in to change notification settings - Fork 198
Open
Description
edition = "2024";
message Foo {
repeated float fs = 1;
}import 'test.pb.dart';
void main() {
final foo = Foo();
foo.fs.add(0.123456789);
print(foo);
print(Foo.fromBuffer(foo.writeToBuffer()));
}Prints:
fs: 0.123456789
fs: 0.12345679104328156
This is because the value being set isn't valid float, but the validation function isn't correct and accepts it:
protobuf.dart/protobuf/lib/src/protobuf/field_error.dart
Lines 140 to 143 in 27730db
| bool _isFloat32(double value) => | |
| value.isNaN || | |
| value.isInfinite || | |
| (-3.4028234663852886E38 <= value) && (value <= 3.4028234663852886E38); |
In this particular case, it doesn't handle the numbers between the min. and max. specified by this function, but are have more fractional digits that a 32-bit float can represent. There could be other bugs too.
Ideally you would convert the number to a float and back to a double, and check that you get the same number back, but we can't easily to this in Dart. Not sure if there's a performant way to do it, without writing the number to a Float32List or a ByteData and reading it back.
Metadata
Metadata
Assignees
Labels
No labels