On Tue, 20 Mar 2001, Dave Thomas wrote:
> Jim Freeze <jim / freeze.org> writes:
> > On Tue, 20 Mar 2001, Hee-Sob Park wrote:
> >
> > > I think it's difference in operator precedence.
> > >
> > > Try following code:
> > >
> > > p (2.4/0.2).to_i  # -> 12.0
> > > p ((2.4/0.2).to_i) #-> 11
> > >
> > Can you explain this?
> > I would call this a bug.
>
> Remember that method calls take parameters in parentheses. The
> examples probably make more sense it you remove the space after the
> name of the method call ('p' in this case)
>
>  p(2.4/0.2).to_i
>     is equivalent to   (p(2.4/.2)).to_i
>
>  p((2.4/0.2).to_i)
>
>
> Why the difference? 2.4/0.2 isn't represented exactly as a binary
> number, and to_i truncates floats.
>

Hmmm..., yes, that makes it clearer.
I guess what bothers me now is that 2.4/0.2 is not printed by p as
11.99999999. (12.0).to_i should never equal 11.

Is p doing something to floats before it prints them?

Here is my little test:

irb(main):009:0> p 11.99999999
11.99999999
nil
irb(main):010:0> p 11.999999999
12.0
nil

It seems like p is doing a fixed fractional conversion before printing.
Does ruby have internal accuracy vs precision variables that can be
adjusted?

=========================================================
Jim Freeze
jim / freeze.org
---------------------------------------------------------
No comment at this time.
http://www.freeze.org
=========================================================