summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/forms/fields.py14
1 files changed, 5 insertions, 9 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 3750e85af1..8494e316dd 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -5,6 +5,7 @@ Field classes.
import copy
import datetime
import itertools
+import math
import os
import re
import uuid
@@ -306,13 +307,11 @@ class FloatField(IntegerField):
def validate(self, value):
super().validate(value)
-
- # Check for NaN (which is the only thing not equal to itself) and +/- infinity
- if value != value or value in (Decimal('Inf'), Decimal('-Inf')):
+ if value in self.empty_values:
+ return
+ if not math.isfinite(value):
raise ValidationError(self.error_messages['invalid'], code='invalid')
- return value
-
def widget_attrs(self, widget):
attrs = super().widget_attrs(widget)
if isinstance(widget, NumberInput) and 'step' not in widget.attrs:
@@ -352,10 +351,7 @@ class DecimalField(IntegerField):
super().validate(value)
if value in self.empty_values:
return
- # Check for NaN, Inf and -Inf values. We can't compare directly for NaN,
- # since it is never equal to itself. However, NaN is the only value that
- # isn't equal to itself, so we can use this to identify NaN
- if value != value or value == Decimal("Inf") or value == Decimal("-Inf"):
+ if not math.isfinite(value):
raise ValidationError(self.error_messages['invalid'], code='invalid')
def widget_attrs(self, widget):