LimitedPrecisionReal.st
author Stefan Vogel <sv@exept.de>
Tue, 28 Apr 2020 16:22:44 +0200
changeset 25376 88a3329875ba
parent 25068 54fe36ff8729
permissions -rw-r--r--
#REFACTORING by stefan class: MethodDictionary class removed: #newWithCapacity: moved to superclass
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
21861
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
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
21896
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
diff changeset
    19
	instanceVariableNames:''
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
diff changeset
    20
	classVariableNames:''
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
diff changeset
    21
	poolDictionaries:''
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
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
21861
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
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.
22288
5a955973c6a9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22212
diff changeset
    78
    You may never ever use them when exact results are neeed (i.e. when computing money!!)
21811
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,
22288
5a955973c6a9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22212
diff changeset
   107
    however, the downside is that CPU-architects did not agree on a common format and precision: 
5a955973c6a9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22212
diff changeset
   108
    some use 80 bits, 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
        
22288
5a955973c6a9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22212
diff changeset
   156
        '%60.58f' printf:{ 1 asQDouble exp }    -> '2.71828182845904523536028747135266249775724709369995957496698'   (>200 bits)
21842
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.
22787
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   170
    Take a look at FloatArray, DoubleArray, HalfFloatArray etc.
21771
2264c7d8e15b #OTHER by mawalch
mawalch
parents: 21270
diff changeset
   171
21811
0370229cfed8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21807
diff changeset
   172
    
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   173
    Comparing Floats:
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   174
    =================
22787
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   175
    Due to rounding errors (usually on the last bit(s)), you shalt not compare two floating point numbers
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   176
    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
   177
    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
   178
    Usually, the print functions take this into consideration and return a (faked) '0.1'.
22787
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   179
    However, this half bit error may accumulate, for example, when multiplying that by 0.1 then by 100, 
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   180
    the error may get large enough to be no longer pushed under the rug by the print function, 
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   181
    and you will get '0.9999999999999' from it.
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   182
22787
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   183
    Also, comparing against a proper 1.0 (which is representable as an exact power of 2), 
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   184
    you will get a false result.
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   185
    i.e. (0.1 * 0.1 * 100 ~= 1.0) and (0.1 * 0.1 * 100 - 1.0) ~= 0.0
21771
2264c7d8e15b #OTHER by mawalch
mawalch
parents: 21270
diff changeset
   186
    This often confuses non-computer scientists (and occasionally even some of those).
22787
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   187
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   188
    For this, you should always provide an epsilon value, when comparing two non-integer numbers. 
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   189
    The epsilon value is the distance you accept two number to be apart to be still considered equal. 
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   190
    Effectively the epsilon says are those nearer than this epsilon?.
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   191
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   192
    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
   193
    and get a reasonable answer for big numbers. But what if we compare two tiny numbers?
22787
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   194
    Then a reasonable epsilon must also be much smaller!!
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   195
17206
83666440217a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16781
diff changeset
   196
    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
   197
    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
   198
    but instead the number of distinct floating point numbers that the two compared floats may be apart.
22787
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   199
    That is: the number of actually representable numbers between those two. 
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   200
    Effectively, that is the difference between the two mantissas, 
b4f6d9a8bf5e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22786
diff changeset
   201
    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
   202
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1201
diff changeset
   203
    [author:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1201
diff changeset
   204
        Claus Gittinger
1893
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
   205
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
   206
    [see also:]
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
   207
        Fraction FixedPoint
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
   208
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
   209
! !
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   210
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   211
!LimitedPrecisionReal class methodsFor:'instance creation'!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   212
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   213
fromInteger:anInteger
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   214
    "return a float with anInteger's value.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   215
     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
   216
     with a large integer 
0370229cfed8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21807
diff changeset
   217
     (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
   218
     (see Float decimalPrecision, LongFloat decimalPrecision."
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   219
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   220
    |newFloat sign absVal completeAbsVal delta mask|
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   221
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   222
    newFloat := self zero.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   223
    sign := anInteger sign.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   224
    (sign ~~ 0) ifTrue:[
24289
59df64616ff0 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24283
diff changeset
   225
        "be careful to round to nearest even floating point value -
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   226
         thanks to Nicolas Cellier for this code"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   227
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   228
        absVal := anInteger abs.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   229
        delta := absVal highBit - self precision.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   230
        delta > 0 ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   231
            completeAbsVal := absVal.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   232
            "eliminate insignificant/trailing bits"
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
   233
            absVal := absVal rightShift: delta.
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   234
            "inexact := trailingBits ~= 0.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   235
             Round to nearest even"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   236
            (completeAbsVal bitAt:delta) ~= 0 ifTrue:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   237
                mask := (1 bitShift:delta-1) - 1.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   238
                ((completeAbsVal bitAnd:mask) ~= 0 or:[absVal odd]) ifTrue:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   239
                    absVal := absVal + 1
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   240
                ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   241
            ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   242
        ] ifFalse: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   243
            delta := 0
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   244
        ].
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   245
        absVal digitLength to:1 by:-1 do:[:i |
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   246
            newFloat := (newFloat * 256) + (absVal digitByteAt:i).
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   247
        ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   248
        delta ~~ 0 ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   249
            newFloat := newFloat timesTwoPower:delta.
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   250
        ].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   251
        (sign < 0) ifTrue:[
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   252
            newFloat := newFloat negated
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   253
        ]
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   254
    ].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   255
    ^ newFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   256
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   257
7447
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
     ShortFloat fromInteger:2
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   260
     12345678901234567890 asShortFloat            
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   261
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   262
     1234567890 asFloat                     
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   263
     1234567890 asFloat asInteger                    
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   264
     -1234567890 asFloat asInteger                    
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   265
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   266
     12345678901234567890 asFloat storeString            
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   267
     12345678901234567890 asFloat asInteger   
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   268
     -12345678901234567890 asFloat asInteger   
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   269
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   270
     12345678901234567890 asLongFloat           
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   271
     12345678901234567890 asLongFloat asInteger 
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   272
     -12345678901234567890 asLongFloat asInteger 
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   273
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   274
     123456789012345678901234567890 asLongFloat           
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   275
     123456789012345678901234567890 asLongFloat asInteger  
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   276
     -123456789012345678901234567890 asLongFloat asInteger  
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   277
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   278
     1234567890123456789012345678901234567890 asLongFloat           
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   279
     1234567890123456789012345678901234567890 asLongFloat asInteger  
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   280
     -1234567890123456789012345678901234567890 asLongFloat asInteger
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   281
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   282
     'this test is on 65 bits'.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   283
     self assert: 16r1FFFFFFFFFFFF0801 asDouble ~= 16r1FFFFFFFFFFFF0800 asDouble.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   284
     'this test is on 64 bits'.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   285
     self assert: 16r1FFFFFFFFFFFF0802 asDouble ~= 16r1FFFFFFFFFFFF0800 asDouble.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   286
     'nearest even is upper'.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   287
     self assert: 16r1FFFFFFFFFFF1F800 asDouble = 16r1FFFFFFFFFFF20000 asDouble.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   288
     'nearest even is lower'.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   289
     self assert: 16r1FFFFFFFFFFFF0800 asDouble = 16r1FFFFFFFFFFFF0000 asDouble.
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   290
    "
21811
0370229cfed8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21807
diff changeset
   291
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
   292
    "Modified: / 25-08-2017 / 12:34:44 / cg"
24289
59df64616ff0 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24283
diff changeset
   293
    "Modified (format): / 08-06-2019 / 02:17:43 / Claus Gittinger"
7447
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
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   296
fromLimitedPrecisionReal:anLPReal
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   297
    "return a float with anLPReals value.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   298
     You might loose bits when doing this.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   299
     Slow fallback."
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|
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   302
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   303
    anLPReal isFinite ifFalse:[
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   304
        anLPReal isNaN ifTrue:[^ self NaN].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   305
        anLPReal negative ifTrue:[^ self negativeInfinity].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   306
        ^ self infinity
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   307
    ].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   308
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   309
    fract := anLPReal asTrueFraction.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   310
    ^ (self fromInteger:fract numerator) / (self fromInteger:fract denominator)
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   311
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   312
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   313
fromNumerator:numerator denominator:denominator
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   314
    "Create a limited precision real from a Rational.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   315
     This version will answer the nearest flotaing point value,
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   316
     according to IEEE 754 round to nearest even default mode"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   317
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   318
    |a b q r exponent floatExponent n ha hb hq q1 eMin precision nSubHq|
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   319
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   320
    a := numerator abs.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   321
    b := denominator abs.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   322
    ha := a highBit.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   323
    hb := b highBit.    
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   324
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   325
    precision := self precision.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   326
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   327
    "If both numerator and denominator are represented exactly in floating point number,
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   328
     then fastest thing to do is to use hardwired float division"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   329
    (ha <= precision and:[hb <= precision]) ifTrue: [
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   330
        ^ (self coerce:numerator) / (self coerce:denominator)
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   331
    ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   332
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   333
    "Try and obtain a mantissa with 1 bit excess precision.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   334
     First guess is rough, we might get one more bit or one less"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   335
    exponent := ha - hb - precision - 1.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   336
    exponent > 0
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   337
        ifTrue: [b := b bitShift: exponent]
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
   338
        ifFalse: [a := a rightShift: exponent].
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   339
    q := a quo: b.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   340
    r := a - (q * b).
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   341
    hq := q highBit.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   342
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   343
    "check for gradual underflow, in which case we should use less bits"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   344
    floatExponent := exponent + hq - 1.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   345
    eMin := 2 - (1 bitShift: (self numBitsInExponent - 1)).
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   346
    n := floatExponent >= eMin
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   347
        ifTrue: [precision + 1]
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   348
        ifFalse: [precision + 1 + floatExponent - eMin].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   349
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   350
    nSubHq := n - hq.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   351
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   352
    nSubHq < 0 ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   353
        exponent := exponent + hq - n.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   354
        r := (q bitAnd: (1 bitShift:nSubHq) - 1) * b + r.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   355
        q := q bitShift:nSubHq
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   356
    ] ifFalse:[nSubHq > 0 ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   357
        exponent := exponent + hq - n.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   358
        q1 := (r bitShift:nSubHq) quo: b.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   359
        q := (q bitShift:nSubHq) bitAnd: q1.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   360
        r := r - (q1 * b)
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   361
    ]].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   362
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   363
    "check if we should round upward.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   364
     The case of exact half (q bitAnd: 1) isZero not & (r isZero)
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   365
     will be handled by self fromInteger: conversion"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   366
    ((q bitAnd:1) ~~ 0 and:[r ~~ 0]) ifTrue:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   367
        q := q + 1
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   368
    ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   369
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   370
    (numerator negative xor:denominator negative) ifTrue: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   371
        q := q negated.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   372
    ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   373
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   374
    ^ (self coerce:q) timesTwoPower:exponent
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   375
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   376
    "
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
                Float fromNumerator:12345678901234567890 denominator:987654321
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
        |fraction|
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   384
        fraction := 12345678901234567890//987654321.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   385
        Time millisecondsToRun:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   386
            1000000  timesRepeat:[
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   387
                fraction asFloat
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   388
            ].
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   389
        ]
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   390
    "
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
   391
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
   392
    "Modified: / 25-08-2017 / 12:34:55 / cg"
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   393
!
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   394
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   395
new:aNumber
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   396
    "catch this message - not allowed for floats/doubles"
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   397
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   398
    self error:'Floats/Doubles cannot be created with new:'
16444
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
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   401
readFrom:aStringOrStream onError:exceptionBlock
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   402
    "read a float from a string"
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
    |num|
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   405
21270
fd671950be41 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20978
diff changeset
   406
    num := super readFrom:aStringOrStream onError:[^ exceptionBlock value].
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   407
    ^ self coerce:num
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   408
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   409
    "
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   410
     Float readFrom:'.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   411
     Float readFrom:'0.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   412
     Float readFrom:'0'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   413
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   414
     ShortFloat readFrom:'.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   415
     ShortFloat readFrom:'0.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   416
     ShortFloat readFrom:'0'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   417
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   418
     LongFloat readFrom:'.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   419
     LongFloat readFrom:'0.1'
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   420
     LongFloat readFrom:'0'
21270
fd671950be41 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20978
diff changeset
   421
fd671950be41 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20978
diff changeset
   422
     LimitedPrecisionReal readFrom:'bla' onError:nil
fd671950be41 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20978
diff changeset
   423
     Float readFrom:'bla' onError:nil
fd671950be41 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20978
diff changeset
   424
     ShortFloat readFrom:'bla' onError:nil
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   425
    "
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   426
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   427
    "Created: / 07-01-1998 / 16:17:19 / cg"
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   428
    "Modified: / 23-11-2010 / 14:35:41 / cg"
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   429
! !
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   430
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   431
!LimitedPrecisionReal class methodsFor:'class initialization'!
5989
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
initialize
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   434
    "initialize ANSI compliant float globals"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   435
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   436
    Smalltalk at:#FloatE put:ShortFloat.
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   437
    Smalltalk at:#FloatD put:Float.
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   438
    Smalltalk at:#FloatQ put:LongFloat.
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   439
21896
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
diff changeset
   440
    "/ not defined in ANSI, but consistent in its naming
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
diff changeset
   441
    Smalltalk at:#FloatQD put:QDouble.
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
diff changeset
   442
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   443
    "
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   444
     self initialize
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   445
    "
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   446
21896
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
diff changeset
   447
    "Created: / 07-09-2001 / 14:02:45 / cg"
9248407959ca #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21869
diff changeset
   448
    "Modified (comment): / 21-06-2017 / 13:59:08 / cg"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   449
! !
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   450
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   451
!LimitedPrecisionReal class methodsFor:'constants & defaults'!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   452
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   453
NaN
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   454
    "return the constant NaN (not a Number) in my representation.
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   455
     Here, based on the assumption that division of zero by zero generates a NaN
25042
a1dcce5c4904 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25024
diff changeset
   456
     (which is defined as such in the IEEE standard).
a1dcce5c4904 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25024
diff changeset
   457
     If a subclass does not, it has to redefine this method and generate a NaN differently"
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   458
18281
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
   459
    |zero|
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
   460
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
   461
    zero := self zero.
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
   462
    ^ zero uncheckedDivide:zero.
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   463
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   464
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   465
      ShortFloat NaN  
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   466
      Float NaN       
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   467
      LongFloat NaN   
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   468
      LargeFloat NaN   
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
   469
      IEEEFloat NaN   
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   470
    "
21825
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   471
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   472
    "Modified (comment): / 16-06-2017 / 11:03:28 / cg"
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   473
    "Modified (comment): / 06-06-2019 / 16:52:43 / Claus Gittinger"
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   474
!
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   475
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   476
computeEpsilon
24437
6af740056b8a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24327
diff changeset
   477
    "compute the maximum relative spacing of instances of mySelf
24960
16f178c7083c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24916
diff changeset
   478
     (i.e. the value-delta of the least significant bit).
16f178c7083c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24916
diff changeset
   479
     See https://en.wikipedia.org/wiki/Machine_epsilon"
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   480
24960
16f178c7083c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24916
diff changeset
   481
    ^ (self coerce:self radix) raisedToInteger:(self precision - 1) negated.
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   482
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   483
    "
24916
c8316eee6b9a #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24705
diff changeset
   484
      Float radix 
21946
3dbb97c34aec #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21896
diff changeset
   485
      Float precision
3dbb97c34aec #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21896
diff changeset
   486
      
24960
16f178c7083c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24916
diff changeset
   487
      ShortFloat computeEpsilon -> 1.192093e-07 
16f178c7083c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24916
diff changeset
   488
      Float computeEpsilon      -> 2.22044604925031E-16
24962
b361d5f42d97 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24961
diff changeset
   489
      LongFloat computeEpsilon  -> 1.084202172485504434E-19
24972
a43ec2d24bef #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24966
diff changeset
   490
      QDouble computeEpsilon    -> 7.77876909732643E-62    
24962
b361d5f42d97 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24961
diff changeset
   491
24916
c8316eee6b9a #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24705
diff changeset
   492
      QuadFloat computeEpsilon   
c8316eee6b9a #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24705
diff changeset
   493
      QuadFloat radix
c8316eee6b9a #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24705
diff changeset
   494
      (QuadFloat coerce:QuadFloat radix)
c8316eee6b9a #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24705
diff changeset
   495
      2 asQuadFloat
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   496
    "
21946
3dbb97c34aec #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21896
diff changeset
   497
3dbb97c34aec #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21896
diff changeset
   498
    "Modified (comment): / 03-07-2017 / 12:12:06 / cg"
22852
403f7356efc1 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22851
diff changeset
   499
    "Modified: / 10-05-2018 / 01:19:25 / stefan"
24437
6af740056b8a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24327
diff changeset
   500
    "Modified (comment): / 19-07-2019 / 17:26:30 / Claus Gittinger"
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   501
!
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
   502
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   503
eBias
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   504
    "Answer the exponent's bias; 
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   505
     that is the offset of the zero exponent when stored.
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   506
     The computation below assumes standard IEEE format"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   507
17655
b8f27b84fda3 class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 17380
diff changeset
   508
    ^ (1 << (self numBitsInExponent-1)) - 1
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   509
17655
b8f27b84fda3 class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 17380
diff changeset
   510
    "
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   511
     Float eBias       -> 1023
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   512
     ShortFloat eBias  -> 127
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   513
     LongFloat eBias   -> 16383
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   514
     QuadFloat eBias   -> 16383
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   515
     OctaFloat eBias   -> 262143
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   516
     QDouble eBias     -> 1023
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   517
    "
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   518
!
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   519
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   520
emax
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   521
    "return the largest exponent.
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   522
     The computation below assumes standard IEEE format"
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   523
25023
668578d60026 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 25015
diff changeset
   524
    ^ (1 << (self numBitsInExponent-1))-1
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   525
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   526
    "
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   527
     Float emax       -> 1023
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   528
     ShortFloat emax  -> 127
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   529
     LongFloat emax   -> 16383
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   530
     QuadFloat emax   -> 16383
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   531
     OctaFloat emax   -> 262143
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   532
     QDouble emax     -> 1023
17655
b8f27b84fda3 class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 17380
diff changeset
   533
    "
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   534
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   535
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   536
emin
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   537
    "return the smallest exponent.
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   538
     The computation below assumes standard IEEE format"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   539
25023
668578d60026 #BUGFIX by exept
Claus Gittinger <cg@exept.de>
parents: 25015
diff changeset
   540
    ^ 2 - (1 << (self numBitsInExponent-1))
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   541
18135
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   542
    "
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   543
     Float emin       -> -1022
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   544
     ShortFloat emin  -> -126
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   545
     LongFloat emin   -> -16382
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   546
     QuadFloat emin   -> -16382
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   547
     OctaFloat emin   -> -262142
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   548
     QDouble emin     -> -1022
18135
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   549
    "
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   550
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   551
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   552
epsilon
21802
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   553
    "return the maximum relative spacing of instances of mySelf
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   554
     (i.e. the value-delta of the least significant bit)"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   555
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   556
    ^ self radix asFloat raisedToInteger:(1 - self precision)
6575
b07cd54c4375 Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents: 6195
diff changeset
   557
b07cd54c4375 Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents: 6195
diff changeset
   558
    "
22851
df11e296e70d #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 22787
diff changeset
   559
     Float epsilon       -> 2.22044604925031E-16
df11e296e70d #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 22787
diff changeset
   560
     ShortFloat epsilon  -> 1.192093e-07
df11e296e70d #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 22787
diff changeset
   561
     LongFloat epsilon   -> 1.084202172485504434E-19
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
   562
     QDouble epsilon     -> 7.778769097326426826491248689356e-62
6575
b07cd54c4375 Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents: 6195
diff changeset
   563
    "
21802
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   564
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   565
    "Modified (comment): / 12-06-2017 / 18:52:31 / cg"
22851
df11e296e70d #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 22787
diff changeset
   566
    "Modified (comment): / 10-05-2018 / 01:08:48 / stefan"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   567
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   568
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   569
fmax
18135
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   570
    "The largest value allowed by instances of this class."
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   571
22499
a3a85bb6afb8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 22305
diff changeset
   572
    |radix radixReal|
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   573
18135
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   574
    radix := self radix.
22499
a3a85bb6afb8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 22305
diff changeset
   575
    radixReal := (self fromInteger: radix).
21807
8f59475d9e48 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 21802
diff changeset
   576
    ^ ((self fromInteger: 1) -
22499
a3a85bb6afb8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 22305
diff changeset
   577
            (radixReal timesTwoPower: self precision negated - 1)) * radix
a3a85bb6afb8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 22305
diff changeset
   578
            * (radixReal timesTwoPower: self emax - 1)
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   579
18135
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   580
    "
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
   581
     Float fmax       -> 1.79769313486232E+308
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
   582
     ShortFloat fmax  -> 3.402823e+38
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
   583
     LongFloat fmax   -> 1.189731495357231765E+4932
21807
8f59475d9e48 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 21802
diff changeset
   584
     QDouble fmax 
18135
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   585
    "
20978
1500cfbc46c8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20058
diff changeset
   586
21807
8f59475d9e48 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 21802
diff changeset
   587
    "Modified (comment): / 14-06-2017 / 19:17:39 / cg"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   588
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   589
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   590
fmin
18135
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   591
    "The smallest value allowed by instances of this class."
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   592
18135
6b108770c16d class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 18127
diff changeset
   593
    ^ self subclassResponsibility
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   594
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   595
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   596
infinity
24304
2fd864309577 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24294
diff changeset
   597
    "return an instance of myself which represents positive infinity (for my instances).
2fd864309577 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24294
diff changeset
   598
     Warning: do not compare equal against infinities;
2fd864309577 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24294
diff changeset
   599
     instead, check using isFinite or isInfinite"
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   600
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   601
    ^ self unity uncheckedDivide:self zero
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   602
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   603
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   604
      ShortFloat infinity  
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   605
      Float infinity       
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   606
      LongFloat infinity   
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   607
      LargeFloat infinity   
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
   608
      IEEEFloat infinity   
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   609
    "
21861
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   610
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   611
    "Modified (comment): / 20-06-2017 / 13:46:52 / cg"
24304
2fd864309577 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24294
diff changeset
   612
    "Modified (comment): / 09-06-2019 / 12:52:52 / Claus Gittinger"
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   613
!
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   614
19359
43b079b6245c #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18854
diff changeset
   615
maxSmallInteger
22610
f449d47f1f5c #DOCUMENTATION by mawalch
mawalch
parents: 22499
diff changeset
   616
    "answer the largest possible SmallInteger value as instance of myself"
19359
43b079b6245c #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18854
diff changeset
   617
43b079b6245c #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18854
diff changeset
   618
    ^ self fromInteger:SmallInteger maxVal.
43b079b6245c #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18854
diff changeset
   619
43b079b6245c #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18854
diff changeset
   620
    "
21861
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   621
     Float maxSmallInteger.
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   622
     LongFloat maxSmallInteger.
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   623
     ShortFloat maxSmallInteger.
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   624
     QDouble maxSmallInteger.
19359
43b079b6245c #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18854
diff changeset
   625
    "
21861
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   626
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   627
    "Modified (comment): / 20-06-2017 / 14:07:27 / cg"
22610
f449d47f1f5c #DOCUMENTATION by mawalch
mawalch
parents: 22499
diff changeset
   628
    "Modified (comment): / 14-03-2018 / 20:11:28 / mawalch"
19359
43b079b6245c #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18854
diff changeset
   629
!
43b079b6245c #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18854
diff changeset
   630
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   631
negativeInfinity
24304
2fd864309577 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24294
diff changeset
   632
    "return an instance of myself which represents negative infinity (for my instances).
2fd864309577 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24294
diff changeset
   633
     Warning: do not compare equal against infinities;
2fd864309577 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24294
diff changeset
   634
     instead, check using isFinite or isInfinite"
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   635
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   636
    ^ self unity negated uncheckedDivide:self zero
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   637
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   638
    "
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   639
      ShortFloat negativeInfinity   
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   640
      Float negativeInfinity       
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   641
      LongFloat negativeInfinity   
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
   642
      LargeFloat negativeInfinity   
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
   643
      QDouble negativeInfinity   
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
   644
      IEEEFloat negativeInfinity   
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
   645
    "
21861
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   646
ea0c1b915dab #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21860
diff changeset
   647
    "Modified (comment): / 20-06-2017 / 13:46:42 / cg"
24304
2fd864309577 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24294
diff changeset
   648
    "Modified (comment): / 09-06-2019 / 12:55:35 / Claus Gittinger"
7356
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
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   651
!LimitedPrecisionReal class methodsFor:'queries'!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   652
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   653
decimalEmax
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   654
    "Answer how many digits of accuracy this class supports"
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   655
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   656
    ^ (self emax / (10.0 log:self radix))
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   657
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   658
    "
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   659
     ShortFloat emax   
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   660
     ShortFloat decimalEmax  
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   661
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   662
     Float emax  
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   663
     Float emin
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   664
     Float decimalEmax  
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   665
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   666
     LongFloat emax        
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   667
     LongFloat emin        
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   668
     LongFloat decimalEmax  
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   669
    "
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   670
!
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   671
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   672
decimalPrecision
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   673
    "return the number of valid decimal digits"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   674
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   675
    ^ (self precision / (10.0 log:self radix)) 
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   676
        floor   "use floor, since the last decimal digit is usually wrong"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   677
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   678
    "
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   679
     HalfFloat decimalPrecision  -> 3
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   680
     ShortFloat decimalPrecision -> 7
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   681
     Float decimalPrecision      -> 16 
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   682
     LongFloat decimalPrecision  -> 19
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   683
     QuadFloat decimalPrecision  -> 34
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   684
     OctaFloat decimalPrecision  -> 71
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   685
     QDouble decimalPrecision    -> 61
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   686
    "
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   687
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   688
    "Modified (comment): / 06-06-2019 / 13:21:04 / Claus Gittinger"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   689
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   690
21825
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   691
defaultPrintPrecision
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   692
    "return the number of decimal digits printed by default"
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   693
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   694
    ^ 6
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   695
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   696
    "
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   697
     ShortFloat defaultPrintPrecision -> 5 
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   698
     Float defaultPrintPrecision      -> 6 
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   699
     LongFloat defaultPrintPrecision  -> 8 
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   700
     QDouble defaultPrintPrecision    -> 10
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   701
     LargeFloat defaultPrintPrecision -> 12
21825
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   702
    "
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   703
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   704
    "Created: / 17-06-2017 / 02:56:43 / cg"
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   705
!
4f3eeda86497 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21811
diff changeset
   706
12120
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   707
denormalized
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   708
    "Return whether the instances of this class can
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   709
     represent values in denormalized format."
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   710
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   711
    ^ true
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   712
!
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
   713
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   714
exactDecimalPrecision
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   715
    "return the exact number of decimal digits"
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   716
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   717
    ^ ((self precision) / (10.0 log:self radix))
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   718
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   719
    "
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   720
     HalfFloat exactDecimalPrecision  -> 3.612359947967774002
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   721
     ShortFloat exactDecimalPrecision -> 7.224719895935548004
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   722
     Float exactDecimalPrecision      -> 15.95458977019100184      
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   723
     LongFloat exactDecimalPrecision  -> 19.26591972249479468
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   724
     QuadFloat exactDecimalPrecision  -> 34.01638951002987185
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   725
     OctaFloat exactDecimalPrecision  -> 71.34410897236353654
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
   726
     QDouble exactDecimalPrecision    -> 61.41011911545215804
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   727
    "
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   728
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   729
    "Created: / 06-06-2019 / 13:19:17 / Claus Gittinger"
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   730
!
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   731
20058
049707a0931d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19359
diff changeset
   732
hasSharedInstances
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   733
    "return true if this class can share instances when stored binary, 
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   734
     that is, instances with the same value can be stored by reference.
20058
049707a0931d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19359
diff changeset
   735
     Although not really shared, floats should be treated
049707a0931d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19359
diff changeset
   736
     so, to be independent of the implementation of the arithmetic methods."
049707a0931d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19359
diff changeset
   737
049707a0931d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19359
diff changeset
   738
    ^ true
049707a0931d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19359
diff changeset
   739
!
049707a0931d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19359
diff changeset
   740
8892
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   741
isAbstract
11225
32a2c7f432eb comment
Claus Gittinger <cg@exept.de>
parents: 10946
diff changeset
   742
    "Return if this class is an abstract class.
32a2c7f432eb comment
Claus Gittinger <cg@exept.de>
parents: 10946
diff changeset
   743
     True is returned for LimitedPrecisionReal here; false for subclasses."
32a2c7f432eb comment
Claus Gittinger <cg@exept.de>
parents: 10946
diff changeset
   744
8892
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   745
    ^ self == LimitedPrecisionReal
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   746
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   747
    "
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   748
     1.0 class isAbstract
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   749
    "
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   750
!
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8422
diff changeset
   751
17221
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   752
isIEEEFormat
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   753
    "return true, if this machine represents floats in IEEE format.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   754
     Currently, no support is provided for non-ieee machines
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   755
     to convert their floats into this (which is only relevant,
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   756
     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
   757
     machine).
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   758
     Machines with non-IEEE format are VAXen and IBM370-type systems
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   759
     (among others). Today, every system uses IEEE format floats."
17221
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   760
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   761
%{
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   762
#ifdef __s390__
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   763
    RETURN(false);
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   764
#endif
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   765
%}.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   766
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   767
    ^ true "/ this may be a lie
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   768
!
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   769
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   770
numBitsInExponent
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   771
    "return the number of bits in the exponent"
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
    ^ self subclassResponsibility
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   774
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   775
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   776
numBitsInIntegerPart
16781
4619d7da013a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16731
diff changeset
   777
    "answer the number of bits in the integer part of the mantissa.
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   778
     I.e. 0 is returned if there is a hidden bit, 1 if not.
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
   779
     Most floating point formats are normalized to get rid of the extra bit."
16781
4619d7da013a class: LimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 16731
diff changeset
   780
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   781
    ^ 0
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
   782
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
   783
    "Modified (comment): / 28-05-2019 / 08:58:11 / Claus Gittinger"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   784
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   785
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   786
numBitsInMantissa
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   787
    "return the number of bits in the mantissa (the significant)
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   788
     Typically the precision is 1 more than the significant due to the hidden bit"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   789
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   790
    ^ self subclassResponsibility
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   791
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   792
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   793
precision
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   794
    "return the number of valid mantissa bits"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   795
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   796
    ^ self numBitsInMantissa + 1 - self numBitsInIntegerPart 
21802
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   797
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   798
    "
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   799
      HalfFloatArray precision  
21802
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   800
      ShortFloat precision  
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   801
      Float precision       
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   802
      LongFloat precision   
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   803
      QDouble precision  
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   804
    "
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   805
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
   806
    "Modified (comment): / 12-06-2017 / 18:47:54 / cg"
24264
45aec98294cc #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 24229
diff changeset
   807
    "Modified (comment): / 06-06-2019 / 13:25:00 / Claus Gittinger"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   808
!
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   809
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   810
radix
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   811
    "answer the radix of my instance's exponent"
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   812
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   813
    self subclassResponsibility
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   814
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   815
    "Created: / 7.9.2001 / 14:08:20 / cg"
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   816
! !
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
   817
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   818
!LimitedPrecisionReal methodsFor:'accessing'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   819
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   820
at:index
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   821
    "redefined to prevent access to individual bytes in a real."
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   822
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   823
    self error:'not allowed for floats/doubles'
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   824
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   825
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   826
at:index put:aValue
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   827
    "redefined to prevent access to individual bytes in a real"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   828
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   829
    self error:'not allowed for floats/doubles'
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   830
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   831
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   832
!LimitedPrecisionReal methodsFor:'arithmetic'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   833
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   834
* aNumber
11727
e0ffd06e50fd comment
Claus Gittinger <cg@exept.de>
parents: 11516
diff changeset
   835
    "return the product of the receiver and the argument."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   836
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   837
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   838
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   839
"/    ^ aNumber productFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   840
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   841
    ^ aNumber productFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   842
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   843
    "Modified: 17.4.1996 / 12:35:36 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   844
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   845
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   846
+ aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   847
    "return the sum of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   848
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   849
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   850
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   851
"/    ^ aNumber sumFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   852
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   853
    ^ aNumber sumFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   854
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   855
    "Modified: 17.4.1996 / 12:35:55 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   856
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   857
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   858
- aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   859
    "return the difference of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   860
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   861
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   862
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   863
"/    ^ aNumber differenceFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   864
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   865
    ^ aNumber differenceFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   866
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   867
    "Modified: 17.4.1996 / 12:36:07 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   868
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   869
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   870
/ aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   871
    "return the quotient of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   872
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   873
    ((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[
7141
033f2c26d8e6 replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents: 6890
diff changeset
   874
        ^ ZeroDivide raiseRequestWith:thisContext.
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   875
    ].
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   876
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   877
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   878
"/    ^ aNumber quotientFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   879
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   880
    ^ aNumber quotientFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   881
4455
2d31d0d986be Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents: 3898
diff changeset
   882
    "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
   883
    "Modified: / 26.7.1999 / 10:46:11 / stefan"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   884
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   885
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   886
// aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   887
    "return the integer quotient of dividing the receiver by aNumber with
1882
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   888
     truncation towards negative infinity."
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   889
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   890
    ^ (self / aNumber) floor asInteger
1882
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   891
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   892
    "Modified: 5.11.1996 / 11:45:37 / cg"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   893
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   894
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   895
ceiling
7398
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
   896
    ^ self asTrueFraction ceiling
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   897
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   898
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
   899
floor
7398
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
   900
    ^ self asTrueFraction floor
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   901
!
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   902
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   903
timesTwoPower:anInteger
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   904
    "multiply self by a power of two.
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   905
     Implementation takes care of preserving class and avoiding overflow/underflow
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   906
     Thanks to Nicolas Cellier for this code"
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   907
12761
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   908
    |half two h1 h2|
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   909
16444
dcf226d85a5a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 16405
diff changeset
   910
    two := self coerce:2.0.
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   911
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
   912
    anInteger abs highBit >= (self numBitsInExponent - 2) ifTrue:[
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   913
        half := anInteger // 2.
12761
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   914
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   915
        anInteger even ifTrue:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   916
            "/ halves are equal
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   917
            h2 := h1 := (two raisedToInteger: half).
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   918
        ] ifFalse:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   919
            h1 := (two raisedToInteger: half).
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   920
            anInteger > 0 ifTrue:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   921
                h2 := h1 * two.
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   922
            ] ifFalse:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   923
                h2 := (two raisedToInteger: anInteger - half).
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   924
            ].
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   925
        ].
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   926
        ^ (self * h1) * h2
9406
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   927
    ] ifFalse: [
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   928
        ^ self * (two raisedToInteger: anInteger).
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   929
    ]
Stefan Vogel <sv@exept.de>
parents: 9123
diff changeset
   930
12758
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   931
    "
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   932
     (1 asShortFloat timesTwoPower: 3) class = ShortFloat.
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   933
     (1 asLongFloat timesTwoPower: 1024).
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   934
     (1 asFloat timesTwoPower: -1024) timesTwoPower: 1024.
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
   935
     (1 asLongFloat timesTwoPower: -1024) timesTwoPower: 1024.
12761
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   936
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   937
     (2.0 asShortFloat timesTwoPower: -150) timesTwoPower: 150    
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   938
     (2.0 asLongFloat timesTwoPower: -150) timesTwoPower: 150   
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   939
     (2.0 asFloat timesTwoPower: -150) timesTwoPower: 150       
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   940
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   941
     (2.0 asShortFloat timesTwoPower: -149) timesTwoPower: 149  
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   942
     (2.0 asLongFloat timesTwoPower: -149) timesTwoPower: 149    
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   943
     (2.0 asFloat timesTwoPower: -149) timesTwoPower: 149        
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   944
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   945
     Time millisecondsToRun:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   946
        1000000 timesRepeat:[
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   947
            (2.0 timesTwoPower: 150)
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   948
        ]
56be28f201c4 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12758
diff changeset
   949
     ]  
12758
347c9501543c comments
Claus Gittinger <cg@exept.de>
parents: 12120
diff changeset
   950
    "
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
   951
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
   952
    "Modified (comment): / 28-05-2019 / 09:08:25 / Claus Gittinger"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   953
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   954
17221
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   955
!LimitedPrecisionReal methodsFor:'bytes access'!
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   956
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   957
digitBytes
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
   958
    "answer the float's digit bytes imnIEEE format.
17221
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   959
     Use the native machine byte ordering."
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   960
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   961
    |sz "{ Class:SmallInteger }"
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   962
     bytes|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   963
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   964
    sz := self basicSize.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   965
    bytes := ByteArray new:sz.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   966
    1 to:sz do:[:i|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   967
        bytes at:i put:(self basicAt:i).
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   968
    ].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   969
    ^ bytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   970
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   971
    "
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   972
        Float pi digitBytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   973
        ShortFloat pi digitBytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   974
    "
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   975
!
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   976
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   977
digitBytesMSB:msb
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   978
    "answer the float's digit bytes im IEEE format.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   979
     If msb == true, use MSB byte order, otherwise LSB byte order."
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   980
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   981
    |sz "{ Class:SmallInteger }"
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   982
     bytes|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   983
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   984
    self class isIEEEFormat ifFalse:[self error:'unsupported operation'].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   985
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   986
    sz := self basicSize.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   987
    bytes := ByteArray new:sz.
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   988
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   989
    (UninterpretedBytes isBigEndian == msb) ifTrue:[
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   990
        1 to:sz do:[:i|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   991
            bytes at:i put:(self basicAt:i).
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   992
        ].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   993
    ] ifFalse:[
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   994
        "reverse the bytes"
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   995
        1 to:sz do:[:i|
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   996
            bytes at:sz-i+1 put:(self basicAt:i).
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   997
        ].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   998
    ].
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
   999
    ^ bytes
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1000
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1001
    "
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1002
        Float pi digitBytesMSB:false
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1003
        Float pi digitBytesMSB:true
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1004
        ShortFloat pi digitBytesMSB:false
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1005
        ShortFloat pi digitBytesMSB:true
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1006
    "
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1007
! !
1ee235ddab7f class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17206
diff changeset
  1008
5238
f7a816a660a3 categories
Claus Gittinger <cg@exept.de>
parents: 4615
diff changeset
  1009
!LimitedPrecisionReal methodsFor:'coercing & converting'!
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1010
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1011
asFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1012
    ^ Float fromLimitedPrecisionReal:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1013
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1014
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1015
asFraction
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1016
    "Answer a rational number (Integer or Fraction) representing the receiver.
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1017
     This conversion uses the continued fraction method to approximate 
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1018
     a floating point number.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1019
     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
  1020
     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
  1021
    "
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1022
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1023
    |num1 denom1 num2 denom2 int frac newD temp limit|
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1024
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1025
    self isFinite ifFalse:[
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1026
"/        ^ self asMetaNumber.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1027
        ^ self class
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1028
            raise:#domainErrorSignal
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1029
            receiver:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1030
            selector:#asFraction
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1031
            arguments:#()
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1032
            errorString:'Cannot represent non-finite number as fraction'
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1033
    ].
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1034
7394
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
  1035
    limit := 10 raisedToInteger:self defaultNumberOfDigits.
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1036
    num1 := self truncated. 
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1037
    denom1 := 1.            "The first of two alternating denominators"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1038
    num2 := 1.              "The second numerator"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1039
    denom2 := 0.            "The second denominator--will update"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1040
    int := num1.            "The integer part of self"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1041
    frac := self fractionPart.             
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1042
    [frac = 0] whileFalse:[                
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1043
        newD := 1.0 / frac.                   
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1044
        int := newD truncated.        
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1045
        frac := newD fractionPart.      "save the fractional part for next time"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1046
        temp := num2.                   "old numerator and save it"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1047
        num2 := num1.                         
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1048
        num1 := num1 * int + temp.      "Update first numerator"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1049
        temp := denom2.                 "old denominator and save it"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1050
        denom2 := denom1.                    
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1051
        denom1 := int * denom1 + temp.  "Update first denominator"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1052
        limit < denom1 ifTrue:[
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1053
            "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
  1054
            num2 = 0.0 ifTrue:[
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1055
                "Is second denominator 0?"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1056
                ^ Fraction numerator:num1 denominator:denom1
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1057
            ].
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1058
            ^ Fraction numerator:num2 denominator:denom2
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1059
        ]
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1060
    ].
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1061
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1062
    "If fractional part is zero, return the first ratio"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1063
    denom1 = 1 ifTrue:[
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1064
        "Am i really an Integer?"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1065
        ^ num1 "Yes, return Integer result"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1066
    ].
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1067
    "Otherwise return Fraction result"
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1068
    ^ Fraction numerator:num1 denominator:denom1
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1069
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1070
    "
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1071
     1.1 asFraction      
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1072
     1.2 asFraction      
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1073
     0.3 asFraction   
7394
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
  1074
     0.5 asFraction  
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
  1075
     (1/5) asFloat asFraction  
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1076
     (1/8) asFloat asFraction  
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1077
     (1/13) asFloat asFraction 
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
  1078
     (1/10) asFloat asFraction 
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
  1079
     (1/10) asFloat asTrueFraction asFixedPoint scale:20 
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
  1080
     3.14159 asFixedPoint scale:20        
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
  1081
     3.14159 storeString       
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
  1082
     3.14159 asFraction asFloat storeString       
7394
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
  1083
     1.3 asFraction            
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
  1084
     1.0 asFraction            
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
  1085
     1E6 asFraction            
c18ce6a5a437 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7390
diff changeset
  1086
     1E-6 asFraction            
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1087
    "
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2388
diff changeset
  1088
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1089
    "Modified: / 25.10.1997 / 16:41:19 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1090
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1091
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1092
asInteger
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1093
    "return an integer with same value - might truncate"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1094
3185
1c7bd090a822 removed unused local vars
Claus Gittinger <cg@exept.de>
parents: 3148
diff changeset
  1095
    |max maxF|
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1096
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1097
    self isFinite ifFalse:[
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1098
"/        ^ self asMetaNumber.
3395
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3225
diff changeset
  1099
        ^ self class
7418
521174de046d Nan asInteger raises DomainError
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  1100
            raise:#domainErrorSignal
3395
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3225
diff changeset
  1101
            receiver:self
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3225
diff changeset
  1102
            selector:#asInteger
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6185
diff changeset
  1103
            arguments:#()
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1104
            errorString:'Cannot represent non-finite number as integer'
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1105
    ].
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1106
4615
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1107
    self abs < 2e16 ifTrue:[
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1108
        "/ NOTICE: this must be redefined in float
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1109
        "/ subclasses to handle the smallinteger range;
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1110
        "/ i.e. this may only be invoked for reals
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1111
        "/ which are NOT within the smallInt range.
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1112
        "/ otherwise, endless recursion is the consequence.
3426
a648de89ac72 comment
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1113
4615
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1114
        max := SmallInteger maxVal // 2 + 1.
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1115
        maxF := max asFloat.
3426
a648de89ac72 comment
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1116
4615
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1117
        ^ (self quo:maxF) * max + (self rem:maxF) truncated
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1118
    ].
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1119
    ^ self asTrueFraction
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1120
3137
f334b17f0347 bif speedup in asInteger with large floats
ca
parents: 3060
diff changeset
  1121
    "
4615
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1122
     12345.0 asInteger     
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1123
     1e15 asInteger        
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1124
     1e33 asInteger asFloat
e480d1e6090f largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents: 4455
diff changeset
  1125
     1e303 asInteger asFloat
3137
f334b17f0347 bif speedup in asInteger with large floats
ca
parents: 3060
diff changeset
  1126
    "
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1127
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6185
diff changeset
  1128
    "Modified: / 16.11.2001 / 14:15:33 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1129
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1130
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1131
asLargeFloat
23054
2f647561edc5 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22852
diff changeset
  1132
    "return a large float with (approximately) my value.
2f647561edc5 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22852
diff changeset
  1133
     If the largeFloat class is not present, a regular float is returned"
21802
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
  1134
23056
83d82a00d252 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23054
diff changeset
  1135
    ^ (LargeFloat ? LongFloat ? Float) fromLimitedPrecisionReal:self
21802
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
  1136
89005eece5f4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21771
diff changeset
  1137
    "Modified (comment): / 12-06-2017 / 20:57:05 / cg"
23056
83d82a00d252 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23054
diff changeset
  1138
    "Modified: / 07-06-2018 / 16:28:03 / Claus Gittinger"
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1139
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1140
24155
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1141
asLargeFloatPrecision:n
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1142
    "return a large float with (approximately) my value.
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1143
     If the largeFloat class is not present, a regular float is returned"
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1144
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1145
    LargeFloat notNil ifTrue:[
24156
6ce252f37307 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24155
diff changeset
  1146
        ^ (LargeFloat fromLimitedPrecisionReal:self) precision:n
24155
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1147
    ].
24156
6ce252f37307 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24155
diff changeset
  1148
    ^ (LongFloat ? Float) fromLimitedPrecisionReal:self
6ce252f37307 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24155
diff changeset
  1149
6ce252f37307 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24155
diff changeset
  1150
    "
6ce252f37307 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24155
diff changeset
  1151
     1.0 asLargeFloatPrecision:10
6ce252f37307 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24155
diff changeset
  1152
    "
24155
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1153
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1154
    "Created: / 26-05-2019 / 04:01:22 / Claus Gittinger"
24156
6ce252f37307 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24155
diff changeset
  1155
    "Modified (format): / 26-05-2019 / 11:02:53 / Claus Gittinger"
24155
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1156
!
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1157
7378
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1158
asLimitedPrecisionReal
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1159
    "return a float of any precision with same value"
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1160
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1161
   ^ self
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1162
!
e71a28f6b712 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1163
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1164
asLongFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1165
    ^ LongFloat fromLimitedPrecisionReal:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1166
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1167
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1168
asRational
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1169
    "Answer a Rational number--Integer or Fraction--representing the receiver.
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1170
     Same as asFraction fro st-80 compatibility."
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1171
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1172
    ^ self asFraction
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1173
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1174
    "
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1175
     1.1 asRational      
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1176
     1.2 asRational      
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1177
     0.3 asRational   
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1178
     0.5 asRational 
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1179
     (1/5) asFloat asRational
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1180
     (1/8) asFloat asRational  
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1181
     (1/13) asFloat asRational 
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1182
     3.14159 asRational        
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1183
     3.14159 asRational asFloat       
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1184
     1.3 asRational  
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1185
     1.0 asRational  
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1186
    "
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1187
!
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1188
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1189
asShortFloat
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1190
    ^ ShortFloat fromLimitedPrecisionReal:self
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1191
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1192
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1193
asTrueFraction
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1194
    "Answer a fraction or integer that EXACTLY represents self,
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1195
     an any-precision IEEE floating point number, consisting of:
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1196
        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
  1197
        optional numExtraBits between mantissa and exponent (normalized flag for ext-real)
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1198
        numExponentBits bits of 2s complement exponent
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1199
        1 sign bit.
24291
cdddcb8a204f #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24289
diff changeset
  1200
     Taken from Float's asTrueFraction"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1201
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1202
    |shifty sign expPart exp fraction fractionPart result zeroBitsCount
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1203
     numBytes numBits numBitsInMantissa maskMantissa numBitsInExponent maskExponent
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1204
     biasExponent numIntegerBits|
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1205
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1206
    self isFinite ifFalse:[
18281
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1207
        ^ self asMetaNumber
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1208
"/        ^ self class
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1209
"/            raise:#domainErrorSignal
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1210
"/            receiver:self
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1211
"/            selector:#asTrueFraction
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1212
"/            arguments:#()
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1213
"/            errorString:'Cannot represent non-finite float as a fraction'.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1214
    ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1215
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1216
    "Extract the bits of an IEEE anySize float "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1217
    numBytes := self basicSize.
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1218
    numIntegerBits := self numBitsInIntegerPart.
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1219
    numBitsInMantissa := self numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1.
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1220
    numBitsInExponent := self numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1221
    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
  1222
    biasExponent := maskExponent bitShift:-1.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1223
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1224
    shifty := LargeInteger basicNew numberOfDigits:numBytes.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1225
    UninterpretedBytes isBigEndian ifTrue:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1226
        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
  1227
    ] ifFalse:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1228
        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
  1229
    ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1230
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1231
    " Extract the sign and the biased exponent "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1232
    sign := (shifty bitAt:numBits+1) == 0 ifTrue: [1] ifFalse: [-1].
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1233
    expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1234
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1235
    " 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
  1236
    fractionPart := shifty bitAnd:maskMantissa.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1237
    ( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0  ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1238
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1239
    numIntegerBits == 0 ifTrue:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1240
        " 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
  1241
        fraction := fractionPart bitOr: (maskMantissa + 1).
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1242
    ] ifFalse:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1243
        fraction := fractionPart.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1244
    ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1245
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1246
    "Unbias exponent"
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1247
    exp := biasExponent - expPart + (numBitsInMantissa - numIntegerBits).
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1248
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1249
    " 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
  1250
      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
  1251
      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
  1252
    exp negative ifTrue: [
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1253
        result := sign * (fraction rightShift: exp) 
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1254
    ] ifFalse:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1255
        zeroBitsCount := fraction lowBit - 1.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1256
        exp := exp - zeroBitsCount.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1257
        exp <= 0 ifTrue: [
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1258
            zeroBitsCount := zeroBitsCount + exp.
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1259
            result := sign * (fraction rightShift:zeroBitsCount) 
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1260
        ] ifFalse: [
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1261
            result := Fraction
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1262
                    numerator: (sign * (fraction rightShift: zeroBitsCount))
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1263
                    denominator: (1 bitShift:exp) 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1264
        ] 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1265
    ].
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1266
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1267
    "Low cost validation omitted after extensive testing"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1268
    "(result asFloat = self) ifFalse: [self error: 'asTrueFraction validation failed']."
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1269
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1270
    ^ result 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1271
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1272
    "
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1273
     0.3 asFloat asTrueFraction   
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1274
     0.3 asShortFloat asTrueFraction  
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1275
     0.3 asLongFloat asTrueFraction   
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1276
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1277
     1.25 asTrueFraction     
7480
74f8bf8628e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7447
diff changeset
  1278
     1.25 asShortFloat asTrueFraction     
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1279
     0.25 asTrueFraction     
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1280
     -0.25 asTrueFraction    
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1281
     3e37 asTrueFraction     
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1282
18281
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1283
     LongFloat NaN asTrueFraction              
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1284
     LongFloat infinity asTrueFraction          
01a58214d842 class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 18135
diff changeset
  1285
     LongFloat negativeInfinity asTrueFraction 
7390
1e770c64a291 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7378
diff changeset
  1286
    "
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1287
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1288
    "Modified: / 25-08-2017 / 12:34:08 / cg"
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1289
    "Modified: / 28-05-2019 / 09:04:52 / Claus Gittinger"
24291
cdddcb8a204f #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24289
diff changeset
  1290
    "Modified (comment): / 08-06-2019 / 13:59:40 / Claus Gittinger"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1291
! !
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
!LimitedPrecisionReal methodsFor:'comparing'!
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
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1296
    "return true, if the argument is greater"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1297
1201
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1298
    ^ aNumber lessFromFloat:self asFloat
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1299
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1300
    "Modified: 17.4.1996 / 13:34:50 / cg"
701
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
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1303
!LimitedPrecisionReal methodsFor:'double dispatching'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1304
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1305
differenceFromFraction:aFraction
24277
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1306
    "sent when a fraction does not know how to subtract the receiver"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1307
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1308
    |d|
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1309
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1310
    d := aFraction denominator.
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1311
    ^ (aFraction numerator - (self * d)) / d
24277
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1312
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1313
    "Modified (comment): / 07-06-2019 / 02:19:52 / Claus Gittinger"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1314
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1315
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1316
productFromFraction:aFraction
24277
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1317
    "sent when a fraction does not know how to multiply the receiver"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1318
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1319
    ^ self * aFraction numerator / aFraction denominator
24277
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1320
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1321
    "Modified (comment): / 07-06-2019 / 02:19:47 / Claus Gittinger"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1322
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1323
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1324
quotientFromFraction:aFraction
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1325
    "Return the quotient of the argument, aFraction and the receiver.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1326
     Sent when aFraction does not know how to divide by the receiver."
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1327
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1328
    ^ aFraction numerator / (self * aFraction denominator)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1329
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1330
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1331
sumFromFraction:aFraction
24277
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1332
    "sent when a fraction does not know how to add the receiver"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1333
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1334
    |d|
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 := aFraction denominator.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1337
    ^ (self * d + aFraction numerator) / d
24277
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1338
96315a6ab1f3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24264
diff changeset
  1339
    "Modified (comment): / 07-06-2019 / 02:19:35 / Claus Gittinger"
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
23402
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1360
!LimitedPrecisionReal methodsFor:'printing'!
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1361
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1362
printStringScientific
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1363
    "return a 'user friendly' scientific printString.
24961
55894b2aa882 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24960
diff changeset
  1364
     Notice: this returns a Text object with superscript digits,
55894b2aa882 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24960
diff changeset
  1365
     which requires a font capapble of displaying it correctly.
23402
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1366
     Also: the returned string is not meant to be read back - purely for GUIs"
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1367
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1368
    |s idx|
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1369
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1370
    s := PrintfScanf printf:'%e' argument:self.
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1371
    idx := s indexOfAny:'eE'.
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1372
    idx == 0 ifTrue:[^ s].
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1373
    ^ (s copyTo:idx-1) , '×10' , ((s copyFrom:idx+1) asText emphasisAllAdd:#superscript)
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1374
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1375
    "
24961
55894b2aa882 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24960
diff changeset
  1376
     1.23456 printString            -> '1.23456'
55894b2aa882 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24960
diff changeset
  1377
     1.23456 printStringScientific  1.23456×100 (with superscript zero at end)
55894b2aa882 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24960
diff changeset
  1378
     1.23e14 printStringScientific  1.23×1014
24643
4953a421be84 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24437
diff changeset
  1379
     PrintfScanf printf:'%e' argument:1.23456
23402
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1380
    "
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1381
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1382
    "Created: / 05-10-2018 / 13:38:44 / Claus Gittinger"
50a54f696d5e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23056
diff changeset
  1383
! !
22851
df11e296e70d #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 22787
diff changeset
  1384
22305
4d905f534925 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22288
diff changeset
  1385
!LimitedPrecisionReal methodsFor:'queries'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1386
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1387
decimalEmax
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1388
    "Answer how many digits of exponent-accuracy this class supports"
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1389
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1390
    ^ self class decimalEmax
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1391
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1392
    "
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1393
     1.0 asShortFloat emax   
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1394
     1.0 asShortFloat decimalEmax  
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1395
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1396
     1.0 asFloat emax  
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1397
     1.0 asFloat emin
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1398
     1.0 asFloat decimalEmax  
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1399
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1400
     1.0 asLongFloat emax        
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1401
     1.0 asLongFloat emin        
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1402
     1.0 asLongFloat decimalEmax  
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1403
    "
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1404
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1405
    "Created: / 10-10-2017 / 15:46:43 / cg"
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1406
!
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1407
22305
4d905f534925 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22288
diff changeset
  1408
decimalPrecision
25068
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1409
    "Answer how many significant decimal digits (accuracy) this class supports"
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1410
25068
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1411
    ^ (self precision / (10.0 log:self radix)) 
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1412
        floor   "use floor, since the last decimal digit is usually wrong"
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1413
22305
4d905f534925 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22288
diff changeset
  1414
    "
4d905f534925 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22288
diff changeset
  1415
     1.0 asShortFloat decimalPrecision
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1416
     1.0 asFloat decimalPrecision   
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1417
     1.0 asLongFloat decimalPrecision     
22305
4d905f534925 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22288
diff changeset
  1418
     1.0 asQDouble decimalPrecision
4d905f534925 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22288
diff changeset
  1419
     1.0 asLargeFloat decimalPrecision
4d905f534925 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22288
diff changeset
  1420
    "
8422
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1421
22305
4d905f534925 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22288
diff changeset
  1422
    "Created: / 10-10-2017 / 15:46:43 / cg"
8422
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1423
!
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1424
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1425
defaultNumberOfDigits
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1426
    "Answer how many digits of accuracy this class supports"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1427
16663
11d8aeef6f55 Fuzzy float compare support
Stefan Vogel <sv@exept.de>
parents: 16637
diff changeset
  1428
    ^ self class decimalPrecision 
8422
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1429
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1430
    "
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1431
        Float new defaultNumberOfDigits
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1432
        LongFloat new defaultNumberOfDigits
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1433
        ShortFloat new defaultNumberOfDigits
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1434
        QDouble new defaultNumberOfDigits
8422
c206b4711b6e Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
  1435
    "
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1436
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1437
    "Modified (comment): / 20-06-2017 / 12:58:12 / cg"
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1438
!
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1439
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1440
eBias
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1441
    "Answer the exponent's bias;
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1442
     that is the offset of the zero exponent when stored"
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1443
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1444
    ^ (1 << (self numBitsInExponent-1)) - 1
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1445
!
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1446
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1447
emax
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1448
    "return the largest exponent.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1449
     The computation below assumes standard IEEE format"
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1450
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1451
    ^ (1 << (self numBitsInExponent-1)) - 1
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1452
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1453
    "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1454
     Float emax       -> 1023
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1455
     ShortFloat emax  -> 127
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1456
     LongFloat emax   -> 16383
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1457
     QuadFloat emax   -> 16383
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1458
     OctaFloat emax   -> 262143
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1459
     QDouble emax     -> 1023
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1460
    "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1461
!
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1462
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1463
emin
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1464
    ^ 1 - self emax
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1465
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1466
!
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1467
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1468
exponent
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
  1469
    "extract a normalized float's (unbiased) exponent.
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1470
     This is a fallback for systems which do not provide frexp in their math lib,
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
  1471
     and also for error reporting (NaN or Inf).
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1472
     The returned value depends on the float-representation of
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1473
     the underlying machine and is therefore highly unportable.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1474
     This is not for general use.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1475
     This assumes that the mantissa is normalized to
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1476
     0.5 .. 1.0 and the float's value is mantissa * 2^exp"
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1477
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1478
    |shifty expPart exp numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1479
     biasExponent numIntegerBits fractionPart|
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1480
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1481
    (self isFinite) ifFalse:[
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1482
        ^ self class
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1483
            raise:#domainErrorSignal
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1484
            receiver:self
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1485
            selector:#exponent
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1486
            arguments:#()
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1487
            errorString:'bad receiver (Inf or NaN) in exponent'
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1488
    ].    
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1489
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1490
    "Extract the bits of an IEEE anySize float "
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1491
    numBytes := self basicSize.
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1492
    numBitsInMantissa := self numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1.
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1493
    numBitsInExponent := self numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1.
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1494
    numIntegerBits := self numBitsInIntegerPart.
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1495
    biasExponent := maskExponent bitShift:-1.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1496
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1497
    shifty := LargeInteger basicNew numberOfDigits:numBytes.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1498
    UninterpretedBytes isBigEndian ifTrue:[
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1499
        1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)].
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1500
    ] ifFalse:[
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1501
        1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)].
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1502
    ].
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1503
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1504
    " Extract the sign and the biased exponent "
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1505
    expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent.
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1506
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1507
    " Extract fractional part; answer 0 if this is a true 0.0 value "
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1508
    fractionPart := shifty bitAnd:maskMantissa.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1509
    ( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0  ].
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1510
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1511
    exp := expPart - biasExponent + 1.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1512
    ^ exp
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1513
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1514
    "
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1515
     0.3 asFloat exponent  
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1516
     0.3 asShortFloat exponent  
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1517
     0.3 asLongFloat exponent  
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1518
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1519
     0.0 exponent2      0
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1520
     1.0 exponent2      1
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1521
     2.0 exponent2      2
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1522
     3.0 exponent2      2
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1523
     4.0 exponent2      3
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1524
     0.5 exponent2      0
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1525
     0.4 exponent2      -1
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1526
     0.25 exponent2     -1
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1527
     0.00000011111 exponent2  -23 
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1528
    "
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1529
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1530
    "Modified: / 25-08-2017 / 12:34:23 / cg"
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1531
    "Modified: / 28-05-2019 / 09:05:02 / Claus Gittinger"
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1532
!
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1533
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1534
exponentBits
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1535
    "extract the biased exponentBits"
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1536
     
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1537
    |shifty numBytes numBitsInMantissa exponent anyBit|
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1538
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1539
    (self isNaN or:[self isInfinite]) ifTrue:[
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1540
        ^ self class
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1541
            raise:#domainErrorSignal
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1542
            receiver:self
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1543
            selector:#mantissa
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1544
            arguments:#()
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1545
            errorString:'bad receiver (Inf or NaN) in mantissa'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1546
    ].    
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1547
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1548
    "Extract the bits of an IEEE anySize float "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1549
    numBytes := self basicSize.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1550
    shifty := LargeInteger basicNew numberOfDigits:numBytes.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1551
    anyBit := 0.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1552
    UninterpretedBytes isBigEndian ifTrue:[
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1553
        1 to:numBytes do:[:i | |byte| shifty digitAt:(numBytes+1-i) put:(byte := self basicAt:i). anyBit := anyBit bitOr:byte].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1554
    ] ifFalse:[
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1555
        1 to:numBytes do:[:i | |byte| shifty digitAt:i put:(byte := self basicAt:i). anyBit := anyBit bitOr:byte].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1556
    ].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1557
    anyBit == 0 ifTrue:[^ 0].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1558
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1559
    numBitsInMantissa := self numBitsInMantissa. 
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1560
    exponent := shifty rightShift:numBitsInMantissa.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1561
    exponent := exponent bitAnd:((1 bitShift:self numBitsInExponent)-1).
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1562
    ^ exponent
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1563
    
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1564
    "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1565
     0.0 mantissaBits    
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1566
     0.0 exponentBits 
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1567
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1568
     1.0 mantissaBits  hexPrintString '10000000000000' 
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1569
     1.0 exponentBits                 1023 16r3FF 
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1570
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1571
     2.0 mantissaBits  hexPrintString '10000000000000' 
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1572
     2.0 exponentBits                 1024 16r400
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1573
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1574
     3.0 mantissaBits  hexPrintString '18000000000000'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1575
     3.0 exponentBits                 1024 16r400
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1576
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1577
     4.0 mantissaBits  hexPrintString '10000000000000'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1578
     4.0 exponentBits                 1025 16r401
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1579
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1580
     5.0 mantissaBits  hexPrintString '14000000000000'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1581
     5.0 exponentBits                 1025 16r401
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1582
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1583
    -5.0 mantissaBits  hexPrintString '14000000000000'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1584
    -5.0 exponentBits                 1025 16r401
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1585
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1586
     0.1 mantissaBits  hexPrintString '1999999999999A'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1587
     0.1 exponentBits                 1019 16r3FB
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1588
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1589
     0.3 mantissaBits  hexPrintString '13333333333333'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1590
     0.3 exponentBits                 1021 16r3FD
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1591
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1592
     0.3 asShortFloat mantissaBits    10066330 16r99999A
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1593
     0.3 asShortFloat exponentBits    125 16r7D
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1594
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1595
     0.3 asLongFloat mantissaBits     11068046444225730560 16r9999999999999800
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1596
     0.3 asLongFloat exponentBits     16381 16r3FFD
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1597
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1598
     0.3 asQDouble mantissaBits  
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1599
    "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1600
    "Modified: / 28-05-2019 / 09:05:10 / Claus Gittinger"
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1601
!
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1602
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1603
fmax
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1604
    ^ self class fmax
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1605
!
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1606
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1607
fmin
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1608
    ^ self class fmin
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1609
!
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1610
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1611
fractionalPart
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1612
    "This has been renamed to #fractionPart for ST80 compatibility.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1613
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1614
     extract the after-decimal fraction part.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1615
     the floats value is 
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1616
        float truncated + float fractionalPart"
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1617
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1618
    <resource:#obsolete>
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1619
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1620
    self obsoleteMethodWarning:'please use #fractionPart'.
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1621
    ^ self fractionPart
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1622
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1623
    "Modified: / 28.10.1998 / 17:10:12 / cg"
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1624
    "Created: / 28.10.1998 / 17:10:32 / cg"
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1625
!
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1626
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1627
mantissa
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1628
    "extract a normalized float's mantissa.
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1629
     That is a float of the same type as the receiver,
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1630
     such that:
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1631
        (2 ^ f exponent) * f mantissa = f
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1632
     This is a fallback for systems which do not provide frexp in their math lib,
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1633
     and also for error reporting (NaN or Inf)."
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1634
     
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1635
    |m numBitsInMantissa numBitsInExponent maskExponent numIntegerBits
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1636
     bits "maskMantissa shifty expPart numBytes 
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1637
     biasExponent  fractionPart"|
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1638
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1639
    (self isNaN or:[self isInfinite]) ifTrue:[
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1640
        ^ self class
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1641
            raise:#domainErrorSignal
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1642
            receiver:self
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1643
            selector:#mantissa
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1644
            arguments:#()
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1645
            errorString:'bad receiver (Inf or NaN) in mantissa'
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1646
    ].
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1647
    numBitsInExponent := self numBitsInExponent. 
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1648
    numBitsInMantissa := self numBitsInMantissa.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1649
    numIntegerBits := self numBitsInIntegerPart.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1650
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1651
    maskExponent := (1 bitShift:numBitsInExponent) - 1.
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1652
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1653
    "/ Extract the bits of the IEEE float, mask out everything except the characteristic
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1654
    "/ and the sign; set the exponent to eBias
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1655
    m := self copy.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1656
    bits := LargeInteger digitBytes:self digitBytes.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1657
    "/ mask out the exponent
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1658
    bits := bits bitClear:(maskExponent bitShift:numBitsInMantissa + numIntegerBits).
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1659
    "/ set to zero
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1660
    bits := bits bitOr:(self eBias - 1 bitShift:numBitsInMantissa + numIntegerBits).
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1661
    1 to:self basicSize do:[:i | m basicAt:i put:(bits digitAt:i)].
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1662
    ^ m.
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1663
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1664
"/    "Extract the bits of an IEEE anySize float "
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1665
"/    numBytes := self basicSize.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1666
"/    numBitsInMantissa := maskMantissa := (1 bitShift:numBitsInMantissa) - 1.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1667
"/    biasExponent := maskExponent bitShift:-1.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1668
"/
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1669
"/    shifty := LargeInteger basicNew numberOfDigits:numBytes.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1670
"/    UninterpretedBytes isBigEndian ifTrue:[
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1671
"/        1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)].
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1672
"/    ] ifFalse:[
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1673
"/        1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)].
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1674
"/    ].
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1675
"/
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1676
"/    expPart := (shifty rightShift:numBitsInMantissa) bitAnd: maskExponent.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1677
"/
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1678
"/    "/ Extract fractional part; answer 0 if this is a true 0.0 value
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1679
"/    fractionPart := shifty bitAnd:maskMantissa.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1680
"/    fractionPart == 0 ifTrue:[
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1681
"/        expPart == 0 ifTrue:[
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1682
"/            ^ 0
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1683
"/        ]
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1684
"/    ].
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1685
"/
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1686
"/    "/ or in any hidden bit
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1687
"/    numIntegerBits == 0 ifTrue:[
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1688
"/        fractionPart := fractionPart bitOr:(1 bitShift:numBitsInMantissa).
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1689
"/    ].
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1690
"/    "/ should I make it a limited precision real?
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1691
"/    ^ fractionPart / (1 bitShift:numBitsInMantissa).
21853
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1692
    
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1693
    "
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1694
     0.3 asFloat mantissa  
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1695
     0.3 asShortFloat mantissa  
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1696
     0.3 asLongFloat mantissa  
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1697
     0.3 asQDouble mantissa  
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1698
    "
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1699
9486a8f34bff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21852
diff changeset
  1700
    "Created: / 20-06-2017 / 11:36:51 / cg"
22212
9a10518551e1 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22057
diff changeset
  1701
    "Modified: / 25-08-2017 / 12:34:34 / cg"
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1702
    "Modified: / 28-05-2019 / 09:05:10 / Claus Gittinger"
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1703
!
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1704
25015
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1705
mantissaBits
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1706
    "extract a normalized float's mantissaBits (incl. any hidden bit)."
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1707
     
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1708
    |shifty numBytes numBitsInMantissa maskMantissa fractionPart hiddenBit anyBit|
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1709
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1710
    (self isNaN or:[self isInfinite]) ifTrue:[
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1711
        ^ self class
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1712
            raise:#domainErrorSignal
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1713
            receiver:self
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1714
            selector:#mantissa
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1715
            arguments:#()
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1716
            errorString:'bad receiver (Inf or NaN) in mantissa'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1717
    ].    
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1718
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1719
    "Extract the bits of an IEEE anySize float "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1720
    numBytes := self basicSize.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1721
    shifty := LargeInteger basicNew numberOfDigits:numBytes.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1722
    anyBit := 0.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1723
    UninterpretedBytes isBigEndian ifTrue:[
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1724
        1 to:numBytes do:[:i | |byte| shifty digitAt:(numBytes+1-i) put:(byte := self basicAt:i). anyBit := anyBit bitOr:byte].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1725
    ] ifFalse:[
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1726
        1 to:numBytes do:[:i | |byte| shifty digitAt:i put:(byte := self basicAt:i). anyBit := anyBit bitOr:byte].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1727
    ].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1728
    anyBit == 0 ifTrue:[^ 0].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1729
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1730
    numBitsInMantissa := self numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1731
    (self numBitsInIntegerPart == 0) ifTrue:[
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1732
        hiddenBit := (1 bitShift:numBitsInMantissa).
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1733
    ] ifFalse:[
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1734
        hiddenBit := 0.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1735
    ].
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1736
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1737
    " Extract fractional part; answer 0 if this is a true 0.0 value "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1738
    fractionPart := shifty bitAnd:maskMantissa.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1739
    ^ fractionPart bitOr:hiddenBit.
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1740
    
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1741
    "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1742
     0.0 mantissaBits    
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1743
     1.0 mantissaBits  hexPrintString '10000000000000' 
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1744
     2.0 mantissaBits  hexPrintString '10000000000000' 
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1745
     3.0 mantissaBits  hexPrintString '18000000000000'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1746
     4.0 mantissaBits  hexPrintString '10000000000000'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1747
     5.0 mantissaBits  hexPrintString '14000000000000'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1748
     0.1 mantissaBits  hexPrintString '1999999999999A'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1749
     0.3 mantissaBits  hexPrintString '13333333333333'
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1750
     0.3 asShortFloat mantissaBits    10066330              16r99999A
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1751
     0.3 asLongFloat mantissaBits     11068046444225730560  16r9999999999999800
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1752
    "
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1753
    "Modified: / 28-05-2019 / 09:05:10 / Claus Gittinger"
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1754
!
a2deebae940a #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 24992
diff changeset
  1755
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1756
numBitsInExponent
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1757
    "answer the number of bits in the exponent
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1758
     11 for double precision:
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1759
        seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1760
     8 for single precision:
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1761
        seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1762
     15 for long floats (x86):
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1763
        00000000 00000000 seeeeeee eeeeeeee immmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1764
     15 for long floats (sparc):
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1765
        seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm...
24966
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
  1766
     15 for quad floats:
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
  1767
        seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm...
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
  1768
     15 for octuple floats:
bb9e1a00b090 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24962
diff changeset
  1769
        seeeeeee eeeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm...
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1770
     other for LargeFloats"
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1771
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1772
    ^ self class numBitsInExponent
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1773
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1774
    "Created: / 28-05-2019 / 08:52:44 / Claus Gittinger"
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1775
!
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1776
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1777
numBitsInIntegerPart
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1778
    "answer the number of bits in the integer part of the mantissa.
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1779
     Most floating point formats are normalized to get rid of the extra bit.
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1780
     (i.e. except for LongFloats and LargeFloats,
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1781
      instances are normalized to exclude any integer bit"
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1782
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1783
    ^ self class numBitsInIntegerPart
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1784
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1785
    "Created: / 28-05-2019 / 08:52:52 / Claus Gittinger"
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1786
!
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1787
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1788
numBitsInMantissa
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1789
    "answer the number of bits in the mantissa (the significant)
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1790
     any hidden bits are not counted.
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1791
     11 for half precision:
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1792
        seeeemmm mmmmmmmm
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1793
     23 for single precision:
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1794
        seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1795
     52 for double precision:
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1796
        seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1797
     64 for longfloat precision (x86):
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1798
        00000000 00000000 seeeeeee eeeeeeee immmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1799
     112 for longfloat precision (sparc):
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1800
        seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm...
25024
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1801
     112 for quadfloat precision:
57c1f14cf202 #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 25023
diff changeset
  1802
        seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm...
24203
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1803
    "
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1804
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1805
    ^ self class numBitsInMantissa
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1806
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1807
    "
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1808
     1.0 numBitsInMantissa
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1809
     1.0 asShortFloat numBitsInMantissa
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1810
     1.0 asLongFloat numBitsInMantissa
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1811
    "
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1812
5907279a90ef #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 24199
diff changeset
  1813
    "Created: / 28-05-2019 / 08:52:34 / Claus Gittinger"
5883
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1814
!
658ff91cef9e fixed asFraction
Claus Gittinger <cg@exept.de>
parents: 5868
diff changeset
  1815
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1816
precision
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1817
    "return the number of valid mantissa bits.
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1818
     Should be redefined in classes which allow per-instance precision specification"
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1819
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1820
    ^ self class precision
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1821
!
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1822
25068
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1823
radix
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1824
    "answer the radix of the exponent
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1825
     Typically, but not required to be, this will be 2 
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1826
     (as floats ary usually represented as IEEE binary floats)"
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1827
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1828
    ^ self class radix
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1829
!
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1830
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1831
size
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1832
   "redefined since reals are kludgy (ByteArry)"
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1833
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1834
   ^ 0
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5470
diff changeset
  1835
! !
5470
5e56d8cff0c9 General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5357
diff changeset
  1836
25068
54fe36ff8729 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25056
diff changeset
  1837
24155
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1838
!LimitedPrecisionReal methodsFor:'special access'!
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1839
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1840
partValues:aBlock
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1841
    "invoke aBlock with sign, exponent and abs(mantissa)"
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1842
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1843
    ^ aBlock 
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1844
        value:self sign
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1845
        value:self exponent
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1846
        value:self mantissa abs
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1847
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1848
    "
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1849
     1.0 partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa].
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1850
     2.0 partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa].
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1851
     -1.0 partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa].
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1852
     -2.0 partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa].
24229
9e72879e5365 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24203
diff changeset
  1853
9e72879e5365 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24203
diff changeset
  1854
     1.0 asShortFloat partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa].
9e72879e5365 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24203
diff changeset
  1855
     1.0 asLongFloat partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa].
9e72879e5365 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24203
diff changeset
  1856
     1.0 asLargeFloat partValues:[:sign :exp :mantissa | Transcript showCR:'%1/%2/%3' with:sign with:exp with:mantissa].
24155
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1857
    "
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1858
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1859
    "Created: / 26-05-2019 / 03:11:28 / Claus Gittinger"
24229
9e72879e5365 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24203
diff changeset
  1860
    "Modified (comment): / 29-05-2019 / 03:49:20 / Claus Gittinger"
24155
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1861
! !
d2c7144d7899 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23402
diff changeset
  1862
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1863
!LimitedPrecisionReal methodsFor:'testing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1864
3148
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1865
isFinite
24705
4caa65eee05e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24643
diff changeset
  1866
    "return true, if the receiver is a finite float (not NaN and not +/-INF)"
4caa65eee05e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24643
diff changeset
  1867
3148
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1868
   ^ self subclassResponsibility
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1869
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1870
    "Created: / 7.1.1998 / 12:02:06 / stefan"
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1871
!
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1872
6185
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1873
isFloat
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1874
    "return true, if the receiver is some kind of floating point number;
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1875
     false is returned here.
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1876
     Same as #isLimitedPrecisionReal, but a better name ;-)"
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1877
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1878
    ^ true
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1879
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1880
    "Created: / 14.11.2001 / 14:57:55 / cg"
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1881
!
2029fdbadfde +isFloat
Claus Gittinger <cg@exept.de>
parents: 6086
diff changeset
  1882
3148
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1883
isInfinite
24705
4caa65eee05e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24643
diff changeset
  1884
    "return true, if the receiver is an infinite float (+Inf or -Inf).
3148
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1885
     These are not created by ST/X float operations (they raise an exception);
24705
4caa65eee05e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24643
diff changeset
  1886
     however, inline C-code could produce them."
3148
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1887
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1888
    ^ (self isFinite or:[self isNaN]) not.
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1889
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1890
    "
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1891
        1.0 isInfinite
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1892
        (0.0 uncheckedDivide: 0.0) isInfinite
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1893
        (1.0 uncheckedDivide: 0.0) isInfinite
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1894
    "
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1895
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1896
    "Modified: / 7.1.1998 / 12:01:30 / stefan"
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1897
!
1434542209e7 New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1898
6086
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1899
isLimitedPrecisionReal
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1900
    "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
  1901
     true is returned here - the method is redefined from Object."
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1902
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1903
    ^ true
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1904
!
0bd476397581 added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents: 5989
diff changeset
  1905
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1906
isNaN
24705
4caa65eee05e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24643
diff changeset
  1907
    "return true, if the receiver is an invalid float (NaN - not a number).
4caa65eee05e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24643
diff changeset
  1908
     These are not created by ST/X float operations (they raise an exception);
4caa65eee05e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24643
diff changeset
  1909
     however, inline C-code could produce them."
4caa65eee05e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24643
diff changeset
  1910
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1911
   ^ self subclassResponsibility
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1912
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1913
    "Modified: 12.2.1997 / 16:45:27 / cg"
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1914
!
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1915
6890
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1916
isNegativeZero
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1917
    "many systems have two float.Pnt zeros"
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1918
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1919
    ^ (self isZero) and:[self printString first == $-]
6890
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1920
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  1921
    "
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1922
     0.0 asLongFloat isNegativeZero     
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1923
     -0.0 asLongFloat isNegativeZero       
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1924
     -1.0 asLongFloat isNegativeZero       
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1925
     1.0 asLongFloat isNegativeZero       
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1926
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1927
     0.0 asLargeFloat isNegativeZero     
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1928
     -0.0 asLargeFloat isNegativeZero       
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1929
    "
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1930
    "
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1931
     0.0 asLongFloat isZero                      
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1932
     -0.0 asLongFloat isZero       
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1933
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1934
     0.0 > 0   
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1935
     -0.0 < 0       
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1936
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1937
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1938
24327
76ae4c6b6da3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24304
diff changeset
  1939
isZero
76ae4c6b6da3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24304
diff changeset
  1940
    "return true, if the receiver is zero"
76ae4c6b6da3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24304
diff changeset
  1941
76ae4c6b6da3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24304
diff changeset
  1942
    ^ self = self class zero
76ae4c6b6da3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24304
diff changeset
  1943
76ae4c6b6da3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24304
diff changeset
  1944
    "Created: / 10-06-2019 / 21:58:45 / Claus Gittinger"
76ae4c6b6da3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24304
diff changeset
  1945
!
76ae4c6b6da3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24304
diff changeset
  1946
5357
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1947
numberOfBits
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1948
    "return the size (in bits) of the real;
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1949
     typically, this is 64 for Floats and 32 for ShortFloats,
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1950
     but who knows ..."
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1951
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1952
    self subclassResponsibility
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1953
!
39860dd8b0f5 query for number of bits
ps
parents: 5238
diff changeset
  1954
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1955
positive
18854
ab188ceb8115 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18834
diff changeset
  1956
    "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
  1957
7447
64fab8614c76 double dispatching, converting
Claus Gittinger <cg@exept.de>
parents: 7418
diff changeset
  1958
    ^ self sign >= 0
1201
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1959
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
  1960
    "Modified: 17.4.1996 / 13:35:10 / cg"
17380
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1961
!
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1962
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1963
sign
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1964
    "return the sign of the receiver (-1, 0 or 1)"
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1965
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1966
    self strictlyPositive  ifTrue:[^ 1].
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1967
    self negative ifTrue:[^ -1].
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1968
    ^ 0
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1969
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1970
    "
24992
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  1971
     -1.0 sign
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  1972
     -0.0 sign
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  1973
     1.0 sign
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  1974
     0.0 sign
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  1975
     Infinity infinity sign
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  1976
     Infinity infinity negated sign
17380
c8f2a7e0edbd class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 17221
diff changeset
  1977
    "
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1978
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  1979
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1980
!LimitedPrecisionReal methodsFor:'truncation & rounding'!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1981
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1982
ceilingAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1983
    ^ self coerce:(self ceiling).
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1984
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1985
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1986
     0.4 asLongFloat ceilingAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1987
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1988
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1989
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1990
floorAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1991
    ^ self coerce:(self floor).
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1992
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1993
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1994
     0.4 asLongFloat floorAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1995
    "
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1996
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  1997
25052
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  1998
integerAndFractionParts
25056
1b672602ae59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25052
diff changeset
  1999
    "return the integer and the fraction part of the receiver as a pair
1b672602ae59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25052
diff changeset
  2000
     of floats (i.e. the result of the modf function).
1b672602ae59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25052
diff changeset
  2001
     Adding the parts gives the original value"
1b672602ae59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25052
diff changeset
  2002
25052
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2003
    |iPart|
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2004
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2005
    iPart := self truncatedAsFloat.
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2006
    ^ { iPart . (self - iPart) } 
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2007
!
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2008
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2009
integerPart
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2010
    "return a float with value from digits before the decimal point
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2011
     (i.e. the truncated value)"
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2012
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2013
    ^ self truncatedAsFloat
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2014
    "/ ^ self truncated asFloat
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2015
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2016
    "
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2017
     1234.56789 integerPart  
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2018
     1.2345e6 integerPart
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2019
     12.5 integerPart
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2020
     -12.5 integerPart
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2021
     (5/3) integerPart
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2022
     (-5/3) integerPart
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2023
     (5/3) truncated
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2024
     (-5/3) truncated
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2025
    "
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2026
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2027
    "Created: / 28.10.1998 / 17:14:56 / cg"
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2028
    "Modified: / 5.11.2001 / 17:54:22 / cg"
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2029
!
2790ed2ce0ff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 25042
diff changeset
  2030
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  2031
roundedAsFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  2032
    self negative ifTrue:[ ^ (self - 0.5) ceilingAsFloat ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  2033
    ^ (self + 0.5) floorAsFloat
7398
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  2034
!
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  2035
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  2036
truncatedAsFloat
24992
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  2037
    self positive ifTrue:[
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  2038
        ^ self floorAsFloat
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  2039
    ].
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  2040
    ^ self ceilingAsFloat
e930708281e0 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24979
diff changeset
  2041
    "/ ^ self coerce:(self truncated).
7398
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  2042
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  2043
    "
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  2044
     0.4 asLongFloat truncatedAsFloat
0f96525b931e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 7394
diff changeset
  2045
    "
22786
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2046
!
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2047
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2048
truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2049
    "truncates to the precision of the float.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2050
     This is slightly different from truncated.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2051
     Taking for example 1e32,
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2052
     the printed representation will be 1e32,
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2053
     but the actual value, when truncating to an integer
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2054
     would be 100000003318135351409612647563264.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2055
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2056
     This is due to the inaccuracy in the least significant bits,
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2057
     and the way the print-converter compensates for this.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2058
     This method tries to generate an integer value which corresponds
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2059
     to what is seen in the float's printString.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2060
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2061
     Here, a slow fallback (generating and rescanning the printString)
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2062
     is provided, which should work on any float number.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2063
     Specialized versions in subclasses may be added for more performance
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2064
     (however, this is probably only used rarely)"
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2065
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2066
    |pow10 printString intVal s sign nFract ch expSign exp pI|
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2067
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2068
    pow10 := #(10 100 1000 10000 100000 1000000 10000000 100000000 1000000000).
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2069
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2070
    printString := self printString.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2071
    intVal := 0.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2072
    sign := 1.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2073
    nFract := 0.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2074
    exp := 0.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2075
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2076
    "/ fetch the mantissa
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2077
    s := printString readStream.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2078
    s peek == $- ifTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2079
        sign := -1.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2080
        s next.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2081
    ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2082
    intVal := s nextDecimalInteger.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2083
    s peek == $. ifTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2084
        s next.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2085
        [(ch := s peek) notNil and:[ch isDigit]] whileTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2086
            intVal := intVal * 10 + (s next digitValue).
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2087
            nFract := nFract + 1.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2088
        ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2089
    ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2090
    ('eEdDqQ' includes:s peek) ifTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2091
        expSign := 1.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2092
        s next.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2093
        (ch := s peek) == $- ifTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2094
            expSign := -1.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2095
            s next.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2096
        ] ifFalse:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2097
            ch == $+ ifTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2098
                s next.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2099
            ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2100
        ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2101
        exp := s nextDecimalInteger.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2102
        exp := exp * expSign.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2103
    ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2104
    exp := exp - nFract.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2105
    exp < 0 ifTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2106
        [exp < 0] whileTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2107
            pI := (exp negated) min:pow10 size.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2108
            intVal := intVal / (pow10 at:pI).
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2109
            exp := exp + pI.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2110
        ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2111
        intVal := intVal asInteger.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2112
    ] ifFalse:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2113
        [exp > 0] whileTrue:[
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2114
            pI := exp min:pow10 size.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2115
            intVal := intVal * (pow10 at:pI).
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2116
            exp := exp - pI.
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2117
        ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2118
    ].
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2119
    ^ intVal
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2120
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2121
    "
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2122
     1e32 asShortFloat truncated
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2123
     1e32 asShortFloat truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2124
     1.234e10 asShortFloat truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2125
     1234e-1 asShortFloat truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2126
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2127
     1e32 truncated
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2128
     1e32 truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2129
     1.234e10 truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2130
     1234e-1 truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2131
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2132
     1e32 asLongFloat truncated
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2133
     1e32 asLongFloat truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2134
     1.234e10 asLongFloat truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2135
     1234e-1 asLongFloat truncatedToPrecision
7375ac3023c4 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22610
diff changeset
  2136
    "
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  2137
! !
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7141
diff changeset
  2138
8395
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  2139
!LimitedPrecisionReal methodsFor:'visiting'!
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  2140
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  2141
acceptVisitor:aVisitor with:aParameter
16731
03aa0206cdda comment/format only
Claus Gittinger <cg@exept.de>
parents: 16663
diff changeset
  2142
    "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
  2143
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  2144
    ^ aVisitor visitFloat:self with:aParameter
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  2145
! !
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8202
diff changeset
  2146
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2147
!LimitedPrecisionReal class methodsFor:'documentation'!
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  2148
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2149
version
18834
40b9e84c3c92 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18281
diff changeset
  2150
    ^ '$Header$'
12120
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
  2151
!
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
  2152
a6a3e7a673a4 added: #denormalized
Claus Gittinger <cg@exept.de>
parents: 11727
diff changeset
  2153
version_CVS
18834
40b9e84c3c92 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18281
diff changeset
  2154
    ^ '$Header$'
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
  2155
! !
6890
8e74eff54c3d added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents: 6641
diff changeset
  2156
16405
0d357280097a class: LimitedPrecisionReal
Stefan Vogel <sv@exept.de>
parents: 12761
diff changeset
  2157
5989
8e3ff880f561 ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents: 5954
diff changeset
  2158
LimitedPrecisionReal initialize!