Forum: Ruby on Rails 12 / 16 = 0

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2006-04-02 10:17
(Received via mailing list)
Hi,

Just thought it was weird that Ruby doesn't automatically cast ints to
floats. Seems so un-rubyish

irb(main):006:0> 12/16
=> 0
irb(main):007:0> 12.to_f / 16.to_f
=> 0.75

Jeroen
Aa1126cd36ac8570684b7923cff2a6c1?d=identicon&s=25 Gábor SEBESTY�N (Guest)
on 2006-04-02 10:32
(Received via mailing list)
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.

Gábor
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2006-04-02 10:44
(Received via mailing list)
Gábor SEBESTY�N wrote:
> irb(main):004:0>
>     System.out.println("12.0/16 = " + 12.0/16);
> 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
96bd2ea8ba5fb5e5d5d984497d90f04f?d=identicon&s=25 Brian L. (Guest)
on 2006-04-02 11:02
(Received via mailing list)
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.
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matthew Palmer (Guest)
on 2006-04-02 11:09
(Received via mailing list)
On Sun, Apr 02, 2006 at 10:41:36AM +0200, Jeroen Houben wrote:
> >=> 0.75
> >irb(main):004:0>
>
> 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).

So you perform the integer division 12 / 16, get a result of 0, and then
cast that to a float 0.0?  Not exactly useful.  What you really need to
do
is perform a modulus operation and then do an integer division if the
result
of that is 0 and a floating point cast/division otherwise.  Quite
expensive.

Also, if you really, really need integer division, then the PHP way
leaves
you out in the cold -- AFAIK, there's no 'integer division' operator in
PHP,
whereas, presumably, you can call .to_f on at least one of your
arguments in
Ruby if you want a floating point result.

- Matt
6805b35d0a8ea3ede0a7da2d4cf5ae77?d=identicon&s=25 Jonathan Weiss (Guest)
on 2006-04-02 11:12
(Received via mailing list)
Jeroen Houben 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

No, this is exactly why Ruby is a strong dynamically typed language.
Doing autoconversion is a very bad thing as unintentioned things can
happen.

If I tell the computer to dived two integers, I get an integer back. If
I tell the computer to contatinate two strings I get a string back. If I
tell the computer to add a string to a number Ruby should tell me "I
can't do it".

That's the whole point abouot strongly typed languages!


Jonathan
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2006-04-02 11:27
(Received via mailing list)
Jonathan Weiss wrote:
> happen.
>
> If I tell the computer to dived two integers, I get an integer back. If
> I tell the computer to contatinate two strings I get a string back. If I
> tell the computer to add a string to a number Ruby should tell me "I
> can't do it".
>
> That's the whole point abouot strongly typed languages!

That's the kind of answer I was looking for. So I can definitely see why
  strongly typed languages are safe and predictable and all, but why
(just out of interest) does PHP (and in the future phython?) have it?

Jeroen
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2006-04-02 11:30
(Received via mailing list)
Matthew Palmer wrote:
>>> => 0.75
>>> irb(main):004:0>
>> 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).
>
> So you perform the integer division 12 / 16, get a result of 0, and then
> cast that to a float 0.0?  Not exactly useful.  What you really need to do
> is perform a modulus operation and then do an integer division if the result
> of that is 0 and a floating point cast/division otherwise.  Quite expensive.
>

No I just wanted a float as the result of a division between two
integers. I know how to do that (call .to_f on one of the integers). I
was just wondering why Ruby behaves the way it does.

J
6805b35d0a8ea3ede0a7da2d4cf5ae77?d=identicon&s=25 Jonathan Weiss (Guest)
on 2006-04-02 11:54
(Received via mailing list)
>
> That's the kind of answer I was looking for. So I can definitely see why
>  strongly typed languages are safe and predictable and all, but why
> (just out of interest) does PHP (and in the future phython?) have it?

I can't comment about Python but PHP is like Perl weakly typed.

So in PHP/Perl

5 + "6" results in 11

While in Ruby (or e.g. Java) you have to say

5 + "6".to_i results in 11


Weakly typed languages do so because this behavious is somethimes handy
as you save some characters as you do not have to explicitly convert
types.

The problem is that this can become dangerous when you didn't want the
autoconversion but forgot to explicitly tell the interpreter. Especially
if one part of the input comes from the user. Say hello to many kinds of
user injected code like SQLinjection or other malicious behaviour.

E.g. what is

5 + "6a" ??

Depending on the language it could be

11
"56a"

Or an exception. Now if it is the string "56a" and you really need a
number further down in your code?


Jonathan
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2006-04-02 16:07
(Received via mailing list)
On 4/2/06, Jeroen Houben <jeroen@terena.nl> wrote:
> >>> irb(main):003:0> 12.0 / 16
> >
>
Well it would not make me happy, but I am all about choice :-)

class Fixnum
  def /(int)
    self.to_f / int
  end
end

p 12 / 16

pth
22ff4abebc9bfd299524bb14449d95dc?d=identicon&s=25 Erik van Oosten (Guest)
on 2006-04-03 09:29
(Received via mailing list)
irb(main):001:0> 12.quo(16)
=> 0.75

     Erik.


Jeroen Houben schreef:
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 Julian 'Julik' Tarkhanov (Guest)
on 2006-04-03 14:27
(Received via mailing list)
On 3-apr-2006, at 9:26, Erik van Oosten wrote:

> irb(main):001:0> 12.quo(16)
> => 0.75

Wow!
855c677aca7319a44da19fb583b9f320?d=identicon&s=25 Nicolas Cavigneaux (Guest)
on 2006-04-03 14:53
(Received via mailing list)
Le Lundi 03 Avril 2006 14:25, Julian 'Julik' Tarkhanov a écrit :
> On 3-apr-2006, at 9:26, Erik van Oosten wrote:
> > irb(main):001:0> 12.quo(16)
> > => 0.75

There's also 12.0 / 16.0. You muste tell ruby that you're using float
not
integer.

Bye.
D4e51fd9554030ab55c379fdc1a34826?d=identicon&s=25 Keith Lancaster (klancaster)
on 2006-04-03 15:46
Julian 'Julik' Tarkhanov wrote:
> On 3-apr-2006, at 9:26, Erik van Oosten wrote:
>
>> irb(main):001:0> 12.quo(16)
>> => 0.75
>
> Wow!

I anxiously await the day when I can see an example like that and say "I
knew that". Of course, given my memory these days, maybe I *had* seen
that... :-)

Keith
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-04-03 15:56
Jonathan Weiss wrote:
> No, this is exactly why Ruby is a strong dynamically typed language.
> Doing autoconversion is a very bad thing as unintentioned things can
> happen.
>
> If I tell the computer to dived two integers, I get an integer back.
>
> That's the whole point abouot strongly typed languages!

irb(main):002:0> 10000000.class
=> Fixnum
irb(main):003:0> 10000000.class
=> Fixnum
irb(main):004:0> (10000000 * 1000000).class
=> Bignum

Except where Ruby does autoconversion to make your life easier.

For the record, I don't think Ruby should do the int->float conversion,
but it's for reasons of precision, not type purity.

What does Smalltalk do in similar circumstances ?  (not a leading
question, I genuinely don't know :)

Alan
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-04-03 16:51
(Received via mailing list)
2006/4/3, Alan Francis <alancfrancis@gmail.com>:
> What does Smalltalk do in similar circumstances ?  (not a leading
> question, I genuinely don't know :)

The version of Squeak I tested has real Fraction objects.  Which means
12/16 gets converted to a 3/4 Fraction.

Bye !
22ff4abebc9bfd299524bb14449d95dc?d=identicon&s=25 Erik van Oosten (Guest)
on 2006-04-03 17:40
(Received via mailing list)
irb(main):001:0> require 'mathn'
=> true
irb(main):002:0> 12 / 16
=> 3/4

;)

     Erik.


Francois Beausoleil schreef:
This topic is locked and can not be replied to.