Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

fix issue with repr and Decimal #488

Closed
wants to merge 2 commits into from
Closed

fix issue with repr and Decimal #488

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Aug 11, 2017

Given the issue #431 fixing a precision issue while using python 2.x, as demonstrated by the example below:

$ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
>>> float(repr(0.8289445733333332)) == 0.8289445733333332
True
>>> float(str(0.8289445733333332)) == 0.8289445733333332
False
>>>

We have now an issue as described in #430 and #482 with both Python and Python3 and the use of both Decimal Python type and repr, because :

>>> from decimal import Decimal
>>> str(Decimal(0.8289445733333332))
'0.828944573333333156739399782964028418064117431640625'
>>> repr(Decimal(0.8289445733333332))
"Decimal('0.828944573333333156739399782964028418064117431640625')"

In Python 3, str() and repr() now provides the same result with a float:

$ python3
Python 3.5.3+ (default, Jun  7 2017, 23:23:48) 
>>> float(repr(0.8289445733333332)) == 0.8289445733333332
True
>>> float(str(0.8289445733333332)) == 0.8289445733333332
True
>>>

But the problems remain while using both Decimal and repr(). The pull request offers a solution which should work both for python2 and python3.

Regards,
Carl

@sebito91
Copy link
Contributor

sebito91 commented Sep 7, 2017

Why make this so convoluted, since we're passing this to _is_float already (and succeeding), why not just return repr(float(value))?

@@ -89,7 +89,14 @@ def _escape_value(value):
elif isinstance(value, integer_types) and not isinstance(value, bool):
return str(value) + 'i'
elif _is_float(value):
return repr(value)
reprvalue = repr(value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just make this return repr(float(value)) since you've already passed the _is_float test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I went that way because I didn't want to lose precision of the Decimal, but I don't know if it's worth it, your solution is quite shorter and more elegant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're referring to your example earlier, even if Decimal resolves to a more granular value than what you sent it may not be what you actually want. If you wanted that value down to the full granularity you should provide that to begin with (IMHO of course).

Do you want to take a stab at changing your PR? If so, can you please add a quick test to check both a non-Decimal and Decimal value?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, since your Decimal value is set to 16 digits, you should get the repr value of the 16-digit float instead of a (potentially) longer, more granular definition.

In [1]: from decimal import Decimal

In [2]: str(Decimal(0.8289445733333332))
Out[2]: '0.828944573333333156739399782964028418064117431640625'

In [3]: repr(Decimal(0.8289445733333332))
Out[3]: "Decimal('0.828944573333333156739399782964028418064117431640625')"

In [4]: repr(float(Decimal(0.8289445733333332)))
Out[4]: '0.8289445733333332'

@sebito91 sebito91 self-assigned this Apr 8, 2020
sebito91 added a commit that referenced this pull request Apr 9, 2020
sebito91 added a commit that referenced this pull request Apr 9, 2020
sebito91 added a commit that referenced this pull request Apr 10, 2020
…precision (#813)

* chore(line_protocol): update repr value of floats to properly handle precision. Closes #488

* chore(line_protocol): fix repr and handle boolean values

* chore(CHANGELOG): update to include reference to PR#488
ocworld pushed a commit to AhnLab-OSS/influxdb-python that referenced this pull request Apr 13, 2020
…precision (influxdata#813)

* chore(line_protocol): update repr value of floats to properly handle precision. Closes influxdata#488

* chore(line_protocol): fix repr and handle boolean values

* chore(CHANGELOG): update to include reference to PR#488
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant