# Float Arithmetic: Return A Fixnum When Float == Fixnum

**URL:** <https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655>\
**Category:** Ruby\
**Created:** [December 1, 2009, 9:45am UTC](https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655 "2009-12-01T09:45:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 1, 2009, 9:45am UTC](https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655/1 "2009-12-01T09:45:55Z")

</div>

> > 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?

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 1, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655/2 "2009-12-01T14:52:52Z")

</div>

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.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 1, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655/3 "2009-12-01T15:15:48Z")

</div>

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](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

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

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 1, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655/4 "2009-12-01T17:26:59Z")

</div>

MaggotChild wrote:

> On Dec 1, 5:52ï¿½am, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 1, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655/5 "2009-12-01T17:20:41Z")

</div>

On Dec 1, 5:52 am, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> > > > j == j.to\_i ? j.to\_i : j  
> > > > =\> 2
> 
> > Is there a “better” way?
> 
> Why even bother?

OCD

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 1, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655/6 "2009-12-01T21:24:46Z")

</div>

Marnen Laibow-Koser wrote:

> > > > =\> 2  
> > > > removed\_email\_address@domain.invalid  
> > > > 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”

Glen

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 2, 2009, 5:30am UTC](https://www.ruby-forum.com/t/float-arithmetic-return-a-fixnum-when-float-fixnum/179655/7 "2009-12-02T05:30:31Z")

</div>

On Dec 1, 12:24 pm, “Glen F. Pankow” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
