Trace:

the_20atan2_20function

This shows you the differences between two versions of the page.

the_20atan2_20function [2018/03/31 13:19] 127.0.0.1 external edit |
the_20atan2_20function [2018/04/17 19:02] (current) tbest3112 Added syntax highlighting |
||
---|---|---|---|

Line 2: | Line 2: | ||

//by Richard Russell, September 2007//\\ \\ BBC BASIC includes the **ATN** (arctangent) function as standard, but it does not include the **ATAN2** function available in some other programming languages. The difference between them is that **ATN** takes a single parameter and returns its arctangent in the range -PI/2 to +PI/2, whereas **ATAN2** takes two parameters and returns the arctangent of their //quotient// in the range **-PI to +PI**. In other words **ATAN2** is able to work out the correct //quadrant// for the result.\\ \\ For more details on the significance and uses of the **ATAN2** function see the relevant [[http://en.wikipedia.org/wiki/Atan2|Wikipedia article]].\\ \\ **ATAN2** can be implemented in BBC BASIC using the following code:\\ \\ | //by Richard Russell, September 2007//\\ \\ BBC BASIC includes the **ATN** (arctangent) function as standard, but it does not include the **ATAN2** function available in some other programming languages. The difference between them is that **ATN** takes a single parameter and returns its arctangent in the range -PI/2 to +PI/2, whereas **ATAN2** takes two parameters and returns the arctangent of their //quotient// in the range **-PI to +PI**. In other words **ATAN2** is able to work out the correct //quadrant// for the result.\\ \\ For more details on the significance and uses of the **ATAN2** function see the relevant [[http://en.wikipedia.org/wiki/Atan2|Wikipedia article]].\\ \\ **ATAN2** can be implemented in BBC BASIC using the following code:\\ \\ | ||

+ | <code bb4w> | ||

DEF FNatan2(y,x) : ON ERROR LOCAL = SGN(y)*PI/2 | DEF FNatan2(y,x) : ON ERROR LOCAL = SGN(y)*PI/2 | ||

IF x>0 THEN = ATN(y/x) ELSE IF y>0 THEN = ATN(y/x)+PI ELSE = ATN(y/x)-PI | IF x>0 THEN = ATN(y/x) ELSE IF y>0 THEN = ATN(y/x)+PI ELSE = ATN(y/x)-PI | ||

+ | </code> | ||

Note that the possibility of a **Division by zero** error (if x=0) or a **Number too big** error (if y is very much larger than x) is handled by local error trapping. This is an example of when it is more straightforward to allow the error to occur, and trap it, rather than predict when the error will occur and avoid it. This is because the precise values of x and y which might cause a **Number too big** error are difficult to determine, and depend on the ***FLOAT** mode in use.\\ \\ For this reason take particular care to transcribe the code accurately, since any error you may introduce will be trapped and result in an incorrect return value rather than an error message. | Note that the possibility of a **Division by zero** error (if x=0) or a **Number too big** error (if y is very much larger than x) is handled by local error trapping. This is an example of when it is more straightforward to allow the error to occur, and trap it, rather than predict when the error will occur and avoid it. This is because the precise values of x and y which might cause a **Number too big** error are difficult to determine, and depend on the ***FLOAT** mode in use.\\ \\ For this reason take particular care to transcribe the code accurately, since any error you may introduce will be trapped and result in an incorrect return value rather than an error message. |

the_20atan2_20function.txt ยท Last modified: 2018/04/17 19:02 by tbest3112

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International