Robert K. wrote in post #1122887:
On Mon, Sep 30, 2013 at 5:33 AM, Naga V. [email protected]
wrote:
Also, resorting to computation sometimes doesn’t yield
perfect results as known/anticipated. Example:
(environment: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin12.4.0])
$ ruby -e “puts Math.sin(Math::PI) # I expect 0”
1.2246467991473532e-16
Yes, but
$ ruby -e ‘printf “%.3f\n”, Math.sin(Math::PI)’
0.000
And even more to the point:
irb(main):0> printf ‘%.15f’, Math.sin(Math::PI)
0.000000000000000
That’s pretty damn close to 0, if you ask me. And if you want to to be
closer:
irb> require ‘bigdecimal’
irb> require ‘bigdecimal/math’
irb> s = BigMath.sin BigMath.PI(1000), 1000
=> #<BigDecimal:15e0f88,‘-0.2689451280 5027876503 4668568112 3104626557
2061945196 3039291663 6186366058 1142512668 9630473349 9820495715
1460161087 1416135178 9643003569 7962658932 1349284854 3949064332
2231580786 1782810477 7802537839 7894670545 7187191875 5140592572
2997079271 1467022134 0051624503 9392256267 7538151880 7356193539
4778247007 1704751775 7803001673 0208580982 0134020955 3401193823
2545853466 5213952486 4740472330 3188330824 8018302374 4330382467
8316579518 9813958658 9343529894 8381551772 6329374998 6826958535
2224777855 5844762013 8314549698 6911071442 1532097170 8417746277
2511648311 0416694837 3357370089 2901399127 4605931619 4978883979
4312388437 8581659915 4672899820 9925716083 5388463613 9685203353
5314325878 5756987635 9416315120 5829003988 8531954939 9173477868
5451489623 3195503850 1090751953 3326386266 9317789620 1846949099
8480102044 7147268693 9451391849 4369207480 6337161738 6538414681
4938615648 8539725637 9951290968 9460331883 5099541436 4605805376
0861991522 2911773841 3095486208 8661258701 0301543689 7327349237
4898748746 0207218665 2496099510 7075139899 5717871356 5255356832
2607876419 7170912949 6015774027 191E-1015’,1044(2295)>
I’m pretty sure that if I specify PI to a thousand decimal places, and
instruct the library to calculate sin to a thousand decimal places, and
it gives me a 1015 zeroes after the decimal point, that’s a zero.
I appreciate where you’re going with this library, and adding knowledge
about PI to the trig functions makes sense (although I wonder why
log/Math::E got no love?), but I’m quite certain that anyone who wants
to control the precision of their answers already has the tools at their
disposal to do so.
I also feel a little uneasy that, while I know beforehand how much
accuracy Math.sin and Math::PI provide, using your library I’m no longer
sure of the precision, depending on the parameters I provide. While
Math::PI gives exactly zero, I don’t know which of BigMath::PI(32) or
BigMath::PI(64) will have the same exact result on a given machine, if
either.
Oh and a minor coding style nitpick: in your overridden sin/cos
functions you called the variable ‘rad’ when it should probably have
been called ‘deg’.