The Weird and The Wonderful
The Weird and The Wonderful forum is a place to post Coding Horrors,
Worst Practices, and the occasional flash of brilliance.
We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid
workarounds and developers just not quite getting it. And then somedays we come across  or write 
the truly sublime.
Post your Best, your worst, and your most interesting. But please  no
programming questions . This forum is purely for amusement and discussions on code snippets. All
actual programming questions will be removed.





Why remove them? Why not just uninvert them? Much more challenging.





I found this line of Lua code in a graphical application and thought it qualified as both weird and wonderful.
local PI = math.asin(1) * 2
What an interesting little trick to calculate Pi.
Unfortunately the math library has math.pi. If that was not implemented in an earlier version I would write the whole 3.14etc. rather then use this mathematical trick.





genius
In code we trust !





Fueled By Caffeine wrote: I would write the whole 3.14etc
Good luck with that!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
 Homer





Four decimal places ought to be good enough for anyone.
You'll never get very far if all you do is follow instructions.





In my view, in the absence of a math.pi this is a far more sensible option than the whole 3.14etc. It's not a trick.
Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.





Phil J Pearson wrote: more sensible option than the whole 3.14etc
Meh. Copyandpaste http://www.piday.org/million/[^]
You'll never get very far if all you do is follow instructions.





While PI = math.asin(1) * 2 may be mathematically correct, it's probably not the most accurate representation of π you could create. It depends upon the arcsine function implementation. You might get lucky in case they shortcut the value for an argument of 1, and simply return the library's value of π. If not, they're going to do the arcsine calculation using a numerical method that will approximate something close to π.
In other words, it would be more precise if the library simply gave you its value of π. For casual calculations, the difference may not matter. If you're doing a simulation, having your value of π off by a couple bits could have longterm consequences.
Software Zen: delete this;







General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.