Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

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

I can't understand why

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

is different from

27 ** (1/3) #=> 1
like image 576
Jackie Chan Avatar asked Nov 30 '22 01:11

Jackie Chan


1 Answers

1.0 / 3.0 # => 0.3333333333333333
27 ** 0.333 # => 2.9967059728946346

1 / 3 # => 0
27 ** 0 # => 1

The second is an example of integer division. How many threes are there in one? Zero. Any number in power 0 is 1.

like image 78
Sergio Tulentsev Avatar answered Dec 25 '22 22:12

Sergio Tulentsev