#32559: Add attribute 'step' to FloatField.
-------------------------------------+-------------------------------------
Reporter: Jacob Rief | Owner: Kapil
| Bansal
Type: New feature | Status: assigned
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: FloatField, | Triage Stage: Accepted
NumberInput, step |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Adam Johnson):
On [https://forum.djangoproject.com/t/fixed-32559-add-attribute-step-to-
floatfield/7148 the forum], Kapil wrote:
> Hi,
> I was working on ticket 32559 to add step in FloatField but how to write
validation check for this.
> Due to python floating point issues, I am not able to validate whether
field value is of given step_size or not
The loss of precision in floating points is a real issue. The normal way
to validate would be to use the modulo operator to check there is no
remainder after dividing by the step, but this isn't possible in floating
points with common decimal steps like 0.1:
{{{
>>> step = 0.1
>>> 1 % step == 0
False
>>> 1 % step
0.09999999999999995
}}}
This problem cannot be solved for `FloatField` - I suggest we move the
ticket to modify `DecimalField`, as `Decimal` objects do not have the same
problem:
{{{
>>> from decimal import Decimal
>>> step = Decimal('0.1')
>>> Decimal(1) % step == 0
True
>>> Decimal(1) % step
Decimal('0.0')
}}}
We'd want to enforce that `step` is given as a `Decimal`.
The `float` problem can also occur when constructing a `Decimal` from a
`float`:
{{{
>>> Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
}}}
Perhaps we should have a check that the given step does not have more than
10 digits after the decimal point?
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:9>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.c7b090587134887bd5a1409608e9c582%40djangoproject.com.