Forum: Ruby on Rails slightly OT - Ruby division

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.
E2489efc6ec10ec818b71965909ea109?d=identicon&s=25 Jin Lee (Guest)
on 2005-12-15 23:28
(Received via mailing list)
Hey all,

In my code, it seems that when I divide two integers, the result is an
integer. Is there any way to make it such that when I divide two
integers,
the result is a double?

Any help appreciated,

Jin
3a83969376c805ef5b6042191fdb0ff3?d=identicon&s=25 Andreas S. (andreas)
on 2005-12-15 23:33
Jin Lee wrote:
> Hey all,
>
> In my code, it seems that when I divide two integers, the result is an
> integer. Is there any way to make it such that when I divide two
> integers,
> the result is a double?

No, you have to convert one of them to a float first:
x.to_f/y
55742d912a9003080d202c5522d4de00?d=identicon&s=25 Ken Bowley (Guest)
on 2005-12-15 23:34
(Received via mailing list)
int1 / int2.to_f

this will force it into a float.
01d7a451018ac15518f425078ef00c40?d=identicon&s=25 Phillip Hutchings (Guest)
on 2005-12-15 23:34
(Received via mailing list)
On 16/12/2005, at 11:27 AM, Jin Lee wrote:

> In my code, it seems that when I divide two integers, the result is
> an integer. Is there any way to make it such that when I divide two
> integers, the result is a double?

Ruby never automatically casts (except between Fixnum and Bignum), so
you have to cast to float using Fixnum#to_f

1.to_f / 5.to_f 	# => 0.2


--
Phillip Hutchings
phillip.hutchings@sitharus.com
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2005-12-15 23:37
(Received via mailing list)
This might help:

>> a = 1
=> 1
>> b = 2
=> 2
>> a / b
=> 0
>> a.to_f / b
=> 0.5
>> a / b.to_f
=> 0.5
>> a.to_f / b.to_f
=> 0.5

Regards,
Douglas
24499e811f19343c7dc406104e9b5785?d=identicon&s=25 Belorion (Guest)
on 2005-12-15 23:37
(Received via mailing list)
On 12/15/05, Jin Lee <jinslee@gmail.com> wrote:
>
> Hey all,
>
> In my code, it seems that when I divide two integers, the result is an
> integer. Is there any way to make it such that when I divide two integers,
> the result is a double?
>


a = 10
b = 3
a/b.to_f

-Matt
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2005-12-15 23:40
(Received via mailing list)
On 12/16/05, Jin Lee <jinslee@gmail.com> wrote:
> Hey all,
>
>  In my code, it seems that when I divide two integers, the result is an
> integer. Is there any way to make it such that when I divide two integers,
> the result is a double?
>
>  Any help appreciated,

irb(main):001:0> 5/3
=> 1
irb(main):002:0> 5.to_f/3
=> 1.66666666666667
irb(main):003:0> 5*1.0/3
=> 1.66666666666667

martin
2515bd7fdfa385f8146228ea77583772?d=identicon&s=25 victor (Guest)
on 2005-12-16 17:54
(Received via mailing list)
Jin Lee <jinslee@...> writes:
> Hey all,
> In my code, it seems that when I divide two integers, the result is an
> integer. Is there any way to make it such that when I divide two
> integers, the result is a double?
> Any help appreciated,
> Jin

Apparently Float is not only a class, but also a method! I gather, then,
that
Ruby is sort of Lisp-2 (non-methods and methods have different
namespaces).

#> Float(3)
3.0
#> Float(22)/7
3.14285714285714
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2005-12-16 18:00
(Received via mailing list)
> Apparently Float is not only a class, but also a method! I gather, then, that
> Ruby is sort of Lisp-2 (non-methods and methods have different namespaces).
>
> #> Float(3)
> 3.0
> #> Float(22)/7
> 3.14285714285714

#to_f is usually used instead:

#> 3.to_f
3.0
#> 22.to_f / 7
> 3.14285714285714

--
rick
http://techno-weenie.net
This topic is locked and can not be replied to.