Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

round with precission should be available to all Numeric objects #918

Closed
lighthouse-import opened this Issue May 16, 2011 · 4 comments

Comments

Projects
None yet
1 participant

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6554
Created by Wojciech Wnętrzak - 2011-03-10 10:06:24 UTC

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: 532b770 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?

Imported from Lighthouse.
Comment by Josh Kalderimis - 2011-03-10 10:54:13 UTC

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

Imported from Lighthouse.
Comment by Wojciech Wnętrzak - 2011-03-10 11:29:32 UTC

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

Imported from Lighthouse.
Comment by Wojciech Wnętrzak - 2011-03-10 12:07:14 UTC

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

Attachments saved to Gist: http://gist.github.com/971810

@tomstuart tomstuart pushed a commit to econsultancy/rails that referenced this issue May 17, 2011

@krisselden @wycats krisselden + wycats In PostgreSQLAdapter, switch tables query to use current_schemas func…
…tion [#918 state:resolved]

Signed-off-by: wycats <wycats@gmail.com>
afb786a

@hisas hisas pushed a commit to hisas/rails that referenced this issue May 9, 2017

@jeremy jeremy Merge pull request #918 from kjg/part_return_value_non_existant_header
Account for possibility of absent delivery-status headers
1812977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment