ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Bring Your Calculations to: 
Martin.Skarsaune@kantega.no 
Kantega ¨C Norwegian software craftsmanship since 2003 
¸ß 
ñR 
¶¡ 
#DV14 #ScalaCalculations @MSkarsaune
1.The Situation 
#DV14 #ScalaCalculations @MSkarsaune
Java Number Types - Integer 
? Exact representation of values within range 
? Interoperable through widening conversion 
Integer 
float double byte short int long 
#DV14 #ScalaCalculations @MSkarsaune
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
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
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
2. Inspiration 
#DV14 #ScalaCalculations @MSkarsaune
Smalltalk calculations 
? 
Numbers are objects 
? 
Binary message selectors (+, -, *, / , ¡­.) 
? 
Built in decimals 
? 
Overflows handled 
? 
Precise division 
? 
Interoperable and extendable number types 
#DV14 #ScalaCalculations @MSkarsaune
The Hope 
#DV14 #ScalaCalculations @MSkarsaune
Main Scala language number types 
AnyVal 
Double Int Long 
ScalaNumber 
BigDecimal BigInt 
#DV14 #ScalaCalculations @MSkarsaune
1. Decimal Numbers 
#DV14 #ScalaCalculations @MSkarsaune
Decimal representation 
? Integer value and scale 
? 123456789 
Scale = 3 
123456789 
1000 
? Scale is important! Know your scale! 
#DV14 #ScalaCalculations @MSkarsaune
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
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
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
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
Decimal calculation summary 
? Natural syntax 
? Operators and literals 
? Interoperable with other number types 
? Withouth having to change existing number types 
#DV14 #ScalaCalculations @MSkarsaune
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
4. Overflow 
1_000_000 * 1_000_000 = ? 
1_000_000_000_000 ? 
-727_379_968 
#DV14 #ScalaCalculations @MSkarsaune
Overflow management 
? Smalltalk: 
Automatically converts SmallInteger <=> LargeInteger 
? Java: 
Libraries such as guava or Apache commons-math can 
detect 
#DV14 #ScalaCalculations @MSkarsaune
(Some) Spire number types 
ScalaNumber 
SafeLong Real Rational Complex 
? SafeLong detects overflows and ensures optimal 
representation 
#DV14 #ScalaCalculations @MSkarsaune
SafeLong example 
val trillion : SafeLong = 1000000000000l //SafeLongLong 
val product = trillion * trillion //SafeLongBigInt 
val other = product / trillion //SafeLongLong 
#DV14 #ScalaCalculations @MSkarsaune
3. Division 
2 / 3 = ? 
0 ? 
0.6666666666666666 ? 
0.6666666666666667 ? 
2 
3 
? 
#DV14 #ScalaCalculations @MSkarsaune
Precise division 
? Smalltalk: 
3 / 3 = 1 
2 / 3 = 
2 
3 
* 3 = 2 
#DV14 #ScalaCalculations @MSkarsaune
(Some) Spire number types 
ScalaNumber 
SafeLong Real Rational Complex 
? Real ensures exact division and always chooses the 
most precise representation 
#DV14 #ScalaCalculations @MSkarsaune
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
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
¡­ 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
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
References 
http://www.scala-lang.org/ 
Spire: 
https://github.com/non/spire 
Sample code: 
https://github.com/skarsaune/devoxx.scala.calculations 
#DV14 #ScalaCalculations @MSkarsaune
Thank you for your time! 
#DV14 #ScalaCalculations @MSkarsaune

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
  • 2. 1.The Situation #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
  • 7. 2. Inspiration #DV14 #ScalaCalculations @MSkarsaune
  • 8. Smalltalk calculations ? Numbers are objects ? Binary message selectors (+, -, *, / , ¡­.) ? Built in decimals ? Overflows handled ? Precise division ? Interoperable and extendable number types #DV14 #ScalaCalculations @MSkarsaune
  • 9. The Hope #DV14 #ScalaCalculations @MSkarsaune
  • 10. Main Scala language number types AnyVal Double Int Long ScalaNumber BigDecimal BigInt #DV14 #ScalaCalculations @MSkarsaune
  • 11. 1. Decimal Numbers #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
  • 19. 4. Overflow 1_000_000 * 1_000_000 = ? 1_000_000_000_000 ? -727_379_968 #DV14 #ScalaCalculations @MSkarsaune
  • 20. Overflow management ? Smalltalk: Automatically converts SmallInteger <=> LargeInteger ? Java: Libraries such as guava or Apache commons-math can detect #DV14 #ScalaCalculations @MSkarsaune
  • 21. (Some) Spire number types ScalaNumber SafeLong Real Rational Complex ? SafeLong detects overflows and ensures optimal representation #DV14 #ScalaCalculations @MSkarsaune
  • 22. SafeLong example val trillion : SafeLong = 1000000000000l //SafeLongLong val product = trillion * trillion //SafeLongBigInt val other = product / trillion //SafeLongLong #DV14 #ScalaCalculations @MSkarsaune
  • 23. 3. Division 2 / 3 = ? 0 ? 0.6666666666666666 ? 0.6666666666666667 ? 2 3 ? #DV14 #ScalaCalculations @MSkarsaune
  • 24. Precise division ? Smalltalk: 3 / 3 = 1 2 / 3 = 2 3 * 3 = 2 #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
  • 30. References http://www.scala-lang.org/ Spire: https://github.com/non/spire Sample code: https://github.com/skarsaune/devoxx.scala.calculations #DV14 #ScalaCalculations @MSkarsaune
  • 31. Thank you for your time! #DV14 #ScalaCalculations @MSkarsaune

Editor's Notes

  • #13: DENOMINATOR POWER of ten
  • #27: Actually backed by Rational with denominator of 1.