Float Arithmetic: Return A Fixnum When Float == Fixnum

M

MaggotChild

j = 5/2.0
=> 2.5

I'm fine with this
=> 2.0

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

Is there a "better" way?
 
M

Marnen Laibow-Koser

MaggotChild said:
=> 2.5

I'm fine with this

=> 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.
=> 2

Is there a "better" way?

Why even bother?

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
M

Marnen Laibow-Koser

Marnen said:
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.


Why even bother?

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)

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

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
M

MaggotChild

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.
Why even bother?

OCD
 
G

Glen F. Pankow

Marnen said:
MaggotChild said:
=20
You can always redefine Float.to_s. Changing the underlying type,=20
though, is probably a poor idea.
=20
=20
Apply your OCD in the right places, though! :)
=20
Best,
--=20
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address 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
 
M

MaggotChild

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top