[
https://issues.apache.org/jira/browse/AVRO-1945?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15619632#comment-15619632
]
Stephan Müller commented on AVRO-1945:
--------------------------------------
Thanks for the reply. We use a Kafka server and encode the messages before
sending with Avro. The issue for our use case is now that we have multiple
consumer (database writer, dashboard, ...) and every conumer would have to
handle the floating number seperatly. Switching to Java, which obviously
handles the float values well, is not an option for us. So is there a
workaround to fix the floating point representation without implementing it on
every consumer seperately at least?
> Python float deviation
> ----------------------
>
> Key: AVRO-1945
> URL: https://issues.apache.org/jira/browse/AVRO-1945
> Project: Avro
> Issue Type: Bug
> Components: python
> Affects Versions: 1.8.1
> Environment: Python 2.7.11 with avro 1.8.1
> Python 3.5.2 with avro-python3 1.8.1
> Reporter: Stephan Müller
>
> Unfortunately, the python avro package seems to have problems with float
> numbers.
> After encoding data containing float values into an avro file and decoding it
> back, values with decimals differ a tiny bit from the original value.
> In the following code sequence, the number 1.9 is saved as float. After
> decoding it back, the shown value is 1.899999976158142.
> {code:none}
> import avro.schema
> from avro.datafile import DataFileReader, DataFileWriter
> from avro.io import DatumReader, DatumWriter
> schema_text = """{"namespace": "example.avro",
> "type": "record",
> "name": "Number",
> "fields": [
> {"name": "name", "type": "string"},
> {"name": "number", "type": "float"}
> ]
> }"""
> schema = avro.schema.parse(schema_text)
> writer = DataFileWriter(open("numbers.avro", "wb"), DatumWriter(), schema)
> writer.append({"name": "Float number with one decimal", "number": 1.9})
> writer.close()
> reader = DataFileReader(open("numbers.avro", "rb"), DatumReader())
> for user in reader:
> print(user)
> reader.close()
> {code}
> Script output:
> {code:none}
> {u'name': u'Float number with one decimal', u'number': 1.899999976158142}
> {code}
> Using avro-tools-1.8.1.jar to decode the same created avro file
> (numbers.avro), the displayed floating numbers correspond to the original
> values:
> {code:none}
> $ java -jar avro-tools-1.8.1.jar tojson numbers.avro
> {"name":"Float number with one decimal","number":{"float":1.9}}
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)