Oberon Community Platform Forum
December 15, 2017, 01:11:09 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: longreal representation in MathL.exp or MathL.ln is wrong ?  (Read 1838 times)
fnecati
Jr. Member
**
Posts: 60


« on: September 28, 2012, 03:14:14 PM »

Code:
MODULE MyFloatTest;

(*
In this module, x10^n  (for various x and n ) LONGREAL calculations using MathL.exp
or MathL.ln produce wrong results and wrong floating point representation in WinAos.
However, REAL calculations are correct.

LinuxAos produces correct results.

What could be wrong here?
*)

IMPORT
SYSTEM, Math, MathL, KernelLog, Streams, Reals;

VAR
write: Streams.Writer;

PROCEDURE Exp10x(x: LONGREAL; n: LONGINT): LONGREAL;
BEGIN
RETURN x*MathL.exp(n*MathL.ln(10.0));
END Exp10x;

PROCEDURE Exp10z(x: REAL; n: LONGINT): REAL;
BEGIN
RETURN x*Math.exp(n*Math.ln(10.0));
END Exp10z;

PROCEDURE Do*;
VAR
x, y: LONGREAL;
xz, z: REAL;
  yh, yl: LONGINT;
  ys: SET; (* 32 bit *)
  n: LONGINT;
BEGIN
n := 1;
x := 1.0;
write.String("( ");write.FloatFix(x, 0, 2,0); write.String(" x 10^"); write.Int(n,0);
write.String(") ");write.Ln; write.Update;

(*  y = x . 10^n *)

(* calculate using MathL*)
y := Exp10x(x, n);
write.FloatFix(y, 0, 2, 0);  write.Update;
KernelLog.String(" : bits--> ");
Reals.IntL(y, yh, yl);  (* get bits of 64bit real *)
ys := SYSTEM.VAL(SET, yh); KernelLog.Bits(ys,0,32); (* write high 32 bits *)
ys := SYSTEM.VAL(SET, yl); KernelLog.Bits(ys,0,32); (* write low  32 bits *)
KernelLog.Ln;

(* calculate using Math *)
xz := SHORT(x);
z := Exp10z(xz, n);

write.FloatFix(z, 0, 2, 0); write.Update; 
KernelLog.String(" : bits--> ");
yl := SYSTEM.VAL(LONGINT, z); (* get bits of 32 bit real *)
ys := SYSTEM.VAL(SET, yl);
KernelLog.Bits(ys,0,32);
KernelLog.Ln;

END Do;

BEGIN
Streams.OpenWriter(write, KernelLog.Send);
END MyFloatTest.

MyFloatTest.Do ~


SystemTools.Free MyFloatTest ~

(*

latest WinAos :
(   10.00 x 10^1)
  100.05 : bits --> 0100000001011000111111111111111111111111111111111111111111111111  <*********
  100.00 : bits--> 01000010110010000000000000000000

(   20.00 x 10^1)
  200.00 : bits --> 0100000001101000111111111111111111111111111111111111111111111111  <*********
  200.00 : bits--> 01000011010010000000000000000000

(   1.00 x 10^1)
  10.00 : bits --> 0100000000100011111111111111111111111111111111111111111111111111  <*********
  10.00 : bits--> 01000001001000000000000000000000

(   1.00 x 10^2)
  100.00 : bits --> 0100000001011000111111111111111111111111111111111111111111111110  <*********
  100.00 : bits--> 01000010110010000000000000000001 
 
 
  linuxAos:
(   10.00 x 10^1)
  100.00 : bits--> 0100000001011001000000000000000000000000000000000000000000000010
  100.00 : bits--> 01000010110010000000000000000000
 
(   1.00 x 10^1)
  10.00 : bits--> 0100000000100100000000000000000000000000000000000000000000000001
  10.00 : bits--> 01000001001000000000000000000000
 
 
  *)
 
 
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!