1

I am confused on whether there is or is not any difference between and int of 5 and a float of 5.0, besides the float having a decimal.

What are some of the things I can do with an int that I can't with a float? What is the point of having two separate types, instead of just letting everything be a float in the first place?

Math chiller
  • 4,123
  • 6
  • 28
  • 44
GoTerps3288
  • 41
  • 1
  • 4
  • 3
    IMO it should not be downvoted. It is not a duplicate, not offtopic, not stupid. Maybe the form could be better, but in general it is a good question – radrow Jan 30 '19 at 22:16

5 Answers5

6

They are different data types:

 type(5)     # int
 type(5.0)   # float

And therefore they are not, strictly speaking, the same.

However, they are equal:

5 == 5.0     # true
kindall
  • 178,883
  • 35
  • 278
  • 309
  • 1
    Could you provide an example where this difference matters? – radrow Jan 30 '19 at 22:21
  • When printing them, for example. – kindall Jan 30 '19 at 22:39
  • @radrow: The type affects the operations performed on them. For example, `5/2` will perform integer division, discarding the fraction and producing `2`, while `5.0/2` will perform floating-point division, producing `2.5`. – Eric Postpischil Jan 31 '19 at 03:06
  • @Eric this is not true in current version of python. It was true in python 2 however – radrow Jan 31 '19 at 09:22
  • 1
    @radrow: Okay, they changed division. Nonetheless, integer and floating-point objects behave differently in arithmetic. `print(5 + 2**55 - 2**55)` yields 5 whereas `print(5.0 + 2**55 - 2**55)` yields 8.0 (in an implementation that uses IEEE-754 basic 64-bit binary floating-point). – Eric Postpischil Jan 31 '19 at 13:06
4

They are different types.

>>> type(5)
<type 'int'>
>>> type(5.0)
<type 'float'>

Internally, they are stored differently.

fstop_22
  • 971
  • 5
  • 9
2

5 and 5.0 are different objects in python so 5 is 5.0 is False

But in most cases they behave the same like 5 == 5.0 is True

stefan
  • 188
  • 1
  • 7
  • Better to emphasize that `5` and `5.0` are different objects because they produce objects of different types; even `5 is 5` can be false depending on your Python implementation. – chepner Jan 30 '19 at 22:00
  • FWIW, in most cases they behave very differently. They are very different internally, and can represent a vastly different range of numbers. Each of them has different problems associated with them. – Rudy Velthuis Jan 31 '19 at 00:26
1

As your question is focused on the difference and need of having two different data types I will try to focus on that and answer.

Need for different data type (why not put everything in float?)

Different data type have different memory usage.int uses 2 byte whereas float uses 4 byte.One can use the correct data type in correct palce and save memory

What are some of the things I can do with an int that I can't with a float?

One of the most important thing one should know while using these these two data type is that,"integer division truncates": any fractional part is discarded.To get desired result you should use the correct type.

A nice example is given in the book "The C Programming Language.Book by Brian Kernighan and Dennis Ritchie" which is applicable regardless of the language used.

This statement converts the temparature from Fahrenheit to Celsius.

float celsius=(5/9)*(Fahrenheit-32)

This code will always give you the answer as 0.That is because the answer of 5/9 is 0.5556 which due to truncation is taken as 0.

now look at this code:

float celsius=(5.0/9.0)*(Fahrenheit-32)

This code will give you the correct answer as 5.0/9.0 gives us the value 0.5556. As we have used float value here the compiler does not truncate the value.The float value prevents the truncation of fraction part and gives us our desired answer

I think this will tell you how important is the difference between 5 and 5.0

0

This question is already answered: they have different type.
But what does that mean?

One must think in term of object: they are somehow objects of different classes, and the class dictates the object behavior.

Thus they will behave differently.

It's easier to grasp such things when you are in a pure object oriented language like in Smalltalk, because you clearly can browse the Float and Integer classes and learn how they might differ thru their implementation. In Python, it's more complex because the computation model is multi-layer with notions of types, operators, function, and this complexity is somehow obscuring the basic object oriented principles. But from behavioural point of view, it ends up being the same: Python : terminology 'class' VS 'type'

So what are these differences of Behavior?
They are thin because we make our best effort to have uniform and unsurprising arithmetic (including mixed arithmetic) behavior matching the laws of mathematics, whatever the programming language.

Floating point behaves differently because they keep a limited number of significand bits. It's a necessary trade-off for keeping computations simple and fast. Small integers require few significand bits, so they will behave mostly the same than floating point. But when growing larger, they won't. Here is an arithmetic example:

print(5.0**3 == 5**3)
print(5.0**23 == 5**23)

The former expression will print True, the later False... Because 5^23 requires 54bits to be represented and Python VM will in most case depend on IEEE754 double floating point which provide only 53 bits significand.

aka.nice
  • 9,100
  • 1
  • 28
  • 40