9

I'm curious which form is more efficient, is correct style, etc. I feel like the ".0" approach is much quicker; I'm not sure why the "float" approach is equally appreciated (if it is).

1

7 Answers 7

9

Using float(7) adds some unnecessary overhead—Python has to find the float function in globals() and call it. Using 7.0 does all the necessary conversions at compile-time instead of run-time. You can see this using the Python bytecode disassembler.

>>> import dis
>>> def f(): return 7.0
... 
>>> def g(): return float(7)
... 
>>> dis.dis(f)
  1           0 LOAD_CONST               1 (7.0)
              3 RETURN_VALUE        
>>> dis.dis(g)
  1           0 LOAD_GLOBAL              0 (float)
              3 LOAD_CONST               1 (7)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE        
9

use the 7.0 approach, the float(7) approach is used to transform integer or string types to floats so it's a different use, for example:

a = 7
b = "7"
print float(a)
7.0
print float(b)
7.0
2
  • Ah I see its use now. Thank you!
    – Piper
    Jul 13, 2012 at 18:43
  • 1
    float() incurs some overhead as well. Jul 13, 2012 at 18:50
6

The following are all equivalent in Python:

>>> 7. == 7.0 == float(7) == float("7")
True

I would avoid using float(7) or float("7") when you are hard-coding the value, as the Python interpreter must first cast the value to an integer or a string, and then convert it to floating-point.

To avoid that overhead, use 7. or 7.0 to give Python a float literal.

Of course, float() should still be used to convert other data types to a float.

1

Float literals can be written as 7.0, that is fine as they are automatically of type float.

If you intend to convert an integer or string to float then the float() function is appropriate but that function does not need to be called to write a float literal.

0

7.0 is "better", no need for the cast to float, it will do it automatically.

float() is best saved for casting a non-float to a float.

0

Using float(0) is much more explicit when re-reading your code at a later date, causing less confusion later if you accidentally drop the ".0".

3
  • How exactly does one accidentally drop that? Jul 13, 2012 at 23:31
  • @MichaelHoffman Say you have this_should_be_a_float = 7.0/denominator, where denominator is an int. My thinking was that for future maintainability it might be nice to have a very explicit way to point out that the 7.0 needed to be a float in order to not have a rounding issue in case someone later changed the 7.0 to another constant, such as 6, and forgot to add the ".0". Jul 14, 2012 at 21:34
  • You should use from __future__ import division if you aren't already. Then this won't be a problem. Jul 15, 2012 at 20:00
0

7.0 should be faster. float(7) creates an integer, then call the float() function to convert the integer to a float, so calling float(7) implicates function call overhead as well as any error checking the float() function might do.

For most practical purposes, of course, the difference in speed is unlikely to matter much (unless you're in a deep loop where your code is being called hundreds of millions of times), but there's something inelegant about invoking a conversion function when the interpreter has a built in syntax for constructing floats.

Use float() when you have something that isn't a float (like a string or an integer) that you want to convert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.