I want to add factorial method mathn.rb file as feature of Math module.
#3 [ruby-core:61027] Updated by zzak (Zachary Scott) almost 4 years ago
- Category set to lib
- Status changed from Open to Feedback
- Assignee set to zzak (Zachary Scott)
- Target version set to 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 [ruby-core:64086] Updated by zzak (Zachary Scott) over 3 years ago
- Status changed from Feedback to Closed
#5 [ruby-core:66154] Updated by martin_vahi (Martin Vahi) over 3 years 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%).
#7 [ruby-core:66175] Updated by gogotanaka (Kazuki Tanaka) over 3 years 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.
mathn became deprecated. #10169
#8 [ruby-core:66249] Updated by martin_vahi (Martin Vahi) over 3 years 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
sin(x) cos(x) gamma(x) etc...
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>) etc...
determines some "default" error "size" through its rounding. Due to the exponent mechanism of the IEEE_754, the same property that gives
fd_big=(9**99).to_f 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. :-)
#11 [ruby-core:85305] Updated by martin_vahi (Martin Vahi) 18 days ago
Given that my proposal is more general than the obsoleted mathn.rb,
would it be helpful, if I filed a new Feature Request that would
suggest an addition of a new class to the Ruby stdlib?
For example, the function apply_binary_operator might be
wrapped to a class named "Math_optimizations" and
the watershed concatenation algorithm would be the default
heuristic for the
The heuristic might be later changed by adding
an optional 4. argument to the function call.
The signature would look like:
def Math_optimizations.apply_binary_operator( x_identity_element,array_in, func_operator_that_might_be_noncommutative, heuristic="watershed_concatenation")