6 ms·
Blows my mind that people can come up with this stuff. I'm assuming they came up with the mathematical proofs first and translated that into code, so that has
by barsonme 10y ago
Blows my mind that people can come up with this stuff.
I'm assuming they came up with the mathematical proofs first and translated that into code, so that has something to do with it, correct?
It looks a lot like some crypto algorithms which are a nearly direct translation of the mathematical formulas.
It's not that it's incredibly difficult to follow, but it's just very "math like".
- lokedhs 10y agoThat is my experience. Maths people are not renowned for their ability to write readable or maintainable code. I recently needed an implementation of the Simplex Noise algorithm (that I could port to Common Lisp). I ended up using this one, which works but the code certainly does nothing to help understanding: https://github.com/josephg/noisejs/blob/master/perlin.js https://github.com/josephg/noisejs/blob/master/perlin.js Note that the Javscript implementation is also a port from another language (whose implementation I have failed to find).
- goldenkey 10y agoSeems pretty well commented. There are definitely worse examples. I think you are underestimating the amount of temporary calculations that most mathematical formulas or algorithms require. It's actually a good thing to see so many vars because the variable names combined with the comments make more sense. Any js packer will most certainly get rid of the redundancy of memory allocations, so I'd say the superfluous var style is harmless.
- josephg 10y agoWhen I wrote it I ran that JS code through several rounds of benchmarking to try and eke out some more performance. One of the tweaks I tried was collapsing all the calculations together to use fewer variables to see if that would make it faster. Performance was totally unchanged either way. Looks like V8's optimizer eats those vars for breakfast.
- jcranmer 10y agoIn general, at the optimization IR level of any decent compiler or JIT, most straight-line code gets reduced into some form of expression DAG for common subexpression elimination and other similar optimizations, meaning that var x = a + b; call(x + c); and call(a + b + c); would be literally indistinguishable.
- TULPU 10y agoLooks like it is implemented from this http://stackoverflow.com/a/18516731 http://stackoverflow.com/a/18516731 answer, which itself is inspired from another source
- 2muchcoffeeman 10y agoWhen I was doing numerical analysis and computational physics, this is also exactly what I did. Work out all the equations then port them straight into C or fortran. And I was a comp sci major first and a physics major second. You spend over a decade doing math with single letter variables. Hard habit to break I guess.
- athenot 10y ago> Maths people are not renowned for their ability to write readable or maintainable code. I think it's because in that circle of heavy math coding, there are a different set of well-understood abstractions and shortcuts. It's no different than saying front-end web developers are not writing readable code because they use $(...) instead of elementMatchingSelector(...) or use functions like xhr() instead of xmlHTTPrequest(). Node.js developers don't think twice about the mechanics of callbacks nor to Erlang developers have any mental block about async message semantics. Each field has a lingo that has evolved over time, and those who have been in a field for longer tend to make more shortcuts because they are manipulating a concept for the 100th time and are well versed in it.
- josephg 10y agoHey, someone found those noise functions I ported to JS a few years ago! Sorry for not referencing the original code. The java version I translated from is here: http://webstaff.itn.liu.se/~stegu/simplexnoise/SimplexNoise.java http://webstaff.itn.liu.se/~stegu/simplexnoise/SimplexNoise.... And the paper describing the algorithm is here: http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.... Glad it was useful to you!
- tracker1 10y agoIt's funny, but nothing helps you understand something better than porting it to a new language... Even if you aren't particularly skilled in the shortcuts a given language/platform offers, it does help you understand a given algorithm.
- lokedhs 10y agoI used that code to create a Common Lisp version, the result is here: https://gist.github.com/lokedhs/d5348c4a221e1436000b https://gist.github.com/lokedhs/d5348c4a221e1436000b I used it as a base for a map generator for a strategy game. Thanks a lot for the code, it worked perfectly.
- satysin 10y agoCompression, encoding, encryption, etc. is more maths and computer science than "programming". Of course it technically is programming don't get me wrong but it isn't "make a CRUD app with a simple UI" kind of programming.