-3

This might be a silly question but I read somewhere that floats in Python are equal to doubles in C++. So if I want to check whether a variable is a double or not, should I use the following:

isinstance(v, float)

or this one:

isinstance(v, double)
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
polerto
  • 1,750
  • 5
  • 29
  • 50
  • 2
    Python doesn't have a double type. All floating point numbers are floats. But type-checking is normally discouraged in python. Why do you need to do this? There might be a better alternative. – stranac Jun 22 '12 at 09:58

2 Answers2

7

You can't check for a C/C++ type in Python. If you want to know if a value is a floating-point number, then isinstance(v, float) does it for you. If that returns true, you've got a floating-point value that corresponds to a C double (in CPython), which on typical platforms means a 64-bit IEEE float. Details of the FP format are available as sys.float_info.

If you want smaller floats for some reason, then install Numpy and use its np.float32 or np.float16 types.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
2

sorry, there is no Double in python, python only have floats and int.

In python 2.x we've long int too, but not in 3.x.(don't confuse long int with C's long)

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504