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