Skip to content

Add tag on float imprecision#1395

Merged
Xithrius merged 2 commits into
masterfrom
wookie184-floats-tag
Feb 3, 2021
Merged

Add tag on float imprecision#1395
Xithrius merged 2 commits into
masterfrom
wookie184-floats-tag

Conversation

@wookie184
Copy link
Copy Markdown
Contributor

Adds a tag on float imprecision
image

Adds a tag on float imprecision
@coveralls
Copy link
Copy Markdown

coveralls commented Feb 2, 2021

Coverage Status

Coverage remained the same at 56.593% when pulling 9c1e5fd on wookie184-floats-tag into 79bfab3 on master.

@Shivansh-007
Copy link
Copy Markdown
Contributor

Can we also give an example where there are represented exactly:

# This is because 0.125 is precisely 1/8, or 1/(2^3)
>>> format(0.125, '.25f')
'0.1250000000000000000000000'

Then the math.isclose() for Floats Equality Testing:

>>> x = 0.1 + 0.1 + 0.1
>>> y = 0.3
>>> x == y
False
>>> from math import isclose
>>> x = 0.0000001
>>> y = 0.0000002
>>> 
>>> a = 123456789.01
>>> b = 123456789.02
>>> 
>>> print('x = y:', isclose(x, y, abs_tol=0.0001, rel_tol=0.01))
x = y: True
>>> print('a = b:', isclose(a, b, abs_tol=0.0001, rel_tol=0.01))
a = b: True
>>> 

And last not to use float datatypes while giving it the Decimal Function

# As you can see, since we passed an approximate binary float to the Decimal constructor it did it's best to represent that binary float exactly!!
# So, instead, use strings or tuples in the Decimal constructor.
>>> from decimal import Decimal
>>> format(0.1, '.25f')
'0.1000000000000000055511151'
>>> Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')

@wookie184
Copy link
Copy Markdown
Contributor Author

The tag is already fairly long, and our aim is to try and keep them fairly short so i've only put in what I think is most important, and linked to other things. Regarding your three points:

  1. it's explained at the start of the first link, https://docs.python.org/3/tutorial/floatingpoint.html, much more clearly then I could if I tried to fit it in.
  2. I think the tag would be too big with this explained properly, although I think it could help to add a line like You can also check if two floats are close with math.isclose and link to the docs, especially as it doesn't seem to be mentioned in the links.
  3. It's already mentioned, Note that we enter in the number we want as a string so we don't pass on the imprecision from the float.

@Shivansh-007
Copy link
Copy Markdown
Contributor

The tag is already fairly long, and our aim is to try and keep them fairly short so i've only put in what I think is most important, and linked to other things. Regarding your three points:

  1. it's explained at the start of the first link, https://docs.python.org/3/tutorial/floatingpoint.html, much more clearly then I could if I tried to fit it in.
  2. I think the tag would be too big with this explained properly, although I think it could help to add a line like You can also check if two floats are close with math.isclose and link to the docs, especially as it doesn't seem to be mentioned in the links.
  3. It's already mentioned, Note that we enter in the number we want as a string so we don't pass on the imprecision from the float.

Alright, You can do that for the 2nd point, maybe add a single line example?

@Xithrius Xithrius added p: 3 - low Low Priority and removed p: 2 - normal Normal Priority labels Feb 3, 2021
@wookie184
Copy link
Copy Markdown
Contributor Author

Updated, now looks like this
image

Copy link
Copy Markdown
Contributor

@Shivansh-007 Shivansh-007 left a comment

Choose a reason for hiding this comment

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

Thanks! Looks Good To Me!

Copy link
Copy Markdown
Contributor

@Akarys42 Akarys42 left a comment

Choose a reason for hiding this comment

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

Lovely

@Xithrius Xithrius merged commit c068ffe into master Feb 3, 2021
@Xithrius Xithrius deleted the wookie184-floats-tag branch February 3, 2021 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: tags Related to bot tags p: 3 - low Low Priority t: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants