[Rails] 12 / 16 = 0
Brian L.
bluczkie at andrew.cmu.edu
Sun Apr 2 09:00:54 GMT 2006
There exists intuition to support that, although few commonly used
programming languages take this route. Exceptions seem to be PHP and
Python (soon) which admittedly are closer to ruby than C/C++.
I don't like the idea of winding up with an IEEE float as a result of
an integer operation, being that integers and IEEE floats don't
exactly behave in the same way. I also don't think that it's a good
idea for an operator to change its output type from a precise one to
an imprecise one based on user input. Imprecise and precise numbers
are fundamentally different and should probably not be mixed this
freely.
Maybe the real answer is to add precise math (rationals, decimals) to
ruby, like lisp has. Then you'd end up knowing whether your math was
'precise' at all times without exhibiting this mildly unintuitive
behavior. You'd of course need a div operator for this to work.
On 4/2/06, Jeroen Houben <jeroen at terena.nl> wrote:
> Gábor SEBESTYÉN wrote:
> >
> > On 2006.04.02., at 10:16, Jeroen Houben wrote:
> >
> >> Just thought it was weird that Ruby doesn't automatically cast ints to
> >> floats. Seems so un-rubyish
> >>
> > Try this one:
> >
> > irb(main):003:0> 12.0 / 16
> > => 0.75
> > irb(main):004:0>
> >
> > Place a dot and zero digit after integers to have a floating number.
> > Here's the same in Java:
> >
> > itigga: segabor$ more test.java
> > class Test {
> > public static void main(String[] args) {
> > // first case
> > System.out.println("12/16 = " + 12/16);
> > // second case
> > System.out.println("12.0/16 = " + 12.0/16);
> > }
> > }
> >
> > Running this the output will be:
> >
> > 12/16 = 0
> > 12.0/16 = 0.75
> >
> > If you write "12" language parser will mean it as integer. It is normal.
> > Why should it mean as floating point number?
> > I think it would be a major breakdown in memory footprint if Ruby
> > converted all integers to floating point numbers.
>
> I'm not saying it should treat 12 as a float (I'm not saying it should
> do anything really) but I wonder if it would be a bad thing to
> automatically cast the outcome of a division if needed (like in PHP).
>
> <?php
> $a = 12;
> $b = 16;
> echo gettype($a) . "\n";
> echo gettype($b) . "\n";
> echo gettype($a/$b) . "\n";
> echo gettype(6/3) . "\n";
> ?>
>
> Vigor10:/tmp jeroen$ php test.php
> integer
> integer
> double
> integer
>
> Jeroen
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
More information about the Rails
mailing list