Re: [ooc-compiler] mantissa
Brought to you by:
mva
|
From: Stewart G. <sgr...@us...> - 2007-06-06 01:31:40
|
Hi Norayr, I hope that you are having fun with OOC. The functions that you need are not in the OOC math modules, but they are in "math.h". Fortunately, its easy to make an INTERFACE module to access them from your Oberon code. The function you need is "frexp", which gives you both the exponent and the mantissa: http://www.opengroup.org/onlinepubs/007908799/xsh/frexp.html The interface module looks like this: MODULE ExtraMath [ INTERFACE "C" ; LINK LIB "m" END ]; PROCEDURE ["(double)frexp"] frexp* (x : LONGREAL; VAR exp : LONGINT) : LONGREAL; PROCEDURE ["(float)frexp"] frexpf* (x : REAL; VAR exp : LONGINT) : REAL; END ExtraMath. Basically, you just pass all of the parameters through to the C function. That way, you don't need to write any wrapper code. The second function works for REAL values by casting the argument (from float to double) and the result (from double to float) during the call. This is the way that RealMath works too - it is a good module to study in cases like this. Now you can do this: PROCEDURE TestReal(x : REAL); VAR mantissa : REAL; exponent : LONGINT; BEGIN mantissa := ExtraMath.frexpf(x, exponent); Out.String("Input:"); Out.Real(x, 0, 0); Out.Ln; Out.String("Mantissa:"); Out.Real(mantissa, 0, 0); Out.Ln; Out.String("Exponent:"); Out.LongInt(exponent, 0); Out.Ln; END TestReal; Note that a Component Pascal REAL corresponds to a LONGREAL in OOC ("double" in C), so you will have to decide if you want to maintain source-level or binary compatability with the ported code. For source compatability, you would just maintain the original REAL definitions and accept a slight loss in precision. For binary compatability you would replace all of the original REALs with LONGREALs (might be messy, depending on the original code). Complete examples are attached. Just extract the files to your "src" area and do: oo2c -Mv TestExtraMath I hope this helps. Cheers, Stewart texts writer wrote: > Hello. > First of all, I want to thanks you for great compiler, oo2c > I am porting one application from blackbox cp to oo2c, and created > CompatMath.Mod to solve compatibility problems. > In the original application I am trying to make minimal changes like > IMPORT Math := CompatMath; > And this is the listing of emulation module: > > MODULE CompatMath; > > IMPORT LowReal, RealMath; > (* > PROCEDURE Mantissa* (x : REAL) : REAL; > BEGIN > RETURN LowReal.fracpart(x); > END Mantissa; > *) > PROCEDURE Exponent* (x : REAL) : INTEGER; > BEGIN > RETURN LowReal.exponent(x); > END Exponent; > > PROCEDURE IntPower* (x : REAL; n : INTEGER) : REAL; > VAR i : INTEGER; > BEGIN > IF (n = 0) THEN RETURN 1 END; > RETURN RealMath.power (x, n); > END IntPower; > > PROCEDURE Log*( x : REAL) : REAL; > BEGIN > RETURN RealMath.log(x, 10); > END Log; > > PROCEDURE Eps*() : REAL; > BEGIN > RETURN LowReal.ulp(1.0); > END Eps; > > END CompatMath. > > Question is, I am not sure howto make wrapper for Mantissa function. > If anyone has an idea then please, help me :) > Thanks > > Norayr Chilingaryan > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > ooc-compiler mailing list > ooc...@li... > https://lists.sourceforge.net/lists/listinfo/ooc-compiler > |