Skip to content

BigDecimal Arithmetics for Scala (BigDecimalMath4Scala)

Notifications You must be signed in to change notification settings

janesser/BigDecimalMath4Scala

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BigDecimal Arithmetics for Scala (BigDecimalMath4Scala)

Be as precise as you like (or as much as you can afford in terms of runtime). BigDecimal precision is maintained throughout calculations. That means function parameter and result have equal scale. ZERO is a special case anyway.

Maven Site at: site

Actually scala.math.BigDecimal and alike are almost equal (or worse) than their Java correspondants java.math.BigDecimal. Having some functional structures at hand may improve the way to think about computating formulas.

Intention of this extending library is:

  • to make use of BigDecimal easier
  • to make code more fluent to read
  • to give control over the scale and precision of calculation
  • to provide a rich feature set of numeric computations (which match the every requested precision).

Steps to integrate:

  • add snapshot repository

    • sbt-style

        resolvers += "bigdecimalmath-snapshots" at "https://github.com/janesser/BigDecimalMath4Scala/tree/mvn-repo/de/esserjan/scala/bigdecimalmath_2.12"
      
    • maven-style

        <repositories>
        	<repository>
        		<id>bigdecimalmath-snapshots</id>
        		<url>https://github.com/janesser/BigDecimalMath4Scala/tree/mvn-repo/de/esserjan/scala/bigdecimalmath_2.12</url>
        		<releases>
        			<enabled>false</enabled>
        		</releases>
        		<snapshots>
        			<enabled>true</enabled>
        		</snapshots>
        	</repository>
        </repositories>
      
  • add dependency to your pom.xml

    • sbt-style

        libraryDependencies += "de.esserjan.scala" % "bigdecimalmath_2.12" % "0.0.1-SNAPSHOT"
      
    • maven-style

        <dependency>
        	<groupId>de.esserjan.scala</groupId>
        	<artifactId>bigdecimalmath_2.12</artifactId>
        	<version>0.0.1-SNAPSHOT</version>
        </dependency>
      
  • insert into your code

      import de.esserjan.scala.BigDecimalMath._
    

ScalaDoc is available here.

BigIntOps

Negation by multiplication with BigInt(-1). As long as the number was not zero before, the result's signum multiplied with the input signum is always "-1".

Factorial with tail-recursive ideomatic implementation. A negative signum will not affect the calculation but result in a negative result.

BigDecimalOps

neg by multiplication with BigDecimal(-1). Last mentioned inherits MathContext from input. As long as the number was not zero before, the result's signum multiplied with the input signum is always "-1".

	BigDecimal(x).neg

inv one by x. invertible indicates if inv may be called.

	BigDecimal(x).inv

factorial based on BigInt#factorial. If not BigDecimal#isWhole this operation has no result, e.g. thows an IllegalArgumentException. TODO Guess precision required for result, because it is a magnitude more than the input.

	BigDecimal(x).factorial

Trigonometry

Sine (lat. Sinus), except for trivial cases, is interpolated by a taylor expansion.

Cosine (lat. Cosinus), except for trivial cases, is based on the sinus-function.

The approximation is dynamically continued until the precision reaches the input's ULP.

Roots

Square-root evaluated by newton-iteration.

Compaction of BigDecimal is done with reparsing toEngineeringString(), so that the result is deflated to some power-of-ten term.

The approximation is dynamically continued until the precision reaches the input's ULP.

Exp

Exp, except for trivial cases, is interpolated by a taylor expansion.

	BigDecimal(x).exp

Ln, except for trivial cases, is interpolated by a taylor expansion.

	BigDecimal(x).ln

The approximation is dynamically continued until the precision reaches the input's ULP.

Misc.

BigDecimalTools

Some generic computation algorithms.

  • evalSeries Evaluate series summation. The goodEnough criterium function is going to determine the number of components summed dynamically, given the MathContext, the number of already summed components, the current component's and the accumulator's value. Some generic goodEnough criteria are predefined.

      trait EvalSeriesGoodEnough extends Function4[java.math.MathContext, Int, BigDecimal, BigDecimal, Boolean]
      case class SinceIndexGoodEnough(val n: Int) extends EvalSeriesGoodEnough {
      	def apply(mc: java.math.MathContext, k: Int, fkx: BigDecimal, acc: BigDecimal): Boolean = k > n
      }
      case object SufficientlyPreciseGoodEnough extends EvalSeriesGoodEnough {
      	def apply(mc: java.math.MathContext, k: Int, fkx: BigDecimal, acc: BigDecimal) = (fkx + acc).round(mc) == acc.round(mc)
      }
      
      def evalSeries(x: BigDecimal,
      	f: (Int) => (BigDecimal => BigDecimal),
      	goodEnough: EvalSeriesGoodEnough):BigDecimal
    
  • newton Approximate by iteration. The goodEnough criterium function implies the determination of the iteration, given the number of the iteration step and the current approximation. Some generic goodEnough criteria are predefined.

      trait NewtonGoodEnough extends Function2[Int, BigDecimal, Boolean]
      case class NewtonResultPrecise(fn: BigDecimal => BigDecimal, precision: BigDecimal) extends NewtonGoodEnough {    
      	def apply(i: Int, x: BigDecimal): Boolean = {
      		val mc = new java.math.MathContext(x.precision)
      		val delta = x - fn(x)
      		delta.abs <= precision
      	}
      }
    
      def newton(
      	x: BigDecimal,
      	fn: BigDecimal => BigDecimal,
      	goodEnough: NewtonGoodEnough): BigDecimal
    

Licence

BigDecimalMath is provided to you as free software under the Apache License, Version 2.0.

Releases

0.0.2 Q1'17 bigdecimalmath_2.12 upgraded to scala 2.12 0.0.1 Q2'14 bigdecimalmath_2.11 with a basic set of operations.

Contribution

Feel free to offer pull requests or create issues for your suggestions.

Contact

januszesser@twitter

Flattr this

See also

Based on A Java Math.BigDecimal Implementation of Core Mathematical Functions.

Inspired by BigDecimalMath.scala.

Similar to Spire.

About

BigDecimal Arithmetics for Scala (BigDecimalMath4Scala)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages