1

I am trying to validate float and double values using python. Since python supports large numbers, I would like to do validate the given number in within the range of float(32 bit floating point representation) or double(64 bit floating point representation)? How can I achieve it?

def is_int(x):
    int_max_value = 2^31 - 1
    int_min_value = -2^31
    if int_min_value <= x <= int_max_value:
        return True
    else:
        return False

def is_float(x):
    # need to implement this
    pass
newbie
  • 1,282
  • 3
  • 20
  • 43
  • Can you provide examples of what you want validated, and what the corresponding results should be? – Scott Hunter Nov 07 '19 at 16:53
  • Maybe duplicate of [What is the maximum float in Python?](https://stackoverflow.com/questions/3477283) or [Maximum allowed value for a numpy data type](https://stackoverflow.com/q/23189506) (Python float values are always double, and I don't think [`ctypes`](https://docs.python.org/3/library/ctypes.html) provides float limits information, so you may resort to NumPy for other precision types). – jdehesa Nov 07 '19 at 16:58
  • Note that floating numbers include positive and negative infinity, so technically _any_ number would always be within range. – jdehesa Nov 07 '19 at 16:59
  • This is going to be difficult to do using builtin types since [floats _are_ doubles in python](https://stackoverflow.com/a/6663292/4739755) – b_c Nov 07 '19 at 17:05

2 Answers2

1

One way to reliably get the limits of floating point formats is to construct the values from their binary representation with struct.

import struct
FLOAT_MAX = struct.unpack('>f', b'\x7f\x7f\xff\xff')
FLOAT_MIN = struct.unpack('>f', b'\xff\x7f\xff\xff')
MAX_DOUBLE = struct.unpack('>d', b'\x7f\xef\xff\xff\xff\xff\xff\xff')
MIN_DOUBLE = struct.unpack('>d', b'\xff\xef\xff\xff\xff\xff\xff\xff')
jdehesa
  • 58,456
  • 7
  • 77
  • 121
0

Found a solution like below after referring this

DOUBLE_MAX = 1.7976931348623158E+308     # 64 bit floating point max value
DOUBLE_MIN = 2.2250738585072014E-308     # 64 bit floating point max value
def is_double(value):
    return DOUBLE_MIN <= value <= DOUBLE_MAX

FLOAT_MAX = 3.402823466E+38     # 32 bit floating point max value
FLOAT_MIN = 1.175494351E-38     # 32 bit floating point max value
def is_float(value):
    return FLOAT_MIN <= value <= FLOAT_MAX

is_float(123e20)
is_double(123e20)

is_float(123e203)
is_double(123e203)

is_float(123e2030)
is_double(123e2030)
newbie
  • 1,282
  • 3
  • 20
  • 43
  • 1
    Note that you don't need to write `if condition: return True else: return False`, this is logically equivalent to `return condition`. – kaya3 Nov 07 '19 at 17:08
  • This won't work on all floats. For example `x = FLOAT_MIN - 1` returns False from `is_float`, but python states that x is indeed a float `type(x) -> float` – b_c Nov 07 '19 at 17:10
  • I am not looking for the python datatype. I am actually looking for pyspark datatypes as it has float and double datatypes explicitly. So I need to validate them before inserting them into frame – newbie Nov 07 '19 at 17:14
  • You should include that in your question then as that's some pretty important context – b_c Nov 07 '19 at 18:07