You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Ryan Blue (JIRA)" <ji...@apache.org> on 2016/10/28 22:37:58 UTC

[jira] [Commented] (AVRO-1945) Python float deviation

    [ https://issues.apache.org/jira/browse/AVRO-1945?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15616822#comment-15616822 ] 

Ryan Blue commented on AVRO-1945:
---------------------------------

I don't think this is an Avro bug. It looks like it is due to floating point representation. If I serialize 1.9 in ipython I get the same result:

{code:title=ipython}
In [1]: 1.9
Out[1]: 1.9

In [2]: import struct

In [3]: struct.unpack("f", struct.pack("f", 1.9))
Out[3]: (1.899999976158142,)
{code}

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