LimitedPrecisionReal.st
author Stefan Vogel <sv@exept.de>
Tue, 16 Dec 2014 13:20:28 +0100
changeset 17221 1ee235ddab7f
parent 17206 83666440217a
child 17380 c8f2a7e0edbd
permissions -rw-r--r--
class: LimitedPrecisionReal added: #digitBytesMSB: #isIEEEFormat #digitBytes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16636
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
     1
"{ Encoding: utf8 }"
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
     2
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     3
"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     4
 COPYRIGHT (c) 1994 by Claus Gittinger
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
     5
	      All Rights Reserved
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     6
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    13
"
5470
5e56d8cff0c9 General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5357
diff changeset
    14
"{ Package: 'stx:libbasic' }"
5e56d8cff0c9 General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5357
diff changeset
    15
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    16
Number subclass:#LimitedPrecisionReal
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    17
	instanceVariableNames:''
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    18
	classVariableNames:''
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    19
	poolDictionaries:''
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    20
	category:'Magnitude-Numbers'
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    21
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    22
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    23
!LimitedPrecisionReal class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    24
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    25
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    26
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    27
 COPYRIGHT (c) 1994 by Claus Gittinger
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
    28
	      All Rights Reserved
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    29
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    30
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    31
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    33
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    34
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    35
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    36
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    37
!
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    38
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    39
documentation
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    40
"
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    41
    Abstract superclass for any-precision floating point numbers (i.e. IEE floats and doubles).
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    42
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    43
    Due to historic reasons, ST/X's Floats are what Doubles are in ST-80.
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    44
    The reason is that in some Smalltalk's, double floats are called Float, and no single float exists (VSE, V'Age),
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    45
    whereas in others, there are both Float and Double classes (VisualWorks).
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    46
    In order to allow code from both families to be loaded into ST/X without a missing class error,
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    47
    we decided to use IEE doubles as the internal representation of Float and make Double an alias to it.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    48
    This should work for either family.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    49
    If you really only want single precision floating point numbers, use ShortFloat instances.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    50
    But be aware, that there is usually no advantage (neither in memory usage, due to memory alignment restrictions),
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    51
    nor in speed, as these days, the CPUs are just as fast doing double precision operations.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    52
    (There is a difference when doing bulk operations, and you should consider using FloatArray for those).
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1201
diff changeset
    53
7405
9b0334a4591b comments
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
    54
    Range and Precision of Storage Formats:
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    55
    =======================================
7405
9b0334a4591b comments
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
    56
16636
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    57
      Format |   Class    |   Array Class   | Bits / Significant  | Smallest Pos Number | Largest Pos Number | Significant Digits 
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    58
             |            |                 |      (Binary)       |                     |                    |     (Decimal)
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    59
      -------+------------+-----------------+---------------------+---------------------+--------------------+--------------------    
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    60
      half   |     --     | HalfFloatArray  |    16 / 11          |  6.10... x 10−5     |  6.55...  x 10+5   |      3.3
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    61
      -------+------------+-----------------+---------------------+---------------------+--------------------+--------------------    
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    62
      single | ShortFloat | FloatArray      |    32 / 24          |  1.175...x 10-38    |  3.402... 10+38    |      6-9
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    63
      -------+------------+-----------------+---------------------+---------------------+--------------------+--------------------    
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    64
      double | Float      | DoubleArray     |    64 / 53          |  2.225...x 10-308   |  1.797... 10+308   |     15-17
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    65
      -------+------------+-----------------+---------------------+---------------------+--------------------+--------------------    
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    66
      double | LongFloat  |     --          |   128 / 113         |  3.362...x 10-4932  |  1.189... 10+4932  |     33-36
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    67
      ext    |            |                 |                     |                     |                    |
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    68
      (SPARC)|            |                 |                     |                     |                    |
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    69
      -------+            |                 |---------------------+---------------------+--------------------+--------------------    
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    70
      double |            |                 |    96 / 64          |  3.362...x 10-4932  |  1.189... 10+4932  |     18-21
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    71
      ext    |            |                 |                     |                     |                    |
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    72
      (x86)  |            |                 |                     |                     |                    |
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    73
      -------+------------+-----------------+---------------------+---------------------+--------------------+--------------------    
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    74
        --   | LargeFloat |     --          |     arbitrary       |  arbitrarily small  |  arbitrarily large |     arbitrary
42a6cf3b51ba class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16444
diff changeset
    75
      -------+------------+-----------------+---------------------+---------------------+--------------------+--------------------    
7405
9b0334a4591b comments
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
    76
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    77
    HalfFloats are only supported in fixed array containers. This was added for OpenGL and other graphic libraries which allow
16637
9723be1cb8d7 class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16636
diff changeset
    78
    for texture, and vertex data to be passed quickly in that format (see http://www.opengl.org/wiki/Small_Float_Formats).
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    79
    Long- and LargeFloat are not supported as array containers. 
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    80
    These formats are seldom used for bulk data.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    81
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    82
    Bulk Containers:
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    83
    ================
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    84
    If you have a vector or matrix (and especially: large one's) of floating point numbers, the well known
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    85
    Array is a very inperformant choice. The reason is that it keeps pointers to each of its elements, and each element
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    86
    (if it is a float) is itself stored somewhere in the object memory.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    87
    Thus, there is both a space overhead (every float object has an object header, for class and other information), and
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    88
    also a performance overhead (extra indirection, cache misses and alignment inefficiencies).
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    89
    For this, the bulk numeric containers are provided, which keep the elements unboxed and properly aligned.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    90
    Use them for matrices and large numeric vectors. They also provide some optimized bulk operation methods,
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    91
    such as adding, multiplying etc.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    92
    
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    93
    Comparing Floats:
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    94
    =================
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    95
    Due to rounding errors (usually on the last bit), you shalt not compare two floating point numbers
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    96
    using the #= operator. For example, the value 0.1 cannot be represented as a sum of powers of two fractions,
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    97
    and will therefore be always an approximation with a half bit error in the last bit of the mantissa.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    98
    Usually, the print functions take this into consideration and return a (faked) '0.1'.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
    99
    However, this half bit error may accumulate, for example, when multiplying that by 10, the error may get large
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   100
    enough to be no longer pushed under the rug by the print function, and you will get '0.9999999999999' from it.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   101
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   102
    Also, comparing against a proper 1.0 (which is representable as an exact power of 2), you will get a false result.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   103
    (i.e. 0.1 * 10 ~= 1.0)
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   104
    This often confuses non-computer scientists (and even some of those are ocasionally).
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   105
    For this, you should always provide an epsilon value, when comparing two numbers. The epsilon value is
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   106
    the distance you accept two number to be apart to be still considered equal. Effectively the epsilon says
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   107
    are those nearer than this epsilon?.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   108
    Now we could say is the delta between two numbers smaller than 0.00001,
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   109
    and get a reasonable answer for big numbers. But what if we compare two tiny numbers?
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   110
    Then a reasonable epsilon must also be much smaller. 
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   111
    Actually, the epsilon should always be computed dynamically depending on the two values compared.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   112
    That is what the #isAlmostEqualTo:nEpsilon: method does for you. It does not take an absolute epsilon,
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   113
    but instead the number of distinct floating point numbers that the two compared floats may be apart.
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   114
    That is: the number of actually representable numbers between those two. Effectively, that is the difference between
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   115
    the two mantissas, when the numbers are scaled to the same exponent, taking the number of mantissa bits into account.
7405
9b0334a4591b comments
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   116
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1201
diff changeset
   117
    [author:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1201
diff changeset
   118
        Claus Gittinger
1893
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
   119
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
   120
    [see also:]
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
   121
        Fraction FixedPoint
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
   122
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
   123
! !
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   124
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   125
!LimitedPrecisionReal class methodsFor:'instance creation'!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   126
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   127
coerce:aNumber
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   128
    ^ self subclassResponsibility.
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   129
!
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   130
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   131
fromInteger:anInteger
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   132
    "return a float with anInteger's value.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   133
     Since floats have a limited precision, you usually loose bits when doing this
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   134
     (see Float decimalPrecision, LongFloat decimalPrecision."
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   135
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   136
    |newFloat sign absVal completeAbsVal delta mask|
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   137
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   138
    newFloat := self zero.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   139
    sign := anInteger sign.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   140
    (sign ~~ 0) ifTrue:[
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   141
        "be careful to do round to nearest even floating point value -
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   142
         thanks to Nicolas Cellier for this code"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   143
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   144
        absVal := anInteger abs.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   145
        delta := absVal highBit - self precision.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   146
        delta > 0 ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   147
            completeAbsVal := absVal.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   148
            "eliminate insignificant/trailing bits"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   149
            absVal := absVal bitShift: delta negated.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   150
            "inexact := trailingBits ~= 0.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   151
             Round to nearest even"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   152
            (completeAbsVal bitAt:delta) ~= 0 ifTrue:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   153
                mask := (1 bitShift:delta-1) - 1.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   154
                ((completeAbsVal bitAnd:mask) ~= 0 or:[absVal odd]) ifTrue:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   155
                    absVal := absVal + 1
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   156
                ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   157
            ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   158
        ] ifFalse: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   159
            delta := 0
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   160
        ].
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   161
        absVal digitLength to:1 by:-1 do:[:i |
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   162
            newFloat := (newFloat * 256) + (absVal digitByteAt:i).
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   163
        ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   164
        delta ~~ 0 ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   165
            newFloat := newFloat timesTwoPower:delta.
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   166
        ].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   167
        (sign < 0) ifTrue:[
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   168
            newFloat := newFloat negated
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   169
        ]
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   170
    ].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   171
    ^ newFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   172
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   173
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   174
    "
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   175
     ShortFloat fromInteger:2
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   176
     12345678901234567890 asShortFloat            
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   177
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   178
     1234567890 asFloat                     
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   179
     1234567890 asFloat asInteger                    
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   180
     -1234567890 asFloat asInteger                    
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   181
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   182
     12345678901234567890 asFloat storeString            
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   183
     12345678901234567890 asFloat asInteger   
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   184
     -12345678901234567890 asFloat asInteger   
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   185
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   186
     12345678901234567890 asLongFloat           
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   187
     12345678901234567890 asLongFloat asInteger 
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   188
     -12345678901234567890 asLongFloat asInteger 
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   189
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   190
     123456789012345678901234567890 asLongFloat           
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   191
     123456789012345678901234567890 asLongFloat asInteger  
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   192
     -123456789012345678901234567890 asLongFloat asInteger  
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   193
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   194
     1234567890123456789012345678901234567890 asLongFloat           
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   195
     1234567890123456789012345678901234567890 asLongFloat asInteger  
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   196
     -1234567890123456789012345678901234567890 asLongFloat asInteger
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   197
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   198
     'this test is on 65 bits'.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   199
     self assert: 16r1FFFFFFFFFFFF0801 asDouble ~= 16r1FFFFFFFFFFFF0800 asDouble.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   200
     'this test is on 64 bits'.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   201
     self assert: 16r1FFFFFFFFFFFF0802 asDouble ~= 16r1FFFFFFFFFFFF0800 asDouble.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   202
     'nearest even is upper'.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   203
     self assert: 16r1FFFFFFFFFFF1F800 asDouble = 16r1FFFFFFFFFFF20000 asDouble.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   204
     'nearest even is lower'.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   205
     self assert: 16r1FFFFFFFFFFFF0800 asDouble = 16r1FFFFFFFFFFFF0000 asDouble.
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   206
    "
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   207
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   208
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   209
fromLimitedPrecisionReal:anLPReal
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   210
    "return a float with anLPReals value.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   211
     You might loose bits when doing this.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   212
     Slow fallback."
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   213
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   214
    |fract|
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   215
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   216
    anLPReal isFinite ifFalse:[
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   217
        anLPReal isNaN ifTrue:[^ self NaN].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   218
        anLPReal negative ifTrue:[^ self negativeInfinity].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   219
        ^ self infinity
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   220
    ].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   221
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   222
    fract := anLPReal asTrueFraction.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   223
    ^ (self fromInteger:fract numerator) / (self fromInteger:fract denominator)
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   224
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   225
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   226
fromNumerator:numerator denominator:denominator
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   227
    "Create a limited precision real from a Rational.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   228
     This version will answer the nearest flotaing point value,
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   229
     according to IEEE 754 round to nearest even default mode"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   230
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   231
    |a b q r exponent floatExponent n ha hb hq q1 eMin precision nSubHq|
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   232
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   233
    a := numerator abs.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   234
    b := denominator abs.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   235
    ha := a highBit.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   236
    hb := b highBit.    
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   237
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   238
    precision := self precision.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   239
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   240
    "If both numerator and denominator are represented exactly in floating point number,
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   241
     then fastest thing to do is to use hardwired float division"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   242
    (ha <= precision and:[hb <= precision]) ifTrue: [
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   243
        ^ (self coerce:numerator) / (self coerce:denominator)
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   244
    ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   245
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   246
    "Try and obtain a mantissa with 1 bit excess precision.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   247
     First guess is rough, we might get one more bit or one less"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   248
    exponent := ha - hb - precision - 1.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   249
    exponent > 0
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   250
        ifTrue: [b := b bitShift: exponent]
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   251
        ifFalse: [a := a bitShift: exponent negated].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   252
    q := a quo: b.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   253
    r := a - (q * b).
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   254
    hq := q highBit.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   255
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   256
    "check for gradual underflow, in which case we should use less bits"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   257
    floatExponent := exponent + hq - 1.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   258
    eMin := 2 - (1 bitShift: (self numBitsInExponent - 1)).
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   259
    n := floatExponent >= eMin
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   260
        ifTrue: [precision + 1]
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   261
        ifFalse: [precision + 1 + floatExponent - eMin].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   262
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   263
    nSubHq := n - hq.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   264
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   265
    nSubHq < 0 ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   266
        exponent := exponent + hq - n.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   267
        r := (q bitAnd: (1 bitShift:nSubHq) - 1) * b + r.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   268
        q := q bitShift:nSubHq
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   269
    ] ifFalse:[nSubHq > 0 ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   270
        exponent := exponent + hq - n.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   271
        q1 := (r bitShift:nSubHq) quo: b.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   272
        q := (q bitShift:nSubHq) bitAnd: q1.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   273
        r := r - (q1 * b)
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   274
    ]].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   275
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   276
    "check if we should round upward.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   277
     The case of exact half (q bitAnd: 1) isZero not & (r isZero)
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   278
     will be handled by self fromInteger: conversion"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   279
    ((q bitAnd:1) ~~ 0 and:[r ~~ 0]) ifTrue:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   280
        q := q + 1
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   281
    ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   282
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   283
    (numerator negative xor:denominator negative) ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   284
        q := q negated.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   285
    ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   286
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   287
    ^ (self coerce:q) timesTwoPower:exponent
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   288
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   289
    "
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   290
        Time millisecondsToRun:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   291
            1000000  timesRepeat:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   292
                Float fromNumerator:12345678901234567890 denominator:987654321
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   293
            ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   294
        ]
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   295
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   296
        |fraction|
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   297
        fraction := 12345678901234567890//987654321.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   298
        Time millisecondsToRun:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   299
            1000000  timesRepeat:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   300
                fraction asFloat
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   301
            ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   302
        ]
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   303
    "
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   304
!
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   305
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   306
new:aNumber
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   307
    "catch this message - not allowed for floats/doubles"
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   308
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   309
    self error:'Floats/Doubles cannot be created with new:'
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   310
!
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   311
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   312
readFrom:aStringOrStream onError:exceptionBlock
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   313
    "read a float from a string"
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   314
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   315
    |num|
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   316
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   317
    num := super readFrom:aStringOrStream onError:exceptionBlock.
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   318
    ^ self coerce:num
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   319
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   320
    "
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   321
     Float readFrom:'.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   322
     Float readFrom:'0.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   323
     Float readFrom:'0'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   324
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   325
     ShortFloat readFrom:'.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   326
     ShortFloat readFrom:'0.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   327
     ShortFloat readFrom:'0'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   328
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   329
     LongFloat readFrom:'.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   330
     LongFloat readFrom:'0.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   331
     LongFloat readFrom:'0'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   332
    "
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   333
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   334
    "Created: / 07-01-1998 / 16:17:19 / cg"
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   335
    "Modified: / 23-11-2010 / 14:35:41 / cg"
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   336
! !
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   337
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   338
!LimitedPrecisionReal class methodsFor:'class initialization'!
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   339
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   340
initialize
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   341
    "initialize ANSI compliant float globals"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   342
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   343
    Smalltalk at:#FloatE put:ShortFloat.
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   344
    Smalltalk at:#FloatD put:Float.
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   345
    Smalltalk at:#FloatQ put:LongFloat.
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   346
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   347
    "
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   348
     self initialize
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   349
    "
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   350
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   351
    "Created: / 7.9.2001 / 14:02:45 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   352
    "Modified: / 7.9.2001 / 14:02:54 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   353
! !
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   354
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   355
!LimitedPrecisionReal class methodsFor:'constants & defaults'!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   356
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   357
NaN
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   358
    "return the constant NaN (not a Number)."
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   359
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   360
    ^ self zero uncheckedDivide:self zero
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   361
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   362
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   363
      ShortFloat NaN  
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   364
      Float NaN       
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   365
      LongFloat NaN   
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   366
      LargeFloat NaN   
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   367
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   368
!
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   369
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   370
computeEpsilon
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   371
    "return the maximum relative spacing"
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   372
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   373
    ^ self radix asFloat raisedToInteger:(1 - self precision).
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   374
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   375
    "
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   376
      ShortFloat computeEpsilon  
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   377
      Float computeEpsilon       
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   378
      LongFloat computeEpsilon   
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   379
    "
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   380
!
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   381
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   382
e
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   383
    "return the closest approximation of the irrational number e"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   384
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   385
    self subclassResponsibility
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   386
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   387
    "Modified: / 7.9.2001 / 14:05:02 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   388
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   389
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   390
emax
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   391
    "return the largest exponent"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   392
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   393
    self subclassResponsibility
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   394
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   395
    "Created: / 7.9.2001 / 14:05:28 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   396
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   397
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   398
emin
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   399
    "return the smallest exponent"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   400
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   401
    self subclassResponsibility
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   402
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   403
    "Created: / 7.9.2001 / 14:05:35 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   404
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   405
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   406
epsilon
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   407
    "return the maximum relative spacing"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   408
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   409
    ^ self radix asFloat raisedToInteger:(1 - self precision)
6575
b07cd54c4375 Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents: 6195
diff changeset
   410
b07cd54c4375 Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents: 6195
diff changeset
   411
    "
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   412
      ShortFloat epsilon  
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   413
      Float epsilon       
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   414
      LongFloat epsilon   
6575
b07cd54c4375 Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents: 6195
diff changeset
   415
    "
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   416
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   417
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   418
fmax
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   419
    "return the largest value allowed"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   420
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   421
    self subclassResponsibility
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   422
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   423
    "Created: / 7.9.2001 / 14:06:56 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   424
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   425
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   426
fmin
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   427
    "return the minimum value allowed"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   428
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   429
    self subclassResponsibility
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   430
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   431
    "Created: / 7.9.2001 / 14:07:06 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   432
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   433
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   434
infinity
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   435
    "return a float representing infinity"
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   436
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   437
    ^ self unity uncheckedDivide:self zero
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   438
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   439
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   440
      ShortFloat infinity  
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   441
      Float infinity       
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   442
      LongFloat infinity   
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   443
      LargeFloat infinity   
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   444
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   445
!
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   446
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   447
negativeInfinity
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   448
    "return a float representing negative infinity"
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   449
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   450
    ^ self unity negated uncheckedDivide:self zero
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   451
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   452
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   453
      ShortFloat negativeInfinity   
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   454
      Float negativeInfinity       
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   455
      LongFloat negativeInfinity   
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   456
      LargeFloat negativeInfinity   
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   457
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   458
!
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   459
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   460
pi
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   461
    "return the closest approximation of the irrational number pi"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   462
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   463
    self subclassResponsibility
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   464
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   465
    "Created: / 7.9.2001 / 14:07:35 / cg"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   466
! !
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   467
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   468
!LimitedPrecisionReal class methodsFor:'queries'!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   469
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   470
decimalPrecision
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   471
    "return the number of valid decimal digits"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   472
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   473
    ^ ((self precision + 1) / (10.0 log:self radix)) ceiling   "use ceiling, since the last decimal digit contains some precision"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   474
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   475
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   476
     ShortFloat decimalPrecision 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   477
     Float decimalPrecision      
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   478
     LongFloat decimalPrecision  
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   479
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   480
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   481
12120
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   482
denormalized
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   483
    "Return whether the instances of this class can
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   484
     represent values in denormalized format."
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   485
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   486
    ^ true
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   487
!
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   488
8892
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   489
isAbstract
11225
32a2c7f432eb comment
Claus Gittinger <cg@exept.de>
parents: 10946
diff changeset
   490
    "Return if this class is an abstract class.
32a2c7f432eb comment
Claus Gittinger <cg@exept.de>
parents: 10946
diff changeset
   491
     True is returned for LimitedPrecisionReal here; false for subclasses."
32a2c7f432eb comment
Claus Gittinger <cg@exept.de>
parents: 10946
diff changeset
   492
8892
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   493
    ^ self == LimitedPrecisionReal
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   494
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   495
    "
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   496
     1.0 class isAbstract
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   497
    "
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   498
!
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   499
17221
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   500
isIEEEFormat
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   501
    "return true, if this machine represents floats in IEEE format.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   502
     Currently, no support is provided for non-ieee machines
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   503
     to convert their floats into this (which is only relevant,
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   504
     if such a machine wants to send floats as binary to some other
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   505
     machine).
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   506
     Machines with non-IEEE format are VAXed and IBM370-type systems
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   507
     (among others). Today, most systems use IEEE format floats."
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   508
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   509
%{
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   510
#ifdef __s390__
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   511
    RETURN(false);
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   512
#endif
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   513
%}.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   514
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   515
    ^ true "/ this may be a lie
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   516
!
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   517
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   518
numBitsInExponent
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   519
    "return the number of bits in the exponent"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   520
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   521
    ^ self subclassResponsibility
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   522
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   523
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   524
numBitsInIntegerPart
16781
4619d7da013a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16731
diff changeset
   525
    "answer the number of bits in the integer part of the mantissa.
4619d7da013a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16731
diff changeset
   526
     Most floating point formats are normalized to get rid of the extra
4619d7da013a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16731
diff changeset
   527
     bit."
4619d7da013a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16731
diff changeset
   528
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   529
    ^ 0
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   530
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   531
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   532
numBitsInMantissa
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   533
    "return the number of bits in the mantissa
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   534
     (typically 1 less than the precision due to the hidden bit)"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   535
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   536
    ^ self subclassResponsibility
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   537
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   538
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   539
precision
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   540
    "return the number of valid mantissa bits"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   541
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   542
    ^ self numBitsInMantissa + 1 - self numBitsInIntegerPart 
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   543
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   544
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   545
radix
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   546
    "return the radix (base)"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   547
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   548
    self subclassResponsibility
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   549
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   550
    "Created: / 7.9.2001 / 14:08:20 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   551
! !
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   552
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   553
!LimitedPrecisionReal methodsFor:'accessing'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   554
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   555
at:index
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   556
    "redefined to prevent access to individual bytes in a real."
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   557
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   558
    self error:'not allowed for floats/doubles'
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   559
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   560
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   561
at:index put:aValue
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   562
    "redefined to prevent access to individual bytes in a real"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   563
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   564
    self error:'not allowed for floats/doubles'
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   565
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   566
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   567
!LimitedPrecisionReal methodsFor:'arithmetic'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   568
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   569
* aNumber
11727
e0ffd06e50fd comment
Claus Gittinger <cg@exept.de>
parents: 11516
diff changeset
   570
    "return the product of the receiver and the argument."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   571
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   572
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   573
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   574
"/    ^ aNumber productFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   575
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   576
    ^ aNumber productFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   577
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   578
    "Modified: 17.4.1996 / 12:35:36 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   579
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   580
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   581
+ aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   582
    "return the sum of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   583
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   584
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   585
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   586
"/    ^ aNumber sumFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   587
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   588
    ^ aNumber sumFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   589
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   590
    "Modified: 17.4.1996 / 12:35:55 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   591
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   592
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   593
- aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   594
    "return the difference of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   595
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   596
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   597
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   598
"/    ^ aNumber differenceFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   599
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   600
    ^ aNumber differenceFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   601
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   602
    "Modified: 17.4.1996 / 12:36:07 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   603
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   604
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   605
/ aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   606
    "return the quotient of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   607
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   608
    ((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[
7141
033f2c26d8e6 replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents: 6890
diff changeset
   609
        ^ ZeroDivide raiseRequestWith:thisContext.
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   610
    ].
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   611
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   612
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   613
"/    ^ aNumber quotientFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   614
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   615
    ^ aNumber quotientFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   616
4455
2d31d0d986be Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents: 3898
diff changeset
   617
    "Modified: / 17.4.1996 / 12:36:21 / cg"
2d31d0d986be Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents: 3898
diff changeset
   618
    "Modified: / 26.7.1999 / 10:46:11 / stefan"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   619
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   620
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   621
// aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   622
    "return the integer quotient of dividing the receiver by aNumber with
1882
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   623
     truncation towards negative infinity."
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   624
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   625
    ^ (self / aNumber) floor asInteger
1882
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   626
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   627
    "Modified: 5.11.1996 / 11:45:37 / cg"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   628
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   629
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   630
ceiling
7398
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
   631
    ^ self asTrueFraction ceiling
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   632
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   633
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   634
floor
7398
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
   635
    ^ self asTrueFraction floor
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   636
!
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   637
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   638
timesTwoPower:anInteger
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   639
    "multiply self by a power of two.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   640
     Implementation takes care of preserving class and avoiding overflow/underflow
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   641
     Thanks to Nicolas Cellier for this code"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   642
12761
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   643
    |half two h1 h2|
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   644
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   645
    two := self coerce:2.0.
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   646
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   647
    anInteger abs highBit >= (self class numBitsInExponent - 2) ifTrue:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   648
        half := anInteger // 2.
12761
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   649
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   650
        anInteger even ifTrue:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   651
            "/ halves are equal
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   652
            h2 := h1 := (two raisedToInteger: half).
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   653
        ] ifFalse:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   654
            h1 := (two raisedToInteger: half).
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   655
            anInteger > 0 ifTrue:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   656
                h2 := h1 * two.
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   657
            ] ifFalse:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   658
                h2 := (two raisedToInteger: anInteger - half).
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   659
            ].
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   660
        ].
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   661
        ^ (self * h1) * h2
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   662
    ] ifFalse: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   663
        ^ self * (two raisedToInteger: anInteger).
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   664
    ]
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   665
12758
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   666
    "
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   667
     (1 asShortFloat timesTwoPower: 3) class = ShortFloat.
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   668
     (1 asLongFloat timesTwoPower: 1024).
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   669
     (1 asFloat timesTwoPower: -1024) timesTwoPower: 1024.
12761
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   670
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   671
     (2.0 asShortFloat timesTwoPower: -150) timesTwoPower: 150    
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   672
     (2.0 asLongFloat timesTwoPower: -150) timesTwoPower: 150   
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   673
     (2.0 asFloat timesTwoPower: -150) timesTwoPower: 150       
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   674
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   675
     (2.0 asShortFloat timesTwoPower: -149) timesTwoPower: 149  
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   676
     (2.0 asLongFloat timesTwoPower: -149) timesTwoPower: 149    
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   677
     (2.0 asFloat timesTwoPower: -149) timesTwoPower: 149        
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   678
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   679
     Time millisecondsToRun:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   680
        1000000 timesRepeat:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   681
            (2.0 timesTwoPower: 150)
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   682
        ]
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   683
     ]  
12758
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   684
    "
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   685
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   686
17221
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   687
!LimitedPrecisionReal methodsFor:'bytes access'!
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   688
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   689
digitBytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   690
    "answer the float's digit bytes im IEEE format.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   691
     Use the native machine byte ordering."
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   692
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   693
    |sz "{ Class:SmallInteger }"
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   694
     bytes|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   695
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   696
    sz := self basicSize.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   697
    bytes := ByteArray new:sz.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   698
    1 to:sz do:[:i|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   699
        bytes at:i put:(self basicAt:i).
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   700
    ].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   701
    ^ bytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   702
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   703
    "
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   704
        Float pi digitBytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   705
        ShortFloat pi digitBytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   706
    "
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   707
!
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   708
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   709
digitBytesMSB:msb
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   710
    "answer the float's digit bytes im IEEE format.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   711
     If msb == true, use MSB byte order, otherwise LSB byte order."
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   712
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   713
    |sz "{ Class:SmallInteger }"
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   714
     bytes|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   715
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   716
    self class isIEEEFormat ifFalse:[self error:'unsupported operation'].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   717
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   718
    sz := self basicSize.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   719
    bytes := ByteArray new:sz.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   720
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   721
    (UninterpretedBytes isBigEndian == msb) ifTrue:[
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   722
        1 to:sz do:[:i|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   723
            bytes at:i put:(self basicAt:i).
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   724
        ].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   725
    ] ifFalse:[
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   726
        "reverse the bytes"
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   727
        1 to:sz do:[:i|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   728
            bytes at:sz-i+1 put:(self basicAt:i).
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   729
        ].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   730
    ].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   731
    ^ bytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   732
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   733
    "
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   734
        Float pi digitBytesMSB:false
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   735
        Float pi digitBytesMSB:true
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   736
        ShortFloat pi digitBytesMSB:false
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   737
        ShortFloat pi digitBytesMSB:true
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   738
    "
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   739
! !
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   740
5238
f7a816a660a3 categories
Claus Gittinger <cg@exept.de>
parents: 4615
diff changeset
   741
!LimitedPrecisionReal methodsFor:'coercing & converting'!
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   742
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   743
asFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   744
    ^ Float fromLimitedPrecisionReal:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   745
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   746
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   747
asFraction
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   748
    "Answer a rational number (Integer or Fraction) representing the receiver.
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   749
     This conversion uses the continued fraction method to approximate 
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   750
     a floating point number.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   751
     In contrast to #asTrueFraction, which returns exactly the value of the float,
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   752
     this rounds in the last significant bit of the floating point number.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   753
    "
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   754
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   755
    |num1 denom1 num2 denom2 int frac newD temp limit|
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   756
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   757
    self isFinite ifFalse:[
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   758
"/        ^ self asMetaNumber.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   759
        ^ self class
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   760
            raise:#domainErrorSignal
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   761
            receiver:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   762
            selector:#asFraction
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   763
            arguments:#()
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   764
            errorString:'Cannot represent non-finite number as fraction'
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   765
    ].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   766
7394
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
   767
    limit := 10 raisedToInteger:self defaultNumberOfDigits.
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   768
    num1 := self truncated. 
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   769
    denom1 := 1.            "The first of two alternating denominators"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   770
    num2 := 1.              "The second numerator"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   771
    denom2 := 0.            "The second denominator--will update"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   772
    int := num1.            "The integer part of self"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   773
    frac := self fractionPart.             
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   774
    [frac = 0] whileFalse:[                
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   775
        newD := 1.0 / frac.                   
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   776
        int := newD truncated.        
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   777
        frac := newD fractionPart.      "save the fractional part for next time"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   778
        temp := num2.                   "old numerator and save it"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   779
        num2 := num1.                         
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   780
        num1 := num1 * int + temp.      "Update first numerator"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   781
        temp := denom2.                 "old denominator and save it"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   782
        denom2 := denom1.                    
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   783
        denom1 := int * denom1 + temp.  "Update first denominator"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   784
        limit < denom1 ifTrue:[
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   785
            "Is ratio past float precision?  If so, pick which of the two ratios to use"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   786
            num2 = 0.0 ifTrue:[
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   787
                "Is second denominator 0?"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   788
                ^ Fraction numerator:num1 denominator:denom1
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   789
            ].
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   790
            ^ Fraction numerator:num2 denominator:denom2
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   791
        ]
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   792
    ].
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   793
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   794
    "If fractional part is zero, return the first ratio"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   795
    denom1 = 1 ifTrue:[
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   796
        "Am i really an Integer?"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   797
        ^ num1 "Yes, return Integer result"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   798
    ].
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   799
    "Otherwise return Fraction result"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   800
    ^ Fraction numerator:num1 denominator:denom1
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   801
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   802
    "
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   803
     1.1 asFraction      
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   804
     1.2 asFraction      
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   805
     0.3 asFraction   
7394
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
   806
     0.5 asFraction  
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
   807
     (1/5) asFloat asFraction  
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   808
     (1/8) asFloat asFraction  
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   809
     (1/13) asFloat asFraction 
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   810
     (1/10) asFloat asFraction 
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   811
     (1/10) asFloat asTrueFraction asFixedPoint scale:20 
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   812
     3.14159 asFixedPoint scale:20        
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   813
     3.14159 storeString       
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   814
     3.14159 asFraction asFloat storeString       
7394
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
   815
     1.3 asFraction            
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
   816
     1.0 asFraction            
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
   817
     1E6 asFraction            
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
   818
     1E-6 asFraction            
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   819
    "
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2388
diff changeset
   820
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   821
    "Modified: / 25.10.1997 / 16:41:19 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   822
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   823
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   824
asInteger
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   825
    "return an integer with same value - might truncate"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   826
3185
1c7bd090a822 removed unused local vars
Claus Gittinger <cg@exept.de>
parents: 3148
diff changeset
   827
    |max maxF|
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   828
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   829
    self isFinite ifFalse:[
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   830
"/        ^ self asMetaNumber.
3395
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3225
diff changeset
   831
        ^ self class
7418
521174de046d Nan asInteger raises DomainError
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
   832
            raise:#domainErrorSignal
3395
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3225
diff changeset
   833
            receiver:self
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3225
diff changeset
   834
            selector:#asInteger
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6185
diff changeset
   835
            arguments:#()
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   836
            errorString:'Cannot represent non-finite number as integer'
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   837
    ].
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   838
4615
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   839
    self abs < 2e16 ifTrue:[
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   840
        "/ NOTICE: this must be redefined in float
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   841
        "/ subclasses to handle the smallinteger range;
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   842
        "/ i.e. this may only be invoked for reals
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   843
        "/ which are NOT within the smallInt range.
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   844
        "/ otherwise, endless recursion is the consequence.
3426
a648de89ac72 comment
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
   845
4615
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   846
        max := SmallInteger maxVal // 2 + 1.
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   847
        maxF := max asFloat.
3426
a648de89ac72 comment
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
   848
4615
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   849
        ^ (self quo:maxF) * max + (self rem:maxF) truncated
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   850
    ].
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   851
    ^ self asTrueFraction
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   852
3137
f334b17f0347 bif speedup in asInteger with large floats
ca
parents: 3060
diff changeset
   853
    "
4615
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   854
     12345.0 asInteger     
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   855
     1e15 asInteger        
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   856
     1e33 asInteger asFloat
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
   857
     1e303 asInteger asFloat
3137
f334b17f0347 bif speedup in asInteger with large floats
ca
parents: 3060
diff changeset
   858
    "
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   859
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6185
diff changeset
   860
    "Modified: / 16.11.2001 / 14:15:33 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   861
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   862
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   863
asLargeFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   864
    ^ LargeFloat fromLimitedPrecisionReal:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   865
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   866
7378
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
   867
asLimitedPrecisionReal
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
   868
    "return a float of any precision with same value"
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
   869
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
   870
   ^ self
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
   871
!
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
   872
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   873
asLongFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   874
    ^ LongFloat fromLimitedPrecisionReal:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   875
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   876
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   877
asRational
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   878
    "Answer a Rational number--Integer or Fraction--representing the receiver.
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   879
     Same as asFraction fro st-80 compatibility."
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   880
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   881
    ^ self asFraction
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   882
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   883
    "
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   884
     1.1 asRational      
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   885
     1.2 asRational      
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   886
     0.3 asRational   
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   887
     0.5 asRational 
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   888
     (1/5) asFloat asRational
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   889
     (1/8) asFloat asRational  
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   890
     (1/13) asFloat asRational 
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   891
     3.14159 asRational        
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   892
     3.14159 asRational asFloat       
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   893
     1.3 asRational  
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   894
     1.0 asRational  
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   895
    "
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   896
!
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
   897
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   898
asShortFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   899
    ^ ShortFloat fromLimitedPrecisionReal:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   900
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   901
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   902
asTrueFraction
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   903
    "Answer a fraction or integer that EXACTLY represents self,
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   904
     an any-precision IEEE floating point number, consisting of:
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   905
        numMantissaBits bits of normalized mantissa (i.e. with hidden leading 1-bit)
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   906
        optional numExtraBits between mantissa and exponent (normalized flag for ext-real)
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   907
        numExponentBits bits of 2s complement exponent
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   908
        1 sign bit.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   909
     Taken from Floats asTrueFraction"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   910
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   911
    |shifty sign expPart exp fraction fractionPart result zeroBitsCount
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   912
     numBytes numBits numBitsInMantissa maskMantissa numBitsInExponent maskExponent
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   913
     biasExponent numIntegerBits|
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   914
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   915
    self isFinite ifFalse:[
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   916
"/        ^ self asMetaNumber
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   917
        ^ self class
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   918
            raise:#domainErrorSignal
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   919
            receiver:self
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   920
            selector:#asTrueFraction
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   921
            arguments:#()
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   922
            errorString:'Cannot represent non-finite float as a fraction'.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   923
    ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   924
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   925
    "Extract the bits of an IEEE anySize float "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   926
    numBytes := self basicSize.
16781
4619d7da013a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16731
diff changeset
   927
    numIntegerBits := self class numBitsInIntegerPart.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   928
    numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   929
    numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   930
    numBits := numBitsInMantissa + numBitsInExponent. "not (numBytes * 8 - 1) -- there could be 80 float bits in 96 overall bits"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   931
    biasExponent := maskExponent bitShift:-1.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   932
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   933
    shifty := LargeInteger basicNew numberOfDigits:numBytes.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   934
    UninterpretedBytes isBigEndian ifTrue:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   935
        1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   936
    ] ifFalse:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   937
        1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   938
    ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   939
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   940
    " Extract the sign and the biased exponent "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   941
    sign := (shifty bitAt:numBits+1) == 0 ifTrue: [1] ifFalse: [-1].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   942
    expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   943
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   944
    " Extract fractional part; answer 0 if this is a true 0.0 value "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   945
    fractionPart := shifty bitAnd:maskMantissa.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   946
    ( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0  ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   947
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   948
    numIntegerBits == 0 ifTrue:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   949
        " Replace omitted leading 1 in fraction (Notice: quadIEEE format does not do this)"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   950
        fraction := fractionPart bitOr: (maskMantissa + 1).
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   951
    ] ifFalse:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   952
        fraction := fractionPart.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   953
    ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   954
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   955
    "Unbias exponent"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   956
    exp := biasExponent - expPart + (numBitsInMantissa - numIntegerBits).
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   957
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   958
    " Form the result. When exp>fractionalPrecision, the exponent is adjusted by
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   959
      the number of trailing zero bits in the fraction to minimize
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   960
      the (huge) time otherwise spent in #gcd: of fraction handling code."
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   961
    exp negative ifTrue: [
7480
74f8bf8628e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7447
diff changeset
   962
        result := sign * (fraction bitShift: exp negated) 
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   963
    ] ifFalse:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   964
        zeroBitsCount := fraction lowBit - 1.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   965
        exp := exp - zeroBitsCount.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   966
        exp <= 0 ifTrue: [
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   967
            zeroBitsCount := zeroBitsCount + exp.
7480
74f8bf8628e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7447
diff changeset
   968
            result := sign * (fraction bitShift:zeroBitsCount negated) 
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   969
        ] ifFalse: [
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   970
            result := Fraction
7480
74f8bf8628e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7447
diff changeset
   971
                    numerator: (sign * (fraction bitShift: zeroBitsCount negated))
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   972
                    denominator: (1 bitShift:exp) 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   973
        ] 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   974
    ].
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   975
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   976
    "Low cost validation omitted after extensive testing"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   977
    "(result asFloat = self) ifFalse: [self error: 'asTrueFraction validation failed']."
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   978
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   979
    ^ result 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   980
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   981
    "
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   982
     0.3 asFloat asTrueFraction   
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   983
     0.3 asShortFloat asTrueFraction  
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   984
     0.3 asLongFloat asTrueFraction   
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   985
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   986
     1.25 asTrueFraction     
7480
74f8bf8628e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7447
diff changeset
   987
     1.25 asShortFloat asTrueFraction     
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   988
     0.25 asTrueFraction     
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   989
     -0.25 asTrueFraction    
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   990
     3e37 asTrueFraction     
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   991
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   992
     Float NaN asTrueFraction               -> error
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   993
     Float infinity asTrueFraction          -> error
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   994
     Float negativeInfinity asTrueFraction  -> error
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   995
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   996
!
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   997
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   998
exponent
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   999
    "extract a normalized floats exponent.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1000
     The returned value depends on the float-representation of
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1001
     the underlying machine and is therefore highly unportable.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1002
     This is not for general use.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1003
     This assumes that the mantissa is normalized to
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1004
     0.5 .. 1.0 and the floats value is mantissa * 2^exp"
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1005
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1006
    |shifty expPart exp numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1007
     biasExponent numIntegerBits fractionPart|
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1008
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1009
    "Extract the bits of an IEEE anySize float "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1010
    numBytes := self basicSize.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1011
    numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1012
    numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1013
    numIntegerBits := self class numBitsInIntegerPart.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1014
    biasExponent := maskExponent bitShift:-1.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1015
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1016
    shifty := LargeInteger basicNew numberOfDigits:numBytes.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1017
    UninterpretedBytes isBigEndian ifTrue:[
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1018
        1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)].
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1019
    ] ifFalse:[
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1020
        1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)].
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1021
    ].
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1022
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1023
    " Extract the sign and the biased exponent "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1024
    expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1025
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1026
    " Extract fractional part; answer 0 if this is a true 0.0 value "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1027
    fractionPart := shifty bitAnd:maskMantissa.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1028
    ( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0  ].
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1029
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1030
    exp := expPart - biasExponent + 1.
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1031
    ^ exp
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1032
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1033
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1034
     0.3 asFloat exponent  
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1035
     0.3 asShortFloat exponent  
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1036
     0.3 asLongFloat exponent  
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1037
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1038
     0.0 exponent2      0
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1039
     1.0 exponent2      1
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1040
     2.0 exponent2      2
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1041
     3.0 exponent2      2
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1042
     4.0 exponent2      3
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1043
     0.5 exponent2      0
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1044
     0.4 exponent2      -1
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1045
     0.25 exponent2     -1
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1046
     0.00000011111 exponent2  -23 
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1047
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1048
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1049
3898
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1050
fractionalPart
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1051
    "This has been renamed to #fractionPart for ST80 compatibility.
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1052
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1053
     extract the after-decimal fraction part.
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1054
     the floats value is 
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1055
        float truncated + float fractionalPart"
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1056
5868
d497030b1df7 Use <resource:#obsolete>
Stefan Vogel <sv@exept.de>
parents: 5557
diff changeset
  1057
    <resource:#obsolete>
d497030b1df7 Use <resource:#obsolete>
Stefan Vogel <sv@exept.de>
parents: 5557
diff changeset
  1058
3898
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1059
    self obsoleteMethodWarning:'please use #fractionPart'.
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1060
    ^ self fractionPart
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1061
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1062
    "Modified: / 28.10.1998 / 17:10:12 / cg"
818a12afad05 moved methods around
Claus Gittinger <cg@exept.de>
parents: 3732
diff changeset
  1063
    "Created: / 28.10.1998 / 17:10:32 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1064
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1065
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1066
!LimitedPrecisionReal methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1067
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1068
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1069
    "return true, if the argument is greater"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1070
1201
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1071
    ^ aNumber lessFromFloat:self asFloat
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1072
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1073
    "Modified: 17.4.1996 / 13:34:50 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1074
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1075
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1076
!LimitedPrecisionReal methodsFor:'copying'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1077
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1078
deepCopy
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1079
    "return a deep copy of myself
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1080
     - because storing into floats is not recommended/allowed, its ok to return the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1081
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1082
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1083
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1084
10946
40514596d473 deepCopy change
ab
parents: 10680
diff changeset
  1085
deepCopyUsing:aDictionary postCopySelector:postCopySelector
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1086
    "return a deep copy of myself
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1087
     - because storing into floats is not recommended/allowed, its ok to return the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1088
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1089
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1090
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1091
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1092
shallowCopy
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1093
    "return a shallow copy of the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1094
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1095
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1096
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1097
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1098
simpleDeepCopy
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1099
    "return a deep copy of the receiver
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1100
     - because storing into floats is not recommended/allowed, its ok to return the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1101
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1102
    ^ self
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1103
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1104
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1105
!LimitedPrecisionReal methodsFor:'double dispatching'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1106
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1107
differenceFromFraction:aFraction
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
  1108
    "sent when a fraction does not know how to subtract the receiver, a float"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1109
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1110
    |d|
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1111
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1112
    d := aFraction denominator.
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1113
    ^ (aFraction numerator - (self * d)) / d
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1114
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1115
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1116
productFromFraction:aFraction
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
  1117
    "sent when a fraction does not know how to multiply the receiver, a float"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1118
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1119
    ^ self * aFraction numerator / aFraction denominator
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1120
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1121
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1122
quotientFromFraction:aFraction
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1123
    "Return the quotient of the argument, aFraction and the receiver.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1124
     Sent when aFraction does not know how to divide by the receiver."
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1125
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1126
    ^ aFraction numerator / (self * aFraction denominator)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1127
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1128
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1129
sumFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1130
    "sent when a fraction does not know how to add the receiver, a float"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1131
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1132
    |d|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1133
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1134
    d := aFraction denominator.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1135
    ^ (self * d + aFraction numerator) / d
10680
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1136
!
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1137
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1138
sumFromTimestamp:aTimestamp
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1139
    "I am to be interpreted as seconds, return the timestamp this number of seconds
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1140
     after aTimestamp"
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1141
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1142
    ^ aTimestamp addMilliseconds:(self * 1000) truncated.
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1143
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1144
    "
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1145
     Timestamp now sumFromTimestamp:aTimestamp   
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1146
     100.0 sumFromTimestamp:Timestamp now 
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1147
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1148
     |t1 t2|
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1149
     t1 := Timestamp now. 
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1150
     t2 := 1.5 sumFromTimestamp:t1.
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1151
     t1 inspect. t2 inspect.
d05947397052 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 9406
diff changeset
  1152
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1153
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1154
16731
03aa0206cdda comment/format only
Claus Gittinger <cg@exept.de>
parents: 16663
diff changeset
  1155
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1156
!LimitedPrecisionReal methodsFor:'printing & storing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1157
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1158
printOn:aStream
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1159
    "append a printed representation of the receiver to
3194
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1160
     the argument, aStream.
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1161
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1162
     LimitedPrecisonReal and its subclasses use #printString instead of
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1163
     #printOn: as basic print mechanism."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1164
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1165
    aStream nextPutAll:self printString
3194
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1166
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1167
    "Modified: / 20.1.1998 / 14:10:46 / stefan"
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1168
!
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1169
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1170
printString
3194
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1171
    "return a printed representation of the receiver
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1172
     LimitedPrecisonReal and its subclasses use #printString instead of
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1173
     #printOn: as basic print mechanism."
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1174
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1175
    ^ self subclassResponsibility
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1176
3194
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1177
    "Created: / 17.4.1996 / 12:12:20 / cg"
16073834a825 Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents: 3185
diff changeset
  1178
    "Modified: / 20.1.1998 / 14:10:47 / stefan"
8422
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1179
!
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1180
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1181
storeOn:aStream
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1182
    "append a printed representation of the receiver to
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1183
     the argument, aStream.
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1184
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1185
     LimitedPrecisonReal and its subclasses use #storeString instead of
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1186
     #storeOn: as basic store mechanism."
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1187
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1188
    aStream nextPutAll:self storeString
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1189
!
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1190
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1191
storeString
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1192
    "return a printed representation of the receiver
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1193
     LimitedPrecisonReal and its subclasses use #storeString instead of
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1194
     #storeOn: as basic print mechanism."
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1195
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1196
    ^ self subclassResponsibility
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1197
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1198
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1199
!LimitedPrecisionReal methodsFor:'queries'!
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1200
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1201
defaultNumberOfDigits
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1202
    "Answer how many digits of accuracy this class supports"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1203
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
  1204
    ^ self class decimalPrecision 
8422
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1205
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1206
    "
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1207
        Float new defaultNumberOfDigits
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1208
        LongFloat new defaultNumberOfDigits
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1209
        ShortFloat new defaultNumberOfDigits
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1210
    "
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1211
!
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1212
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1213
precision
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1214
    "return the number of valid mantissa bits.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1215
     Should be redefined in classes which allow per-instance precision specification"
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1216
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1217
    ^ self class precision
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1218
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1219
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1220
size
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1221
   "redefined since reals are kludgy (ByteArry)"
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1222
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1223
   ^ 0
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1224
! !
5470
5e56d8cff0c9 General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5357
diff changeset
  1225
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
  1226
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1227
!LimitedPrecisionReal methodsFor:'testing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1228
3148
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1229
isFinite
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1230
   ^ self subclassResponsibility
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1231
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1232
    "Created: / 7.1.1998 / 12:02:06 / stefan"
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1233
!
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1234
6185
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1235
isFloat
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1236
    "return true, if the receiver is some kind of floating point number;
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1237
     false is returned here.
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1238
     Same as #isLimitedPrecisionReal, but a better name ;-)"
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1239
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1240
    ^ true
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1241
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1242
    "Created: / 14.11.2001 / 14:57:55 / cg"
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1243
!
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1244
3148
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1245
isInfinite
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1246
    "return true, if the receiver is an infinite float (Inf).
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1247
     These are not created by ST/X float operations (they raise an exception);
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1248
     however, inline C-code could produce them ..."
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1249
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1250
    ^ (self isFinite or:[self isNaN]) not.
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1251
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1252
    "
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1253
        1.0 isInfinite
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1254
        (0.0 uncheckedDivide: 0.0) isInfinite
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1255
        (1.0 uncheckedDivide: 0.0) isInfinite
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1256
    "
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1257
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1258
    "Modified: / 7.1.1998 / 12:01:30 / stefan"
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1259
!
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1260
6086
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1261
isLimitedPrecisionReal
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1262
    "return true, if the receiver is some kind of limited precision real (i.e. floating point) number;
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1263
     true is returned here - the method is redefined from Object."
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1264
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1265
    ^ true
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1266
!
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1267
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1268
isNaN
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1269
   ^ self subclassResponsibility
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1270
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1271
    "Modified: 12.2.1997 / 16:45:27 / cg"
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1272
!
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1273
6890
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1274
isNegativeZero
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1275
    "many systems have two float.Pnt zeros"
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1276
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1277
    ^ (self isZero) and:[self printString first == $-]
6890
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1278
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1279
    "
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1280
     0.0 asLongFloat isNegativeZero     
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1281
     -0.0 asLongFloat isNegativeZero       
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1282
     -1.0 asLongFloat isNegativeZero       
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1283
     1.0 asLongFloat isNegativeZero       
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1284
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1285
     0.0 asLargeFloat isNegativeZero     
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1286
     -0.0 asLargeFloat isNegativeZero       
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1287
    "
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1288
    "
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1289
     0.0 asLongFloat isZero                      
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1290
     -0.0 asLongFloat isZero       
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1291
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1292
     0.0 > 0   
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1293
     -0.0 < 0       
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1294
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1295
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1296
5357
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1297
numberOfBits
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1298
    "return the size (in bits) of the real;
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1299
     typically, this is 64 for Floats and 32 for ShortFloats,
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1300
     but who knows ..."
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1301
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1302
    self subclassResponsibility
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1303
!
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1304
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1305
positive
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1306
    "return true if the receiver is greater or equal to zero"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1307
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1308
    ^ self sign >= 0
1201
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1309
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1310
    "Modified: 17.4.1996 / 13:35:10 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1311
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1312
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1313
!LimitedPrecisionReal methodsFor:'truncation & rounding'!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1314
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1315
ceilingAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1316
    ^ self coerce:(self ceiling).
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1317
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1318
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1319
     0.4 asLongFloat ceilingAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1320
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1321
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1322
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1323
floorAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1324
    ^ self coerce:(self floor).
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1325
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1326
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1327
     0.4 asLongFloat floorAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1328
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1329
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1330
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1331
roundedAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1332
    self negative ifTrue:[ ^ (self - 0.5) ceilingAsFloat ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1333
    ^ (self + 0.5) floorAsFloat
7398
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  1334
!
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  1335
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  1336
truncatedAsFloat
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  1337
    ^ self coerce:(self truncated).
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  1338
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  1339
    "
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  1340
     0.4 asLongFloat truncatedAsFloat
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  1341
    "
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1342
! !
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1343
8395
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  1344
!LimitedPrecisionReal methodsFor:'visiting'!
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  1345
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  1346
acceptVisitor:aVisitor with:aParameter
16731
03aa0206cdda comment/format only
Claus Gittinger <cg@exept.de>
parents: 16663
diff changeset
  1347
    "dispatch for visitor pattern; send #visitFloat:with: to aVisitor."
8395
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  1348
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  1349
    ^ aVisitor visitFloat:self with:aParameter
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  1350
! !
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  1351
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1352
!LimitedPrecisionReal class methodsFor:'documentation'!
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1353
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1354
version
17221
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1355
    ^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.81 2014-12-16 12:20:28 stefan Exp $'
12120
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
  1356
!
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
  1357
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
  1358
version_CVS
17221
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1359
    ^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.81 2014-12-16 12:20:28 stefan Exp $'
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1360
! !
6890
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1361
16405
0d357280097a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 12761
diff changeset
  1362
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
  1363
LimitedPrecisionReal initialize!