Feature #9528

mathn.rb library

Added by Umair Amjad over 1 year ago. Updated 11 months ago.

Assignee:Zachary Scott


I want to add factorial method mathn.rb file as feature of Math module.

the_code.rb Magnifier - contains it all, including demo (7.49 KB) Martin Vahi, 11/09/2014 06:33 AM

run_demo.bash - shows, how to run the demo (591 Bytes) Martin Vahi, 11/09/2014 06:34 AM


#1 Updated by Umair Amjad over 1 year ago

Please guide how can I contribute, I have code written on my local.

#2 Updated by Charlie Somerville over 1 year ago

  • Priority changed from 5 to Normal

Hi Umair,

You should attach a .patch file and wait for feedback.

#3 Updated by Zachary Scott over 1 year ago

  • Category set to lib
  • Status changed from Open to Feedback
  • Assignee set to Zachary Scott
  • Target version set to current: 2.2.0

If you're using subversion you can use the svn diff or svn di command to output a patch, and then just upload the file. For example:

svn diff lib/mathn.rb > my-patch-to-mathn.diff

You can read more about svn diff in the svnbook

Bonus points, when requesting a feature please try to give as many details as possible about your feature:

1) What is your proposed change?
2) Why would people use it? (use cases)
3) Why should this be added to ruby?

Are just a few of the questions you could try to answer. We have some more detailed documentation on contributing.rdoc

#4 Updated by Zachary Scott about 1 year ago

  • Status changed from Feedback to Closed

Its been 5 months without any feedback, so I'm closing this.

If you have any specific questions about how to contribute, please feel free to reply or email the list ruby-core@ruby-lang.org or email me personally at zzak@ruby-lang.org

May the Ruby be with you..

#5 Updated by Martin Vahi 11 months ago

Well, I have the same wish, except that I also have a demo code available.

A speed optimized demo can be downloaded from


and run by


For 100000.factorial the speed difference is literally roughly 20-fold (not just 20%).

#6 Updated by Hiroshi SHIBATA 11 months ago

  • Status changed from Closed to Open

#7 Updated by gogo tanaka 11 months ago

I like your propose. But I'd be glad if Math.gamma(x) could make sense for you : )


Even if we'er gonna add new method, except mathn might be better.
Because mathn became deprecated. #10169

Thanks, gogo.

#8 Updated by Martin Vahi 11 months ago

I wasn't aware of the existence of the gamma function before reading Your comment. I guess I got a bit smarter due to Your comment. Thank You for that. :-)

According to some sources, including the


it seems to me that the gamma function is an approximation. I think that a clean solution for functions that are based on approximations should always have a maximum error size as a second argument. For example,


is actually calculated through series and is never absolutely correct. Therefore the


should be

sin(x,absolute_value_of_max_error=<some default value>)
cos(x,absolute_value_of_max_error=<some default value>)
gamma(x,absolute_value_of_max_error=<some default value>)

The IEEE_754


determines some "default" error "size" through its rounding. Due to the exponent mechanism of the IEEE_754, the same property that gives

puts "No difference detected." if fd_big == (fd_big+1.to_f)

there is no single minimum approximation-result-changing value for the error size. Therefore, to find a clean solution for the proper implementation of the gamma/sin/cos/etc. function(s), further work has to be done and that's probably going to be pretty complex and time consuming. However, it is a fact that the current Math.gamma(x) implementation is flawed, because it gives IEEE_754 "infinity" for Math.gamma(10000). That probably limits cryptography related experiments.

The good news is that it seems (at least to me) that dependency wise factorial of integers is very general. Even some forms of the gamma(x) formulae depend on factorials of integers. That's why it seems to me that the proposed


do not clutter the stdlib. That is to say, as of my current comment, I stick with my initial proposal.

Well, one way or the other, I still thank You all for Your answers and efforts. :-)

Also available in: Atom PDF