Skip to content

Instantly share code, notes, and snippets.

@bmaggi
Created October 15, 2018 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmaggi/9dce1f29bf81d39046a6b23fe662f284 to your computer and use it in GitHub Desktop.
Save bmaggi/9dce1f29bf81d39046a6b23fe662f284 to your computer and use it in GitHub Desktop.
In scala floating addition/multiplication are commutative but not associative with big numbers
// floating addition/multiplication are commutative but not associative with big numbers
val small = 1e-200
val big = 1e200
val invBig = -big
(big +invBig)+small // Double = 1.0E-200
big+(invBig+small) // Double = 0.0
List(big,invBig,small).reduceLeft(_+_) // Double = 1.0E-200
List(big,invBig,small).reduceRight(_+_) // Double = 0.0
(small*big)*big // Double = 1.0E200
small*(big*big) // Double = Infinity
List(small,big,big).reduceLeft(_*_) // Double = 1.0E200
List(small,big,big).reduceRight(_*_) // Double = Infinity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment