What is a float?

Question

What makes a number a float?

Float is short for floating point and consists of the real numbers. Unlike integers, which can only be whole number values, floats can be any number in between, like 1.5, 3.14, etc. And Python is smart enough to know that any time you put a period in a number to convert it to floating point, so you may see numbers like `70.` In future lessons, which is the same as writing `70.0`.

5 Likes

Hi!

I am having a problem with printing float numbers. Here is the part of the code:

``````def F(x):
return 1/(x)

for i in range(-10,11):
if i == 0:
print 0, F(0.0000001)   #to avoid dividing by zero, yet getting something "realistic"
else:
print i,  F(i)
``````

And it prints the following:
-10 -1
-9 -1
-8 -1
-7 -1
-6 -1
-5 -1
-4 -1
-3 -1
-2 -1
-1 -1
0 10000000.0
1 1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0

So far, I have tried the following:

• {].format(F(x)) = prints just more zeros
• float(F(x)) = prints 0.0 instead of 0
• describve range() as float = gives error message (expected range, get float instead)

The function prints the value correctly if I describe it as: F(3.) instead of F(3) (=0). So the function is fine, but the printing is tricky.

Thank you for help!

okay, that is the output you are currently get. What is the desired output?

this wonâ€™t work, the rounding down has already occurred, so you only cast to float after the fact. The division itself should involve a float then

Hi!

I hope to get F(x) more accurately than 0. Perhaps 2-4 decimals. The format (table like printing) is already fine

`.format()` supports control over the amount of digits you want to print. I donâ€™t see the problem? You might want to ensure that F function returns a float (that the result of the division is a float, not a integer)

python2 will round down to the nearest integer if the division involves two integers, however, if the division involves a float then python wonâ€™t floor (round down)

I believe that the function does not return the value as float, because with .format() problem does not disappear. Thus, I have to change the F(x) somehow.

Thank you!

i believe i mentioned that twice indeed:

Anyway, the problem solved by adding a line as follows:

def F(x):
x = float(x)
return 1/x

why cast `x` to a float? I would simply use `1.0`, then you also have a float

Because this is a part of â€śbiggerâ€ť code I try write piece by piece. I test each function, class, etc. separately and then add them together - one by one. x is the variable in here and â€ś1â€ť is 1 for simplicity. Number one will be replaced by another value from another function later on. Of course I will test it, if I need to separately define x as float in the ready code but now it is easier to control (as float).

I hope this explanation makes sense.

you could consider `1/float(x)`, anyway, if you write bigger code i would recommend following naming convention:

https://www.python.org/dev/peps/pep-0008/

so then function name would be for example `hello_world` (preferable) or `helloWorld` is also okay. Function names should be more descriptive then a single letter.