3 ms·
def sum = series.map(_.sum).foldLeft(0.0)(_+_) Gah, no, stop. Remember, kids: everytime you use naive map-reduce for floating point arithmetic operations, God
by jfager 17y ago
def sum = series.map(_.sum).foldLeft(0.0)(_+_)
Gah, no, stop. Remember, kids: everytime you use naive map-reduce for floating point arithmetic operations, God kills a kitten.
- asolove 17y agoI personally didn't realize this was that inefficient. Can someone give the preferable alternative?
- jfager 17y agoIt's not an efficiency issue. Adding floating point numbers naively left-to-right can introduce huge errors, even in small data sets. http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems http://en.wikipedia.org/wiki/Floating_point#Accuracy_problem...
- gclaramunt 17y agoBut is not limited to map/reduce... every time you use floating point god kills a kitten
- michaelneale 17y agoUsing scala almost daily, I don't think I have ever done that, with floating point anyway - looks like one line slideware code for demos.
- sreque 17y agoIn a perfect world, a compiler would optimize this code to be equivalent to summing in a simple loop, at least if series were a projection or had a non-strict map function. Whether scalac and the JVM can work together to do this right now or not is beyond my knowledge. Hopefully specialization in Scala 2.8 will at least get the JVM one major step closer.
- jfager 17y agoSumming floating point numbers in a simple loop is just as bad. It's not an issue of efficiency, it's an issue of accuracy.
- sreque 17y agoI guess I've been causing the death of kittens for a while then. I couldn't, however, deduce from your wikipedia link or from a quick google search what the correct way of adding floating point numbers should be, though. Even the MS compiler guys at http://msdn.microsoft.com/en-us/library/aa289157%28VS.71%29.aspx http://msdn.microsoft.com/en-us/library/aa289157%28VS.71%29.... are writing code snippets that sum floating point numbers in a loop. edit: You're write jfager. I didn't read the article I linked to very carefully. The article actually has an example algorithm, the Kahan sum, for reducing the error caused by summing floating point numbers in a loop!
- jfager 17y agoThe whole point of the article you linked to is how to avoid summing floating point numbers in a simple loop.