ShortFloat.st
author Claus Gittinger <cg@exept.de>
Thu, 21 May 1998 13:38:05 +0200
changeset 3470 fa7cda7fb45e
parent 3429 50c2ce8d5a5f
child 3471 91bad1642e2f
permissions -rw-r--r--
added exponent & fractionalPart

"
 COPYRIGHT (c) 1996 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"



LimitedPrecisionReal variableByteSubclass:#ShortFloat
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Magnitude-Numbers'
!

!ShortFloat primitiveDefinitions!
%{

/*
 * includes, defines, structure definitions
 * and typedefs come here.
 */

#if defined (_AIX)
# include <float.h>
#endif
#if defined(IRIX)
# include <nan.h>
#endif
#if defined(LINUX)
# include <nan.h>
#endif
#if defined(solaris) || defined(sunos)
# include <nan.h>
#endif

#ifdef WIN32
/*
 * no finite(x) ?
 * no isnan(x) ?
 */
# ifndef finite 
#  define finite(x)     1
# endif
# ifndef isnan
#  define isnan(x)      0
# endif
#endif

#ifdef realIX
/*
 * no finite(x)
 */
# ifndef finite
#  define finite(x)     1
# endif
#endif

%}
! !

!ShortFloat class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"


!

documentation
"
    ShortFloats represent rational numbers with limited precision. In ST/X, Float uses
    the underlying C-compilers double implementation, while ShortFloats are
    mapped onto C-floats.
    Therefore instances of Float are usually represented by the 8-byte IEE 
    double precision float format, while ShortFloats use 4byte IEE format.
    (but there is no guaranty).

    Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are
    ST-80's Floats respectively.
    This may change in one of the next versions (at least on machines, which 
    provide different float and double types in their C-compiler.

    WARNING:
    The layout of shortFloat instances is known by the runtime system and the compiler;
    you may not add instance variables here. 
    Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, 
    and does its float-checks by an identity compare with the ShortFloat-class. 
    (i.e. your subclasses instances may not be recognized as float-like objects, 
     thus mixed mode arithmetic will always coerce them, effectively slowing things down).

    This may be changed, to use a flag bit in the class.

    [author:]
	Claus Gittinger

    [see also:]
	Number
	Float Fraction FixedPoint Integer
"

! !

!ShortFloat class methodsFor:'instance creation'!

basicNew
    "return a new shortFloat - here we return 0.0
     - shortFloats are usually NOT created this way ...
     Its implemented here to allow things like binary store & load
     of shortFloats. (but even this support will go away eventually, its not
     a good idea to store the bits of a float - the reader might have a
     totally different representation - so floats will eventually be 
     binary stored in a device independent format."

%{  /* NOCONTEXT */
    OBJ newFloat;

    __qMKSFLOAT(newFloat, 0.0);
    RETURN (newFloat);
%}
!

readFrom:aStringOrStream onError:exceptionBlock
    "read a shortFloat from a string"

    |num|

    num := super readFrom:aStringOrStream onError:nil.
    num isNil ifTrue:[  
	^ exceptionBlock value
    ].
    ^ num asShortFloat

    "
     ShortFloat readFrom:'0.1'
     ShortFloat readFrom:'0'
    "

    "Modified: / 7.1.1998 / 16:17:59 / cg"
! !

!ShortFloat class methodsFor:'constants'!

pi
    "return the constant pi as ShortFloat"

    ^ 3.14159 asShortFloat

    "Modified: 23.4.1996 / 09:26:31 / cg"
!

unity
    "return the neutral element for multiplication (1.0) as ShortFloat"

    ^ 1.0 asShortFloat

    "Modified: 23.4.1996 / 09:26:51 / cg"
!

zero
    "return the neutral element for addition (0.0) as ShortFloat"

    ^ 0.0 asShortFloat

    "Modified: 23.4.1996 / 09:26:45 / cg"
! !

!ShortFloat class methodsFor:'queries'!

isBuiltInClass
    "return true if this class is known by the run-time-system.
     Here, true is returned for myself, false for subclasses."

    ^ self == ShortFloat

    "Modified: 23.4.1996 / 16:00:23 / cg"
!

isIEEEFormat
    "return true, if this machine represents floats in IEEE format.
     Currently, no support is provided for non-ieee machines
     to convert their floats into this (which is only relevant,
     if such a machine wants to send floats as binary to some other
     machine).
     Machines with non-IEEE format are VAXed and IBM370-type systems
     (among others). Today, most systems use IEEE format floats."

    ^ true "/ this may be a lie
! !

!ShortFloat methodsFor:'arithmetic'!

* aNumber
    "return the product of the receiver and the argument, aNumber"

%{  /* NOCONTEXT */

    OBJ newFloat;
    float result;
    double dResult;

    if (__isSmallInteger(aNumber)) {
	result = __shortFloatVal(self) * (float)(__intVal(aNumber));
retResult:
	__qMKSFLOAT(newFloat, result);
	RETURN ( newFloat );
    }
    if (__isShortFloat(aNumber)) {
	result = __shortFloatVal(self) * __shortFloatVal(aNumber);
	goto retResult;
    }
    if (__isFloatLike(aNumber)) {
	dResult = (double) __shortFloatVal(self)* __floatVal(aNumber);
	__qMKFLOAT(newFloat, dResult);
	RETURN ( newFloat );

    }
%}.
    ^ aNumber productFromShortFloat:self
!

+ aNumber
    "return the sum of the receiver and the argument, aNumber"

%{  /* NOCONTEXT */

    OBJ newFloat;
    float result;
    double dResult;

    if (__isSmallInteger(aNumber)) {
	result = __shortFloatVal(self) + (float)(__intVal(aNumber));
retResult:
	__qMKSFLOAT(newFloat, result);
	RETURN ( newFloat );
    }
    if (__isShortFloat(aNumber)) {
	result = __shortFloatVal(self) + __shortFloatVal(aNumber);
	goto retResult;
    }
    if (__isFloatLike(aNumber)) {
	dResult = (double) __shortFloatVal(self) + __floatVal(aNumber);
	__qMKFLOAT(newFloat, dResult);
	RETURN ( newFloat );

    }
%}.
    ^ aNumber sumFromShortFloat:self
!

- aNumber
    "return the difference of the receiver and the argument, aNumber"

%{  /* NOCONTEXT */

    OBJ newFloat;
    float result;
    double dResult;

    if (__isSmallInteger(aNumber)) {
	result = __shortFloatVal(self) - (float)(__intVal(aNumber));
retResult:
	__qMKSFLOAT(newFloat, result);
	RETURN ( newFloat );
    }
    if (__isShortFloat(aNumber)) {
	result = __shortFloatVal(self) - __shortFloatVal(aNumber);
	goto retResult;
    }
    if (__isFloatLike(aNumber)) {
	dResult = (double) __shortFloatVal(self) - __floatVal(aNumber);
	__qMKFLOAT(newFloat, dResult);
	RETURN ( newFloat );

    }
%}.
    ^ aNumber differenceFromShortFloat:self
!

/ aNumber
    "return the quotient of the receiver and the argument, aNumber"

%{  /* NOCONTEXT */

    OBJ newFloat;
    float result, val;
    double dResult, dVal;

    if (__isSmallInteger(aNumber)) {
	if (aNumber != __MKSMALLINT(0)) {
	    result = __shortFloatVal(self) / (float)(__intVal(aNumber));
retResult:
	    __qMKSFLOAT(newFloat, result);
	    RETURN ( newFloat );
	}
    }
    if (__isShortFloat(aNumber)) {
	val = __shortFloatVal(aNumber);
	if (val != 0.0) {
	    result = __shortFloatVal(self) / val;
	    goto retResult;
	}
    }
    if (__isFloatLike(aNumber)) {
	dVal = __floatVal(aNumber);
	if (dVal != 0.0) {
	    dResult = (double) __shortFloatVal(self) / dVal;
	    __qMKFLOAT(newFloat, dResult);
	}
	RETURN ( newFloat );

    }
%}.
    ((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[
	"
	 No, you shalt not divide by zero
	"
	^ DivisionByZeroSignal raise.
    ].
    ^ aNumber quotientFromFloat:self

!

negated
    "return myself negated"

%{  /* NOCONTEXT */
    OBJ newFloat;
    float rslt = - __shortFloatVal(self);

    __qMKSFLOAT(newFloat, rslt);
    RETURN ( newFloat );
%}

!

uncheckedDivide:aNumber
    "return the quotient of the receiver and the argument, aNumber
     Do not check for divide by zero (return NaN or infinity)"

%{  /* NOCONTEXT */

    OBJ newFloat;
    float result, val;
    double dResult, dVal;

    if (__isSmallInteger(aNumber)) {
        result = __shortFloatVal(self) / (float)(__intVal(aNumber));
retResult:
        __qMKSFLOAT(newFloat, result);
        RETURN ( newFloat );
    }
    if (__isShortFloat(aNumber)) {
        val = __shortFloatVal(aNumber);
        result = __shortFloatVal(self) / val;
        goto retResult;
    }
    if (__isFloatLike(aNumber)) {
        dVal = __floatVal(aNumber);
        dResult = (double) __shortFloatVal(self) / dVal;
        __qMKFLOAT(newFloat, dResult);
        RETURN ( newFloat );
    }
%}.
    ^ aNumber quotientFromFloat:self

    "
      0.0 asShortFloat uncheckedDivide:0
      1.0 asShortFloat uncheckedDivide:0.0
    "

! !

!ShortFloat methodsFor:'coercion and converting'!

asDouble
    "ST80 compatibility - return a Float with same value as the receiver"

    ^ self asFloat
!

asFloat
    "return a Float with same value as the receiver"

%{  /* NOCONTEXT */

    OBJ newFloat;
    double dVal = (double)__shortFloatVal(self);

    __qMKFLOAT(newFloat, dVal);
    RETURN ( newFloat );
%}

    "
     1.0 asShortFloat asFloat 
    "
!

asInteger
    "return an integer with same value - might truncate"

%{  /* NOCONTEXT */
    float fVal;

    fVal = __shortFloatVal(self);
    if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) {
	RETURN ( __MKSMALLINT( (INT)fVal) );
    }
%}.
    ^ super asInteger

    "
     12345.0 asShortFloat asInteger
     1e15 asShortFloat asInteger
    "
!

asShortFloat
    "return a ShortFloat with same value as the receiver - thats me"

    ^ self
!

exponent
    "extract a normalized floats exponent.
     This assumes that the mantissa is normalized to
     0.5 .. 1.0 and the floats value is mantissa * 2^exp"

%{  /* NOCONTEXT */

    double frexp();
    double frac;
    INT pwr;

/*    errno = 0;
*/
    frac = frexp( (double)(__shortFloatVal(self)), &pwr);
/*    if (errno == 0) {
*/
{
        if (pwr == 0) {
            RETURN (__MKSMALLINT(0));
        }
        RETURN (__MKSMALLINT(pwr-1));
    }
%}.
    ^ self primitiveFailed

    "
     1.0 asShortFloat exponent    
     1.0 asShortFloat fraction    
     0.5 asShortFloat exponent   
     0.25 asShortFloat exponent   
     0.00000011111 asShortFloat exponent   
    "

!

fractionalPart
    "extract a normalized floats mantissa.
     This assumes that the mantissa is normalized to
     0.5 .. 1.0 and the floats value is man * 2^exp"

%{  /* NOCONTEXT */

    double modf();
    double frac, trunc;

/*
    errno = 0;
*/
    frac = modf((double)(__shortFloatVal(self)), &trunc);
    if (1 /* errno == 0 */) {
        RETURN (__MKFLOAT(frac));
    }
%}.
    ^ self primitiveFailed

    "
     1.0 asShortFloat fractionalPart    
     0.5 asShortFloat fractionalPart    
     0.25 asShortFloat fractionalPart   
     3.14159 asShortFloat fractionalPart   
     12345673.14159 asShortFloat fractionalPart   
     123456731231231231.14159 asShortFloat fractionalPart   
    "

!

generality
    "return the generality value - see ArithmeticValue>>retry:coercing:"

    ^ 70


! !

!ShortFloat methodsFor:'comparing'!

< aNumber
    "return true, if the argument is greater"

%{  /* NOCONTEXT */

    if (__isSmallInteger(aNumber)) {
	RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false );
    }
    if (__isFloatLike(aNumber)) {
	RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false );
    }
    if (__isShortFloat(aNumber)) {
	RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false );
    }
%}.
    ^ aNumber lessFromShortFloat:self

    "
     1.0 asShortFloat > (1/3)
    "
!

<= aNumber
    "return true, if the argument is greater or equal"

%{  /* NOCONTEXT */

    if (__isSmallInteger(aNumber)) {
	RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false );
    }
    if (__isFloatLike(aNumber)) {
	RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false );
    }
    if (__isShortFloat(aNumber)) {
	RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false );
    }
%}.
    ^ self retry:#<= coercing:aNumber

!

= aNumber
    "return true, if the arguments value are equal by value"

%{  /* NOCONTEXT */

    if (__isSmallInteger(aNumber)) {
	RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false );
    }
    if (__isFloatLike(aNumber)) {
	RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false );
    }
    if (__isShortFloat(aNumber)) {
	RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false );
    }
%}.
    ^ self retry:#= coercing:aNumber

!

> aNumber
    "return true, if the argument is less"

%{  /* NOCONTEXT */

    if (__isSmallInteger(aNumber)) {
	RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false );
    }
    if (__isFloatLike(aNumber)) {
	RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false );
    }
    if (__isShortFloat(aNumber)) {
	RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false );
    }
%}.
    ^ self retry:#> coercing:aNumber
!

>= aNumber
    "return true, if the argument is less or equal"

%{  /* NOCONTEXT */

    if (__isSmallInteger(aNumber)) {
	RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false );
    }
    if (__isFloatLike(aNumber)) {
	RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false );
    }
    if (__isShortFloat(aNumber)) {
	RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false );
    }
%}.
    ^ self retry:#>= coercing:aNumber
!

hash
    "return a number for hashing; redefined, since floats compare
     by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same
     as 3 hash."

    |i|

    (self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[
	i := self asInteger.
	self = i ifTrue:[
	    ^ i hash
	].
    ].

    "
     mhmh take some of my value-bits to hash on
    "
    ^ (((self basicAt:4) bitAnd:16r3F) bitShift:24) +
      ((self basicAt:3) bitShift:16) +
      ((self basicAt:2) bitShift:8) +
      (self basicAt:1)

    "
     1.2345 hash      
     1.2345 asShortFloat hash 
     1.0 hash             
     1.0 asShortFloat hash  
    "
!

~= aNumber
    "return true, if the arguments value are not equal"

%{  /* NOCONTEXT */

    if (__isSmallInteger(aNumber)) {
	RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false );
    }
    if (__isFloatLike(aNumber)) {
	RETURN ( (double)(__shortFloatVal(self) !=  __floatVal(aNumber)) ? true : false );
    }
    if (__isShortFloat(aNumber)) {
	RETURN ( (__shortFloatVal(self) !=  __shortFloatVal(aNumber)) ? true : false );
    }
%}.
    ^ self retry:#~= coercing:aNumber

! !

!ShortFloat methodsFor:'printing & storing'!

printString
    "return a printed representation of the receiver
     LimitedPrecisonReal and its subclasses use #printString instead of
     #printOn: as basic print mechanism."

%{  /* NOCONTEXT */

    char buffer[64];
    REGISTER char *cp;
    OBJ s;

    /*
     * actually only needed on sparc: since thisContext is
     * in a global register, which gets destroyed by printf,
     * manually save it here - very stupid ...
     */
    __BEGIN_PROTECT_REGISTERS__

#ifdef SYSV
    sprintf(buffer, "%.6lg", (double)__shortFloatVal(self));
#else
    sprintf(buffer, "%.6G", (double)__shortFloatVal(self));
#endif

    __END_PROTECT_REGISTERS__

    /* 
     * kludge to make integral float f prints as "f.0" (not as "f" as printf does)
     * (i.e. look if string contains '.' or 'e' and append '.0' if not)
     */
    for (cp = buffer; *cp; cp++) {
        if ((*cp == '.') || (*cp == 'e')) break;
    }
    if (! *cp) {
        *cp++ = '.';
        *cp++ = '0';
        *cp = '\0';
    }

    s = __MKSTRING(buffer COMMA_SND);
    if (s != nil) {
        RETURN (s);
    }
%}.
    "
     memory allocation (for the new string) failed.
     When we arrive here, there was no memory, even after a garbage collect.
     This means, that the VM wanted to get some more memory from the
     OS, which was not kind enough to give it.
     Bad luck - you should increase the swap space on your machine.
    "
    ^ ObjectMemory allocationFailureSignal raise.
! !

!ShortFloat methodsFor:'testing'!

isFinite
    "return true, if the receiver is a finite float 
     i.e. not NaN and not infinite."

%{  /* NOCONTEXT */

    double dV = (double) __shortFloatVal(self);

    /*
     * notice: on machines which do not provide
     * a finite() macro or function (WIN32), 
     * this may always return true here ...
     */
    if (finite(dV)) { RETURN (true); }
%}.
    ^false

    "
        1.0 asShortFloat isFinite
        (0.0 asShortFloat uncheckedDivide: 0.0) isFinite
        (1.0 asShortFloat uncheckedDivide: 0.0) isFinite
    "
!

isNaN
    "return true, if the receiver is an invalid float (NaN - not a number).
     These are not created by ST/X float operations (they raise an exception);
     however, inline C-code could produce them ..."

%{  /* NOCONTEXT */

    double dV = (double)(__shortFloatVal(self));

    /*
     * notice: on machines which do not provide
     * a finite() macro or function (WIN32), 
     * this may always return false here ...
     */
    if (isnan(dV)) { RETURN (true); }

#if 0 /* Currently all our systems support isnan()
       * If not, you have to fix librun/jinterpret.c also.
       */

    /*
     * sigh - every vendor is playing its own game here ...
     * Q: what are standards worth, anyway ?
     */
#ifdef IS_NAN
    if (IS_NAN(dV)) { RETURN (true); }
    RETURN (false);
#endif

#ifdef IS_QNAN
    if (IS_QNAN(dV)) { RETURN (true); }
    RETURN (false);
#endif

#ifdef FLT_SNAN
    if (dV == FLT_SNAN) { RETURN (true); }
    RETURN (false);
#endif

#ifdef FLT_QNAN
    if (dV == FLT_QNAN) { RETURN (true); }
    RETURN (false);
#endif

#ifdef _SNANF
    if (dV == _SNAN) { RETURN (true); }
    RETURN (false);
#endif

#ifdef _QNANF
    if (dV == _QNAN) { RETURN (true); }
    RETURN (false);
#endif

#ifdef IsPosNAN
    if IsPosNAN(dV) { RETURN (true); }
    RETURN (false);
#endif

#ifdef IsNegNAN
    if IsNegNAN(dV) { RETURN (true); }
    RETURN (false);
#endif

#ifdef NAN
    if (dV == NAN) { RETURN (true); }
    RETURN (false);
#endif

#ifdef NaN
    if (NaN(dV)) { RETURN (true); }
    RETURN (false);
#endif

#endif /* 0 */
%}.
    ^ false

    "
        1.0 asShortFloat isNaN
        (0.0 asShortFloat uncheckedDivide: 0.0) isNaN
    "
!

negative
    "return true if the receiver is less than zero"

%{  /* NOCONTEXT */

    RETURN ( (__shortFloatVal(self) < 0.0) ? true : false );
%}
!

positive
    "return true if the receiver is greater or equal to zero"

%{  /* NOCONTEXT */

    RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false );
%}
!

strictlyPositive
    "return true if the receiver is greater than zero"

%{  /* NOCONTEXT */

    RETURN ( (__shortFloatVal(self) > 0.0) ? true : false );
%}
! !

!ShortFloat class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.33 1998-05-21 11:37:46 cg Exp $'
! !