Implementing BigDecimal.power()
-------------------------------
Key: JRUBY-1156
URL: http://jira.codehaus.org/browse/JRUBY-1156
Project: JRuby
Issue Type: Improvement
Components: Core Classes/Modules
Affects Versions: JRuby 1.0.0RC3
Environment: Mac OS X (10.4.9), Java 1.5.0_07
Reporter: David Rupp
Attachments: BigDecimal_power_drupp_v1.diff
The existing implementation of BigDecimal.power() is broken -- e.g.,
BigDecimal.new("10").power(1).to_s("F") returns "100". If it weren't broken, it
would be very inefficient for large numbers in the exponent, requiring O(n)
multiplications. I've replaced the default implementation (patch included) with
the "repeated squaring" technique, as described in the Javadoc for
java.math.BigDecimal.pow() (as of Java 5), along with a workaround for negative
exponents, which even Java 5 does not handle in the basic pow() method.
I've done some informal benchmarking that show that this algorithm, while still
an order of magnitude slower than C Ruby, is about 4 orders of magnitude faster
than the existing implementation. Also, the existing implementation starts
bogging down for relatively small numbers (>= 10**16), where C Ruby and the
repeated squaring algorithm both handle much larger values in the exponent
easily. Unfortunately, this speedup holds only when running a JRuby
non-interactively. In jirb, it's still pretty pokey. Anyone have any thoughts
as to why that might be?
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email