Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-1945

Python float deviation

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Not A Bug
    • 1.8.1
    • None
    • python
    • None
    • Python 2.7.11 with avro 1.8.1
      Python 3.5.2 with avro-python3 1.8.1

    Description

      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.

      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()
      

      Script output:

      {u'name': u'Float number with one decimal', u'number': 1.899999976158142}
      

      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:

      $ java -jar avro-tools-1.8.1.jar tojson numbers.avro
      {"name":"Float number with one decimal","number":{"float":1.9}}
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            smueller18 Stephan Müller
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: