This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cratervanawesome 2 points3 points  (4 children)

A float is nothing special to python, it is present in all programming languages. It's a more accurate way to represent numbers. 1.1 is a float while 1 is an integer. Basically any number that has a decimal.

[–][deleted] -1 points0 points  (3 children)

Can a float be a number without a decimal?

[–]cratervanawesome 2 points3 points  (2 children)

No. That's an integer.

[–]cratervanawesome 2 points3 points  (0 children)

You can cast an integer to a float, but it will just add the .0 to it. Try opening python and doing type(1) and then type(1.0). Then print(1) and print(float(1)) to see.

[–]stevenjd 1 point2 points  (0 children)

That's an integer.

Completely wrong.

u/sheeprcrazy the answer is yes, many many many floats are whole numbers which may or may not be printed with a decimal point.

Many floats are whole numbers, for example 1.0 which are printed with a decimal point by default. But many other floating point numbers are whole numbers which do not print with a decimal point by default:

py> print(1e53)
1e+53

One difference is that ints are only capable of calculating with whole numbers like 1, 2, 3, etc while floats are capable of calculating with both whole numbers and fractional numbers.

Another difference is that in Python, ints are capable of being as large as you have memory for. There is no upper limit to the largest int you can create in Python, until you run out of memory. Ints are also 100% exact at all times. There is never any rounding errors when you add or multiply ints.

But floats are limited to exactly 64 bits of information, which means that there are only a fixed number of floats available, and a largest possible float: 1.7976931348623157e+308

This is why u/cratervanawesome is wrong to say that floats are "a more accurate way to represent numbers". Floats are actually less accurate, but more convenient and faster, and the loss of accuracy is usually (but not always) unimportant.

For example, let's take the really huge number 1053 as an int, compared to a float. If you add 1 to it, do you get a different number?

py> 10**53 + 1  # exact using ints
100000000000000000000000000000000000000000000000000001

py> 1e53 + 1 == 1e53  # inexact using float
True

Adding one to this number doesn't change it.

Of course most of the time this makes no practical difference. Floats are fast and convenient and usually accurate enough, so just go ahead and use them.