I cannot get return of floats through expressions

Asked

Viewed 208 times

3

When I try to give print(6/10) get a int (0) instead of a float (0.6)

And if I give type(6/10) returns int.

I’m following this tutorial and doing exactly the same steps in both pyCharm and IDLE, however it manages to get results 'float' and I do not.

SS

  • No idea, with me it worked http://ideone.com/WN9h4h. I find it hard to go wrong.

  • @bigown use Linux. Does it have something to do?

  • ideone also uses. And it could not make a difference depending on the OS.

  • Meanwhile you can help the ganete to create the new site for IT subjects that are not programming. Commit (don’t forget to confirm the email). If you have the minimum number of interested parties, the site will be created. And if you can point to other people, too, that would be even nicer. http://area51.stackexchange.com/proposals/84282/super-user-em-portugues?referrer=iQLJgvwwKWiaSZMVw5WkVIw2. If you would like to know more details, please see: http://meta.pt.stackoverflow.com/q/2482/101 log in to let me know that you have read it and I can delete it.

  • 1

    @mustache I’ll send 1 print

  • @bigown updated the answer with the print!

  • @bigown What is this?!

Show 2 more comments

2 answers

6


The problem is that you are using Python 2.7. I do not recommend using it, prefer to use version 3.0 on. First because you are following a tutorial using this version, second because there are no gains in using an older version unless you have legacy code, which is not the case.

There really is this difference in behavior. Python 2.7 had these "problems" that were obviously fixed in the latest version to follow the Python philosophy and not use the philosophy of another language as it had before.

DeviaSerFloat = 6/10 #inteiro ou flutuante dependendo da implementação
EhFloat = 6.0/10.0 #força ponto flutuante
EhInt = 6//10 #força inteiro
print(DeviaSerFloat)
print(EhFloat)
print(EhInt)

See the Python 2.7 running on ideone.

Now look at 3.0 working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    This is not a "problem", simply the design of the language to some extent followed that of C (where integer divided by integer gave integer), and then stopped following. The line 2.X has maintained the old behaviour for compatibility, which is wise. There is no reason to avoid 2.7, there is a good reason to use it of course (for example, the latest LTS version of Ubuntu still uses 2.X, which means that this may be the standard version found on several servers).

  • It’s a problem for him. He wants to follow a tutorial done for 3.0 but use 2.7. This is a problem. I think there’s only one sensible reason to use 2.7, to have legacy code that’s not his case. And if the old version didn’t have a problem with that, why did they fix it? Or do you think they broke it?

  • 1

    Well, the tendency of modern languages is to use the "least astonishing principle": make the division "correct", and if you want a division that respects the type use a different operator. So no, I don’t think they screwed up, I agree it was a breakthrough. About using 2.7, until very recently I used it for two reasons: 1) need libraries that only had pro 2.7 (I think it goes into your legacy code category); 2) need to support environments that only had 2.X (and few possibilities to install a newer version). Nowadays these problems are quite minimized.

5

This is due to a change in the semantics of the division between the 2.X and 3.X versions of Python.

In Python 2.X operations between integers always return an integer, so that 6/10 is interpreted as the entire division - and thus rounded to zero. It is necessary to convert one of the operands to float to make the division into floating point: 6.0/10 or 6/10.0. This behavior was "inherited" from C-type languages.

In Python 3.X the division between integers can return a floating point. To make an entire division in this version you need to use another operator: 6//10. This behavior is most common in modern languages, especially dynamic typing.

The entire division operator works also in Python 2.X - and it returns an integer even when its operands are floats: 6.0 // 10.0 will give zero. Already the common division operator (called "true Division") maintains the default behavior of its version, for compatibility, but if you want to use the new semantics in Python 2.X just do the following import:

from __future__ import division

From there, the division will work as in 3.X (if this needs to be done in each source file or not, I’m not sure, I would have to test or consult some reference).

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.