QuadFloat.st
changeset 4983 696dd6d07736
child 4991 9d86d1eb2a65
equal deleted inserted replaced
4982:8a3c297b60b1 4983:696dd6d07736
       
     1 "{ Package: 'stx:libbasic2' }"
       
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
       
     5 LimitedPrecisionReal variableByteSubclass:#QuadFloat
       
     6 	instanceVariableNames:''
       
     7 	classVariableNames:'QuadFloatZero QuadFloatOne Pi E Epsilon NaN PositiveInfinity
       
     8 		NegativeInfinity Halfpi HalfpiNegative'
       
     9 	poolDictionaries:''
       
    10 	category:'Magnitude-Numbers'
       
    11 !
       
    12 
       
    13 !QuadFloat class methodsFor:'documentation'!
       
    14 
       
    15 documentation
       
    16 "
       
    17     QuadFloats represent rational numbers with limited precision
       
    18     and are mapped to IEEE quadruple precision format (128bit).
       
    19     If the underlying cpu supports them natively, the machine format (long double) is
       
    20     used. Otherwise, a software emulation is done, which is much slower.
       
    21     Thus only use them, if you really need the additional precision;
       
    22     if not, use Float (which are doubles) or LongFloats which usually have IEEE extended precision (80bit).
       
    23 
       
    24     QuadFloats give you definite 128 bit quadruple floats,
       
    25     thus, code using quadFloats is guaranteed to be portable from one architecture to another.
       
    26 
       
    27     Representation:
       
    28         gcc-sparc:
       
    29             128bit quadruple IEEE floats (16bytes);
       
    30             112 bit mantissa,
       
    31             16 bit exponent,
       
    32             34 decimal digits (approx.)
       
    33 
       
    34     Mixed mode arithmetic:
       
    35         quadFloat op anyFloat    -> longFloat
       
    36         longFloat op complex     -> complex
       
    37 
       
    38     Range and precision of storage formats: see LimitedPrecisionReal >> documentation
       
    39 
       
    40     [author:]
       
    41         Claus Gittinger
       
    42 
       
    43     [see also:]
       
    44         Number
       
    45         Float ShortFloat LongFloat Fraction FixedPoint Integer Complex
       
    46         FloatArray DoubleArray
       
    47         https://en.wikipedia.org/wiki/Extended_precision
       
    48 "
       
    49 ! !
       
    50 
       
    51 !QuadFloat class methodsFor:'instance creation'!
       
    52 
       
    53 basicNew
       
    54     "return a new quadFloat - here we return 0.0
       
    55      - QuadFloats are usually NOT created this way ...
       
    56      Its implemented here to allow things like binary store & load
       
    57      of quadFloats.
       
    58      (but it is not a good idea to store the bits of a float - the reader might have a
       
    59       totally different representation - so floats should be
       
    60       binary stored in a device independent format)."
       
    61 
       
    62 %{  /* NOCONTEXT */
       
    63 #ifndef __SCHTEAM__
       
    64     OBJ newFloat;
       
    65     if (sizeof(long double) == sizeof(quadfloat)) {
       
    66         __qMKLFLOAT(newFloat, 0.0);   /* OBJECT ALLOCATION */
       
    67     } else {
       
    68         __qMKQFLOAT(newFloat, 0.0);   /* OBJECT ALLOCATION */
       
    69     }
       
    70     RETURN (newFloat);
       
    71 #endif /* not SCHTEAM */
       
    72 %}
       
    73 
       
    74     "Created: / 06-06-2019 / 17:18:58 / Claus Gittinger"
       
    75 !
       
    76 
       
    77 fromFloat:aFloat
       
    78     "return a new quadFloat, given a float value"
       
    79 
       
    80 %{  /* NOCONTEXT */
       
    81 #ifndef __SCHTEAM__
       
    82     OBJ newFloat;
       
    83     float128 f;
       
    84 
       
    85     if (__isFloatLike(aFloat)) {
       
    86         float f = __floatVal(aFloat);
       
    87         float128 qf = (LONGFLOAT)f;
       
    88         __qMKQFLOAT(newFloat, lf);   /* OBJECT ALLOCATION */
       
    89         RETURN (newFloat);
       
    90     }
       
    91 #endif
       
    92 %}.
       
    93     self error:'invalid argument'
       
    94 
       
    95     "
       
    96      QuadFloat fromFloat:123.0
       
    97      123.0 asQuadFloat
       
    98      123 asQuadFloat
       
    99     "
       
   100 
       
   101     "Created: / 06-06-2019 / 18:01:03 / Claus Gittinger"
       
   102 ! !
       
   103 
       
   104 !QuadFloat class methodsFor:'coercing & converting'!
       
   105 
       
   106 coerce:aNumber
       
   107     "convert the argument aNumber into an instance of the receiver's class and return it."
       
   108 
       
   109     ^ aNumber asQuadFloat.
       
   110 
       
   111     "Created: / 06-06-2019 / 16:51:01 / Claus Gittinger"
       
   112 ! !
       
   113 
       
   114 !QuadFloat class methodsFor:'constants'!
       
   115 
       
   116 NaN
       
   117     "return a shortFloat which represents not-a-Number (i.e. an invalid number)"
       
   118 
       
   119     NaN isNil ifTrue:[
       
   120         NaN := super NaN
       
   121     ].
       
   122     ^ NaN
       
   123 
       
   124     "
       
   125      self NaN
       
   126     "
       
   127 
       
   128     "Created: / 06-06-2019 / 16:56:09 / Claus Gittinger"
       
   129 !
       
   130 
       
   131 e
       
   132     "return the constant e as quadFloat"
       
   133 
       
   134     E isNil ifTrue:[
       
   135         "/ eDigits has enough digits for 128bit IEEE quads
       
   136         "/ do not use as a literal constant here - we cannot depend on the underlying C-compiler here...
       
   137         E  := self readFrom:(Number eDigits)
       
   138     ].
       
   139     ^ E
       
   140 
       
   141     "Created: / 06-06-2019 / 17:01:54 / Claus Gittinger"
       
   142 !
       
   143 
       
   144 pi
       
   145     "return the constant pi as quadFloat"
       
   146 
       
   147     Pi isNil ifTrue:[
       
   148         "/ piDigits has enough digits for 128bit IEEE quads
       
   149         "/ do not use as a literal constant here - we cannot depend on the underlying C-compiler here...
       
   150         Pi  := self readFrom:(Number piDigits)
       
   151     ].
       
   152     ^ Pi
       
   153 
       
   154     "Created: / 06-06-2019 / 17:09:51 / Claus Gittinger"
       
   155 ! !
       
   156 
       
   157 !QuadFloat class methodsFor:'documentation'!
       
   158 
       
   159 version_CVS
       
   160     ^ '$Header$'
       
   161 ! !
       
   162