eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'ShortFloat':

Home

everywhere
www.exept.de
for:
[back]

Class: ShortFloat


Inheritance:

   Object
   |
   +--Magnitude
      |
      +--ArithmeticValue
         |
         +--Number
            |
            +--LimitedPrecisionReal
               |
               +--ShortFloat

Package:
stx:libbasic
Category:
Magnitude-Numbers
Version:
rev: 1.103 date: 2009/12/18 14:41:46
user: cg
file: ShortFloat.st directory: libbasic
module: stx stc-classLibrary: libbasic
Author:
Claus Gittinger

Description:


ShortFloats represent rational numbers with limited precision.
They use the C-compilers 'float' format, which is usually the IEE single float format.

In contrast to Floats (which use the C-compilers 64bit 'double' format),
ShortFloats give you 32 bit floats.

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 some of 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.

Mixed mode arithmetic:
    shortFloat op shortFloat   -> shortFloat
    shortFloat op fix         -> shortFloat
    shortFloat op fraction    -> shortFloat
    shortFloat op integer     -> shortFloat
    shortFloat op longFloat   -> longFloat
    shortFloat op float       -> float
    shortFloat op complex     -> complex

Representation:
        32bit single precision IEE floats
        23 bit mantissa,
        8 bit exponent,
        6 decimal digits (approx)

Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation


Related information:

    Number
    Float
    LongFloat
    Fraction
    FixedPoint
    Integer
    Complex
    FloatArray
    DoubleArray

Class protocol:

binary storage
o  readBinaryIEEESingleFrom: aStream
read a float value from the binary stream, aStream,
interpreting the next bytes as an IEEE formatted 4-byte float

o  readBinaryIEEESingleFrom: aStream into: aFloat
read a float value from the binary stream, aStream,
interpreting the next bytes as an IEEE formatted 4-byte float

o  storeBinaryIEEESingle: aFloat on: aStream
store aFloat as an IEEE formatted 4-byte float
onto the binary stream, aStream

constants
o  e
return the constant e as ShortFloat

o  pi
return the constant pi as ShortFloat

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

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

instance creation
o  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.

o  fastFromString: aString at: startIndex
return the next ShortFloat from the string starting at startIndex.
No spaces are skipped.

This is a specially tuned entry (using a low-level C-call), which
returns garbage if the argument string is not a valid float number.
It has been added to allow higher speed string decomposition into numbers,
especially for mass-data.

o  readFrom: aStringOrStream
read a shortFloat from a string

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

queries
o  exponentCharacter

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

o  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.

o  numBitsInExponent
answer the number of bits in the exponent
This is an IEEE float, where 8 bits are available:
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm

o  numBitsInMantissa
answer the number of bits in the mantissa.
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available:
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm

o  precision
answer the precision of a ShortFloat (in bits)
This is an IEEE float, where only the fraction from the normalized mantissa is stored
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits
(although only 23 are needed in the binary representation)

o  radix
answer the radix of a ShortFloats exponent
This is an IEEE float, which is represented as binary


Instance protocol:

arithmetic
o  * aNumber
return the product of the receiver and the argument.

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

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

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

o  abs
return the absolute value of the receiver
reimplemented here for speed

o  negated
return myself negated

o  uncheckedDivide: aNumber
return the quotient of the receiver and the argument, aNumber.
Do not check for divide by zero (return NaN or Infinity).
This operation is provided for emulators of other languages/semantics,
where no exception is raised for these results (i.e. Java).
Its only defined if the arguments type is the same as the receivers.

coercing & converting
o  asFloat
return a Float with same value as the receiver.
Redefined for performance (machine can do it faster)

o  asInteger
return an integer with same value - might truncate

o  asLongFloat

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

o  coerce: aNumber
convert the argument aNumber into an instance of the receivers class and return it.

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

comparing
o  < aNumber
return true, if the argument is greater

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

o  = aNumber
return true, if the argument represents the same numeric value
as the receiver, false otherwise

o  > aNumber
return true, if the argument is less

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

o  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.

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

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

o  printfPrintString: formatString
non-standard: return a printed representation of the receiver
as specified by formatString, which is defined by printf.
If you use this, be aware, that specifying doubles differs on
systems; on SYSV machines you have to give something like %lf,
while on BSD systems the format string has to be %F.
Also, the resulting string may not be longer than 255 bytes -
since thats the (static) size of the buffer.
This method is NONSTANDARD and may be removed without notice.
WARNNG: this goes directly to the C-printf function and may therefore me inherently unsafe.
Please use the printf: method, which is safe as it is completely implemented in Smalltalk.

o  storeString
return a printed representation of the receiver;
all valid digits are printed.
LimitedPrecisonReal and its subclasses use #storeString instead of
#storeOn: as basic print mechanism.

private accessing
o  basicAt: index
return an internal byte of the float.
The value returned here depends on byte order, float representation etc.
Therefore, this method should be used strictly private.

Notice:
the need to redefine this method here is due to the
inability of many machines to store floats in non-double aligned memory.
Therefore, on some machines, the first 4 bytes of a float are left unused,
and the actual float is stored at index 5 .. 12.
To hide this at one place, this method knows about that, and returns
values as if this filler wasnt present.

o  basicAt: index put: value
set an internal byte of the float.
The value to be stored here depends on byte order, float representation etc.
Therefore, this method should be used strictly private.

Notice:
the need to redefine this method here is due to the
inability of many machines to store floats in non-double aligned memory.
Therefore, on some machines, the first 4 bytes of a float are left unused,
and the actual float is stored at index 5 .. 12.
To hide this at one place, this method knows about that, and returns
values as if this filler wasnt present.

special access
o  exponent
extract a normalized floats exponent.
The returned value depends on the float-representation of
the underlying machine and is therefore highly unportable.
This is not for general use.
This assumes that the mantissa is normalized to
0.5 .. 1.0 and the floats value is mantissa * 2^exp

o  mantissa
extract a normalized floats mantissa.
The returned value depends on the float-representation of
the underlying machine and is therefore highly unportable.
This is not for general use.
This assumes that the mantissa is normalized to
0.5 .. 1.0 and the floats value is mantissa * 2^exp

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

o  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 ...

o  isNegativeZero
many systems have two float.Pnt zeros

o  negative
return true if the receiver is less than zero

o  numberOfBits
return the size (in bits) of the real;
typically, 32 is returned here,
but who knows ...

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

o  strictlyPositive
return true if the receiver is greater than zero

truncation & rounding
o  ceiling
return the smallest integer which is greater or equal to the receiver.

o  ceilingAsFloat
return the smallest integer-valued float greater or equal to the receiver.
This is much like #ceiling, but avoids a (possibly expensive) conversion
of the result to an integer.
It may be useful, if the result is to be further used in another float-operation.

o  floor
return the integer nearest the receiver towards negative infinity.

o  floorAsFloat
return the float which represents the next lower
integer nearest the receiver towards negative infinity.
Much like floor, but returns a float result - useful if the result
will be used in another float operation, to avoid costy int-conversion.

o  fractionPart
extract the after-decimal fraction part.
such that (self truncated + self fractionPart) = self

o  rounded
return the receiver rounded to the nearest integer

o  roundedAsFloat
return the receiver rounded to the nearest integer as a float.
This is much like #rounded, but avoids a (possibly expensive) conversion
of the result to an integer.
It may be useful, if the result is to be further used in another float-operation.

o  truncated
return the receiver truncated towards zero as an integer

o  truncatedAsFloat
return the receiver truncated towards zero as a float.
This is much like #truncated, but avoids a (possibly expensive) conversion
of the result to an integer.
It may be useful, if the result is to be further used in another
float-operation.



ST/X 6.1.1; WebServer 1.620 at exept:8081; Wed, 23 May 2012 21:02:03 GMT