[Oberon] real math

Jörg joerg.straube at iaeth.ch
Thu Nov 13 17:14:46 CET 2014


Hi

Here a little test with AOS on Windows:

- - - - - - 
MODULE RealTest;
IMPORT Commands, SYSTEM;

PROCEDURE Do*(ctx: Commands.Context);
  VAR
    r: REAL;
    epsilon: REAL;
    digits: INTEGER;
  BEGIN
    ctx.out.Int( SYSTEM.SIZEOF(REAL), 0); ctx.out.Ln;
    
    r := 480 / 100 * 100;
    ctx.out.Int( ENTIER(r*1000000), 0); ctx.out.Ln;

    r := 480 / 100; r := r * 100;
    ctx.out.Int( ENTIER(r*1000000), 0); ctx.out.Ln;

    r := 100; r := 480 / r * 100;
    ctx.out.Int( ENTIER(r*1000000), 0); ctx.out.Ln;

    epsilon := 1; digits := 1;
    WHILE 1+epsilon # 1 DO epsilon := epsilon / 10; INC(digits) END;
    ctx.out.Int( digits, 0); ctx.out.Ln;
    
  END Do;

BEGIN
END RealTest.Do ~
- - - - - - 

The output is
4
480000000
480000030
480000000
17

The "4" tells us, that this compiler stores REAL as 4 bytes. So, we can
roughly assume 7 digits accuracy.

The first expression is not calculated at run time but at compile time. It
just shows that the compiler internally calculates very accurately :-)

With the second expression, I forced the intermediate result of the division
to REAL with a visible rounding error.
 
More interesting is the third expression. This expression has to be
evaluated at run time, but has no visible rounding error. So why is this?

The answer is shown with the last WHILE loop. The result "17" tells us, that
to evaluate expressions the compiler generates code with 17 digits of
accuracy (=LONGREAL) before being rounded to a certain type.

br
Jörg

-----Original Message-----
From: oberon at moravcik.info [mailto:oberon at moravcik.info] 
Sent: Donnerstag, 13. November 2014 12:48
To: ETH Oberon and related systems
Subject: Re: [Oberon] real math

Hi Jörg,

just tested your code below and it outputs again 480....32 as in my other
email I sent today.

Basically I need to test that 480/100*100 is 480 without some decimal digits
at the end
because I am comparing two computed real numbers if they are less, greater
or equal.
Comparing 480.0 with 480.0...32 does not make those two equal. 

I rounded the computed real numbers to 2 decimal digits which is enough
precision in my case.
This way the problem was solved but I do not think this is the general
solution.
Note that linux gcc compiler returns 480 
 printf("result: %3.15f", 480.0/100.0*100.0);


Zdenek



On Mon, Nov 10, 2014 at 06:50:02PM +0100, Jörg wrote:
> 
> Hi Zdenek
> 
> I just compiled your code with AOS on Windows.
> 
> - - - - 
> MODULE RealTest;
> IMPORT Commands;
> 
> PROCEDURE Do*(ctx: Commands.Context);
>   VAR
>     r: REAL;
>   BEGIN
>     r := 480 / 100 * 100;
>     ctx.out.Int( ENTIER(r*1000000), 0);
>   END Do;
> 
> BEGIN
> END RealTest.Do ~
> - - - - - 
> 
> On my PC the output is 480000000. So, I don't have the same effect as you
> have.
> 
> Generally, checking REALs on equality is a little dangerous. You should
> rather subtract two REALs and check for an epsilon>0.
> 
> I wonder what the use case of your code is? What do you want to find out
by
> dividing 480 by 100 and multiplying it by 100 again. Do you want to round
a
> number to two digits?
> 
> br
> Jörg
> 
> -----Original Message-----
> From: oberon at moravcik.info [mailto:oberon at moravcik.info] 
> Sent: Sonntag, 9. November 2014 20:36
> To: oberon at lists.inf.ethz.ch
> Subject: [Oberon] real math
> 
> Hi,
> 
> following code returns imprecise real number:
> 
>  r : REAL;
> 
>  r := 480 / 100 * 100;
>  out.Int( ENTIER(r * 1000000) , 0 );
> 
> outputs 48000019 instead of 48000000
> 
> How to check that 480/100*100 is 480 again ?
> 
> Thanks
> Zdenek
> 
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon



> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon


--
Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
https://lists.inf.ethz.ch/mailman/listinfo/oberon
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 6097 bytes
Desc: not available
Url : https://lists.inf.ethz.ch/pipermail/oberon/attachments/20141113/2d206339/attachment.bin 


More information about the Oberon mailing list