From: Dr John Stockton Subject: Re: ! inverse SIN(x) and COS(x) function Date: 28 Nov 1999 00:00:00 GMT Message-ID: Content-Transfer-Encoding: 8bit References: <81orm6$liq2@forums.borland.com> <81p117$lku4@forums.borland.com> <81p9qu$liu7@forums.borland.com> Organization: Home, 'Surrey', UK Content-Type: text/plain;charset=iso-8859-1 Mime-Version: 1.0 Newsgroups: borland.public.delphi.objectpascal JRS: In article <81p9qu$liu7@forums.borland.com> of Sat, 27 Nov 1999 13:12:05 in news:borland.public.delphi.objectpascal, PhilippeRanger@ compuserve.remove-this.com wrote: ><Find sources of the ArcSin and ArcCos functions in the Trig section of >http://www.efg2.com/Lab/Library/Delphi/MathFunctions/General.htm >You'll need the math unit (or EZMath, TPMath1, MathX, or some other >unit). >>> > >ArcSin and ArcCos are in all versions of the Math unit. For the Strandard >version (no Math), there's ArcTan in System. Sqr(sin) + Sqr(cos) = 1, so -- > > angleInRads = arcTan(sinVal / sqrt(1 - sqr(sinVal))) Usually. But not if abs(SinVal)=1. And you may need Pi-that. See, for example, which includes the following (and methods using ArcTan but not asm), which appear to be correct both for BP7 and for D3. {$IFDEF TjM} (* Adapted From: Terje Mathisen Date: Tue, 30 Apr 1996 15:33:26 +0200 o tan(y) = sin(y) / cos(y) o tan(y) = sqrt(1-cos(y)^2) / cos(y) o so, arccos(x) = FPATAN(sqrt(1-x^2),x) *) function ArcCos2(x : extended) : extended ; assembler ; asm fld [x] { X } fld st(0) { X X } fmul st,st(0) { X^2 X } fld1 { 1 X^2 X } fsubrp st(1),st { (1-X^2) X } FSQRT { sqrt(1-X^2) X } fxch st(1) { X sqrt(1-X^2) } fpatan { Uses both arguments } end {ArcCos2} ; function ArcSin2(x : extended) : extended ; assembler ; asm fld [x] { X } fld st(0) { X X } fmul st,st(0) { X^2 X } fld1 { 1 X^2 X } fsubrp st(1),st { (1-X^2) X } FSQRT { sqrt(1-X^2) X } fpatan { Uses both arguments } end {ArcSin2} ; (* It is possible that the FSUB instruction should have been FSUBP instead of FSUBRP, and you might need to interpose an FXCH ST(1) between the last two instructions, but otherwise, it should work. Terje.Mathisen@hda.hydro.com *) {$ENDIF TjM} {$IFDEF PZa} { From: Paul Zank } function ArcTan2(y : extended ; x : extended) : extended ; { The '87, '287, '387, i486, and i586 have an op code to do ATAN2! It is advertised to be well behaved and execute in 290 cycles on a i486. This would be about 3 uSec on a 100MHz i486 machine! It is also supposed to be very accurate. More information can be found in the "i486 Microprocessor - Programmers Reference Manual" } assembler ; asm fld [y] ; fld [x] ; fpatan end ; function ArcSin2(x : extended) : extended ; begin ArcSin2 := ArcTan2(x, Sqrt(1-Sqr(x))) ; { ArcTan2 is used instead of ArcTan to avoid exception when x = 1 or -1 } end ; function ArcCos2(x : extended) : extended ; begin ArcCos2 := ArcTan2(Sqrt(1-Sqr(x)), x) ; { ArcTan2 is used instead of ArcTan to avoid exception when x = 0} end ; {$ENDIF PZa} H'mm - I see that Delphi 3 standard has ArcTan2 in the Help :-) but in the Math unit :-( -- and that the Help for ArcTan (in System) repeats the incorrect ArcCos noted long ago in BP7. See the URL above again. Someone should check D5 help - and D6. already treats the essence of this, with . -- © John Stockton, Surrey, UK. jrs@merlyn.demon.co.uk Turnpike v4.00 MIME. © Web - FAQqish topics, acronyms & links. PAS, EXE in - see 00index.txt. Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036) Used with permission