• OBNC 0.16.1 Issue with SYSTEM.VAL

    From Guy T.@21:1/5 to All on Sat Feb 29 19:54:57 2020
    Hello all,

    I'm using OBNC on MacOS configured with INTEGER and REAL of length 4.

    Here is a program that is puzzling me. I would expect both REAL values to be the same after calling SYSTEM.VAL twice, but it's not the case:

    MODULE toto;
    IMPORT Out, SYSTEM;
    VAR
    i: REAL;
    j: INTEGER;
    k: REAL;
    BEGIN
    i := 123.456;
    j := SYSTEM.VAL(INTEGER, i);
    k := SYSTEM.VAL(REAL, j);

    IF i = k THEN
    Out.String("OK")
    ELSE
    Out.String("NOT OK!"); Out.Ln;
    Out.Real(i,0); Out.String(" vs "); Out.Real(k,0)
    END;

    Out.Ln; Out.String("INTEGER Size: "); Out.Int(SYSTEM.SIZE(INTEGER), 0);
    Out.Ln; Out.String("REAL Size: "); Out.Int(SYSTEM.SIZE(REAL), 0);
    Out.Ln;
    END toto.

    The result I get is the following:

    $ obnc toto.Mod
    $ ./toto
    NOT OK!
    1.234560E+02 vs 1.230000E+02
    INTEGER Size: 4
    REAL Size: 4


    Any help would be appreciated.

    Cheers!

    Guy T.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From cfbsoftware@gmail.com@21:1/5 to Guy T. on Sun Mar 1 01:01:10 2020
    On Sunday, March 1, 2020 at 2:24:57 PM UTC+10:30, Guy T. wrote:

    The result I get is the following:

    $ obnc toto.Mod
    $ ./toto
    NOT OK!
    1.234560E+02 vs 1.230000E+02
    INTEGER Size: 4
    REAL Size: 4


    I just ran your test program using the Astrobe for Cortex-M4 and Astrobe for RISC5 Oberon compilers and it was successful. It may be due to a deficiency / limitation / bug in OBNC.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From miasap@21:1/5 to Guy T. on Sun Mar 1 11:00:44 2020
    On 2020-03-01 04:54, Guy T. wrote:
    I would expect both REAL values to be the same after calling
    SYSTEM.VAL twice, but it's not the case:
    I can confirm that OBNC is incorrect in this case. The function call SYSTEM.VAL(T, n) is translated into the C code `(T) n' which performs
    value conversion instead of keeping the bit pattern of the value. I will
    soon release an update. Thanks for reporting the issue.

    Regards,
    Karl

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Guy T.@21:1/5 to miasap on Sun Mar 1 03:20:24 2020
    On Sunday, March 1, 2020 at 5:00:45 AM UTC-5, miasap wrote:
    On 2020-03-01 04:54, Guy T. wrote:
    I would expect both REAL values to be the same after calling
    SYSTEM.VAL twice, but it's not the case:
    I can confirm that OBNC is incorrect in this case. The function call SYSTEM.VAL(T, n) is translated into the C code `(T) n' which performs
    value conversion instead of keeping the bit pattern of the value. I will
    soon release an update. Thanks for reporting the issue.

    Regards,
    Karl

    Thank you very much for your help! In the interim, here is the hand made module in C I'm using to resolve the issue:

    #include ".obnc/Reals.h"
    #include <obnc/OBNC.h>

    #define OBERON_SOURCE_FILENAME "Reals.Mod"

    union {
    OBNC_REAL f;
    OBNC_INTEGER i;
    } w;
    void Reals__RealToInt_(OBNC_REAL r_, OBNC_INTEGER *i_) {
    w.f = r_; *i_ = w.i;
    }
    void Reals__IntToReal_(OBNC_INTEGER i_, OBNC_REAL *r_) {
    w.i = i_; *r_ = w.f;
    }
    void Reals__Init(void) {
    }

    Cheers!

    Guy T.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Guy T.@21:1/5 to novemb...@gmail.com on Thu Mar 5 03:56:59 2020
    On Thursday, March 5, 2020 at 6:25:17 AM UTC-5, novemb...@gmail.com wrote:
    On Sunday, 1 March 2020 03:54:57 UTC, Guy T. wrote:
    Hello all,

    I'm using OBNC on MacOS configured with INTEGER and REAL of length 4.

    Here is a program that is puzzling me. I would expect both REAL values to be the same after calling SYSTEM.VAL twice, but it's not the case:

    MODULE toto;
    IMPORT Out, SYSTEM;
    VAR
    i: REAL;
    j: INTEGER;
    k: REAL;
    BEGIN
    i := 123.456;
    j := SYSTEM.VAL(INTEGER, i);
    k := SYSTEM.VAL(REAL, j);

    IF i = k THEN
    Out.String("OK")
    ELSE
    Out.String("NOT OK!"); Out.Ln;
    Out.Real(i,0); Out.String(" vs "); Out.Real(k,0)
    END;

    Out.Ln; Out.String("INTEGER Size: "); Out.Int(SYSTEM.SIZE(INTEGER), 0);
    Out.Ln; Out.String("REAL Size: "); Out.Int(SYSTEM.SIZE(REAL), 0);
    Out.Ln;
    END toto.

    The result I get is the following:

    $ obnc toto.Mod
    $ ./toto
    NOT OK!
    1.234560E+02 vs 1.230000E+02
    INTEGER Size: 4
    REAL Size: 4


    Any help would be appreciated.

    Cheers!

    Guy T.


    I dont have a Oberon compiler at the moment. Thus I am unable to
    test this for myself. But I am curious to know so please help:

    When I first read the post I thought it was obvious.

    It all comes down to 3 lines:

    i := 123.456;
    j := SYSTEM.VAL(INTEGER, i);
    k := SYSTEM.VAL(REAL, j);

    j=123
    k=1.23e02

    Then you are comparing i & k and naturally they are not equal
    i=1.23456e02 k=1.23e02 as your output shows.

    You are comparing real numbers and I cannot see anywhere where
    you have restricted the precision of the comparison.

    What am I missing ?

    SYSTEM.VAL is a mean to change the type associated to a variable without changing the memory content. In the example the variable i is seen as REAL -> INTEGER -> REAL, but the internal representation of the content, bits by bits, is not changed.

    In C, a way to get the same result would be to use an union structure like this: union { float real; int integer; } val; and use statements like this: val.real = i; j = val.integer; to exchange the meaning of the content between floating point and
    integer types.

    As there is no modification to the content, the equality of values when comparing two real variables is preserved, as we are comparing the same data, bits per bits.

    Hope this is clear enough.

    Cheers!

    Guy T.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From november.nihal@gmail.com@21:1/5 to Guy T. on Thu Mar 5 03:25:16 2020
    On Sunday, 1 March 2020 03:54:57 UTC, Guy T. wrote:
    Hello all,

    I'm using OBNC on MacOS configured with INTEGER and REAL of length 4.

    Here is a program that is puzzling me. I would expect both REAL values to be the same after calling SYSTEM.VAL twice, but it's not the case:

    MODULE toto;
    IMPORT Out, SYSTEM;
    VAR
    i: REAL;
    j: INTEGER;
    k: REAL;
    BEGIN
    i := 123.456;
    j := SYSTEM.VAL(INTEGER, i);
    k := SYSTEM.VAL(REAL, j);

    IF i = k THEN
    Out.String("OK")
    ELSE
    Out.String("NOT OK!"); Out.Ln;
    Out.Real(i,0); Out.String(" vs "); Out.Real(k,0)
    END;

    Out.Ln; Out.String("INTEGER Size: "); Out.Int(SYSTEM.SIZE(INTEGER), 0);
    Out.Ln; Out.String("REAL Size: "); Out.Int(SYSTEM.SIZE(REAL), 0);
    Out.Ln;
    END toto.

    The result I get is the following:

    $ obnc toto.Mod
    $ ./toto
    NOT OK!
    1.234560E+02 vs 1.230000E+02
    INTEGER Size: 4
    REAL Size: 4


    Any help would be appreciated.

    Cheers!

    Guy T.


    I dont have a Oberon compiler at the moment. Thus I am unable to
    test this for myself. But I am curious to know so please help:

    When I first read the post I thought it was obvious.

    It all comes down to 3 lines:

    i := 123.456;
    j := SYSTEM.VAL(INTEGER, i);
    k := SYSTEM.VAL(REAL, j);

    j=123
    k=1.23e02

    Then you are comparing i & k and naturally they are not equal
    i=1.23456e02 k=1.23e02 as your output shows.

    You are comparing real numbers and I cannot see anywhere where
    you have restricted the precision of the comparison.

    What am I missing ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From november.nihal@gmail.com@21:1/5 to Guy T. on Thu Mar 5 06:02:46 2020
    On Thursday, 5 March 2020 11:57:00 UTC, Guy T. wrote:
    On Thursday, March 5, 2020 at 6:25:17 AM UTC-5, novemb...@gmail.com wrote:
    On Sunday, 1 March 2020 03:54:57 UTC, Guy T. wrote:
    Hello all,

    I'm using OBNC on MacOS configured with INTEGER and REAL of length 4.

    Here is a program that is puzzling me. I would expect both REAL values to be the same after calling SYSTEM.VAL twice, but it's not the case:

    MODULE toto;
    IMPORT Out, SYSTEM;
    VAR
    i: REAL;
    j: INTEGER;
    k: REAL;
    BEGIN
    i := 123.456;
    j := SYSTEM.VAL(INTEGER, i);
    k := SYSTEM.VAL(REAL, j);

    IF i = k THEN
    Out.String("OK")
    ELSE
    Out.String("NOT OK!"); Out.Ln;
    Out.Real(i,0); Out.String(" vs "); Out.Real(k,0)
    END;

    Out.Ln; Out.String("INTEGER Size: "); Out.Int(SYSTEM.SIZE(INTEGER), 0);
    Out.Ln; Out.String("REAL Size: "); Out.Int(SYSTEM.SIZE(REAL), 0);
    Out.Ln;
    END toto.

    The result I get is the following:

    $ obnc toto.Mod
    $ ./toto
    NOT OK!
    1.234560E+02 vs 1.230000E+02
    INTEGER Size: 4
    REAL Size: 4


    Any help would be appreciated.

    Cheers!

    Guy T.


    I dont have a Oberon compiler at the moment. Thus I am unable to
    test this for myself. But I am curious to know so please help:

    When I first read the post I thought it was obvious.

    It all comes down to 3 lines:

    i := 123.456;
    j := SYSTEM.VAL(INTEGER, i);
    k := SYSTEM.VAL(REAL, j);

    j=123
    k=1.23e02

    Then you are comparing i & k and naturally they are not equal
    i=1.23456e02 k=1.23e02 as your output shows.

    You are comparing real numbers and I cannot see anywhere where
    you have restricted the precision of the comparison.

    What am I missing ?

    SYSTEM.VAL is a mean to change the type associated to a variable without changing the memory content. In the example the variable i is seen as REAL -> INTEGER -> REAL, but the internal representation of the content, bits by bits, is not changed.

    In C, a way to get the same result would be to use an union structure like this: union { float real; int integer; } val; and use statements like this: val.real = i; j = val.integer; to exchange the meaning of the content between floating point and
    integer types.

    As there is no modification to the content, the equality of values when comparing two real variables is preserved, as we are comparing the same data, bits per bits.

    Hope this is clear enough.

    Cheers!

    Guy T.

    Thank you for the explanation. I can see now where I was going wrong.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)