Why can't I divide a fixnum by another fixnum?
Asked Answered
C

3

5

I'm currently trying to divide counts["email"] a hash containing the number 82,000 by a variable total which contains the value 1.3 million.

When I run puts counts["email"]/total I get 0.

Why can't I perform division on these?

Canute answered 25/6, 2012 at 21:23 Comment(1)
If both operands are integers, Ruby performs integer division: mathworld.wolfram.com/IntegerDivision.htmlPhotographer
N
7

Because that's how Ruby works: when you divide integer to integer, you get an integer. In this case that'll be 0, because it's the integer part of the result.

To get the float result, just tell Ruby that you actually need a float! There're many ways to do it, I guess the most simple would be just convert one of the operand to Float...

puts counts["email"]/total.to_f
Nerve answered 25/6, 2012 at 21:30 Comment(5)
Not just Ruby, this is the behavior of a majority of programming languages! Converting the denominator to a float isn't the only way to solve this either. If mathematical accuracy is desired, Rational(counts["email"], total) would be superior. Note that Rational is in the standard library in versions prior to 1.9.Filmore
I guess the moot point is whether the language dynamically- or statically-typed. Perl, JavaScript and PHP (the languages I worked with the most) would print the float value here - even though the latter has the 'Float' type.Nerve
Ruby and Python 2.7 (not Python 3) are dynamically typed languages where integer division results in the integer quotient rather than a float.Filmore
Somehow I never knew they changed it in Python 3... Amazing.Nerve
@raina77ow: Ruby and Python are dynamically and strongly typed, but PHP, Perl, and JavaScript are dynamically and weakly typed.Describe
H
9

You are performing division, although not the one you expected. There are many different ways to divide integers in Ruby:

# Integer division:
5 / 4     # => 1

# Floating point division:
5.fdiv(4) # => 1.25

# Rational division:
5.quo(4)  # => Rational(5, 4)

You can also convert one of your integers to a Float or a Rational:

5.to_f / 4 # => 1.25
5.to_r / 4 # => Rational(5, 4)

Note that first calling fdiv directly is faster than calling to_f and then using the / operator. It also makes it clear that you are using floating point division.

Hysteric answered 25/6, 2012 at 22:19 Comment(1)
A comment for the -1 would be appreciated.Faena
N
7

Because that's how Ruby works: when you divide integer to integer, you get an integer. In this case that'll be 0, because it's the integer part of the result.

To get the float result, just tell Ruby that you actually need a float! There're many ways to do it, I guess the most simple would be just convert one of the operand to Float...

puts counts["email"]/total.to_f
Nerve answered 25/6, 2012 at 21:30 Comment(5)
Not just Ruby, this is the behavior of a majority of programming languages! Converting the denominator to a float isn't the only way to solve this either. If mathematical accuracy is desired, Rational(counts["email"], total) would be superior. Note that Rational is in the standard library in versions prior to 1.9.Filmore
I guess the moot point is whether the language dynamically- or statically-typed. Perl, JavaScript and PHP (the languages I worked with the most) would print the float value here - even though the latter has the 'Float' type.Nerve
Ruby and Python 2.7 (not Python 3) are dynamically typed languages where integer division results in the integer quotient rather than a float.Filmore
Somehow I never knew they changed it in Python 3... Amazing.Nerve
@raina77ow: Ruby and Python are dynamically and strongly typed, but PHP, Perl, and JavaScript are dynamically and weakly typed.Describe
D
0

You need to use floats in order to get the "full" result.

counts["email"] / total.to_f
Dode answered 25/6, 2012 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.