exist icon indicating copy to clipboard operation
exist copied to clipboard

[BUG] arithmetic

Open KitWallace opened this issue 1 year ago • 4 comments

xquery version "3.1"; let $d := 30.0 return ($d * 3.14159 div 180.0, $d * 3.14159 div 180)

In exist 6.2.0, the results are

0.523598333333333333 0.522222222222222222

on eXist 3.0.1

0.523598333333333333 0.523598333333333333

on Saxon via https://xqueryfiddle.liberty-development.net/

the results are

0.52359833333333333333333 0.5235983333333333333333

KitWallace avatar Sep 05 '23 09:09 KitWallace

Thanks @KitWallace for reporting this issue. I cannot reproduce this issue with 7.0.0-SNAPSHOT It seems only 6.2.0 is affected so far.

line-o avatar Sep 05 '23 10:09 line-o

I could also reproduce this issue on 6.1.0

line-o avatar Sep 05 '23 10:09 line-o

xquery version "3.1";

module namespace tar="http://exist-db.org/xquery/test/arithmetic";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare variable $tar:number := 30.0;
declare variable $tar:factor := 1.234;

declare
    %test:assertEquals(0.205666666666666667)
function tar:literals-div-integer() {
    30.0 * 1.234 div 180
};

declare
    %test:assertEquals(0.205666666666666667)
function tar:literals-div-decimal() {
    30.0 * 1.234 div 180.0
};

declare
    %test:assertTrue
function tar:literals-div-equality() {
    30.0 * 1.234 div 180 eq
    30.0 * 1.234 div 180.0
};

declare
    %test:assertEquals(0.205666666666666667)
function tar:number-var-div-integer() {
    $tar:number * 1.234 div 180
};

declare
    %test:assertEquals(0.205666666666666667)
function tar:number-var-div-decimal() {
    $tar:number * 1.234 div 180.0
};

declare
    %test:assertTrue
function tar:number-var-div-equality() {
    $tar:number * 1.234 div 180 eq
    $tar:number * 1.234 div 180.0
};

declare
    %test:assertEquals(0.205666666666666667)
function tar:factor-var-div-integer() {
    30.0 * $tar:factor div 180
};

declare
    %test:assertEquals(0.205666666666666667)
function tar:factor-var-div-decimal() {
    30.0 * $tar:factor div 180.0
};

declare
    %test:assertTrue
function tar:factor-var-div-equality() {
    30.0 * $tar:factor div 180 eq
    30.0 * $tar:factor div 180.0
};

declare
    %test:assertEquals(0.205666666666666667)
function tar:variables-div-integer() {
    $tar:number * $tar:factor div 180
};

declare
    %test:assertEquals(0.205666666666666667)
function tar:variables-div-decimal() {
    $tar:number * $tar:factor div 180.0
};

declare
    %test:assertTrue
function tar:variables-div-equality() {
    $tar:number * $tar:factor div 180 eq
    $tar:number * $tar:factor div 180.0 
};

results in

<testsuites>
    <testsuite package="http://exist-db.org/xquery/test/arithmetic" timestamp="2023-09-05T11:26:21.307Z" tests="12" failures="4" errors="0" pending="0" time="PT0.011S">
        <testcase name="factor-var-div-decimal" class="tar:factor-var-div-decimal"/>
        <testcase name="factor-var-div-equality" class="tar:factor-var-div-equality">
            <failure message="assertTrue failed." type="failure-error-code-1"/>
            <output>false</output>
        </testcase>
        <testcase name="factor-var-div-integer" class="tar:factor-var-div-integer">
            <failure message="assertEquals failed." type="failure-error-code-1">0.205666666666666667</failure>
            <output>0.205555555555555556</output>
        </testcase>
        <testcase name="literals-div-decimal" class="tar:literals-div-decimal"/>
        <testcase name="literals-div-equality" class="tar:literals-div-equality"/>
        <testcase name="literals-div-integer" class="tar:literals-div-integer"/>
        <testcase name="number-var-div-decimal" class="tar:number-var-div-decimal"/>
        <testcase name="number-var-div-equality" class="tar:number-var-div-equality">
            <failure message="assertTrue failed." type="failure-error-code-1"/>
            <output>false</output>
        </testcase>
        <testcase name="number-var-div-integer" class="tar:number-var-div-integer">
            <failure message="assertEquals failed." type="failure-error-code-1">0.205666666666666667</failure>
            <output>0.205555555555555556</output>
        </testcase>
        <testcase name="variables-div-decimal" class="tar:variables-div-decimal"/>
        <testcase name="variables-div-equality" class="tar:variables-div-equality"/>
        <testcase name="variables-div-integer" class="tar:variables-div-integer"/>
    </testsuite>
</testsuites>

tested on 6.1.0 running in docker

line-o avatar Sep 05 '23 11:09 line-o

Just to note that this is a particularly nasty bug because it may well be undetected yet yield erroneous results. An analysis of the % error for $d ranging from 1 to 90 shows that the difference is quite small and was only detected by me because I was plotting graphical patterns which were just slightly out of kilter. image

KitWallace avatar Sep 22 '23 09:09 KitWallace