×   Main Menu ALL The 8BS News Manuals (New menu) Links Worth a Look Tools Disc and Basic Webring Site Map 8BS Guestbook Old Guest Book Me The Barnsley Rovers # The BBC and Master Computer Public Domain Library

### A8C3 ATN

Submitted by Steve Fewell

Description:

Call routine &96DA to obtain the Floating-point value (at the BASIC text pointer location) and convert iy to a Float
value (if it was an Integer) and store it in the FWA.

If the FWA Number is zero then exit with FWA = 0.0 and A = #&FF (to indicate a Floating-Point result).
If the FWA value is negative then clear the FWA sign byte (to make the value positive), call the ATN calculation routine,
set the results (FWA) sign byte to #&FF (to make the value negative) [as after the ATN calculation, A = #&FF]
and exit.
Otherwise (FWA value is positive), jump to the ATN calculation routine and exit.

[&A8D5] The ATN Calculation routine:
If the FWA exponent is less than #&81 (i.e. the FWA value is less than 1) then jump to &A8EA to calculate ATN result
for an argument in the range 0.0 - 1.0) and exit.

Otherwise, the FWA value is too large to calculate ATN with, so reduce the FWA value as follows:
* Call &A5E9 to calculate the reciple of the FWA value (FWA = 1 / FWA). The result will be in the range 0.0 - 1.0
* As the FWA value is now in the required range, call &A8EA to calculate the ATN result from the FWA value.
* Set the argp pointer to point to the Floating-Point constant value at location &BF2E, which is the value 1.57079683
* Subtract the FWA (ATN result) value from 1.57079683 [Pi / 2] (FWA = argp - FWA).
* Now, the FWA contains the correct ATN result, so exit with A = #&FF (to indicate that the result is Floating-Point).

[&A8EA] The ATN restricted range (0.0 - 1.0) Calculation routine:
IF the FWA exponent is less than #&73 then the FWA value is too small to calculate (i.e. it is practically 0.0), so
exit with the FWA value unchanged, and with A = #&FF (to indicate a Floating-Point result).

Store the FWA value to temporary Floating-point location &0476 - &047A.
Set the FWB to contain the value -0.5 (using &A576 (Clear FWB) routine, and setting FWB exponent = #&80, FWB Mantissa
byte 1 to #&80 and FWB Sign byte to #&80).
Add the FWB value (-0.5) to the FWA [FWA = FWB + FWA].
Set X to #&97 -> the LSB of the first fraction to apply [i.e. this points to the constant at location &BF97]
Set A to #&C9 -> the LSB of the default value (if the FWA value is too small to evaluate) [i.e. this points to the
constant at location &BFC9]
Set Y to #&04 (the number of cycles to evaluate).
Call routine &A861 which will evaluate the continued-fraction expansion series, as follows:
* If the FWA exponent is less than #&40 thern the FWA is too small to be calculated with, so exit with
the FWA = the default value at &BFC9, which is 0.927295218.
* Calculate the reciple of the FWA value [FWA = 1 / FWA], store the reciple in &046C. Set argp to &BF97.
* Add argp (&BF97 --> -0.0800532048 (in the Floating-point constant table)) to the FWA value.

* Cycle 1:
* Argp = &BF9C (0.155279656). FWA = argp / FWA.
* Argp = &BFA1 (0.440292008). FWA = argp + FWA.
* FWA = &046C (the reciple value) + FWA.

* Cycle 2:
* Argp = &BFA6 (0.215397413). FWA = argp / FWA.
* Argp = &BFAB (0.240938382). FWA = argp + FWA.
* FWA = &046C (the reciple value) + FWA.

* Cycle 3:
* Argp = &BFB0 (0.0767796872). FWA = argp / FWA.
* Argp = &BFB5 (1.07231162). FWA = argp + FWA.
* FWA = &046C (the reciple value) + FWA.

* Cycle 4:
* Argp = &BFBA (0.956538983). FWA = argp / FWA.
* Argp = &BFBF (-0.513841612). FWA = argp + FWA.
* FWA = &046C (the reciple value) + FWA.

* Argp = &BFC4 (-0.254590442). FWA = argp / FWA.
* Argp = &BFC9 (0.927295218). FWA = argp + FWA.
Jump to &A99F to set the argp to the temporary Floating-Point value at location &0476,
multiply the FWA by argp [FWA = argp * FWA] and exit with A = #&FF (to indicate a Floating-point result).

Example 1: ATN(0.0)

FWA is zero, so exit with FWA = 0.0!

Example 2: ATN(-0.75)
Make FWA sign positive (FWA = 0.75)
[&A8EA] Store FWA (0.75) to &0476
FWA = FWA + -0.5 = 0.25
[&A861] FWA = 1 / FWA = 4
Store FWA to &046C
FWA = -0.0800532 + FWA = 3.9199468
FWA = 0.1552796 / FWA = 0.0396126
FWA = 0.4402920 + FWA = 0.4799046
FWA = &046C + FWA = 4.4799046
FWA = 0.2153974 / FWA = 0.0480808
FWA = 0.2409384 + FWA = 0.2890192
FWA = &046C + FWA = 4.2890192
FWA = 0.0767797 / FWA = 0.0179014
FWA = 1.0723116 + FWA = 1.0902131
FWA = &046C + FWA = 5.0902131
FWA = 0.9565390 / FWA = 0.1879172
FWA = -0.5138416 + FWA = -0.3259243
FWA = &046C + FWA = 3.6740757
FWA = -0.2545904 / FWA = -0.0692937
FWA = 0.9272952 + FWA = 0.8580014
FWA = &0476 (0.75) * FWA = 0.6435011
Make FWA sign negative (FWA = -0.6435011)

Example 3: ATN(2.41)
FWA Exponent >= #&81, so: [A8DB] FWA = 1 / FWA = 0.4149377
[&A8EA] Store FWA (2.41) to &0476
FWA = FWA + -0.5 = -0.0850622
[&A861] FWA = 1 / FWA = -11.756098
Store FWA to &046C
FWA = -0.0800532 + FWA = -11.836151
FWA = 0.1552796 / FWA = -0.013119
FWA = 0.4402920 + FWA = 0.4271729
FWA = &046C + FWA = -11.328925
FWA = 0.2153974 / FWA = -0.019013
FWA = 0.2409384 + FWA = 0.2219253
FWA = &046C + FWA = -11.534173
FWA = 0.0767797 / FWA = -0.0066567
FWA = 1.0723116 + FWA = 1.0656549
FWA = &046C + FWA = -10.690443
FWA = 0.9565390 / FWA = -0.089476
FWA = -0.5138416 + FWA = -0.6033176
FWA = &046C + FWA = -12.359416
FWA = -0.2545904 / FWA = 0.0205989
FWA = 0.9272952 + FWA = 0.9478941
FWA = &0476 (0.4149377) * FWA = 0.393317
As the original FWA value has an exponent >= #'&81, FWA = 1.5707963 - FWA = 1.1774793

Example 4: ATN(0.25)
[&A8EA] Store FWA (0.25) to &0476
FWA = FWA + -0.5 = -0.25
[&A861] FWA = 1 / FWA = -4
Store FWA to &046C
FWA = -0.0800532 + FWA = -4.0800532
FWA = 0.1552796 / FWA = -0.0380582
FWA = 0.4402920 + FWA = 0.4022337
FWA = &046C + FWA = -3.5977662
FWA = 0.2153974 / FWA = -0.0598697
FWA = 0.2409384 + FWA = 0.1810686
FWA = &046C + FWA = -3.8189314
FWA = 0.0767797 / FWA = -0.020105
FWA = 1.0723116 + FWA = 1.0522066
FWA = &046C + FWA = -2.9477934
FWA = 0.9565390 / FWA = -0.3244932
FWA = -0.5138416 + FWA = -0.8383348
FWA = &046C + FWA = -4.8383348
FWA = -0.2545904 / FWA = 0.0526194
FWA = 0.9272952 + FWA = 0.9799146
FWA = &0476 (0.25) * FWA = 0.2449786

Example 5: ATN(5.63)
FWA Exponent >= #&81, so: [A8DB] FWA = 1 / FWA = 0.1776198
[&A8EA] Store FWA (0.1776198) to &0476
FWA = FWA + -0.5 = -0.3223802
[&A861] FWA = 1 / FWA = -3.1019275
Store FWA to &046C
FWA = -0.0800532 + FWA = -3.1819807
FWA = 0.1552796 / FWA = -0.0487996
FWA = 0.4402920 + FWA = 0.3914923
FWA = &046C + FWA = -2.7104352
FWA = 0.2153974 / FWA = -0.0794696
FWA = 0.2409384 + FWA = 0.1614686
FWA = &046C + FWA = -2.9404589
FWA = 0.0767797 / FWA = -0.0261114
FWA = 1.0723116 + FWA = 1.0462001
FWA = &046C + FWA = -2.0557274
FWA = 0.9565390 / FWA = -0.4653043
FWA = -0.5138416 + FWA = -0.9791459
FWA = &046C + FWA = -4.0810735
FWA = -0.2545904 / FWA = 0.0623831
FWA = 0.9272952 + FWA = 0.9896783
FWA = &0476 (0.1776198) * FWA = 0.1757864
As the original FWA value has an exponent >= #'&81, FWA = 1.5707963 - FWA = 1.3950098

Example 6: ATN(1.5)
FWA Exponent >= #&81, so: [A8DB] FWA = 1 / FWA = 0.6666666
[&A8EA] Store FWA (0.6666666) to &0476
FWA = FWA + -0.5 = -0.1666666
[&A861] FWA = 1 / FWA = 6
Store FWA to &046C
FWA = -0.0800532 + FWA = 5.9199468
FWA = 0.1552796 / FWA = 0.0262298
FWA = 0.4402920 + FWA = 0.4665219
FWA = &046C + FWA = 6.4665219
FWA = 0.2153974 / FWA = 0.0333096
FWA = 0.2409384 + FWA = 0.274248
FWA = &046C + FWA = 6.274248
FWA = 0.0767797 / FWA = 0.0122372
FWA = 1.0723116 + FWA = 1.089017
FWA = &046C + FWA = 7.089017
FWA = 0.9565390 / FWA = 0.1349325
FWA = -0.5138416 + FWA = -0.378909
FWA = &046C + FWA = 5.6210909
FWA = -0.2545904 / FWA = -0.0452919
FWA = 0.9272952 + FWA = 0.8820032
FWA = &0476 (0.6666666) * FWA = 0.588002
As the original FWA value has an exponent >= #'&81, FWA = 1.5707963 - FWA = 0.9827942

Example 7: ATN(90)
FWA Exponent >= #&81, so: [A8DB] FWA = 1 / FWA = 0.0111111
[&A8EA] Store FWA (0.0111111) to &0476
FWA = FWA + -0.5 = -0.4888888
[&A861] FWA = 1 / FWA = -2.0454545
Store FWA to &046C
FWA = -0.0800532 + FWA = -2.1255077
FWA = 0.1552796 / FWA = -0.0730552
FWA = 0.4402920 + FWA = 0.3672367
FWA = &046C + FWA = -1.6782178
FWA = 0.2153974 / FWA = -0.1283489
FWA = 0.2409384 + FWA = 0.1125895
FWA = &046C + FWA = -1.932865
FWA = 0.0767797 / FWA = -0.0397232
FWA = 1.0723116 + FWA = 1.0325883
FWA = &046C + FWA = -1.0128662
FWA = 0.9565390 / FWA = -0.9443883
FWA = -0.5138416 + FWA = -1.4582299
FWA = &046C + FWA = -3.5036844
FWA = -0.2545904 / FWA = 0.0726636
FWA = 0.9272952 + FWA = 0.9999588
FWA = &0476 (0.0111111) * FWA = 0.0111106
As the original FWA value has an exponent >= #'&81, FWA = 1.5707963 - FWA = 1.5596857

This diagram shows the relationship between the trig functions.

### Disassembly for the ATN routine

 A8C3 032 218 150 20 DA 96 JSR &96DA Get and Check Float (convert if Int) A8C6 032 242 163 20 F2 A3 JSR &A3F2 Obtain Sign of the FWA Floating-Point value A8C9 [ 240 091 F0 5B BEQ 91 --> &A926 Set A to #&FF and exit A8CB 016 008 10 08 BPL 8 --> &A8D5 A8CD d. 100 046 64 2E STZ &2E A8CF 032 213 168 20 D5 A8 JSR &A8D5 A8D2 . 133 046 85 2E STA &2E A8D4 ` 096 60 RTS A8D5 0 165 048 A5 30 LDA &30 A8D7 201 129 C9 81 CMP#&81 A8D9 144 015 90 0F BCC 15 --> &A8EA A8DB 032 233 165 20 E9 A5 JSR &A5E9 Floating-Point Reciple (FWA=1/FWA) A8DE 032 234 168 20 EA A8 JSR &A8EA A8E1 032 137 165 20 89 A5 JSR &A589 Set argp to &BF2E A8E4 032 138 166 20 8A A6 JSR &A68A Floating-Point Subtraction [FWA=argp-FWA] A8E7 169 255 A9 FF LDA#&FF A8E9 ` 096 60 RTS A8EA 0 165 048 A5 30 LDA &30 A8EC s 201 115 C9 73 CMP#&73 A8EE 6 144 054 90 36 BCC 54 --> &A926 Set A to #&FF and exit A8F0 032 013 165 20 0D A5 JSR &A50D Store FWA to &0476 and set argp to &0476 A8F3 v 032 118 165 20 76 A5 JSR &A576 Clear FWB Mantissa bytes 2 to 5 A8F6 169 128 A9 80 LDA#&80 A8F8 < 133 060 85 3C STA &3C A8FA = 133 061 85 3D STA &3D A8FC ; 133 059 85 3B STA &3B A8FE 032 146 166 20 92 A6 JSR &A692 FWA = FWA + FWB A901 162 151 A2 97 LDX#&97 A903 169 201 A9 C9 LDA#&C9 A905 160 004 A0 04 LDY#&04 A907 a 032 097 168 20 61 A8 JSR &A861 Evaluate continued-fraction expansion series A90A L 076 159 169 4C 9F A9 JMP &A99F Multiply FWA by variable at &0476 Or 