This project is archived and is in readonly mode.

#6554 new
Wojciech Wnętrzak

round with precission should be available to all Numeric objects

Reported by Wojciech Wnętrzak | March 10th, 2011 @ 10:06 AM

To be consistent with Ruby 1.9 where we can call round with precision argument on any Numeric object, rounding extension from activesupport should be available to all Numeric subclasses, not only in Float.

After this commit: this is done only for ruby versions less than 1.9.
Calling round with precision on Fixnum is useful with negative precision:

 #=> 22.round(-1)
 #=> 20

I tried to move this extension from float to numeric, but it doesn't work since all subclasses of Numeric has own implementation of round method.
Solution can be to extend all classes directly: Fixnum, Float, Bignum, etc, but I'm not sure if this is right.
Any suggestions?

Comments and changes to this ticket

  • Josh Kalderimis

    Josh Kalderimis March 10th, 2011 @ 10:54 AM

    • Assigned user set to “Josh Kalderimis”
    • Importance changed from “” to “Low”

    Hi Wojciech,

    I think this patch has merit. The first step is to create some failing test cases, and then to take a stab at some code.

    Have a look at the present extensions as the implementation is different for each class type.

    If you have any questions just ping me.


  • Wojciech Wnętrzak

    Wojciech Wnętrzak March 10th, 2011 @ 11:29 AM

    There are only Integer and Float as Numeric subclasses (Bignum and Fixnum are subclasses of Integer), so adding rounding extension to Integer should be enough.
    However after playing a while with ruby 1.9.2, there are strange results:

    ruby-1.9.2-p180 :034 > (66**10).class
     => Fixnum 
    ruby-1.9.2-p180 :035 > 66**10
     => 1568336880910795776 
    ruby-1.9.2-p180 :036 > (66**10).round(2)
     => 1568336880910795800.0 
    ruby-1.9.2-p180 :037 > (77**10).class
     => Bignum 
    ruby-1.9.2-p180 :038 > 77**10
     => 7326680472586200649 
    ruby-1.9.2-p180 :039 > (77**10).round(2)
     => 7326680472586201000.0

    After extending ruby 1.8.7 Integer class with round method (same as for Float):

    ree-1.8.7-2011.03 :020 > (66**10).class
     => Fixnum 
    ree-1.8.7-2011.03 :021 > 66**10
     => 1568336880910795776 
    ree-1.8.7-2011.03 :022 > (66**10).round(2)
     => 1.5683368809108e+18 
    ree-1.8.7-2011.03 :023 > (77**10).class
     => Bignum 
    ree-1.8.7-2011.03 :024 > 77**10
     => 7326680472586200649 
    ree-1.8.7-2011.03 :025 > (77**10).round(2)
     => 7.3266804725862e+18
  • Wojciech Wnętrzak

    Wojciech Wnętrzak March 10th, 2011 @ 12:07 PM

    I'm adding a patch with rounding extension to Integer.
    There is a little difference to Float extension - at the end I'm calling to_i to be sure that Float is not returned (in Ruby 1.9 class is not changed after calling round).

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>