[Oberon] System-7 - Math popular consts

Jörg joerg.straube at iaeth.ch
Thu Feb 28 11:53:55 CET 2019


Tomas

Yes, indeed. I did that in my Math.Mod as well.
REAL accuracy is very tricky!! Even the compiler has issues with it...
If you run this program, you see what I mean:

. . . . . 
MODULE RealTest;
IMPORT S := SYSTEM, Out;
VAR r: REAL;
BEGIN
	r := 3.141593;          Out.Hex(S.VAL(INTEGER, r)); Out.Ln; (* 1 *)
	r := 3.1415927;        Out.Hex(S.VAL(INTEGER, r)); Out.Ln; (* 2 *)
	r := 3.14159265;      Out.Hex(S.VAL(INTEGER, r)); Out.Ln; (* 3 *)
	r := 3.141592654;    Out.Hex(S.VAL(INTEGER, r)); Out.Ln; (* 4 *)
	r := 3.1415926536;  Out.Hex(S.VAL(INTEGER, r)); Out.Ln; (* 5 *)
	r := 3.14159265359;Out.Hex(S.VAL(INTEGER, r)); Out.Ln; (* 6 *)
END RealTest.Do
. . . . . .

One would expect: the longer the mantissa gets, the more accurate the result will be. And after the max accuracy of REALs is reached, it will not change anymore.
Unfortunately that’s not always true (
1 is not accurate enough, as the mantissa is too short. 
2 represents the correct REAL value of pi. 3 as well.
4 silly enough, the compiler calculates it wrongly!!
5 and 6 are correct again.

So if you really, really want to be sure that the value of pi is correct, calculate it offline with very high precision and dump the correct hex value into your code.
The compiler has a hidden, undocumented feature to do so. You can write pi = 40490FDBR; (the last character "R" means: the hex string is interpreted as REAL). But this feature is very rarely used, mainly in advanced numerical modules only....

br
Jörg

Am 28.02.19, 11:03 schrieb "Oberon im Auftrag von Tomas Kral" <oberon-bounces at lists.inf.ethz.ch im Auftrag von thomas.kral at email.cz>:

    Hi,
    
    Just discovered `Math.mod', such a tiny module! I wish to add two
    consts to it.
    
    pi = 3.14159265
    e = 2.71828182
    
    Float format on risc is 
    x = 1.m * 2^(e-127)   
    bit 0: sign, bits 1- 8: e, bits  9-31: m
    
    I believe binary mantisa roughly corresponds to 8 decimal digits.
    
    Now, how to code the consts to make most of the given mantisa?
    
    -- 
    Tomas Kral <thomas.kral at email.cz>
    --
    Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
    https://lists.inf.ethz.ch/mailman/listinfo/oberon
    




More information about the Oberon mailing list