program FPATAN2 { www.merlyn.demon.co.uk } ; (* see FPATAN2.TM (here pruned) : Subject: Re: How to duplicate C function ATAN2 in Delphi? From: Terje Mathisen Date: 18 May 1995 19:25:10 GMT I think you should seriously consider using the FPATAN instruction for this! This x87 opcode implements an IEEE-compliant ATAN2() function, with full extended precision, and the hardware will handle all the special cases for you. If you have numeric exceptions enabled, and input bogus values, the x87 chip will raise the appropriate signal, without the need for upfront testing of parameters. A BP/TP/Delphi-compatible version would look like this: *) Function atan2(y : extended; x : extended): Extended; Assembler; asm fld [y] ; fld [x] ; fpatan end ; (* Total execution time is less than 200 cycles on a Pentium, with less than 1 ulp maximum error, unless you have a Pentium with the FDIV bug, where it could fail almost anywhere after the first 15-20 OK bits! :-) The library function ArcTan(x) is implemented as fpatan(1.0,x), as long as you compile with IEEE reals {$N+} set. Terje -- -Terje Mathisen (include std disclaimer) "almost all programming can be viewed as an exercise in caching" *) var X, Y : extended ; BEGIN ; Writeln('FPATAN2') ; repeat Write('X, Y ?') ; Readln(X, Y) ; Writeln(X:10:6, Y:10:6, 180.0*ATAN2(Y, X)/Pi:12:6) ; until false ; END.