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

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

Your problem has to do with how values are represented. It isn't that Java 
handles float values well because it does the exact same thing that Python does 
if you don't round when you print it out:

{code:title=scala/java}
scala> String.format("%2.15f", Float.valueOf(1.9))
res0: String = 1.899999976158142
{code}

1.9 simply can't be represented exactly as an 
[IEEE754|https://en.wikipedia.org/wiki/Single-precision_floating-point_format] 
float.

> 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