Announcement

Collapse
No announcement yet.

complex numbers in PBWin ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • complex numbers in PBWin ?

    I'm working on a project in which I have to do arithmetics with complex numbers ( z = x + i . y ) . Fi . addition , substraction, multiplying , dividing ...
    I wonder if there is a way in PBWin to deal with such numbers .
    Defining a TYPE for instance ? and write some basic routines myself ?
    Or does PBWIN offers functions ?
    Last edited by Frank Kestens; 15 Sep 2014, 02:52 PM.

  • #2
    As i is an imaginary number, I cannot see how this could be assigned to a variable. I reckon that whatever tricks you come up with, the compiler will complain as it can only handle tangible items (I think). If you used a phrase such as "sqrt-1", you would not be able to do simple maths on it, though you could look for it in an expression and deal with that e.g. cancel it out or square it to get back to the real world with -1.

    An interesting problem that I hope will generate a lot of posts from those more knowledgeable than I am!

    Iain Johnstone
    “None but those who have experienced them can conceive of the enticements of science” - Mary Shelley

    Comment


    • #3
      Frank,
      define your own UDT and then write your own function to handle the calculations.
      If you really only need +-*/ then it's not difficult at all.

      Paul.

      Comment


      • #4
        Code:
        PBCC program
        #COMPILE CON
        #DIM ALL
        
        
        TYPE ComplexEXT
            r AS EXT
            i AS EXT
        
        END TYPE
        
        
        FUNCTION PBMAIN () AS LONG
        
        LOCAL a,b, ans AS ComplexEXT
        
        'a = 1+2i
        a.r = 1
        a.i = 2
        
        'b=3+4i
        b.r=3
        b.i=4
        
        ComplexAdd(a,b,ans)
        PRINT "Add=";ComplexPrint(ans)
        
        ComplexSub(a,b,ans)
        PRINT "Sub=";ComplexPrint(ans)
        
        ComplexMul(a,b,ans)
        PRINT "Mul=";ComplexPrint(ans)
        
        ComplexDiv(a,b,ans)
        PRINT "Div=";ComplexPrint(ans)
        
        
        WAITKEY$
        
        END FUNCTION
        
        
        
        
        FUNCTION ComplexPrint(a AS ComplexEXT) AS STRING
        
        IF a.i >= 0 THEN
            FUNCTION = STR$(a.r) + " +" + STR$(a.i) + "i"
        
        ELSE
            FUNCTION = STR$(a.r) + " -" + STR$(-a.i) + "i"
        
        END IF
        
        END FUNCTION
        
        
        
        SUB ComplexAdd(a AS ComplexEXT, b AS ComplexEXT, ans AS ComplexEXT)
        
        ans.r = a.r + b.r
        ans.i = a.i + b.i
        
        
        END SUB
        
        
        
        SUB ComplexSub(a AS ComplexEXT, b AS ComplexEXT, ans AS ComplexEXT)
        
        ans.r = a.r - b.r
        ans.i = a.i - b.i
        
        
        END SUB
        
        
        
        
        SUB ComplexMul(a AS ComplexEXT, b AS ComplexEXT, ans AS ComplexEXT)
        
        ans.r = a.r * b.r  - a.i * b.i
        ans.i = a.r * b.i  + b.r * a.i
        
        
        END SUB
        
        
        
        SUB ComplexDiv(a AS ComplexEXT, b AS ComplexEXT, ans AS ComplexEXT)
        LOCAL denom AS EXT
        
        denom = (b.r*b.r + b.i*b.i)
        
        
        ans.r = (a.r * b.r  + a.i * b.i)/denom
        ans.i = (a.i * b.r  - b.i * a.r)/denom
        
        
        END SUB

        Comment


        • #5
          Thanks Paul !
          Did you have the code "on hand " ?
          If I have to get sin(z) and exp(z) I guess I have only to define the routines .
          Nice help !
          Last edited by Frank Kestens; 14 Sep 2014, 09:56 AM.

          Comment


          • #6
            Frank,
            I thought I'd written similar code sometime but couldn't find it so I rewrote it for you.
            The EXP and sin are a little more complicated. I'd have to look them up.

            Paul.

            Comment


            • #7
              No problem Paul , I can do this ( sin(z) = f (eix, e-ix )) .
              It's great to have a handle in PBWin to handle such numbers .
              Thx

              Comment


              • #8
                Wait a moment , this may be tricky

                If I want to compute in complex :

                a + b *c

                then I do need nested opertors ? :

                ans = ComplexMul(b,c,ans) ' to perform multiplication ?

                ans = ComplexAdd(a,ComplexMul(b,c,ans),ans) ' to perform adding ?

                Comment


                • #9
                  Frank,
                  unfortunately, PB doesn't allow you to return a UDT as the result of a FUNCTION so you need to break the calculation into steps and do one small part at a time.
                  Code:
                  #DIM ALL
                  
                  TYPE ComplexEXT
                      r AS EXT
                      i AS EXT
                  
                  END TYPE
                  
                  
                  FUNCTION PBMAIN () AS LONG
                  
                  LOCAL a,b,c, ans AS ComplexEXT
                  
                  'a = 1+2i
                  a.r = 1
                  a.i = 2
                  
                  'b=3+4i
                  b.r=3
                  b.i=4
                  
                  'c=5+6i
                  c.r=5
                  c.i=6
                  
                  ComplexMul(b,c,ans)        'get b*c
                  ComplexAdd(a,ans,ans)      'add a to b*c
                  
                  PRINT "a + b*c =";ComplexPrint(ans)
                  
                  
                  WAITKEY$
                  
                  END FUNCTION
                  
                  
                  
                  
                  FUNCTION ComplexPrint(a AS ComplexEXT) AS STRING
                  
                  IF a.i >= 0 THEN
                      FUNCTION = STR$(a.r) + " +" + STR$(a.i) + "i"
                  
                  ELSE
                      FUNCTION = STR$(a.r) + " -" + STR$(-a.i) + "i"
                  
                  END IF
                  
                  END FUNCTION
                  
                  
                  
                  SUB ComplexAdd(a AS ComplexEXT, b AS ComplexEXT, ans AS ComplexEXT)
                  
                  ans.r = a.r + b.r
                  ans.i = a.i + b.i
                  
                  
                  END SUB
                  
                  
                  
                  SUB ComplexSub(a AS ComplexEXT, b AS ComplexEXT, ans AS ComplexEXT)
                  
                  ans.r = a.r - b.r
                  ans.i = a.i - b.i
                  
                  
                  END SUB
                  
                  
                  
                  
                  SUB ComplexMul(a AS ComplexEXT, b AS ComplexEXT, ans AS ComplexEXT)
                  
                  ans.r = a.r * b.r  - a.i * b.i
                  ans.i = a.r * b.i  + b.r * a.i
                  
                  
                  END SUB
                  
                  
                  
                  SUB ComplexDiv(a AS ComplexEXT, b AS ComplexEXT, ans AS ComplexEXT)
                  LOCAL denom AS EXT
                  
                  denom = (b.r*b.r + b.i*b.i)
                  
                  
                  ans.r = (a.r * b.r  + a.i * b.i)/denom
                  ans.i = (a.i * b.r  - b.i * a.r)/denom
                  
                  
                  END SUB

                  Comment


                  • #10
                    Originally posted by Paul Dixon View Post
                    Frank,
                    unfortunately, PB doesn't allow you to return a UDT as the result of a FUNCTION
                    It allows you to return a PTR or DWORD address of the VARPTR(UDT) though, which is even better as it's faster
                    Last edited by Wayne Diamond; 14 Sep 2014, 05:23 PM.
                    -

                    Comment


                    • #11
                      As always, nobody remebers that PB supports COM, and that low-level COM allows to return an structure as the result of a method.

                      An small class with some methods (I have more, but require the use of my headers):

                      Code:
                      ' ########################################################################################
                      ' CAfxComplex Class
                      ' File: CAfxComplex.inc
                      ' Contents: Class to provide support for complex numbers.
                      ' Copyright (c) 2014 José Roca
                      ' Portions Copyright (c) Microsoft Corporation.
                      ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
                      ' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
                      ' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
                      ' ########################################################################################
                      
                      #INCLUDE THIS ONCE
                      %CAFXCOMPLEX_INC = 1
                      
                      #IF NOT %DEF(%COMPLEX_DEFINED)
                      %COMPLEX_DEFINED = 1
                      ' // Size = 16 bytes
                      TYPE tagComplex DWORD
                         x AS DOUBLE   ' real part
                         y AS DOUBLE   ' imaginary part
                      END TYPE
                      #ENDIF
                      
                      ' ########################################################################################
                      ' Complex numbers
                      ' ########################################################################################
                      CLASS CAfxComplex
                      
                      INTERFACE IAfxComplex
                      
                         INHERIT IAutomation
                      
                         ' ========================================================================================
                         ' Uses the cartesian components (x,y) to set the real and imaginary parts of the complex
                         ' ========================================================================================
                         METHOD Set (BYVAL x AS DOUBLE, BYVAL y AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = x
                            z.y = y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the real part of the complex number z.
                         ' ========================================================================================
                         METHOD GetReal (BYREF z AS tagComplex) AS DOUBLE
                            METHOD = z.x
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Sets the real part of the complex number z.
                         ' ========================================================================================
                         METHOD SetReal (BYREF z AS tagComplex, BYVAL x AS DOUBLE)
                            z.x = x
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the imaginary part of the complex number z.
                         ' ========================================================================================
                         METHOD GetImag (BYREF z AS tagComplex) AS DOUBLE
                            METHOD = z.y
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Sets the imaginary part of the complex number z.
                         ' ========================================================================================
                         METHOD SetImag (BYVAL z AS tagComplex, BYVAL y AS DOUBLE)
                            z.y = y
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns -1 if the two complex numbers are equal, or 0 otherwise.
                         ' ========================================================================================
                         METHOD Equal (BYREF z1 AS tagComplex, BYREF z2 AS tagComplex) AS LONG
                            IF z1.x = z2.x AND z1.y = z2.y THEN METHOD = -1
                         END METHOD
                         ' ========================================================================================
                      
                         '/* Complex numbers */
                      
                         ' =====================================================================================
                         ' This method uses the rectangular cartesian components (x,y) to return the complex
                         ' number z = x + i y.
                         ' =====================================================================================
                         METHOD Rect (BYVAL x AS DOUBLE, BYVAL y AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = x
                            z.y = y
                            METHOD = z
                         END METHOD
                         ' =====================================================================================
                      
                         '/* Complex arithmetic operators */
                      
                         ' ========================================================================================
                         ' Returns the sum of the complex numbers a and b, z=a+b.
                         ' ========================================================================================
                         METHOD Add (BYREF a AS tagComplex, BYREF b AS tagComplex) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.x + b.x
                            z.y = a.y + b.y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the difference of the complex numbers a and b, z=a-b.
                         ' ========================================================================================
                         METHOD Sub (BYREF a AS tagComplex, BYREF b AS tagComplex) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.x - b.x
                            z.y = a.y - b.y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the product of the complex numbers a and b, z=ab.
                         ' ========================================================================================
                         METHOD Mul (BYREF a AS tagComplex, BYREF b AS tagComplex) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.x * b.x - a.y * b.y
                            z.y = a.x * b.y + a.y * b.x
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the sum of the complex number a and the real number x, z=a+x.
                         ' ========================================================================================
                         METHOD AddReal (BYREF a AS tagComplex, BYVAL x AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.x + x
                            z.y = a.y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the difference of the complex number a and the real number x, z=a-x.
                         ' ========================================================================================
                         METHOD SubReal (BYREF a AS tagComplex, BYVAL x AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.x - x
                            z.y = a.y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the product of the complex number a and the real number x, z=ax.
                         ' ========================================================================================
                         METHOD MulReal (BYREF a AS tagComplex, BYVAL x AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = x * a.x
                            z.y = x * a.x
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the quotient of the complex number a and the real number x, z=a/x.
                         ' ========================================================================================
                         METHOD DivReal (BYREF a AS tagComplex, BYVAL x AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = z.x / x
                            z.y = z.y / x
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' This method returns the sum of the complex number a and the imaginary number iy, z=a+iy.
                         ' ========================================================================================
                         METHOD AddImag (BYREF a AS tagComplex, BYVAL y AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.x
                            z.y = a.y + y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the difference of the complex number a and the imaginary number iy, z=a-iy.
                         ' ========================================================================================
                         METHOD SubImag (BYREF a AS tagComplex, BYVAL y AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.x
                            z.y = a.y - y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the product of the complex number a and the imaginary number iy, z=a*(iy).
                         ' ========================================================================================
                         METHOD MulImag (BYREF a AS tagComplex, BYVAL y AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = -y * a.y
                            z.y = y * a.y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the quotient of the complex number a and the imaginary number iy, z=a/(iy).
                         ' ========================================================================================
                         METHOD DivImag (BYREF a AS tagComplex, BYVAL y AS DOUBLE) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.y / y
                            z.y = -a.x / y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the complex conjugate of the complex number z, z^* = x - i y.
                         ' ========================================================================================
                         METHOD Conjugate (BYREF a AS tagComplex) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = a.x
                            z.y = -a.y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                         ' ========================================================================================
                         ' Returns the negative of the complex number z, -z = (-x) + i(-y).
                         ' ========================================================================================
                         METHOD Negative (BYREF a AS tagComplex) AS tagComplex
                            LOCAL z AS tagComplex
                            z.x = -a.x
                            z.y = -a.y
                            METHOD = z
                         END METHOD
                         ' ========================================================================================
                      
                      END INTERFACE
                      
                      END CLASS
                      ' ########################################################################################
                      Usage:

                      Code:
                         LOCAL pcn AS IAfxComplex
                         LOCAL z AS tagComplex
                         LOCAL z1 AS tagComplex
                         LOCAL z2 AS tagComplex
                         LOCAL z3 AS tagComplex
                         
                         pcn = CLASS "CAfxComplex"
                         IF ISNOTHING(pcn) THEN EXIT FUNCTION
                         z1 = pcn.Set(-3, 4)
                         z2 = pcn.Set(2, -5)
                         z3 = pcn.Set(1, 1)
                      '   z = pcn.Add(z1, z2)
                         z = pcn.Add(z1, pcn.Mul(z2, z3))
                         ? "Real part = " & STR$(z.x) & $CR & "Imaginary part = " & STR$(z.y)
                      Forum: http://www.jose.it-berater.org/smfforum/index.php

                      Comment


                      • #12
                        If your real and imaginary components in this application are in the range of 16-bit integers, you can do something like ..
                        Code:
                        FUNCTION makeComplex (real AS INTEGER, imaginary as INTEGER) AS LONG 
                        
                           FUNCTION= MAK (LONG, real, imaginary)
                        No, you can't directly do arithmetic on the LONGs. But I'm guessing you can write a couple of FUNCTIONS to do addition, subtraction multiplication and division using a similar format.

                        Need 32-bit real and imaginary components? Change INTEGER/LONG to LONG/QUAD.

                        The speed weenies will tell you to use MACROs instead of functions. Fine.

                        MCM
                        Last edited by Michael Mattias; 15 Sep 2014, 08:26 AM.
                        Michael Mattias
                        Tal Systems Inc. (retired)
                        Racine WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Thanks José , your code works directly !
                          Simply had to put the last code part into the PBMain() and hups ! I get the result !


                          Code:
                          FUNCTION PBMAIN () AS LONG
                             LOCAL pcn AS IAfxComplex
                             LOCAL z AS tagComplex
                             LOCAL z1 AS tagComplex
                             LOCAL z2 AS tagComplex
                             LOCAL z3 AS tagComplex
                              pcn = CLASS "CAfxComplex"
                             IF ISNOTHING(pcn) THEN EXIT FUNCTION
                             z1 = pcn.Set(-3, 5)
                             z2 = pcn.Set(-2, -5)
                             z3 = pcn.Set(1, 1)
                          '   z = pcn.Add(z1, z2)
                             z = pcn.Add(z1, pcn.Mul(z2, z3))
                             ? "Real part = " & STR$(z.x) & $CR & "Imaginary part = " & STR$(z.y)
                          END FUNCTION
                          I want to play more with this .
                          I intend to generate plots of the real and imaginary part of some functions ( for instance (z+1)^n .....and many others ) for educational purposes .

                          BTW :
                          sorry I didn't come to your code . Did a search for "complex number " , but didn't get a match ; further being "new" I'm not familiar with the "COM" concept .
                          Last edited by Frank Kestens; 15 Sep 2014, 03:18 PM.

                          Comment


                          • #14
                            Interesting. Jose and I had the same idea for the arithmetic ... except he went COM 'methods' and I went STDCALL 'functions.' There's probably a meaning in that.

                            MCM
                            Michael Mattias
                            Tal Systems Inc. (retired)
                            Racine WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              Complex division

                              Hi José ,

                              I added this method into my code : complexdivision , which I didn't find in your COM .

                              Code:
                              ' ========================================================================================
                                 ' Returns the division of the complex numbers a and b, z=a/b.
                                 ' ========================================================================================
                                  METHOD Div(BYREF a AS tagComplex, BYREF b AS tagComplex) AS tagComplex
                                      LOCAL denom AS EXT
                                      LOCAL z AS tagComplex
                                      denom = (b.x * b.x + b.y * b.y)
                                      
                                      z.x = (a.x * b.x + a.y * b.y)/denom
                                      z.y = (a.y * b.x - b.y * a.x )/denom
                                      
                                      METHOD = z
                                 END METHOD
                                 ' ========================================================================================

                              Comment


                              • #16
                                Originally posted by Michael Mattias View Post
                                Interesting. Jose and I had the same idea for the arithmetic ... except he went COM 'methods' and I went STDCALL 'functions.' There's probably a meaning in that.
                                MCM
                                Yes , also Paul was close with his approach .
                                Thanks guys ; I have my code with "COM' working .
                                Later I must understand exactly what I'm doing , or what the code does.
                                Saw a lot of new syntax items in this code :laugh:

                                Comment


                                • #17
                                  As you can see, there is not much difference between low-level COM methods and STDCALL functions. The most versatile kind of interface is IAutomation (a misnomer, because it has nothing to do with COM Automation). It is also the least known because it was a late addition, and C++ programmers use IUnknown and VB programmers IDispatch. However, .NET components use it a lot. It has the best of the IUnknown and IDispatch interfaces. It is as fast and lightweight as IUnknown; like IUnknown, accepts any kind of parameters (without the limitations of IDispatch), and like IDispatch, allows the use of properties, besides methods, and return error codes using OBJRESULT, allowing the use of structured error handling (TRY/CATCH). With IUnknown and with standard functions, you usually have to use the result of the method/function to return the error code and, therefore, you have to use intermediate steps and can't use TRY/CATCH.

                                  Of course, they had to do it wrong twice before doing it right.

                                  With a class, you group data and methods, writing code that is reusable.
                                  Forum: http://www.jose.it-berater.org/smfforum/index.php

                                  Comment


                                  • #18
                                    Originally posted by Frank Kestens View Post
                                    Hi José ,

                                    I added this method into my code : complexdivision , which I didn't find in your COM .

                                    Code:
                                    ' ========================================================================================
                                       ' Returns the division of the complex numbers a and b, z=a/b.
                                       ' ========================================================================================
                                        METHOD Div(BYREF a AS tagComplex, BYREF b AS tagComplex) AS tagComplex
                                            LOCAL denom AS EXT
                                            LOCAL z AS tagComplex
                                            denom = (b.x * b.x + b.y * b.y)
                                            
                                            z.x = (a.x * b.x + a.y * b.y)/denom
                                            z.y = (a.y * b.x - b.y * a.x )/denom
                                            
                                            METHOD = z
                                       END METHOD
                                       ' ========================================================================================
                                    I have many methods in my original class, but as it was an example to demonstrate the advantages of using a class for this purpose, I removed most of them because I use in them the math functions of the standard C library.

                                    In my original class, this is the method for Div:

                                    Code:
                                       ' ========================================================================================
                                       ' Returns the quotient of the complex numbers a and b, z=a/b.
                                       ' ========================================================================================
                                       METHOD Div (BYREF a AS tagComplex, BYREF b AS tagComplex) AS tagComplex
                                          LOCAL s AS DOUBLE, z AS tagComplex
                                          s = 1.0 / ME.Abs(b)
                                          z.x = (a.x * (s * b.x) + a.y * (s * b.y)) * s
                                          z.y = (a.y * (s * b.x) - a.x * (s * b.y)) * s
                                          METHOD = z
                                       END METHOD
                                       ' ========================================================================================
                                    and this is the method for Abs:

                                    Code:
                                       ' =====================================================================================
                                       ' Returns the magnitude of the complex number z, |z|.
                                       ' =====================================================================================
                                       METHOD Abs (BYREF z AS tagComplex) AS DOUBLE
                                          METHOD = hypot_(z.x, z.y)
                                       END METHOD
                                       ' =====================================================================================
                                    Abs uses the C function _hypot, that I have declared in my headers (math.inc) as:

                                    Code:
                                    ' ========================================================================================
                                    ' *** All versions ***
                                    ' _CRTIMP double  __cdecl _hypot(_In_ double _X, _In_ double _Y);
                                    ' ========================================================================================
                                    DECLARE FUNCTION hypot_ CDECL IMPORT $CRT_DLLNAME ALIAS "_hypot" ( _
                                       BYVAL x AS DOUBLE _                                  ' __in double x
                                     , BYVAL y AS DOUBLE _                                  ' __in double y
                                     ) AS DOUBLE                                            ' double
                                    ' ========================================================================================
                                    Forum: http://www.jose.it-berater.org/smfforum/index.php

                                    Comment


                                    • #19
                                      Likewise, I have methods for Sin and Cos, that also use C functions.

                                      Code:
                                         ' ========================================================================================
                                         ' Returns the complex sine of the complex number
                                         ' z, sin(z) = (exp(iz) - exp(-iz))/(2i).
                                         ' ========================================================================================
                                         METHOD Sin (BYREF a AS tagComplex) AS tagComplex
                                            LOCAL z AS tagComplex
                                            IF a.y = 0 THEN
                                               z.x = sin_(a.x)
                                            ELSE
                                               z.x = sin_(a.x) * cosh_(a.y)
                                               z.y = cos_(a.x) * sinh_(a.y)
                                            END IF
                                            METHOD = z
                                         END METHOD
                                         ' ========================================================================================
                                      
                                         ' ========================================================================================
                                         ' Returns the complex cosine of the complex number
                                         ' z, cos(z) = (exp(iz) + exp(-iz))/2.
                                         ' ========================================================================================
                                         METHOD Cos (BYREF a AS tagComplex) AS tagComplex
                                            LOCAL z AS tagComplex
                                            IF a.y = 0 THEN
                                               z.x = cos_(a.x)
                                            ELSE
                                               z.x = cos_(a.x) * cosh_(a.y)
                                               z.y = sin_(a.x) * sinh_(-a.y)
                                            END IF
                                            METHOD = z
                                         END METHOD
                                         ' ========================================================================================
                                      Forum: http://www.jose.it-berater.org/smfforum/index.php

                                      Comment


                                      • #20
                                        sin_ and cos_ can be replaced with PB's SIN and COS, and sinh_ and cosh_ with:

                                        The Hyperbolic Sine (SINH) of a value can also be calculated:

                                        SinH = (EXP(Value) - EXP(-Value)) / 2

                                        The Hyperbolic Cosine (COSH) of a value can also be calculated:

                                        CosH = (EXP(Value) + EXP(-Value)) / 2

                                        Although the PB functions are slower than the C ones.
                                        Forum: http://www.jose.it-berater.org/smfforum/index.php

                                        Comment

                                        Working...
                                        X