This project is archived and is in readonly mode.
round with precission should be available to all Numeric objects
-
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
-
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.0After 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
I'm adding a patch with rounding extension to Integer.
There is a little difference to Float extension - at the end I'm callingto_ito be sure that Float is not returned (in Ruby 1.9 class is not changed after calling round).
