Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

round with precission should be available to all Numeric objects

#6554

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: https://github.com/rails/rails/commit/532b77077fc97211cb9f8d047116a... 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?

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

State: new
Milestone: none
Assigned to: Josh Kalderimis Josh Kalderimis
Importance: Low

Activity

  1. Josh Kalderimis
    Josh Kalderimis
    • 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.

    Josh

    March 10th, 2011 @ 10:54 AM

  2. Wojciech Wnętrzak
    Wojciech Wnętrzak

    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
    

    March 10th, 2011 @ 11:29 AM

  3. Wojciech Wnętrzak
    Wojciech Wnętrzak

    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).

    March 10th, 2011 @ 12:07 PM