The document discusses number calculations in Scala. It covers how Scala uses implicit conversions to provide natural syntax for decimal numbers, handles overflows using types like SafeLong, and enables precise division through types like Real. The document also recommends the Spire math library for additional number types and capabilities like Rational and Complex numbers. In conclusion, Scala brings natural syntax through operators and literals, interoperability through implicit conversions, and opportunities for improvement in number handling demonstrated by libraries.
1 of 31
Downloaded 12 times
More Related Content
Bring your calculations to Scala!
1. Bring Your Calculations to:
Martin.Skarsaune@kantega.no
Kantega ¨C Norwegian software craftsmanship since 2003
¸ß
ñR
¶¡
#DV14 #ScalaCalculations @MSkarsaune
3. Java Number Types - Integer
? Exact representation of values within range
? Interoperable through widening conversion
Integer
float double byte short int long
#DV14 #ScalaCalculations @MSkarsaune
4. Java Number Types ¨C Floating point
? Approximate representation of numeric values
? NEVER use for monetary amounts
? Interoperable with integer types through widening
conversion
Floating point Integer
float double byte short int long
#DV14 #ScalaCalculations @MSkarsaune
5. Java Number Types - Boxed
? Immutable
? Calculation possible via primitives
? Interoperability through widening conversion
Floating point Integer Number
boxing
unboxin
Float Double Byte Short Integer Long BigInteger BigDecimal
float double byte short int long
g
#DV14 #ScalaCalculations @MSkarsaune
6. Java Object Number - Extended
? To represent large numbers
? Calculations with methods
? Cumbersome conversions
Poor
interoperabilit
y
Floating point Integer Number ¡°Infinite¡±
Float Double Byte Short Integer Long BigInteger BigDecimal
float double byte short int long
#DV14 #ScalaCalculations @MSkarsaune
12. Decimal representation
? Integer value and scale
? 123456789
Scale = 3
123456789
1000
? Scale is important! Know your scale!
#DV14 #ScalaCalculations @MSkarsaune
13. Decimal notation
? Smalltalk: 2.5s2
? Java: new BigDecimal("2.50")
new BigDecimal(2.5).setScale(2)
? Scala: val dec : BigDecimal = 2.50
How is this possible?
#DV14 #ScalaCalculations @MSkarsaune
14. Scala Implicit Type Conversion
? Implicit type conversion
? Compiler looks for implicit function to convert source type
to target type
? Object scala.math.BigDecimal:
/** Implicit conversion from `Double` to `BigDecimal`. */
implicit def double2bigDecimal(
d: Double): BigDecimal = decimal(d)
#DV14 #ScalaCalculations @MSkarsaune
15. Decimal computation
? Task multiply decimal dec by 100:
? Smalltalk: dec * 100
? Java: dec.multiply(new BigDecimal(100));
? Scala: dec * 100
What does this mean?
#DV14 #ScalaCalculations @MSkarsaune
16. Infix, symbols + implicit conversion
? Special characters as method identifiers (+ , - , ?, ¡Ì, ?, ¦Ð
¡)
? Infix notation: num.*(100) <==> num * 100
? Class scala.math.BigDecimal:
/** Multiplication of BigDecimals */
def * (that: BigDecimal): BigDecimal
= new BigDecimal(
this.bigDecimal.multiply(that.bigDecimal, mc), mc)
? Also works: 100 * num
#DV14 #ScalaCalculations @MSkarsaune
17. Decimal calculation summary
? Natural syntax
? Operators and literals
? Interoperable with other number types
? Withouth having to change existing number types
#DV14 #ScalaCalculations @MSkarsaune
18. Spire math
? Scala calculation library:
https://github.com/non/spire
? Generic algorithms
? Extended number types, such as
? Rational
? Real
? Complex
? Macros provide additional number literal syntax
#DV14 #ScalaCalculations @MSkarsaune
25. (Some) Spire number types
ScalaNumber
SafeLong Real Rational Complex
? Real ensures exact division and always chooses the
most precise representation
#DV14 #ScalaCalculations @MSkarsaune
26. Real example
val numerator: Real = 2 //Real$Exact(2)
val fraction = numerator / 3 //Real$Exact(2/3)
fraction * 3 //Real$Exact(2)
fraction * Real.pi
//Real$Inexact(2.0943951023931954923084289221863352561314)
#DV14 #ScalaCalculations @MSkarsaune
27. Some conversion code of our own
¡.
final class DoubleSyntax(val value : Double) {
def scaled(scale : Int) = BigDecimal(value).setScale(scale)
def i = Complex(0.0, value)
}
object DoubleSyntax {
implicit def doubleToDoubleConverter(value : Double) =
new DoubleSyntax(value)
}
#DV14 #ScalaCalculations @MSkarsaune
28. ¡ that allows us to write
val dec : BigDecimal = 2.50
val dec = 2.5 scaled 2 // BigDecimal(2.50) ¡°2.5s2¡±
val complex = -1.0 + 1.0.i // Complex(-1.0, 1.0)
#DV14 #ScalaCalculations @MSkarsaune
29. Conclusion
? Scala brings
? Natural syntax: special characters + infix notation
? Extendability and interoperability: implicit type conversions
? Possible improvements (as demonstrated by spire math)
? New number types
? New calculations
#DV14 #ScalaCalculations @MSkarsaune