Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Float Arithmetic: Return A Fixnum When Float == Fixnum

Reply
Thread Tools

Float Arithmetic: Return A Fixnum When Float == Fixnum

 
 
MaggotChild
Guest
Posts: n/a
 
      12-01-2009
>> 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?
 
Reply With Quote
 
 
 
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      12-01-2009
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      12-01-2009
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
> (E-Mail Removed)


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

Best,
--┬*
Marnen┬*Laibow-Koser
http://www.marnen.org
(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
MaggotChild
Guest
Posts: n/a
 
      12-01-2009
On Dec 1, 5:52*am, Marnen Laibow-Koser <(E-Mail Removed)> 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
 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      12-01-2009
MaggotChild wrote:
> On Dec 1, 5:52´┐Żam, Marnen Laibow-Koser <(E-Mail Removed)> 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
(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Glen F. Pankow
Guest
Posts: n/a
 
      12-01-2009
Marnen Laibow-Koser wrote:
> MaggotChild wrote:
>> On Dec 1, 5:52=EF=BF=BDam, Marnen Laibow-Koser <(E-Mail Removed)> 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
> (E-Mail Removed)


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


 
Reply With Quote
 
MaggotChild
Guest
Posts: n/a
 
      12-02-2009
On Dec 1, 12:24*pm, "Glen F. Pankow" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Why Fixnum===Fixnum is false? Heesob Park Ruby 5 05-14-2009 12:31 AM
string and fixnum(float) concatenation.... Jun Young Kim Ruby 4 02-24-2009 07:47 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments