×   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   
8-Bit Software

The BBC and Master Computer Public Domain Library

A9DF EXP

Submitted by Steve Fewell

Routine:EXP
Name: EXP (Exponential)
Starting Address: &A9DF
Entry criteria: The FWA contains the value to work with
Exit: The FWA contains the result.

Description:

Get the Numeric value (if integer then convert to Float, if String then Type Mismatch error).
If the FWA value has an exponent greater than #&87 (or equal to #&87 with a mantissa value >= &B3)
and the FWA sign is positive, then generate a EXP range error. Otherwise, if the FWA contains
a negative value then run the Clear FWA routine and exit with FWA = 0.0.
Thus 89.49999 is the largest value before the EXP range error is issued. No error is given
for negative values, as negative numbers with a large exponents return an EXP result of 0.

Call &82E0 to Split the FWA value into its Integer and Fractional parts. This is done as the
separate parts need to be calculated separately. This routine puts the 1-byte Integer value into &49
and the fractional value in the FWA.

Evaluate the EXP continued-fraction expansion series (A861) with X=#&CE, A=#&F6 and Y=#&03.
Thus the parameters to the evaluate series routine are as follows:
Default value (if FWA value too small to calculate) = &BFF6 = 1
First Floating-Point constant to use = &BFCE.
Number of cycles to evaluate = #&03.

The series is evaluated as follows:
If FWA too small then return 1 (&BFF6).
FWA = 1/FWA (store in Temporary Floating-Point variable address &046C)
FWA = 0.071206464 + FWA    [Floating-Point constant &BFCE]
FWA = 0.00710252642 / FWA    [Floating-Point constant &BFD3]
FWA = 0.000254009799 + FWA    [Floating-Point constant &BFD8]
FWA = &046C [1/orig FWA] + FWA
FWA = 0.0166665235 / FWA    [Floating-Point constant &BFDD]
FWA = 0.000000662400541 + FWA    [Floating-Point constant &BFE2]
FWA = &046C [1/orig FWA] + FWA
FWA = 0.0833333324 / FWA    [Floating-Point constant &BFE7]
FWA = -0.499999997 + FWA    [Floating-Point constant &BFEC]
FWA = &046C [1/orig FWA] + FWA
FWA = 1 / FWA    [Floating-Point constant &BFF1]
FWA = 1 + FWA    [Floating-Point constant &BFF6]

This gives the EXP value for the fractional part of the value.
Store the series result to Temporary Floating-Point variable location &0476.
Unpack the Floating point constant at location &BF47 to the FWA.
This value is 2.71828183 (or EXP(1)).
Set A to &49 (the Integer part of the original FWA value).
Call &A5BE to raise the FWA to the power of the Integer value in A.
Multiply the FWA (the EXP result for the Integer part of the value) by
&0476 (the EXP result for the fractional value).
Now, we have the complete EXP result, so exit with the FWA set to this result.

Example 1:
Calculate EXP(1.245)
1.245 => Exponent = &81, Mantissa 1 = &1F, Mantissa 2 = &5C, Mantissa 3 = &28, Mantissa 4 = &F6
?&49 = 1.
FWA = 0.245
Calculate reciple -> FWA = 1/FWA = 4.08163265. Store this value in location &046C.
FWA = 0.071206464 + FWA = 4.15283911706
FWA = 0.00710252642 / FWA = 0.0017102821033
FWA = 0.000254009799 + FWA = 0.0019642919023
FWA = &046C + FWA = 4.083596941902
FWA = 0.0166665235 / FWA = 0.0040813341123
FWA = 0.000000662400541 + FWA = 0.0040819965128
FWA = &046C + FWA = 4.08571464651287
FWA = 0.0833333324 / FWA = 0.02039626836669
FWA = -0.499999997 + FWA = -0.4796037286333
FWA = &046C + FWA = 3.60202892136669
FWA = 1 / FWA = 0.27762131338484
FWA = 1 + FWA = 1.27762131338484. Store in &0476.

FWA = 2.71828183 Raised to the power of 1 = 2.71828183
FWA = 1.27762131338484 * FWA = 3.47293480179. This is the EXP value of 1.245.


Example 2:
Calculate EXP(8.17)
8.17 => Exponent = &84, Mantissa 1 = &02, Mantissa 2 = &B8, Mantissa 3 = &51, Mantissa 4 = &EC
?&49 = 8.
FWA = 0.17
Calculate reciple -> FWA = 1/FWA = 5.882352941176. Store this value in location &046C.
FWA = 0.071206464 + FWA = 5.953559405176
FWA = 0.00710252642 / FWA = 0.00119298825066
FWA = 0.000254009799 + FWA = 0.00144699804966
FWA = &046C + FWA = 5.88379993922566
FWA = 0.0166665235 / FWA = 0.00283261220166
FWA = 0.000000662400541 + FWA = 0.0028332746022
FWA = &046C + FWA = 5.8851862157782
FWA = 0.0833333324 / FWA = 0.014159846323398
FWA = -0.499999997 + FWA = -0.4858401506766
FWA = &046C + FWA = 5.396512790499398
FWA = 1 / FWA = 0.185304851266267
FWA = 1 + FWA = 1.185304851266267. Store in &0476.

FWA = 2.71828183 Raised to the power of 8 = 2980.9580005606
FWA = 1.185304851266267 * FWA = 3533.3439794855. This is the EXP value of 8.17.


Disassembly for the EXP routine

A9DF   032 218 150 20 DA 96 JSR &96DA Get and check Float value
A9E2 0 165 048 A5 30 LDA &30
A9E4   201 135 C9 87 CMP#&87
A9E6   144 015 90 0F BCC 15 --> &A9F7
A9E8   208 006 D0 06 BNE 6 --> &A9F0
A9EA 1 164 049 A4 31 LDY &31
A9EC   192 179 C0 B3 CPY#&B3
A9EE   144 007 90 07 BCC 7 --> &A9F7
A9F0 . 165 046 A5 2E LDA &2E
A9F2   016 200 10 C8 BPL -56 --> &A9BC EXP range error
A9F4 L 076 180 166 4C B4 A6 JMP &A6B4 Clear FWA
A9F7   032 224 130 20 E0 82 JSR &82E0 Split FWA (into Integer/Fractional parts)
A9FA   162 206 A2 CE LDX#&CE
A9FC   169 246 A9 F6 LDA#&F6
A9FE   160 003 A0 03 LDY#&03
AA00 a 032 097 168 20 61 A8 JSR &A861 Evaluate continued-fraction expansion series
AA03   032 013 165 20 0D A5 JSR &A50D Store FWA to &0476
AA06 G 169 071 A9 47 LDA#&47
AA08   032 150 168 20 96 A8 JSR &A896 Load FWA with FP constant at &BF00 + A
AA0B I 165 073 A5 49 LDA &49
AA0D   032 190 165 20 BE A5 JSR &A5BE Raise FWA to power of Integer value in A
AA10   128 141 80 8D BRA -115 --> &A99F Multiply FWA by &0476

 


 Back to 8BS
Or