4 ms·
It's true that the "bad java programmers" version doesn't work for negative numbers, but that's actually much better than his version of gamma, which doesn't wo
by weichi 16y ago
It's true that the "bad java programmers" version doesn't work for negative numbers, but that's actually much better than his version of gamma, which doesn't work for ANY numbers, since it has a bug:
Math.pow(z/Math.E, z)
should be:
Math.pow(tmp2/Math.E, z)
So from this I conclude that there are two types of java programmers: those that over-architect their solutions, and those that don't even both to test the code they put in blog posts ;-)
- jacquesm 16y agoIn fairness I think he literally wrote the code the way he claims he wrote it 'as a rant', so it's not surprising he didn't actually test it. If this was written off the cuff without testing it's actually pretty impressive.
- weichi 16y agoHis Gamma function is even worse than I said above, because not only does it have a bug, it doesn't work at all for negative numbers, which was his big bolded complaint about the original implementation. I'm embarrassed that I didn't see this at first, because it should be really obvious - the approximation would be taking the square root of a negative number! I know he would have caught these bugs had he needed to implement these for a real system. But there is nevertheless an important point here. He is 100% right that over-architecting is a bad idea, and that correctness is more important than architecture. But complaining about a factorial function not handling negative numbers, and then "fixing" the problem by implementing the Gamma function, is doing the same thing all over again. It's really easy to fix the factorial implementation to do something reasonable for negative numbers; it's much harder to implement a Gamma function that works for all input. Why bother unless you really have to?
- steveklabnik 16y agoHe also explicitly says "I compiled it, but I didn't really test it, and I'm not sure I copy/pasted it correctly because that's not the effing point" too...