From: John W Higgins Date: 2009-12-28T12:56:11+09:00 Subject: Re: Trig value errors --001636c93192ad91b9047bc1e224 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Dec 27, 2009 at 6:00 PM, jzakiya wrote: > i.e. (1) 1 = cos^2 + sin^2 > This also means that 0 = 1 - cos^2 - sin^2 > > To 'fix totally' these errors in sin|cos near the x|y axis define sin1| > cos1 here using Ruby's native cos|sin. > > def sin1(x) > y=(1-cos(x)**2)**0.5 # calculate (2) > return 0.0 if y==0 > return sin(x) < 0 ? -y : y # for correct quadrant sign > end > > def cos1(x) > y=(1-sin(x)**2)**0.5 # calculate (2) > return 0.0 if y==0 > return cos(x) < 0 ? -y : y # for correct quadrant sign > end > > You are simply unable to apparently grasp how mathematics works with floating point numbers. So lets make a very simple example which shows that your method fails as badly as does the standard methods. First, to show that the standard methods show no greater precision lets test 87 degrees shall we. 1.0 - (sin(PI/180*87) ** 2 + cos(PI/180*87) ** 2) => 1.11022302462516e-16 So we fail with the standard and yours works 1.0 - (sin1(PI/2/90*87) ** 2 + cos1(PI/2/90*87) ** 2) => 0.0 But wait shall we - lets look at 67 degrees ok? 1.0 - (sin(PI/180*67) ** 2 + cos(PI/180*67) ** 2) => 0.0 1.0 - (sin1(PI/180*67) ** 2 + cos1(PI/180*67) ** 2) => 2.22044604925031e-16 Oh no - we have a problem - your calculation fails when the standard works. There is no way to accurate get these values to ever work with floating point mathematics. There will always be something that fails because all you are doing is trying to cover up one inaccurate calculation with yet another inaccurate calculation. If you hide yourself in a box that is only 1 or 0 shockingly you can make x^2 + y^2 = 1 because you are gaming the values. But you fail to appreciate that the numbers will always have an element of imprecision that you have no control over and you cannot make work to exact numbers when dealing with floating points. Yet again, the standard is the standard because a lot of VERY smart people took the time to analyze the entire spectrum of results instead of limiting themselves myopically to one particular area. John --001636c93192ad91b9047bc1e224--