0

I'm trying to calculate 27^(1/3) = 3. But it turns out not as expected.

>>> 27 ** (1/3)
1
>>> 27 ** (1/3.)
3.0

Firstly, why the first calculation will result in 1, is it because 1/3 is casted to an int so it equals zero?

Secondly, what does the '.' means in Python in general, could I append a dot to any number in order to cast it from int to double?

Thanks.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Judking
  • 6,111
  • 11
  • 55
  • 84
  • 2
    All your guesses are correct. Keep in mind, in python-3, numbers are floating points by default. – mehmetminanc Mar 13 '16 at 20:52
  • 4
    @mehmetminanc: In Python 3, numbers are **not** floating point by default. If you write an integer literal in Python 3 it's still an integer. However, the `/` division operator always performs floating-point division. – PM 2Ring Mar 13 '16 at 20:57
  • FWIW, this is one of the very first topics covered in the official Python 2 [tutorial](https://docs.python.org/2/tutorial/introduction.html#numbers). "The return type of a division (/) operation depends on its operands. If both operands are of type int, floor division is performed and an int is returned. If either operand is a float, classic division is performed and a float is returned. The // operator is also provided for doing floor division no matter what the operands are." – PM 2Ring Mar 13 '16 at 21:07
  • @PM2Ring Oh, I am actually quite surprised. Is there any practical significance between both, though? – mehmetminanc Mar 13 '16 at 21:24
  • 1
    @mehmetminanc: "Is there any practical significance between integers and floats"? Certainly! Python integers can be as large as you want (given memory constraints); floats have a limited range, and limited precision. See Wikipedia's article on [Floating point](https://en.wikipedia.org/wiki/Floating_point) for details. Python uses [IEEE 754 binary64](https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64) for its floats. – PM 2Ring Mar 13 '16 at 21:39

3 Answers3

3

Firstly, why the first calculation will result in 1, is it because 1/3 is casted to an int so it equals zero?

Yes, that's exactly it.

what does the '.' means in Python in general, could I append a dot to any number in order to cast it from int to double?

The . is explicitly making the number a float as 1.0 would be opposed to just 1.

Bernardo Meurer
  • 2,295
  • 5
  • 31
  • 52
2

You experience integer division in python 2.

in python 2:

>>> 2/3
0

In python 3:

>>> 2/3
0.6666666

If you want the python3 behaviour in python2 you can do

from __future__ import division

as your first import.

For the integer division, use

>>> 2 // 3
0

Or you can use 2.0 / 3.0 to use floats.

MaxNoe
  • 14,470
  • 3
  • 41
  • 46
0

In python 2.x, dividing integers defaults to floor division, so any non-integer quotient automatically rounds down to the nearest integer for example

>>> 1/3
0
>>> 5/2
2

So when you try to calculate 27 ** (1/3) you are just calculating 27 ** 0

There are numerous workarounds to the problem, the simplest way would be to just use floating points in the exponent - (1.0/3.0), or even just (1/3.0). However, if you were to be doing a lot of these calculations it might become annoying to constantly type out all the numbers in floating point form. In this case, you could use the future module to utilize Python 3.x division behavior, which always results in a floating point value:

>>> from __future__ import division
>>> 1/3
0.3333333333333333

This way the calculation will never default to floor division. And if at some point you want floor division in the same program, you can just use the // operator to get behavior similar to python 2.x integer division, like so:

>>> 1//3
0
Zach Ward
  • 117
  • 6