W
Wes Gamble
Today I discovered the difference in the meaning of the / (arithmetic
divide) method depending on whether it's caller is an integer or a
float.
I discovered that I had to cast the calling number explicitly to a float
and it seemed totally non-intuitive to me.
A couple of points:
1) I just discovered that this behavior exists in Java and C as well.
Which, frankly, I didn't even realize. Sigh.
2) I've read some other posts and people have mentioned the notion of
type purity with respect to this. I don't see why that's a problem.
When one divides two integers, one isn't asking for one of the integer
arguments to be cast to a float, they're asking for the result of
dividing two integers. Since when is it not allowable for a method to
take arguments of a given type and return a result of a different type?
A couple of questions:
1) I see that there is a divmod operator in Ruby that returns the
quotient and remainder of a division. Why is the overridden / operator
necessary when there is already a way to get int1 DIV int2 by using
int1.divmod(int2)[0]?
2) Is the reason for the behavior of "/" is that this is the behavior of
"/" in C? Why is it that way in C?
Thanks,
Wes
divide) method depending on whether it's caller is an integer or a
float.
I discovered that I had to cast the calling number explicitly to a float
and it seemed totally non-intuitive to me.
A couple of points:
1) I just discovered that this behavior exists in Java and C as well.
Which, frankly, I didn't even realize. Sigh.
2) I've read some other posts and people have mentioned the notion of
type purity with respect to this. I don't see why that's a problem.
When one divides two integers, one isn't asking for one of the integer
arguments to be cast to a float, they're asking for the result of
dividing two integers. Since when is it not allowable for a method to
take arguments of a given type and return a result of a different type?
A couple of questions:
1) I see that there is a divmod operator in Ruby that returns the
quotient and remainder of a division. Why is the overridden / operator
necessary when there is already a way to get int1 DIV int2 by using
int1.divmod(int2)[0]?
2) Is the reason for the behavior of "/" is that this is the behavior of
"/" in C? Why is it that way in C?
Thanks,
Wes