### Author Topic: Need help with math fonctions  (Read 1976 times)

#### Neelix57

• Newbie
• Posts: 25
##### Re: Need help with math fonctions
« Reply #15 on: November 18, 2015, 04:27:04 am »
The error occured on the sine, cosine and tangent lines. This happed in my calculator program only. I put your code in a new file. Here I've got subscript out of range only for tangent with an angle value of 90 degrees. It's normal. For any other value, are the sine, cosine and tangent results equal to zero.

#### Neelix57

• Newbie
• Posts: 25
##### Re: Need help with math fonctions
« Reply #16 on: November 18, 2015, 04:46:14 am »
Hop! I apologize. I realised that I made an error typing the code in the new file. This work fine.

#### codeguy

• Hero Member
• Posts: 3945
• what the h3ll did i name that code?
##### Re: Need help with math fonctions
« Reply #17 on: September 07, 2017, 07:24:00 pm »
The McLaurin series expansion for COS(x), so it's not just a mystery .
Called as such:
PRINT CosineSeries#(_D2R(3), 99) '* if you're working with degrees
PRINT CosineSeries#(3, 99) '* if you're working with radians
'* McLaurin Series expansion for Cosine(x)
Code: [Select]
`FUNCTION CosineSeries# (x AS DOUBLE, tolerance AS INTEGER)DIM CsTolerance AS DOUBLE: CsTolerance = 1 / (10 ^ tolerance)DIM CsSum AS DOUBLE: CsSum = 1DIM CsK AS DOUBLE: CsK = 0DIM CsFactorial AS DOUBLE: CsFactorial = 1DIM CsXt AS DOUBLE: CsXt = 1DIM CsSLast AS DOUBLE: CsSLast = CsSumDIM CsExponent AS INTEGER: CsExponent = 1DIM CSKLast AS DOUBLEDO    CsSLast = CsSum    CSKLast = CsK    DO        CsK = CsK + 1        CsFactorial = CsFactorial * CsK        CsXt = CsXt * x    LOOP UNTIL CsK - CSKLast > 1    IF CsExponent THEN        CsSum = CsSum - (CsXt / CsFactorial)        CsExponent = 0    ELSE        CsSum = CsSum + (CsXt / CsFactorial)        CsExponent = 1    END IFLOOP UNTIL ABS(CsSum - CsSLast) <= CsToleranceCosineSeries# = CsSumEND FUNCTION`

#### codeguy

• Hero Member
• Posts: 3945
• what the h3ll did i name that code?
##### Re: Need help with math fonctions
« Reply #18 on: September 07, 2017, 11:13:41 pm »
Iterative calculation of Natural Log(x)
Code: [Select]
`FUNCTION NaturalLog# (x AS DOUBLE, tolerance AS INTEGER)DIM NaturalLogSum AS DOUBLE: NaturalLogSum = 0DIM NLSUMLAST AS DOUBLE: NLSUMLAST = 0DIM NLTolerance AS DOUBLE: NLTolerance = 1 / (10 ^ tolerance)DIM NaturalLogN AS DOUBLE: NaturalLogN = 1DIM NaturalLogP AS DOUBLE: NaturalLogP = x - 1DIM NaturalLogM AS DOUBLE: NaturalLogM = 1DO    NLSUMLAST = NaturalLogSum    NaturalLogM = NaturalLogM * (NaturalLogP / x)    NaturalLogSum = NaturalLogSum + NaturalLogM / NaturalLogN    NaturalLogN = NaturalLogN + 1LOOP UNTIL ABS(NaturalLogSum - NLSUMLAST) <= NLToleranceNaturalLog# = NaturalLogSumEND FUNCTION`

#### codeguy

• Hero Member
• Posts: 3945
• what the h3ll did i name that code?
##### Re: Need help with math fonctions
« Reply #19 on: September 08, 2017, 12:01:26 am »
Code: [Select]
`'* raises Natural Log Base E to the Xth power. in other words, exp(x) demystifiedFUNCTION PowerBaseE# (x AS DOUBLE, TOLERANCE)DIM pbetolerance AS DOUBLE: pbetolerance = 1 / (10 ^ TOLERANCE)DIM pbefac AS DOUBLE: pbefac = 1DIM pbesum AS DOUBLE: pbesum = 0DIM Powerx AS DOUBLE: Powerx = 1DIM LastPbesum AS DOUBLEDIM pbeU AS DOUBLE: pbeU = 1DO    LastPbesum = pbesum    pbesum = pbesum + Powerx / pbefac    Powerx = Powerx * x    pbefac = pbefac * pbeU    pbeU = pbeU + 1LOOP UNTIL ABS(pbesum - LastPbesum) <= pbetolerancePowerBaseE# = pbesumEND FUNCTION`

#### codeguy

• Hero Member
• Posts: 3945
• what the h3ll did i name that code?
##### Re: Need help with math fonctions
« Reply #20 on: September 13, 2017, 12:42:07 am »
arbitrary precision calculation of e^1
Code: [Select]
`CONST BLongBase = 10000CONST LB = 4NbDigits = 10000size = 1 + NbDigits / LBREDIM e(size) AS LONGREDIM uk(size) AS LONGDIM k AS LONG: k = 1SetToInteger e(), size, 1SetToInteger uk(), size, 1WHILE (NOT (IsZero(uk(), size)))    Div uk(), size, k    Add e(), uk(), size    k = k + 1WENDe\$ = ""FOR k = 0 TO size - 1    t\$ = LTRIM\$(STR\$(e(k)))    WHILE LEN(t\$) < LB        t\$ = "0" + t\$    WEND    e\$ = e\$ + t\$NEXTWHILE LEFT\$(a\$, 1) = "0"    e\$ = MID\$(e\$, 2)WENDPRINT e\$SUB SetToInteger (x() AS LONG, n AS LONG, integr AS LONG)DIM i AS LONGFOR i = 1 TO n - 1    x(i) = 0NEXTx(0) = integrEND SUBFUNCTION IsZero& (x() AS LONG, n AS LONG)DIM i AS LONGFOR i = 0 TO n - 1    IF x(i) THEN        IsZero& = 0        EXIT FUNCTION    END IFNEXTIsZero& = -1END FUNCTIONSUB Add (x() AS LONG, y() AS LONG, n AS LONG)DIM carry AS LONGDIM i AS LONGFOR i = n - 1 TO 0 STEP -1    x(i) = x(i) + y(i) + carry    mpart = x(i) MOD BLongBase    carry = (x(i) - mpart) / BLongBase    x(i) = mpartNEXTEND SUBSUB Div (x() AS LONG, n AS LONG, d AS LONG)DIM carry AS LONGDIM xi AS LONGDIM q AS LONGDIM i AS LONGFOR i = 0 TO n - 1    xi = x(i) + carry * BLongBase    q = xi \ d    carry = xi - q * d    x(i) = qNEXTEND SUB`