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.

Thank you for your help.

codeguy

  • Hero Member
  • *****
  • Posts: 3945
  • what the h3ll did i name that code?
    • stuff at dkm
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 = 1
DIM CsK AS DOUBLE: CsK = 0
DIM CsFactorial AS DOUBLE: CsFactorial = 1
DIM CsXt AS DOUBLE: CsXt = 1
DIM CsSLast AS DOUBLE: CsSLast = CsSum
DIM CsExponent AS INTEGER: CsExponent = 1
DIM CSKLast AS DOUBLE
DO
    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 IF
LOOP UNTIL ABS(CsSum - CsSLast) <= CsTolerance
CosineSeries# = CsSum
END FUNCTION

codeguy

  • Hero Member
  • *****
  • Posts: 3945
  • what the h3ll did i name that code?
    • stuff at dkm
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 = 0
DIM NLSUMLAST AS DOUBLE: NLSUMLAST = 0
DIM NLTolerance AS DOUBLE: NLTolerance = 1 / (10 ^ tolerance)
DIM NaturalLogN AS DOUBLE: NaturalLogN = 1
DIM NaturalLogP AS DOUBLE: NaturalLogP = x - 1
DIM NaturalLogM AS DOUBLE: NaturalLogM = 1

DO
    NLSUMLAST = NaturalLogSum
    NaturalLogM = NaturalLogM * (NaturalLogP / x)
    NaturalLogSum = NaturalLogSum + NaturalLogM / NaturalLogN
    NaturalLogN = NaturalLogN + 1
LOOP UNTIL ABS(NaturalLogSum - NLSUMLAST) <= NLTolerance
NaturalLog# = NaturalLogSum
END FUNCTION

codeguy

  • Hero Member
  • *****
  • Posts: 3945
  • what the h3ll did i name that code?
    • stuff at dkm
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) demystified
FUNCTION PowerBaseE# (x AS DOUBLE, TOLERANCE)
DIM pbetolerance AS DOUBLE: pbetolerance = 1 / (10 ^ TOLERANCE)
DIM pbefac AS DOUBLE: pbefac = 1
DIM pbesum AS DOUBLE: pbesum = 0
DIM Powerx AS DOUBLE: Powerx = 1
DIM LastPbesum AS DOUBLE
DIM pbeU AS DOUBLE: pbeU = 1
DO
    LastPbesum = pbesum
    pbesum = pbesum + Powerx / pbefac
    Powerx = Powerx * x
    pbefac = pbefac * pbeU
    pbeU = pbeU + 1
LOOP UNTIL ABS(pbesum - LastPbesum) <= pbetolerance
PowerBaseE# = pbesum
END FUNCTION

codeguy

  • Hero Member
  • *****
  • Posts: 3945
  • what the h3ll did i name that code?
    • stuff at dkm
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 = 10000
CONST LB = 4

NbDigits = 10000
size = 1 + NbDigits / LB
REDIM e(size) AS LONG
REDIM uk(size) AS LONG
DIM k AS LONG: k = 1
SetToInteger e(), size, 1
SetToInteger uk(), size, 1
WHILE (NOT (IsZero(uk(), size)))
    Div uk(), size, k
    Add e(), uk(), size
    k = k + 1
WEND
e$ = ""
FOR k = 0 TO size - 1
    t$ = LTRIM$(STR$(e(k)))
    WHILE LEN(t$) < LB
        t$ = "0" + t$
    WEND
    e$ = e$ + t$
NEXT
WHILE LEFT$(a$, 1) = "0"
    e$ = MID$(e$, 2)
WEND
PRINT e$

SUB SetToInteger (x() AS LONG, n AS LONG, integr AS LONG)
DIM i AS LONG
FOR i = 1 TO n - 1
    x(i) = 0
NEXT
x(0) = integr
END SUB

FUNCTION IsZero& (x() AS LONG, n AS LONG)
DIM i AS LONG
FOR i = 0 TO n - 1
    IF x(i) THEN
        IsZero& = 0
        EXIT FUNCTION
    END IF
NEXT
IsZero& = -1
END FUNCTION

SUB Add (x() AS LONG, y() AS LONG, n AS LONG)
DIM carry AS LONG
DIM i AS LONG
FOR 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) = mpart
NEXT
END SUB

SUB Div (x() AS LONG, n AS LONG, d AS LONG)
DIM carry AS LONG
DIM xi AS LONG
DIM q AS LONG
DIM i AS LONG
FOR i = 0 TO n - 1
    xi = x(i) + carry * BLongBase
    q = xi \ d
    carry = xi - q * d
    x(i) = q
NEXT
END SUB