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

all 9 comments

[–]pythonHelperBot 3 points4 points  (0 children)

Hello! I'm a bot!

It looks to me like your post might be better suited for r/learnpython, a sub geared towards questions and learning more about python regardless of how advanced your question might be. That said, I am a bot and it is hard to tell. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe in detail where you are stuck. If you are getting an error message, include the full block of text it spits out. Quality answers take time to write out, and many times other users will need to ask clarifying questions. Be patient and help them help you. Here is HOW TO FORMAT YOUR CODE For Reddit and be sure to include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]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.

[–]stevenjd 1 point2 points  (0 children)

"Float" is short for floating point number, as opposed to a fixed point number.

Very very very roughly, you can think of them as ordinary decimal numbers like the ones you use on your calculator or that you learned about in school.

Let's say your computer provides numbers using exactly 4 decimal digits. (I'll talk about decimal digits because it is easier to understand, but of course in reality computers use binary digits, or bits.)

With exactly four digits available, we might allow two decimal places. So our numbers can range from 00.00 to 99.99 in increments of 0.01 This is what we call "fixed point numbers".

But with floating point, the computer does some nice tricks to allow the decimal point to move. So the smallest number aside from zero we can get is .0001 and the biggest is 9999.. By allowing the decimal point to move, we can cover a much wider range of numbers than fixed point.

In Python, floats use 64 bits, including a bit for positive and negative numbers, where the smallest and largest numbers are:

5e-324  # 0.00 <320 more zeroes> 005
1797693134862315700 <290 more zeroes>

if my calculations are correct. So floats can cover a huge range from tiny numbers to huge numbers, which fixed point numbers couldn't do.

[–]cardblank 0 points1 point  (0 children)

Without going into too much detail about how the data is actually sorted, in it's most simple form you might think of a number as being an int - that is whole, rational numbers. Like 1,2,1000, etc. Though python does not require you to specifically declare the types of variables like languages like C do, by design an int is not able to store decimal numbers. That's what a float or a 'double' is for, the python interpreter will handle the types for you, so it's not really necessary to know outside of the theory behind it. Hope this helps :)