Why is `27 ** (1.0 / 3.0)` different from `27 ** (1/3)`?

Please let me know if this is the correct way to get the cubic root.

I do not understand why

27 ** (1.0/3.0) #=> 3 

differs from

 27 ** (1/3) #=> 1 
+4
source share
5 answers
 1.0 / 3.0 # => 0.3333333333333333 27 ** 0.333 # => 2.9967059728946346 1 / 3 # => 0 27 ** 0 # => 1 

The second example is integer division. How many triples are in one? Zero. Any number in degree 0 is equal to 1.

+11
source

The first division is the decimal division, and the last is the whole division

i.e. 1.0/3.0 will give a decimal result, while 1/3 will give an integer result, which in this case I'm 0

the results will therefore be different, as this is the result of either

 27**0.333... 

or

 27**0 

which, of course, are clearly different.

This is enough to force one of the operators to be decimal for the whole operation to get a decimal result, for example. 1 / 3.0 will give 0.3333 ...

+4
source

Integer division leads to integers:

 irb(main):004:0> 1/3 => 0 irb(main):005:0> 1.0/3.0 => 0.3333333333333333 

27**0 = 1 . 27**(1/3) = 3

+4
source

(1/3) returns 0 , since 3 is an integer. in ruby, if you divide by integers both the divisor and the dividend, you will get an integer value. and since everything raised to 0 , 1 , you get 1 as an answer

(1.0/3.0) returns 0.3333 since you do not divide 2 integers to get 3 from 27 ** 0.33...

+3
source

Enter the conversation.

When you calculate 1.0 / 3.0 is decimal

Which is 1.0 / 3.0 = 0.33 #, which is decimal

1/3 - rounded to the nearest integer.

In this way:

 27 ** (1.0/3.0) #=> 3 is different from 27 ** (1/3) #=> 1 
0
source

Source: https://habr.com/ru/post/1468905/


All Articles