Stephan Müller created AVRO-1945:
------------------------------------

             Summary: 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)

Reply via email to