News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

The calculator

Started by RuiLoureiro, May 31, 2012, 10:59:09 PM

Previous topic - Next topic

qWord

hello,
what must be inputted to get the derivate of (e.g.) f(x)=x2?
For: df(x)=x^2 [ENTER] -> df(x=1) [ENTER] we get the output: X=1 and df(X)= 1.0, which is wrong.

regards, qWord
MREAL macros - when you need floating point arithmetic while assembling!

MichaelW

Rui,

I tested one of your expressions in test apps compiled with FreeBASIC and C (MSVC and GCC), and your expression evaluator produces a different result. Sorry, I didn't have time to test the other expressions.

dim as longint i
i = -(&h21 shl 2 + (&h34 and -&h56) shl 3 and &h99 xor &h12)
print i
print hex(i)
sleep

/'
Relevant parts of operator precedence table:

Operator  Description           Associativity

  ^       Exponentiate          Left-to-Right
  -       Negate                Right-to-Left
  *       Multiply              Left-to-Right
  /       Divide                Left-to-Right
  MOD     Modulus               Left-to-Right
  SHL     Shift left            Left-to-Right
  SHR     Shift right           Left-to-Right
  +       Add                   Left-to-Right
  -       Subtract              Left-to-Right
  =       Equal                 Left-to-Right
  <>      Not equal             Left-to-Right
  <       Less than             Left-to-Right
  <=      Less than or equal    Left-to-Right
  >=      Greater than or equal Left-to-Right
  >       Greater than          Left-to-Right
  NOT     Complement            Right-to-Left
  AND     Conjunction           Left-to-Right
  OR      Inclusive Disjunction Left-to-Right
  EQV     Equivalence           Left-to-Right
  IMP     Implication           Left-to-Right
  XOR     Exclusive Disjunction  Left-to-Right
'/


-146
FFFFFFFFFFFFFF6E


#include <conio.h>
#include <stdio.h>
void main( void )
{
    long long i;
    i = -((0x21 << 2) + ((0x34 & -0x56) << 3) & 0x99 ^ 0x12);
    printf("%I64d\n%I64X\n",i,i);
    getch();
}

/*
Relevant parts of operator precedence table:

Operator  Description                 Associativity

  +       Unary plus                  Right-to-left
  -       Unary minus                 Right-to-left
  ~       Bitwise NOT                 Right-to-left
  *       Multiplication              Left-to-right
  /       Division                    Left-to-right
  %       Modulo (remainder)          Left-to-right
  +       Addition                    Left-to-right
  -       Subtraction                 Left-to-right
  <<      Bitwise left shift          Left-to-right
  >>      Bitwise right shift         Left-to-right
  <       Less than                   Left-to-right
  <=      Less than or equal to       Left-to-right
  >       Greater than                Left-to-right
  >=      Greater than or equal to    Left-to-right
  ==      Equal to                    Left-to-right
  !=      Not equal to                Left-to-right
  &       Bitwise AND                 Left-to-right
  ^       Bitwise XOR (exclusive or)  Left-to-right
  |       Bitwise OR (inclusive or)   Left-to-right
  =       Direct assignment           Right-to-left
*/


-146
FFFFFFFFFFFFFF6E

Well Microsoft, here's another nice mess you've gotten us into.

RuiLoureiro

Dave,
            :t
       
hello qWord,

        We have not any way to get a derivative from f(x).
        We should write f(x) and we should write
        df(x) also.
        In this way for f(x)=x^2 we should write
        df(x)=2*x. For instance, for
        f(x)=x^2+sin(x) we have df(x)=2*x+cos(x).
        df(x) is used in one case in the root function.

        Rules: If we define f(x)=x^2 it is wrong to
               define the derivative of f(x) as
               the function df(x)=x^2.
               When we are typing df(x)= ...
               we are defining it.

        note: if f(x)=x^3/3 then df(x)= x^2.

best regards  :t
Rui

RuiLoureiro

MichaelW,

        Thanks for your tests

        When i thought to include logic expressions
        the question was this: what operators to use.
        May be someone should help me

        In any way, i defined the next set of operators

        operators          what to do
        -------------     -----------------------
           - (minus)    invert the operand    => -1100b => 0011b
                        (the same as NOT)

           + (plus)     the same as OR

           OR           the same as "or"      => 11 0101b or  00 1101b => 11 1101b
                          instruction   

          AND        the same as "and"    => 11 0101b and 00 1101b => 00 0101b
                          instruction   

          XOR         the same as "xor"     => 11 0101b xor 00 1101b => 11 1000b
                          instruction   

          SHL           the same as "shl"     => 11 0101b shl 2d       => 1101 0100b
                          instruction   

          SHR           the same as "shr"     => 11 0101b shr 2d       => 0000 1101b
                          instruction   


        We may use or, and, xor, shl, shr also.
        The calculator doesnt use "not" or "NOT".

        -------------------------------
        How the calculator works
        --------------------------------

            First: any number with minus sign is inverted
                   before starting the operation.
                   
                   In this way writing -1100b is the same
                   as writing 0011b (it should be).

            Next:  first operate SHL / SHR from left to right
                   and next all others. We may change this
                   rule using brackets.

            If we want to change this rules we need to use
            brackets. We may use as many as we want.
           
example:
        This:
                                             
           -(&h21 shl 2 + (&h34 and -&h56) shl 3 and &h99 xor &h12)

        is this:
       
    type:   -(21h shl 2d + (34h and -56h) shl 3d and 99h xor 12h)

    we get:     unsigned: 18446744073709551469 / signed: -147
                FFFFFFFFFFFFFF6DH
                11111111111111111111111111111111
                11111111111111111111111101101101B


        So
            1. We solve this: (34h and -56h)   
       
                32
                00000020H
                00000000000000000000000000100000B

            Now the expression is:

                -(21h shl 2d + 20h shl 3d and 99h xor 12h)

            2. From left to right we need to solve 21h shl 2d

               21h shl 2d = 132
                            00000084H
                            00000000000000000000000010000100B

            Now the expression is:

                -(84h + 20h shl 3d and 99h xor 12h)


            3. From left to right we need to solve 20h shl 3d

                20h shl 3d = 256
                             00000100H
                             00000000000000000000000100000000B

            Now the expression is:

                -(84h + 100h and 99h xor 12h) = -(84h or 100h and 99h xor 12h) =


            Now we solve it from left to right:

                    84h or 100h = 388
                                  00000184H
                                  00000000000000000000000110000100B

                = -(184h and 99h xor 12h)


                    184h and 99h = 128
                                   00000080H
                                   00000000000000000000000010000000B

                = -(80h xor 12h)

                    80h xor 12h = 146
                                  00000092H
                                  00000000000000000000000010010010B

                = -92h

            Now we should invert it               

                = -00000000000000000000000010010010B
               
                =   111111111111111111111111 1111 1111
                    111111111111111111111111 0110 1101b

            To use the calculator we should use this: -92h or 0h
            (because -92h gives the two's complement not the inverse)

            The result is this:
                               
            unsigned: 18446744073709551469 / signed: -147
            FFFFFFFFFFFFFF6DH
            111111111111111111111111 1111 1111
            111111111111111111111111 0110 1101B

               
            I think that the problem is (or seems to be) the calculation rules

RuiLoureiro

MichaelW,
            From your data we have

Relevant parts of operator precedence table I:

Operator  Description           Associativity

  ^       Exponentiate          Left-to-Right
  -       Negate                Right-to-Left
  *       Multiply              Left-to-Right
  /       Divide                Left-to-Right
  MOD     Modulus               Left-to-Right
  SHL     Shift left            Left-to-Right
  SHR     Shift right           Left-to-Right
  +       Add                   Left-to-Right
  -       Subtract              Left-to-Right
  =       Equal                 Left-to-Right
  <>      Not equal             Left-to-Right
  <       Less than             Left-to-Right
  <=      Less than or equal    Left-to-Right
  >=      Greater than or equal Left-to-Right
  >       Greater than          Left-to-Right
  NOT     Complement            Right-to-Left
  AND     Conjunction           Left-to-Right
  OR      Inclusive Disjunction Left-to-Right
  EQV     Equivalence           Left-to-Right
  IMP     Implication           Left-to-Right
  XOR     Exclusive Disjunction  Left-to-Right



Relevant parts of operator precedence table II:

Operator  Description                 Associativity

  +       Unary plus                  Right-to-left
  -       Unary minus                 Right-to-left
  ~       Bitwise NOT                 Right-to-left
  *       Multiplication              Left-to-right
  /       Division                    Left-to-right
  %       Modulo (remainder)          Left-to-right
  +       Addition                    Left-to-right
  -       Subtraction                 Left-to-right
  <<      Bitwise left shift          Left-to-right
  >>      Bitwise right shift         Left-to-right
  <       Less than                   Left-to-right
  <=      Less than or equal to       Left-to-right
  >       Greater than                Left-to-right
  >=      Greater than or equal to    Left-to-right
  ==      Equal to                    Left-to-right
  !=      Not equal to                Left-to-right
  &       Bitwise AND                 Left-to-right
  ^       Bitwise XOR (exclusive or)  Left-to-right
  |       Bitwise OR (inclusive or)   Left-to-right
  =       Direct assignment           Right-to-left


--------------------------------------------------------
The calculator operator precedence table III
--------------------------------------------------------

Operator  Description           Associativity  Priority

  SHL     Shift left            Left-to-Right     1
  shl
 
  SHR     Shift right           Left-to-Right     1
  shr
 
  -       Complement            Left-to-Right     0
 
  AND     Conjunction           Left-to-Right     2
  and
 
  OR      Inclusive Disjunction Left-to-Right     2
  or 
  +       Inclusive Disjunction Left-to-Right     2

  XOR     Exclusive Disjunction Left-to-Right     2
  xor

1. First are executed expressions inside brackets

2. All expressions are executed from left to right

3. Priority 0 means that it is executed before any other

4. All expressions with priority 1 are executed before
   that with priority 2

NOTE: for decimal values we must end with "d" or "D"
           For instance, 12345 should be 12345d or 12345D with spaces
           or not

dedndave

you could also add a few logic operations that CPU's don't generally support

NAND = NOT AND
NOR  = NOT OR
XNOR = NOT XOR


when i design circuits, i find that NAND and NOR gates are more versatile than AND and OR gates   :P

RuiLoureiro

Quote from: dedndave on June 11, 2013, 09:01:01 PM
you could also add a few logic operations that CPU's don't generally support

NAND = NOT AND
NOR  = NOT OR
XNOR = NOT XOR


when i design circuits, i find that NAND and NOR gates are more versatile than AND and OR gates   :P
Yes it is true,
Ok, i will do it Dave  :t

dedndave

if f(x) = x2, then f'(x) = 2x, and f''(x) = 2   :biggrin:

RuiLoureiro

Quote from: dedndave on June 11, 2013, 09:05:24 PM
if f(x) = x2, then f'(x) = 2x, and f''(x) = 2   :biggrin:
:biggrin:
Hey, Dave i wrote this:
Quote
In this way for f(x)=x^2 we should write
        df(x)=2*x.
and you should know that 2X is equal to 2*X.
         Dave about this i know what i am doing !

daydreamer

Quote from: dedndave on June 11, 2013, 09:01:01 PM
you could also add a few logic operations that CPU's don't generally support

NAND = NOT AND
NOR  = NOT OR
XNOR = NOT XOR


when i design circuits, i find that NAND and NOR gates are more versatile than AND and OR gates   :P
whats wrong with PANDN instruction?
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

qWord

Quote from: daydreamer2 on June 12, 2013, 10:20:03 PM
Quote from: dedndave on June 11, 2013, 09:01:01 PM
you could also add a few logic operations that CPU's don't generally support

NAND = NOT AND
NOR  = NOT OR
XNOR = NOT XOR


when i design circuits, i find that NAND and NOR gates are more versatile than AND and OR gates   :P
whats wrong with PANDN instruction?
NAND is: C = NOT (A AND B);
PANDN does: C = (NOT A) AND B
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

the PORN operation would be nice, also   :badgrin:

i can't buy those gates in TTL or CMOS DIP packages
but, i can use them in ASIC designs   :P

RuiLoureiro

#27
                        *** The Calculator v2.20 ***

The previous Calculator v2.10 doesnt solve
logic expressions correctly because i didnt write
the basic algorithm correctly - problems with brackets.

Now we have also the text file RulesV2_20.txt where i
say something about the calculator and not only.

You are there,
Read it and then ... say something
Sorry if i am not good in writing words, words, words ...

Good Luck
Thanks      :t
RuiLoureiro

Files: Calcula54.zip & RulesV2_20.zip


        Logic Expressions
        --------------------------

                    The calculator operator precedence table
                    ----------------------------------------------------
                   
                  Operator  Description           Associativity  Priority

                    SHL     Shift left            Left-to-Right     1
                    shl
 
                    SHR     Shift right           Left-to-Right     1
                    shr
 
                    -       Complement (=NOT)     Left-to-Right     0
 
                    AND     Conjunction           Left-to-Right     2
                    and
 
                    OR      Inclusive Disjunction Left-to-Right     2
                    or 
                    +       Inclusive Disjunction Left-to-Right     2

                    XOR     Exclusive Disjunction Left-to-Right     2
                    xor

                    NAND    not AND =-(x.y)       Left-to-Right     2
                    nand

                    NOR     not OR  =-(x+y)       Left-to-Right     2
                    nor

                    NXOR    not XOR               Left-to-Right     2
                    nxor


                    1. Expressions inside brackets are executed first

                    2. All expressions are executed from left to right

                    3. Priority 0 means that it is executed before any other

                    4. All expressions with priority 1 are executed before
                       that with priority 2

                    NOTE: for decimal values we must end with "d" or "D"
                          if it is the last value.
                    For instance, 12345 should be 12345d or 12345D with spaces
                    or not

        ------------
        Examples
        ------------
   
    type:   21h shl 2d + 34h and -56h shl 3d and 99h xor 12h

    we get:     18
                00000012H
                00000000000000000000000000010010B
   
    type:   -(21h shl 2d) + 34h and -56h shl 3d and 99h xor 12h

    we get:     26
                0000001AH
                00000000000000000000000000011010B

    type:   -(21h shl 2d + (34h and -56h) shl 3d and 99h xor 12h)

    we get:     unsigned: 18446744073709551469 / signed: -147
                FFFFFFFFFFFFFF6DH
                11111111111111111111111111111111
                11111111111111111111111101101101B

               
    type:   34h and 56h shl 3d + 24h nand 56 shr 2d nor 33 nxor 20h xor 11111b

    we get:     unsigned: 18446744073709551556 / signed: -60
                FFFFFFFFFFFFFFC4H
                11111111111111111111111111111111
                11111111111111111111111111000100B


    type:   -(34h and (56h shl 3d) or (24h nand 56) shr 2d nor 33+22 nxor 20h) xor 11111b

    we get:     unsigned: 13835058055282163745 / signed: -4611686018427
                C000000000000021H
                11000000000000000000000000000000
                00000000000000000000000000100001B

               
    type:  -(34h and (56h shl 3d) or (24h nand 56) shr 2d nor 33 or 22 nxor 20h) xor 11111b

                unsigned: 13835058055282163745 / signed: -4611686018427
                C000000000000021H
                11000000000000000000000000000000
                00000000000000000000000000100001B


    type:   -(34h and -(56h shl 3d) or -(24h nand 56) shr 2d nor 33+22 nxor 20h) xor 11111b

    we get:     unsigned: 18446744073709551593 / signed: -23
                FFFFFFFFFFFFFFE9H
                11111111111111111111111111111111
                11111111111111111111111111101001B


    type:   -(1101b AND -(56h SHL 11b) OR -(24h nand 56) shr 2d nor 33+01100111b nxor 20h) xor 11111b

    we get:     unsigned: 18446744073709551560 / signed: -56
                FFFFFFFFFFFFFFC8H
                11111111111111111111111111111111
                11111111111111111111111111001000B


    type:   -(000000000000000000000000000001101h AND -(56h SHL 11b) OR -(24h nand 56) shr 2d nor 33+01100111b nxor 20h) xor 11111b

    we get:     unsigned: 18446744073709547208 / signed: -4408
                FFFFFFFFFFFFEEC8H
                11111111111111111111111111111111
                11111111111111111110111011001000B


    type:   (-(1101b AND -(56h SHL 11b) OR -(24h nand 56) shr 2d nor 33+01100111b nxor 20h) xor 11111b)
            or
            (34h and 56h shl 3d + 24h nand 56 shr 2d nor 33 nxor 20h xor 11111b)
            and
            -(21h shl 2d + 34h and -56h shl 3d and 99h xor 12h)

   
    we get:     unsigned: 18446744073709551564 / signed: -52
                FFFFFFFFFFFFFFCCH
                11111111111111111111111111111111
                11111111111111111111111111001100B
   

    -------------------------------------------------------
    conversions decimal-hexadecimal-binary (64 bits)
    -------------------------------------------------------

    type: -12d      (decimal end with "d" or "D")

    we get:     unsigned: 18446744073709551604 / signed: -12
                         
                FFFFFFFFFFFFFFF4H
                11111111111111111111111111111111
                11111111111111111111111111110100B

    type: 18446744073709551604d

    we get:     18446744073709551604d
   
                FFFFFFFFFFFFFFF4H
                11111111111111111111111111111111   
                11111111111111111111111111110100B


    type:   FFFFFFFFFFFFFFF4H

    we get:     unsigned: 18446744073709551604 / signed: -12
                          18446744073709551604
                         
                FFFFFFFFFFFFFFF4H
                11111111111111111111111111111111
                11111111111111111111111111110100B


    type:   FFFFFFFFFFFFFFFFH

    we get:     unsigned: 18446744073709551615 / signed: -1
                FFFFFFFFFFFFFFFFH
                11111111111111111111111111111111
                11111111111111111111111111111111B               


    type:   7FFF FFFF FFFF FFFFh

    we get:     9223372036854775807

                7FFFFFFFFFFFFFFFh
                01111111111111111111111111111111
                11111111111111111111111111111111B
               
    type:   9223372036854775807d   
       
    we get:     9223372036854775807d       
                7FFFFFFFFFFFFFFFH
                01111111111111111111111111111111
                11111111111111111111111111111111B
   

dedndave

nice, as always, Rui   :t

did you add the PORN operator ?   :biggrin:

you could leave the "P" out
i think that indicates bitwise, which - all our operations are bitwise
so, it would be
NOT   C = NOT(A)
AND   C = A AND B
OR    C = A OR B
XOR   C = A XOR B
NAND  C = NOT (A AND B)
NOR   C = NOT (A OR B)
XNOR  C = NOT (A XOR B) = A XOR (NOT B) = (NOT A) XOR B
ANDN  C = A AND (NOT B)
ORN   C = A OR (NOT B)


XORN is not needed - logically the same as XNOR

RuiLoureiro

#29
Quote from: dedndave on June 13, 2013, 03:46:02 AM
nice, as always, Rui   :t

did you add the PORN operator ?   :biggrin:

you could leave the "P" out
i think that indicates bitwise, which - all our operations are bitwise
so, it would be
NOT   C = NOT(A)
AND   C = A AND B
OR    C = A OR B
XOR   C = A XOR B
NAND  C = NOT (A AND B)
NOR   C = NOT (A OR B)
XNOR  C = NOT (A XOR B) = A XOR (NOT B) = (NOT A) XOR B
ANDN  C = A AND (NOT B)
ORN   C = A OR (NOT B)


XORN is not needed - logically the same as XNOR
Hi Dave,
              i will reply soon, i hope!
              i want to post the new powerful calculator with
              new interesting functions. But we need to wait
              because i need to test it.
              Yes it does ORN and ANDN also.
              Replacing constants is now a very simple
              and fast procedure.
              We compute expressions to save as a new or old constant/variable
              The same for matrices.
               We have up to 20 real/logic/matrix names for constants/variables.
               The Inverse matrix is all inside the edit box in any case 2x2 to 20x20.
               Error messages are now better