News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Solving real expressions

Started by RuiLoureiro, March 24, 2012, 06:30:21 PM

Previous topic - Next topic

raymond

Quote from: dedndave on March 30, 2012, 08:18:39 PM
push eax
fld real4 ptr [esp]
pop eax


Values in a register from previous computations would normally be integers. If so, the correct load would be:

fild dword ptr [esp]

Quoteand i want to load it directly into Fpu ?

You are correct: impossible. No single instruction can do that.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dedndave

Quote from: raymond on March 30, 2012, 08:41:02 PMValues in a register from previous computations would normally be integers. If so, the correct load would be:

fild dword ptr [esp]

huh ?
oh - i got it - lol

        push    eax
        fild dword ptr [esp]
        pop     eax

RuiLoureiro

Raymond and Dave,

        Thank you !
       
        Tomorrow calcula27 does factorial ( i hope  :lol)

RuiLoureiro

Hi
    i wrote this procedure to calculate the logarithm base B
    ( B = _Integer32 positive not 1) and it works perfectly
    when i use base e (fldln2) or base 10 (fldlg2).

    1. What is the best: base 10 or base E ?
    2. Is there a directly way to calculate log base 2 ?

Quote
                fld     tbyte ptr OperandZ
               
                cmp     _Integer32, 10
                je      short _fnc3     ; go to log(x)         

                fldln2                  ; or this: fldlg2
                ;fldlg2
                fxch
                fyl2x                   ;->[log2(OperandZ)]*ln(2) = ln(OperandZ) base e

                fstsw ax                ;retrieve exception flags from FPU
                fwait
                shr   al,1              ;test for invalid operation
                jc    _erro0
               
                fild    dword ptr _Integer32

                fldln2                  ; or this: fldlg2
                ;fldlg2
                fxch
                fyl2x                   ;->[log2(_Integer32)]*ln(2) = ln(_Integer32) base e

                fstsw ax                ;retrieve exception flags from FPU
                fwait
                shr   al,1              ;test for invalid operation
                jc    _erro2

                fdiv                    ; st(0)= ln(OperandZ) / ln(_Integer32)
                jmp   _exit
               
    _erro2:     fstp  st
    _erro0:     fstp  st                ;get rid of st(0)
                ret 

dedndave

Log10 are called "common logs" (Log)
Loge are called "natural logs" (Logn)
both are used and should be supported

the FYL2X instruction calculates Y*Log2(X)
that's pretty close   :P
if Y = 1, then it calculates Log2

RuiLoureiro

    Dave,
            Ok thanks, it is exactly that
Quote
                fld     tbyte ptr OperandZ
                fld1
                fxch
                fyl2x                   ->[log2(OperandZ)]*1.0 = log2(OperandZ) base 2
               
                fstsw ax                ;retrieve exception flags from FPU
                fwait
                shr   al,1              ;test for invalid operation
                jc    _erro0
                ret               

Quote
Log10 are called "common logs" (Log)
Loge are called "natural logs" (Logn)
both are used and should be supported
both are supported in calcula27:
            ln(x)   - "natural logs"
            log(x)  - "common logs"

        In the next calcula28 we can use

            logb(x,n)   - base n
            LOGB(x,n)   - base n

        and more ...

dedndave

yah - "ln" is the right way - not "logn"   :U

Ln is used in trig and geometry, etc
Log is used in physics, electronics, etc

for example, a decibel is one tenth of a bel (named after Alexander Graham Bell, of course)
L(Bels) = Log(P2/P1), where P1,2 are power levels and L is a ratio of level change

RuiLoureiro

Hi all,
        With calcula28 we can use

            logb(x,n)   - base n                      [e.g.: logb(23,5) ]
            LOGB(x,n)   - base n
           
            n!          - factorial                        [e.g.: 10! ]
             
            comb(n, k)  - k-combination of n    [e.g.: comb(20,5) ]
            COMB(n, k)

        Try it and say something
        Thanks

i replaced calcula28. Now we can do t=10;k=5;
and comb(n,k)

       
        Here are some results       
Quote
-------------------------------------------------------------------
Type an Expression, Equation or Constants and press enter
1e23
This is your expression: 1e23
This is your Result:   1.000000000000000E+0023
1e23= 1.000000000000000E+0023
---------------------------------------------
Type an Expression, Equation or Constants and press enter
10^23
This is your expression: 10^23
This is your Result:   1.000000000000000E+0023
10^23= 1.000000000000000E+0023
---------------------------------------------
Type an Expression, Equation or Constants and press enter
10!
This is your expression: 10!
This is your Result:   3628800.000000000
10!= 3628800.000000000
---------------------------------------------
Type an Expression, Equation or Constants and press enter
100!
This is your expression: 100!
This is your Result:   9.332621544394415E+0157
100!= 9.332621544394415E+0157
---------------------------------------------
Type an Expression, Equation or Constants and press enter
10!/(2!*(10-2)!)
This is your expression: 10!/(2!*(10-2)!)
This is your Result:   45.00000000000000
10!/(2!*(10-2)!)= 45.00000000000000
---------------------------------------------
Type an Expression, Equation or Constants and press enter
comb(10,2)
This is your expression: comb(10,2)
This is your Result:   45.00000000000000
comb(10,2)= 45.00000000000000
---------------------------------------------
Type an Expression, Equation or Constants and press enter
comb(500,20)
This is your expression: comb(500,20)
This is your Result:   2.667198512837438E+0035
comb(500,20)= 2.667198512837438E+0035
---------------------------------------------
Type an Expression, Equation or Constants and press enter
1000!
This is your expression: 1000!
This is your Result:   4.023872600770939E+2567
1000!= 4.023872600770939E+2567
---------------------------------------------
Type an Expression, Equation or Constants and press enter
log(23)
This is your expression: log(23)
This is your Result:   1.361727836017593
log(23)= 1.361727836017593
---------------------------------------------
Type an Expression, Equation or Constants and press enter
logb(23,10)
This is your expression: logb(23,10)
This is your Result:   1.361727836017593
logb(23,10)= 1.361727836017593
---------------------------------------------
Type an Expression, Equation or Constants and press enter
ln(23)
This is your expression: ln(23)
This is your Result:   3.135494215929150
ln(23)= 3.135494215929150
---------------------------------------------
Type an Expression, Equation or Constants and press enter
t=10;x=13;
This is your expression: t=10;x=13;
Type an Expression, Equation or Constants and press enter
ln(t+x)
This is your expression: ln(t+x)
This is your Result:   3.135494215929150
ln(t+x)= 3.135494215929150
---------------------------------------------
Type an Expression, Equation or Constants and press enter
ln(x-t)
This is your expression: ln(x-t)
This is your Result:   1.098612288668110
ln(x-t)= 1.098612288668110
---------------------------------------------
Type an Expression, Equation or Constants and press enter
ln(3)
This is your expression: ln(3)
This is your Result:   1.098612288668110
ln(3)= 1.098612288668110
---------------------------------------------
Type an Expression, Equation or Constants and press enter
logb(25,12)
This is your expression: logb(25,12)
This is your Result:   1.295370924755994
logb(25,12)= 1.295370924755994
---------------------------------------------
Type an Expression, Equation or Constants and press enter
logb(25,16)
This is your expression: logb(25,16)
This is your Result:   1.160964047443681
logb(25,16)= 1.160964047443681
---------------------------------------------
Type an Expression, Equation or Constants and press enter
logb(12.25,20)
This is your expression: logb(12.25,20)
This is your Result:   0.836365104822369
logb(12.25,20)= 0.836365104822369
---------------------------------------------
Type an Expression, Equation or Constants and press enter
logb(10,2)
This is your expression: logb(10,2)
This is your Result:   3.321928094887362
logb(10,2)= 3.321928094887362
---------------------------------------------

MichaelW

Rui,

Have you considered testing your calculation code against an established library, GSL for example?

http://www.masm32.com/board/index.php?topic=16725.0

eschew obfuscation

RuiLoureiro

Hi MichaelW

        The answer is no. To test some functions i can use
        the Raymond program, but i need more time to do this.
       
        But, in any way, i dont know how to access to that
        "established" library. Is it easy to use ?
       
        My work is to use the correct instructions, coding
        the procedures correctly and in this way i hope
        the result is correct (unless it has a bug).

        Did you test some functions ?
       
        Thank you for your reply Michael  :wink
        Rui Loureiro

RuiLoureiro

        Now, calcula29 has one more function: mean(x1,x2,...,xn)

        It gives the variance and standard deviation
        Try it and say something
        Thanks
Quote
-------------------------------------------------------------------
Type an Expression, Equation or Constants and press enter
mean(10,11,12,13,14,15)

This is your expression: mean(10,11,12,13,14,15)
---------------------------------------------
mean(10,11,12,13,14,15)= 12.50000000000000
Variance:  2.916666666666667
Standard Deviation:  1.707825127659933
Sample Variance:  3.500000000000000
Sample Standard Deviation:  1.870828693386971
---------------------------------------------

dedndave

what we need is to make a little GUI app
with support for variable names, subscripting, and superscripting   :U
add in the normal scientific calculator buttons - with unit conversions, memory, and so on

RuiLoureiro

Hi Dave,

Quote
what we need is to make a little GUI app

        Do you want to share this project with me ?
        Anyone wants ?
       
Quote
with support for variable names

        With calcula29 we can define 10 variable names

        Now, with calcula30 we can solve
        a system of 2 linear equations.
        It is under test but it seems it works correctly.
        I use matrices to solve it.

         We must use aX+bY=c; fX+gY=h;  The first must be X and second Y.
Quote
-------------------------------------------------------------------
Type an Expression, Equation or Constants and press enter
2x+3y=10;x-y=2;
This is your expression: 2x+3y=10;x-y=2;
---------------------------------------------
X=  3.200000000000000
Y=  1.200000000000000
---------------------------------------------
Type an Expression, Equation or Constants and press enter

dedndave

well - most members don't like the way i write code - lol
i don't care for .IF/.ELSEIF/.ENDIF or .WHILE/.ENDW, nor do i care for indenting code
but - all you need is an edit box and a bunch of buttons - toss in some menus   :P
the GUI part would be much easier than the math part