Resolve Math Functions PL/SQL

后端 未结 2 1101
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 13:41

I need to resolve a math equation/function in pl/sql.
What kind of math operations/functions are available in oracle pl/sql that could help me resolve a math function like t

相关标签:
2条回答
  • 2021-02-15 14:14

    Alas the Oracle database is not a mathematical tool. It has lots of arithmetical and statistical functions but it doesn't have built-in functionality capable of interpreting equations. Sorry.


    By sheer coincidence Marc (AKA Odie_63) has recently published a Reverse Polish Notation calculator which he has written in PL/SQL. It doesn't do precisely what you want but I'm including a link for the benefit of any seekers who may stumble upon this thread in the future. Find out more.

    0 讨论(0)
  • 2021-02-15 14:33

    As APC said, there is no built-in functionality to do this. But you can use the WolframAlpha API from PL/SQL:

    declare
        v_equation varchar2(32767) := 
            '(3.5/(1+x))+(3.5/(1+x)^2)+(3.5/(1+x)^3)+(3.5/(1+x)^4)+(100/(1+x)^4)=101.55';
        v_escaped_url varchar2(32767);
        v_uri httpuritype;
        v_xml xmltype;
        v_count number := 1;
    begin
        --Escape the URL.
        --I used chr(38) for ampersand, in case your IDE think it's a substitution variable
        v_escaped_url :=
            'http://api.wolframalpha.com/v2/query?appid=EQGHLV-UYUEYY9ARU'||chr(38)||'input='
            ||utl_url.escape(v_equation, escape_reserved_chars => true)
            ||chr(38)||'format=plaintext';
    
        --Create an HTTPURIType, and get the XML
        v_uri := httpuritype.createUri(v_escaped_url);
        v_xml := v_uri.getXML;
    
        --Complex solutions
        while v_xml.existsNode('/queryresult/pod[@title="Complex solutions"]/subpod['||v_count||']') = 1 loop
            dbms_output.put_line(v_xml.extract('/queryresult/pod[@title="Complex solutions"]/subpod['||v_count||']/plaintext/text()').getStringVal());
            v_count := v_count + 1;
        end loop;
    
        --Real solutions
        v_count := 1;
        while v_xml.existsNode('/queryresult/pod[@title="Real solutions"]/subpod['||v_count||']') = 1 loop
            dbms_output.put_line(v_xml.extract('/queryresult/pod[@title="Real solutions"]/subpod['||v_count||']/plaintext/text()').getStringVal());
            v_count := v_count + 1;
        end loop;
    end;
    /
    

    Results:

    x = -1.00006-0.996229 i
    x = -1.00006+0.996229 i
    x = -1.99623
    x = 0.0308219
    

    There are a lot of potential downsides to this approach. It will be very slow, and the API is not free. My example works because I used my free developer appid, but it's only good for a small number of calls.

    0 讨论(0)
提交回复
热议问题