Float Arithmetic: Return A Fixnum When Float == Fixnum

Discussion in 'Ruby' started by MaggotChild, Dec 1, 2009.

  1. MaggotChild

    MaggotChild Guest

    >> j = 5/2.0
    => 2.5

    I'm fine with this

    >> j = 4/2.0

    => 2.0

    Useless precision -for me. I want 2!

    >> j == j.to_i ? j.to_i : j

    => 2

    Is there a "better" way?
    MaggotChild, Dec 1, 2009
    #1
    1. Advertising

  2. MaggotChild wrote:
    >>> j = 5/2.0

    > => 2.5
    >
    > I'm fine with this
    >
    >>> j = 4/2.0

    > => 2.0
    >
    > Useless precision -for me. I want 2!


    The notation is misleading. It's not a question of precision, but of
    type. You can think of 2.0 as 2:Float, not 2 to 2 significant figures.

    >
    >>> j == j.to_i ? j.to_i : j

    > => 2
    >
    > Is there a "better" way?


    Why even bother?

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 1, 2009
    #2
    1. Advertising

  3. Marnen Laibow-Koser wrote:
    > MaggotChild wrote:
    >>>> j = 5/2.0

    >> => 2.5
    >>
    >> I'm fine with this
    >>
    >>>> j = 4/2.0

    >> => 2.0
    >>
    >> Useless precision -for me. I want 2!

    >
    > The notation is misleading. It's not a question of precision, but of
    > type. You can think of 2.0 as 2:Float, not 2 to 2 significant figures.
    >
    >>
    >>>> j == j.to_i ? j.to_i : j

    >> => 2
    >>
    >> Is there a "better" way?

    >
    > Why even bother?
    >
    > Best,
    > -- 
    > Marnen Laibow-Koser
    > http://www.marnen.org
    >


    It occurs to me that you may also want to play with Rational.

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 1, 2009
    #3
  4. MaggotChild

    MaggotChild Guest

    On Dec 1, 5:52 am, Marnen Laibow-Koser <> wrote:
    > MaggotChild wrote:
    > >>> j = 5/2.0

    > > => 2.5

    >
    > > I'm fine with this

    >
    > >>> j = 4/2.0

    > > => 2.0

    >
    > > Useless precision -for me. I want 2!

    >
    > The notation is misleading.  It's not a question of precision, but of
    > type.  You can think of 2.0 as 2:Float, not 2 to 2 significant figures.


    Yes, I do, but when it comes to printing, Float does not.

    >
    > >>> j == j.to_i ? j.to_i : j

    > > => 2

    >
    > > Is there a "better" way?

    >
    > Why even bother?


    OCD
    MaggotChild, Dec 1, 2009
    #4
  5. MaggotChild wrote:
    > On Dec 1, 5:52�am, Marnen Laibow-Koser <> wrote:
    >>
    >> The notation is misleading. �It's not a question of precision, but of
    >> type. �You can think of 2.0 as 2:Float, not 2 to 2 significant figures.

    >
    > Yes, I do, but when it comes to printing, Float does not.


    You can always redefine Float.to_s. Changing the underlying type,
    though, is probably a poor idea.

    >
    >>
    >> >>> j == j.to_i ? j.to_i : j
    >> > => 2

    >>
    >> > Is there a "better" way?

    >>
    >> Why even bother?

    >
    > OCD


    Apply your OCD in the right places, though! :)

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Dec 1, 2009
    #5
  6. Marnen Laibow-Koser wrote:
    > MaggotChild wrote:
    >> On Dec 1, 5:52=EF=BF=BDam, Marnen Laibow-Koser <> wro=

    te:
    >>> The notation is misleading. =EF=BF=BDIt's not a question of precision=

    , but of
    >>> type. =EF=BF=BDYou can think of 2.0 as 2:Float, not 2 to 2 significan=

    t figures.
    >> Yes, I do, but when it comes to printing, Float does not.

    >=20
    > You can always redefine Float.to_s. Changing the underlying type,=20
    > though, is probably a poor idea.
    >=20
    >>>>>> j =3D=3D j.to_i ? j.to_i : j
    >>>> =3D> 2
    >>>> Is there a "better" way?
    >>> Why even bother?

    >> OCD

    >=20
    > Apply your OCD in the right places, though! :)
    >=20
    > Best,
    > --=20
    > Marnen Laibow-Koser
    > http://www.marnen.org
    >


    Ugly, but what the hey:

    j =3D 2.0
    sprintf('%g', j) --> "2"

    j =3D 2.50000001
    sprintf('%g', j) --> "2.5"

    Works on integers, too:

    j =3D 2
    sprintf('%g', j) --> "2"

    Glen
    Glen F. Pankow, Dec 1, 2009
    #6
  7. MaggotChild

    MaggotChild Guest

    On Dec 1, 12:24 pm, "Glen F. Pankow" <> wrote:
    >> MaggotChild wrote:
    >>
    >> j == j.to_i ? j.to_i : j
    >> => 2
    >> Is there a "better" way?

    >
    > Ugly, but what the hey:
    >
    > j = 2.0
    > sprintf('%g', j) --> "2"
    >
    > j = 2.50000001
    > sprintf('%g', j) --> "2.5"
    >
    > Works on integers, too:
    >
    > j = 2
    > sprintf('%g', j) --> "2"


    Ah, I knew it -a better way. And to think, it was with a printf
    format... All those years of C down the toilet
    MaggotChild, Dec 2, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    621
  2. joshc
    Replies:
    5
    Views:
    553
    Keith Thompson
    Mar 31, 2005
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,533
    James Kanze
    Oct 8, 2009
  4. Jun Young Kim
    Replies:
    4
    Views:
    80
    Jun Young Kim
    Feb 24, 2009
  5. Heesob Park

    Why Fixnum===Fixnum is false?

    Heesob Park, May 13, 2009, in forum: Ruby
    Replies:
    5
    Views:
    122
    Joel VanderWerf
    May 14, 2009
Loading...

Share This Page