Integer.st
author Claus Gittinger <cg@exept.de>
Thu, 24 Jul 2008 11:58:22 +0200
changeset 11095 b3998b045684
parent 11067 58bb64805234
child 11125 89eb8912ef38
permissions -rw-r--r--
printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1988 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
5435
ad6612cc6848 oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
    12
"{ Package: 'stx:libbasic' }"
ad6612cc6848 oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
    13
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    14
Number subclass:#Integer
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
    15
	instanceVariableNames:''
4996
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
    16
	classVariableNames:'DefaultDisplayRadix BCDConversionErrorSignal'
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
    17
	poolDictionaries:''
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
    18
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    21
Object subclass:#ModuloNumber
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    22
	instanceVariableNames:'modulus reciprocal shift'
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    23
	classVariableNames:''
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    24
	poolDictionaries:''
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    25
	privateIn:Integer
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    26
!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    27
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
    28
!Integer class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    29
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    30
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    31
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    32
 COPYRIGHT (c) 1988 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
    33
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    34
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    35
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    36
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    37
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    38
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    39
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    40
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    41
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    42
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    43
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    44
documentation
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    45
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    46
    abstract superclass for all integer numbers.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    47
    See details in concrete subclasses LargeInteger and SmallInteger.
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
    48
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1888
diff changeset
    49
    Mixed mode arithmetic:
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    50
	int op int         -> int
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    51
	int op fix         -> fix; scale is fix's scale
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    52
	int op fraction    -> fraction
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    53
	int op float       -> float
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1888
diff changeset
    54
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
    55
    [Class variables:]
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
    56
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    57
	DefaultDisplayRadix     the radix in which integers present their 
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    58
				displayString (which is used in inspectors)
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    59
				If you are to look at many hex numbers, bitmasks
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    60
				etc. you may set this to 2 or 16.
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    61
				(avoids typing printStringRadix:.. all the time
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    62
				 - I know - I am lazy ;-). Default is 10.
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
    63
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
    64
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
    65
    [author:]
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    66
	Claus Gittinger
1556
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    67
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    68
    [see also:]
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    69
	Number
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    70
	LargeInteger SmallInteger
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    71
	Float ShortFloat Fraction FixedPoint
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    72
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    73
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
    75
!Integer class methodsFor:'instance creation'!
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    76
3044
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    77
byte1:b1 byte2:b2 byte3:b3 byte4:b4
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    78
    "Squeak compatibility:
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    79
     Return an Integer given four value bytes.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    80
     The returned integer is either a Small- or a LargeInteger"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    81
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    82
    |t|
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    83
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    84
    t := b4.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    85
    t := (t bitShift:8) + b3.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    86
    t := (t bitShift:8) + b2.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    87
    ^ (t bitShift:8) + b1.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    88
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    89
    "
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    90
     (Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    91
     (Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    92
    "
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    93
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    94
    "Created: 19.10.1997 / 18:08:52 / cg"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    95
    "Modified: 19.10.1997 / 18:09:04 / cg"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    96
!
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    97
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
    98
fastFromString:aString at:startIndex
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
    99
    "return the next unsigned Integer from the string 
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   100
     as a decimal number, starting at startIndex.
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   101
     The number must be in the native machines int range
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   102
     (i.e. 63bit on alpha / 31 on all others);
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   103
     However, for portability, only use it for 31bit numbers.
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   104
     No spaces are skipped.
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   105
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   106
     This is a specially tuned entry (using a low-level C-call), which
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   107
     returns garbage if the argument string is not a small integer number.
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   108
     It has been added to allow higher speed string decomposition into
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   109
     numbers."
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   110
%{
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   111
     if (__isString(aString) && __isSmallInteger(startIndex)) {
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   112
        char *cp = (char *)(__stringVal(aString));
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   113
        int idx = __intVal(startIndex) - 1;
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   114
        unsigned INT val;
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   115
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   116
        if ((unsigned)idx < __stringSize(aString)) {
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   117
            val = atoi(cp + idx);
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   118
            if (val <= _MAX_INT) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8897
diff changeset
   119
                RETURN(__mkSmallInteger(val));
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   120
            }
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   121
            RETURN (__MKUINT(val));
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   122
        }
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   123
     }
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   124
%}.
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   125
     self primitiveFailed.
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   126
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   127
    "
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   128
     Integer fastFromString:'12345' at:1  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   129
     Integer fastFromString:'12345' at:2  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   130
     Integer fastFromString:'12345' at:3  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   131
     Integer fastFromString:'12345' at:4  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   132
     Integer fastFromString:'12345' at:5  
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   133
     Integer fastFromString:'1234512345' at:1  
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   134
     Integer fastFromString:'2147483647' at:1  
4611
5d77a2fa73ba comment
Claus Gittinger <cg@exept.de>
parents: 4610
diff changeset
   135
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   136
     Integer fastFromString:'4294967295' at:1 
4611
5d77a2fa73ba comment
Claus Gittinger <cg@exept.de>
parents: 4610
diff changeset
   137
     Integer fastFromString:'12345' at:6  
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   138
     Integer fastFromString:'12345' at:0  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   139
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   140
     Time millisecondsToRun:[
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   141
        100000 timesRepeat:[
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   142
            Integer readFrom:'12345'
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   143
        ]
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   144
     ]  
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   145
    "
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   146
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   147
    "
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   148
     Time millisecondsToRun:[
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   149
        100000 timesRepeat:[
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   150
            Integer fastFromString:'12345' at:1  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   151
        ]
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   152
     ]   
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   153
    "
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   154
!
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   155
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   156
fromBCDBytes:aByteArray
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   157
    "given a byteArray in BCD format, return an appropriate integer.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   158
     The byteArray must contain the BCD encoded decimal string,
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   159
     starting with the most significant digits.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   160
     This conversion is useful for some communication protocols,
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   161
     or control systems, which represent big numbers this way...
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   162
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   163
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   164
    |val|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   165
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   166
    val := 0.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   167
    aByteArray do:[:twoDigits |
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   168
        |hi lo|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   169
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   170
        hi := (twoDigits bitShift:-4) bitAnd:16r0F.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   171
        lo := twoDigits bitAnd:16r0F.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   172
        val := (val * 100) + (hi * 10) + lo
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   173
    ].
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   174
    ^ val
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   175
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   176
    "
4921
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
   177
     Integer fromBCDBytes:#[16r12 16r34 16r56]    
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
   178
     Integer fromBCDBytes:#[16r12 16r34 16r56 16r78]   
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
   179
     Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90] 
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
   180
     Integer fromBCDBytes:#[16r98 16r76 16r54] 
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
   181
     Integer fromBCDBytes:#[16r98 16r76 16r54 16r32] 
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
   182
     Integer fromBCDBytes:#[16r98 16r76 16r54 16r32 16r10] 
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   183
     Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   184
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   185
!
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   186
5171
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   187
fromSwappedBCDBytes:aByteArray
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   188
    "given a byteArray in BCD format, return an appropriate integer.
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   189
     The byteArray must contain the BCD encoded decimal string,
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   190
     starting with the LEAST significant digits.
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   191
     This conversion is useful for some communication protocols,
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   192
     or control systems (e.g. SMC), which represent big numbers this way...
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   193
    "
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   194
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   195
    |val|
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   196
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   197
    val := 0.
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   198
    aByteArray do:[:twoDigits |
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   199
        |hi lo|
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   200
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   201
        lo := (twoDigits bitShift:-4) bitAnd:16r0F.
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   202
        hi := twoDigits bitAnd:16r0F.
5882
8574ffbe45fb Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
   203
        lo <= 9 ifTrue:[
8574ffbe45fb Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
   204
            val := (val * 100) + (hi * 10) + lo
8574ffbe45fb Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
   205
        ] ifFalse:[
8574ffbe45fb Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
   206
            "16rF is used to encode an odd number of digits"
8574ffbe45fb Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
   207
            val := (val * 10) + hi.
8574ffbe45fb Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
   208
        ].
5171
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   209
    ].
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   210
    ^ val
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   211
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   212
    "
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   213
     Integer fromSwappedBCDBytes:#[16r12 16r34 16r56]    
5882
8574ffbe45fb Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
   214
     Integer fromSwappedBCDBytes:#[16r12 16r34 16rF6]    
5171
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   215
     Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78]   
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   216
     Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90] 
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   217
     Integer fromSwappedBCDBytes:#[16r98 16r76 16r54] 
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   218
     Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32] 
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   219
     Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32 16r10] 
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   220
     Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90]
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   221
    "
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   222
!
6dc2f5a18953 Add #fromSwappedBCDBytes
Stefan Vogel <sv@exept.de>
parents: 5017
diff changeset
   223
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   224
new:numberOfBytes neg:negative
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   225
    "for ST-80 compatibility:
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   226
     Return an empty Integer (uninitialized value) with space for
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   227
     numberOfBytes bytes (= digitLength). The additional argument
132
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   228
     negative specifies if the result should be a negative number.
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   229
     The digits can be stored byte-wise into the result, using digitAt:put:"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   230
132
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   231
    ^ (LargeInteger basicNew numberOfDigits:numberOfBytes) 
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
   232
		sign:(negative ifTrue:[-1] ifFalse:[1])
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   233
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   234
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   235
readFrom:aStringOrStream 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   236
    "return the next unsigned Integer from the (character-)stream aStream 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   237
     as decimal number. 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   238
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   239
     NOTICE:   
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   240
       This behaves different from the default readFrom:, in returning
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   241
       0 (instead of raising an error) in case no number can be read.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   242
       It is unclear, if this is the correct behavior (ST-80 does this)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   243
       - depending on the upcoming ANSI standard, this may change."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   244
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   245
    ^ self readFrom:aStringOrStream onError:0
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   246
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   247
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   248
     Integer readFrom:(ReadStream on:'foobar')     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   249
     Integer readFrom:(ReadStream on:'foobar') onError:nil  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   250
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   251
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   252
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   253
readFrom:aStringOrStream onError:exceptionBlock
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   254
    "return the next Integer from the (character-)stream aStream,
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   255
     handling initial XXr for arbitrary radix numbers and initial sign.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   256
     Also, all initial whitespace is skipped.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   257
     If the string does not represent a valid integer number,
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   258
     return the value of exceptionBlock."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   259
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   260
    |value|
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   261
7093
60bad14d38fc Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents: 6907
diff changeset
   262
    Error handle:[:ex |
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   263
        ^ exceptionBlock value
1700
25223802076c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1560
diff changeset
   264
    ] do:[
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   265
        |str nextChar negative|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   266
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   267
        str := aStringOrStream readStream.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   268
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   269
        nextChar := str skipSeparators.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   270
        (nextChar == $-) ifTrue:[
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   271
            negative := true.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   272
            str next.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   273
            nextChar := str peekOrNil
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   274
        ] ifFalse:[
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   275
            negative := false
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   276
        ].
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   277
        (nextChar isNil or:[nextChar isDigit not]) ifTrue:[ 
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   278
            "
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   279
             the string does not represent an integer
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   280
            "
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   281
            ^ exceptionBlock value
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   282
        ].
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   283
        value := self readFrom:str radix:10.
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   284
        nextChar := str peekOrNil.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   285
        ((nextChar == $r) or:[ nextChar == $R]) ifTrue:[
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   286
            "-xxr<number> is invalid; should be xxr-<val>"
1700
25223802076c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1560
diff changeset
   287
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   288
            negative ifTrue:[
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   289
                'Integer [warning]: invalid (negative) radix ignored' errorPrintCR.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   290
                negative := false
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   291
            ].
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   292
            str next.
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   293
            value := self readFrom:str radix:value
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   294
        ].
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   295
        negative ifTrue:[
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   296
            value := value negated
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   297
        ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   298
    ].
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   299
    ^ value
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   300
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   301
    "
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   302
     Integer readFrom:'12345'      onError:['wrong']    
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   303
     Integer readFrom:'-12345'     onError:['wrong']    
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   304
     Integer readFrom:'+12345'     onError:['wrong']    
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   305
     Integer readFrom:'16rFFFF'    onError:['wrong']    
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   306
     Integer readFrom:'12345.1234' onError:['wrong'] 
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   307
     Integer readFrom:'foo'        onError:['wrong']
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   308
     Integer readFrom:'foo'                          
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   309
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   310
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   311
    "Created: / 16.11.1995 / 22:48:59 / cg"
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   312
    "Modified: / 14.4.1998 / 19:17:28 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   313
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   314
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   315
readFrom:aStringOrStream radix:radix
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   316
    "return the next unsigned Integer from the (character-)stream aStream 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   317
     in radix; (assumes that the initial XXr has already been read).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   318
     No whitespace-skipping is done.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   319
     Returns 0 if no number available.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   320
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   321
     NOTICE:   
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   322
       This behaves different from the default readFrom:, in returning
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   323
       0 (instead of raising an error) in case no number can be read.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   324
       It is unclear, if this is the correct behavior (ST-80 does this)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   325
       - depending on the upcoming ANSI standard, this may change."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   326
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   327
    ^ self readFrom:aStringOrStream radix:radix onError:0
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   328
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   329
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   330
readFrom:aStringOrStream radix:radix onError:exceptionBlock
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   331
    "return the next unsigned Integer from the (character-)stream aStream 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   332
     in radix; (assumes that the initial XXr has already been read).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   333
     No whitespace-skipping is done.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   334
     Returns the value of exceptionBlock, if no number is available."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   335
4169
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   336
    |str nextChar nextChar2 value 
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   337
     r     "{ Class: SmallInteger }"
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   338
     r2    "{ Class: SmallInteger }"|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   339
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   340
    str := aStringOrStream readStream.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   341
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   342
    nextChar := str peekOrNil.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   343
    (nextChar notNil and:[nextChar isDigitRadix:radix]) ifFalse:[
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   344
        ^ exceptionBlock value
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   345
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   346
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   347
    value := nextChar digitValue.
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   348
    str next.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   349
    nextChar := str peekOrNil.
4169
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   350
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   351
"/ OLD code
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   352
"/    [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   353
"/        str next.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   354
"/        value := value * radix + nextChar digitValue.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   355
"/        nextChar := str peekOrNil.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   356
"/    ].
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   357
"/    ^ value.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   358
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   359
    "/ the code below does the same, but is much faster, if the
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   360
    "/ converted number is large
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   361
    "/ (requires only half as many LargeInt multiplications and additions)
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   362
    "/ It should not be slower for smallIntegers.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   363
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   364
    r := radix.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   365
    r2 := r * r.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   366
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   367
    [nextChar notNil and:[nextChar isDigitRadix:r]] whileTrue:[
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   368
        str next.
4169
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   369
        nextChar2 := str peekOrNil.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   370
        (nextChar2 notNil and:[nextChar2 isDigitRadix:r]) ifTrue:[
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   371
            str next.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   372
            value := (value * r2) + ((nextChar digitValue * r) + nextChar2 digitValue).
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   373
            nextChar := str peekOrNil.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   374
        ] ifFalse:[
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   375
            ^ (value * r) + nextChar digitValue.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   376
        ]
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   377
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   378
    ^ value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   379
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   380
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   381
     Integer readFrom:(ReadStream on:'12345') radix:10  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   382
     Integer readFrom:(ReadStream on:'FFFF') radix:16  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   383
     Integer readFrom:(ReadStream on:'1010') radix:2   
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   384
     Integer readFrom:(ReadStream on:'foobar') radix:10   
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   385
     Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil  
4169
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   386
     Integer readFrom:'gg' radix:10 onError:0                      
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   387
    "
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   388
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   389
    "Modified: / 14.4.1998 / 19:16:46 / cg"
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   390
!
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   391
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   392
readFromRomanString:aStringOrStream
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   393
    "convert a string or stream containing a roman representation into an integer.
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   394
     Raises an exception, if the inputs format is completely wrong.
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   395
     Raises BadRomanNumberFormatError if its wrong, but could be parsed.
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   396
     Notifies via NaiveRomanNumberFormatNotification, if its a bit wrong (naive format).
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   397
     Will read both real and naive roman numbers (see printRomanOn: vs. printRomanOn:naive:),
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   398
     however, a notification is raised for for naive numbers (catch it if you are interested in it)."
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   399
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   400
    |romanValues s c val digitVal prevDigitVal countSame delta 
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   401
     stopOnSeparator finish|
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   402
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   403
    romanValues := Dictionary 
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   404
                    withKeys:#($M $D $C $L $X $V $I) 
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   405
                    andValues:#(1000 500 100 50 10 5 1).
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   406
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   407
    (stopOnSeparator := aStringOrStream isStream) ifFalse:[
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   408
        s := aStringOrStream readStream.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   409
    ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   410
    s atEnd ifTrue:[ 
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   411
        ^ RomanNumberFormatError raiseErrorString:'empty string'
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   412
    ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   413
    val := 0.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   414
    prevDigitVal := 99999.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   415
    countSame := 1.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   416
    finish := false.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   417
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   418
    [s atEnd or:[finish]] whileFalse:[
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   419
        c := s next asUppercase.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   420
        c isSeparator ifTrue:[
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   421
            stopOnSeparator ifFalse:[
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   422
                ^ RomanNumberFormatError raiseErrorString:'garbage at the end'
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   423
            ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   424
            finish := true.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   425
        ] ifFalse:[
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   426
            digitVal := romanValues at:c ifAbsent:nil.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   427
            digitVal isNil ifTrue:[
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   428
                ^ RomanNumberFormatError raiseErrorString:'invalid character'
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   429
            ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   430
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   431
            digitVal = prevDigitVal ifTrue:[
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   432
                ( #( 1 10 100 1000) includes:digitVal) ifFalse:[
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   433
                    ^ RomanNumberFormatError raiseErrorString:'character may not be repeated'
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   434
                ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   435
                val := val + digitVal.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   436
                countSame := countSame + 1.
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   437
                countSame >= 4 ifTrue:[
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   438
                    digitVal ~= 1000 ifTrue:[
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   439
                        countSame > 4 ifTrue:[
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   440
                            "/ this is a bad roman number (such as MCCCCCCCCXXXXXXII);
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   441
                            "/ Its not correct, but sometimes encountered on buildings.
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   442
                            "/ If you do not want to be too picky,
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   443
                            "/ provide a proceeding handler in order to proceed the conversion.
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   444
                            BadRomanNumberFormatError raiseRequestErrorString:'more than 4 occurrences of same character'
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   445
                        ] ifFalse:[
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   446
                            "/ this is a naive roman number (such as VIIII);
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   447
                            "/ Its not correct, but very often encountered (especially as page numbers).
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   448
                            "/ The notification below normally goes unnoticed, unless some input validator
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   449
                            "/ wants to be very picky, and treat this as an error.
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   450
                            "/ To do so, provide a handler for NaiveRomanNumberFormatNotification.
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   451
                            NaiveRomanNumberFormatNotification raiseRequestErrorString:'more than 3 occurrences of same character'.
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   452
                        ]
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   453
                    ]
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   454
                ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   455
            ] ifFalse:[
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   456
                digitVal < prevDigitVal ifTrue:[
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   457
                    val := val + digitVal.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   458
                ] ifFalse:[
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   459
                    countSame == 1 ifFalse:[
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   460
                        ^ RomanNumberFormatError raiseErrorString:'invalid character combination'
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   461
                    ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   462
                    delta := digitVal - prevDigitVal.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   463
                    ( #( 4 9 40 90 400 900) includes:delta) ifFalse:[
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   464
                        ^ RomanNumberFormatError raiseErrorString:'invalid character combination'
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   465
                    ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   466
                    val := val - prevDigitVal.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   467
                    val := val + delta.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   468
                    digitVal := prevDigitVal - 0.1.  "/ trick: prevent prevDigit from arriving again.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   469
                ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   470
                countSame := 1.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   471
            ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   472
            prevDigitVal := digitVal.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   473
        ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   474
    ].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   475
"/    val > 5000 ifTrue:[
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   476
"/        ^ RomanNumberFormatError raiseErrorStirng:'number out of range (1..5000)'
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   477
"/    ].    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   478
    ^ val.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   479
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   480
    "
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   481
     Integer readFromRomanString:'I'                                      
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   482
     Integer readFromRomanString:'II'      
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   483
     Integer readFromRomanString:'III'        
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   484
     Integer readFromRomanString:'IV'         
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   485
     Integer readFromRomanString:'clix'     
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   486
     Integer readFromRomanString:'MIX'       
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   487
     Integer readFromRomanString:'MCMXCIX'       
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   488
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   489
   Naive cases (which are accepted):
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   490
     Integer readFromRomanString:'IIII'        
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   491
     Integer readFromRomanString:'VIIII'         
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   492
     Integer readFromRomanString:'CLXXXXVIIII'        
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   493
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   494
    Error case (not proceedable):
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   495
     Integer readFromRomanString:'LC'      
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   496
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   497
    Error case (proceedable):
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   498
     Integer readFromRomanString:'MCCCCCCCCXXXXXXIIIIII'      
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   499
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   500
     BadRomanNumberFormatError ignoreIn:[
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   501
         Integer readFromRomanString:'MCCCCCCCCXXXXXXIIIIII'      
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   502
     ]   
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   503
    "
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   504
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   505
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   506
    "naive cases:
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   507
     #(
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   508
        'MCMXCIX'           1999
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   509
        'MCMXCVIIII'        1999
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   510
        'MCMLXXXXIX'        1999
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   511
        'MDCCCCXCIX'        1999
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   512
        'MDCCCCXCVIIII'     1999
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   513
        'MDCCCCLXXXXIX'     1999
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   514
        'MDCCCCLXXXXVIIII'  1999
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   515
     ) pairWiseDo:[:goodString :expectedValue |
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   516
        (Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt].
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   517
     ]
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   518
    "
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   519
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   520
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   521
    "error cases:
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   522
      #( 
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   523
        'XIIX'      
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   524
        'VV'        
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   525
        'VVV'        
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   526
        'XXL'         
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   527
        'XLX'        
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   528
        'LC'        
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   529
        'LL'        
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   530
        'DD'        
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   531
     ) do:[:badString |
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   532
        (Integer readFromRomanString:badString onError:nil) notNil ifTrue:[self halt].
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   533
     ]
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   534
    "
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   535
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   536
    "good cases:
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   537
     #( 'I'     1
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   538
        'II'    2
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   539
        'III'   3
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   540
        'IV'    4
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   541
        'V'     5
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   542
        'VI'    6
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   543
        'VII'   7
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   544
        'VIII'  8
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   545
        'IX'    9
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   546
        'X'     10
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   547
        'XI'    11      
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   548
        'XII'   12   
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   549
        'XIII'  13    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   550
        'XIV'   14   
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   551
        'XV'    15  
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   552
        'XVI'   16   
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   553
        'XVII'  17    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   554
        'XVIII' 18     
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   555
        'XIX'   19   
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   556
        'XX'    20      
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   557
        'XXX'   30   
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   558
        'L'     50   
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   559
        'XL'    40    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   560
        'LX'    60    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   561
        'LXX'   70     
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   562
        'LXXX'  80      
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   563
        'CXL'   140    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   564
        'CL'    150    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   565
        'CLX'   160     
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   566
        'MMM'                   3000      
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   567
        'MMMM'                  4000      
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   568
        'MMMMCMXCIX'            4999    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   569
        'MMMMMMMMMCMXCIX'       9999 
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   570
     ) pairWiseDo:[:goodString :expectedValue |
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   571
        (Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt].
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   572
     ]
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   573
    "
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   574
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   575
    "
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   576
      1 to:9999 do:[:n |
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   577
        |romanString|
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   578
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   579
        romanString := String streamContents:[:stream | n printRomanOn:stream].
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   580
        (Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   581
     ]
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   582
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   583
!
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   584
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   585
readFromRomanString:aStringOrStream onError:exceptionalValue
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   586
    "convert a string or stream containing a roman representation into an integer.
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   587
     Raises an exception, if the inputs format is wrong. 
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   588
     Does allow reading of naive (more than 3 in a row) and 
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   589
     bad (not using L and D) roman numbers.
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   590
     (Such numbers can be seen on some medevial buildings. "
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   591
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   592
    |val|
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   593
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   594
    RomanNumberFormatError 
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   595
        handle:[:ex |
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   596
            val := exceptionalValue value
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   597
        ]
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   598
        do:[
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   599
            val := self readFromRomanString:aStringOrStream
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   600
        ].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   601
    ^ val
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   602
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   603
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   604
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   605
     Integer readFromRomanString:'I'    onError:nil                                      
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   606
     Integer readFromRomanString:'II'   onError:nil  
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   607
     Integer readFromRomanString:'III'  onError:nil   
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   608
     Integer readFromRomanString:'IV'   onError:nil     
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   609
     Integer readFromRomanString:'clix' onError:nil       
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   610
     Integer readFromRomanString:'MCMXCIX' onError:nil       
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   611
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   612
   Naive cases (which are accepted):
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   613
     Integer readFromRomanString:'IIII' onError:nil       
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   614
     Integer readFromRomanString:'VIIII' onError:nil        
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   615
     Integer readFromRomanString:'CLXXXXVIIII' onError:nil       
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   616
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   617
   Error cases:
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   618
\     Integer readFromRomanString:'LC'   onError:nil        
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   619
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   620
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   621
    "error cases:
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   622
      #( 
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   623
        'XIIX'      
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   624
        'VV'        
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   625
        'VVV'        
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   626
        'XXL'         
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   627
        'XLX'        
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   628
        'LC'        
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   629
        'LL'        
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   630
        'DD'        
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   631
     ) do:[:badString |
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   632
        (Integer readFromRomanString:badString onError:nil) notNil ifTrue:[self halt].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   633
     ]
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   634
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   635
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   636
    "naive (but handled) cases:
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   637
      #( 
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   638
        'IIII'   4
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   639
        'VIIII'  9
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   640
        'XIIII'  14
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   641
        'XVIIII' 19
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   642
     ) pairWiseDo:[:goodString :expectedValue |
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   643
        (Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt].
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   644
     ]
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   645
    "
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   646
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   647
    "good cases:
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   648
     #( 'I'     1
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   649
        'II'    2
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   650
        'III'   3
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   651
        'IV'    4
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   652
        'V'     5
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   653
        'VI'    6
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   654
        'VII'   7
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   655
        'VIII'  8
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   656
        'IX'    9
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   657
        'X'     10
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   658
        'XI'    11      
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   659
        'XII'   12   
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   660
        'XIII'  13    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   661
        'XIV'   14   
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   662
        'XV'    15  
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   663
        'XVI'   16   
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   664
        'XVII'  17    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   665
        'XVIII' 18     
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   666
        'XIX'   19   
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   667
        'XX'    20      
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   668
        'XXX'   30   
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   669
        'L'     50   
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   670
        'XL'    40    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   671
        'LX'    60    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   672
        'LXX'   70     
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   673
        'LXXX'  80      
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   674
        'CXL'   140    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   675
        'CL'    150    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   676
        'CLX'   160     
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   677
        'MMM'                   3000      
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   678
        'MMMM'                  4000      
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   679
        'MMMMCMXCIX'            4999    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   680
        'MMMMMMMMMCMXCIX'       9999 
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   681
     ) pairWiseDo:[:goodString :expectedValue |
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   682
        (Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   683
     ]
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   684
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   685
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   686
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   687
      1 to:9999 do:[:n |
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   688
        |romanString|
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   689
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   690
        romanString := String streamContents:[:stream | n printRomanOn:stream].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   691
        (Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   692
     ]
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   693
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   694
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   695
    "reading naive numbers:
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   696
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   697
      1 to:9999 do:[:n |
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   698
        |romanString|
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   699
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
   700
        romanString := String streamContents:[:stream | n printRomanOn:stream naive:true].
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
   701
        (Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt].
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   702
     ]
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
   703
    "
8897
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   704
!
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   705
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   706
readFromString:aString radix:base onError:exceptionBlock
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   707
    |str val|
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   708
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   709
    str := ReadStream on:aString.
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   710
    val := self readFrom:str radix:base onError:[^ exceptionBlock value].
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   711
    str atEnd ifFalse:[ ^ exceptionBlock value].
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
   712
    ^ val
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   713
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   714
9141
dc20ee8ba916 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9136
diff changeset
   715
!Integer class methodsFor:'Compatibility-Squeak'!
dc20ee8ba916 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9136
diff changeset
   716
dc20ee8ba916 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9136
diff changeset
   717
readFrom:aStringOrStream base:aBase
dc20ee8ba916 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9136
diff changeset
   718
    "for squeak compatibility"
dc20ee8ba916 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9136
diff changeset
   719
dc20ee8ba916 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9136
diff changeset
   720
    ^ self readFrom:aStringOrStream radix:aBase
dc20ee8ba916 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9136
diff changeset
   721
! !
dc20ee8ba916 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9136
diff changeset
   722
4996
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   723
!Integer class methodsFor:'Signal constants'!
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   724
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   725
bcdConversionErrorSignal
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   726
    "return the signal which is raised when bcd conversion fails
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   727
     (i.e. when trying to decode an invalid BCD number)"
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   728
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   729
    ^ BCDConversionErrorSignal
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   730
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   731
    "Modified: / 15.11.1999 / 20:35:20 / cg"
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   732
! !
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
   733
7444
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   734
!Integer class methodsFor:'class initialization'!
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   735
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   736
initialize
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   737
    BCDConversionErrorSignal isNil ifTrue:[
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   738
        BCDConversionErrorSignal := ConversionError newSignal.
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   739
        BCDConversionErrorSignal nameClass:self message:#bcdConversionErrorSignal.
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   740
        BCDConversionErrorSignal notifierString:'bcd conversion error'.
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   741
    ].
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   742
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   743
    "Modified: / 15.11.1999 / 20:36:04 / cg"
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   744
! !
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   745
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
   746
!Integer class methodsFor:'constants'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   747
7727
bda9f88c6847 Define #epsilon
Stefan Vogel <sv@exept.de>
parents: 7685
diff changeset
   748
epsilon
bda9f88c6847 Define #epsilon
Stefan Vogel <sv@exept.de>
parents: 7685
diff changeset
   749
    "return the maximum relative spacing"
bda9f88c6847 Define #epsilon
Stefan Vogel <sv@exept.de>
parents: 7685
diff changeset
   750
bda9f88c6847 Define #epsilon
Stefan Vogel <sv@exept.de>
parents: 7685
diff changeset
   751
    "don't know, what to really return here.
8635
38674ba49a14 comment
Claus Gittinger <cg@exept.de>
parents: 8573
diff changeset
   752
     Returning 1 gives stupid values when doing some taylor series approximations
38674ba49a14 comment
Claus Gittinger <cg@exept.de>
parents: 8573
diff changeset
   753
     (although it is correct)"
7727
bda9f88c6847 Define #epsilon
Stefan Vogel <sv@exept.de>
parents: 7685
diff changeset
   754
bda9f88c6847 Define #epsilon
Stefan Vogel <sv@exept.de>
parents: 7685
diff changeset
   755
    ^ Float epsilon.
8635
38674ba49a14 comment
Claus Gittinger <cg@exept.de>
parents: 8573
diff changeset
   756
38674ba49a14 comment
Claus Gittinger <cg@exept.de>
parents: 8573
diff changeset
   757
    "
38674ba49a14 comment
Claus Gittinger <cg@exept.de>
parents: 8573
diff changeset
   758
     2 sqrt_withAccuracy:(Integer epsilon)  
38674ba49a14 comment
Claus Gittinger <cg@exept.de>
parents: 8573
diff changeset
   759
     2 sqrt_withAccuracy:1                  
38674ba49a14 comment
Claus Gittinger <cg@exept.de>
parents: 8573
diff changeset
   760
    "
7727
bda9f88c6847 Define #epsilon
Stefan Vogel <sv@exept.de>
parents: 7685
diff changeset
   761
!
bda9f88c6847 Define #epsilon
Stefan Vogel <sv@exept.de>
parents: 7685
diff changeset
   762
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   763
unity
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   764
    "return the neutral element for multiplication (1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   765
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   766
    ^ 1
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   767
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   768
    "Modified: 18.7.1996 / 12:26:43 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   769
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   770
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   771
zero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   772
    "return the neutral element for addition (0)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   773
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   774
    ^ 0
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   775
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   776
    "Modified: 18.7.1996 / 12:26:38 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   777
! !
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   778
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
   779
!Integer class methodsFor:'misc'!
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   780
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   781
displayRadix:aNumber
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
   782
    "being tired of always sending #printStringRadix: in the inspectors,
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   783
     this allows you to change the default print radix for the displayString
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   784
     method."
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   785
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   786
    DefaultDisplayRadix := aNumber
132
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   787
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   788
    "
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   789
     Integer displayRadix:16. 123456 inspect
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   790
     Integer displayRadix:10. 123456 inspect
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   791
    "
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   792
! !
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   793
9125
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   794
!Integer class methodsFor:'prime numbers'!
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   795
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   796
largePrimesUpTo: max do: aBlock
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   797
    "Evaluate aBlock with all primes up to maxValue.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   798
     The Algorithm is adapted from http://www.rsok.com/~jrm/printprimes.html
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   799
     It encodes prime numbers much more compactly than #primesUpTo: 
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   800
     38.5 integer per byte (2310 numbers per 60 byte) allow for some fun large primes.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   801
     (all primes up to SmallInteger maxVal can be computed within ~27MB of memory;
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   802
     the regular #primesUpTo: would require 4 *GIGA*bytes).
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   803
     Note: The algorithm could be re-written to produce the first primes (which require
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   804
     the longest time to sieve) faster but only at the cost of clarity."
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   805
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   806
    | limit flags maskBitIndex bitIndex maskBit byteIndex index primesUpTo2310 indexLimit |
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   807
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   808
    limit := max asInteger - 1.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   809
    indexLimit := max sqrt truncated + 1.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   810
    "Create the array of flags."
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   811
    flags := ByteArray new: (limit + 2309) // 2310 * 60 + 60.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   812
    flags atAllPut: 16rFF. "set all to true"
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   813
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   814
    "Compute the primes up to 2310"
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   815
    primesUpTo2310 := self primesUpTo: 2310.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   816
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   817
    "Create a mapping from 2310 integers to 480 bits (60 byte)"
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   818
    maskBitIndex := Array new: 2310.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   819
    bitIndex := -1. "for pre-increment"
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   820
    maskBitIndex at: 1 put: (bitIndex := bitIndex + 1).
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   821
    maskBitIndex at: 2 put: (bitIndex := bitIndex + 1).
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   822
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   823
    1 to: 5 do:[:i| aBlock value: (primesUpTo2310 at: i)].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   824
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   825
    index := 6.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   826
    2 to: 2309 do:[:n|
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   827
        [(primesUpTo2310 at: index) < n] 
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   828
            whileTrue:[index := index + 1].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   829
        n = (primesUpTo2310 at: index) ifTrue:[
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   830
            maskBitIndex at: n+1 put: (bitIndex := bitIndex + 1).
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   831
        ] ifFalse:[
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   832
            "if modulo any of the prime factors of 2310, then could not be prime"
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   833
            (n \\ 2 = 0 or:[n \\ 3 = 0 or:[n \\ 5 = 0 or:[n \\ 7 = 0 or:[n \\ 11 = 0]]]]) 
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   834
                    ifTrue:[maskBitIndex at: n+1 put: 0]
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   835
                    ifFalse:[maskBitIndex at: n+1 put: (bitIndex := bitIndex + 1)].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   836
        ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   837
    ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   838
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   839
    "Now the real work begins...
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   840
    Start with 13 since multiples of 2,3,5,7,11 are handled by the storage method;
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   841
    increment by 2 for odd numbers only."
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   842
    13 to: limit by: 2 do:[:n|
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   843
        (maskBit := maskBitIndex at: (n \\ 2310 + 1)) = 0 ifFalse:["not a multiple of 2,3,5,7,11"
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   844
            byteIndex := n // 2310 * 60 + (maskBit-1 bitShift: -3) + 1.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   845
            bitIndex := 1 bitShift: (maskBit bitAnd: 7).
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   846
            ((flags at: byteIndex) bitAnd: bitIndex) = 0 ifFalse:["not marked -- n is prime"
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   847
                aBlock value: n.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   848
                "Start with n*n since any integer < n has already been sieved 
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   849
                (e.g., any multiple of n with a number k < n has been cleared 
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   850
                when k was sieved); add 2 * i to avoid even numbers and
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   851
                mark all multiples of this prime. Note: n < indexLimit below
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   852
                limits running into LargeInts -- nothing more."
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   853
                n < indexLimit ifTrue:[
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   854
                    index := n * n.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   855
                    (index bitAnd: 1) = 0 ifTrue:[index := index + n].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   856
                    [index <= limit] whileTrue:[
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   857
                        (maskBit := maskBitIndex at: (index \\ 2310 + 1)) = 0 ifFalse:[
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   858
                            byteIndex := (index // 2310 * 60) + (maskBit-1 bitShift: -3) + 1.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   859
                            maskBit := 255 - (1 bitShift: (maskBit bitAnd: 7)).
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   860
                            flags at: byteIndex put: ((flags at: byteIndex) bitAnd: maskBit).
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   861
                        ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   862
                        index := index + (2 * n)].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   863
                ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   864
            ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   865
        ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   866
    ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   867
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   868
    "
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   869
     Integer largePrimesUpTo:1000000 do:[:i | i > 900000 ifTrue:[self halt] ]
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   870
     (Integer primesUpTo:1000000) inspect
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   871
    "
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   872
!
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   873
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   874
primesUpTo: max
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   875
    "Return a list of prime integers up to the given integer."
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   876
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   877
    ^Array streamContents:[:s| self primesUpTo: max do:[:prime| s nextPut: prime]]
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   878
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   879
    "
9316
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
   880
     Integer primesUpTo: 100 
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
   881
     (Integer primesUpTo: 100) select:[:p | (p >= 10) & (p <= 99)]
9125
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   882
    "
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   883
!
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   884
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   885
primesUpTo: max do: aBlock
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   886
    "Compute aBlock with all prime integers up to the given integer."
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   887
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   888
    | limit flags prime k |
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   889
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   890
    limit := max asInteger - 1.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   891
    "Fall back into #largePrimesUpTo:do: if we'd require more than 100k of memory; 
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   892
    the alternative will only requre 1/154th of the amount we need here and is almost as fast."
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   893
    limit > 25000 ifTrue:[^self largePrimesUpTo: max do: aBlock].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   894
    flags := (Array new: limit) atAllPut: true.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   895
    1 to: limit do: [:i |
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   896
        (flags at: i) ifTrue: [
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   897
            prime := i + 1.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   898
            k := i + prime.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   899
            [k <= limit] whileTrue: [
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   900
                flags at: k put: false.
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   901
                k := k + prime
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   902
            ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   903
            aBlock value: prime
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   904
        ]
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   905
    ].
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   906
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   907
    "
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   908
     Integer primesUpTo: 100
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   909
    "
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   910
! !
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
   911
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   912
!Integer class methodsFor:'queries'!
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   913
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   914
hasSharedInstances
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   915
    "return true if this class has shared instances, that is, instances
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   916
     with the same value are identical.
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   917
     Although not always shared (LargeIntegers), these should be treated
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   918
     so, to be independent of the number of bits in a SmallInt"
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   919
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   920
    ^ true
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   921
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   922
8892
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8635
diff changeset
   923
!
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8635
diff changeset
   924
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8635
diff changeset
   925
isAbstract
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8635
diff changeset
   926
    ^ self == Integer
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   927
! !
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   928
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7173
diff changeset
   929
!Integer methodsFor:'Compatibility-Dolphin'!
6249
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   930
6907
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   931
& aNumber
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   932
    ^ self bitAnd:aNumber
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   933
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   934
    "
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   935
     14 | 1 
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   936
     9 & 8 
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   937
    "
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   938
!
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   939
6249
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   940
highWord
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   941
    "return the high 16 bits of a 32 bit value"
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   942
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   943
    ^ self bitShift:-16
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   944
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   945
    "
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   946
     (16r12345678 highWord) hexPrintString 
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   947
     (16r12345678 lowWord) hexPrintString 
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   948
    "
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   949
!
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   950
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   951
lowWord
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   952
    "return the low 16 bits of a 32 bit value"
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   953
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   954
    ^ self bitAnd:16rFFFF
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   955
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   956
    "
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   957
     (16r12345678 lowWord) hexPrintString    
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   958
     (16r12345678 highWord) hexPrintString   
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   959
    "
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   960
!
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   961
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   962
mask:integerMask set:aBoolean
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   963
    "Answer the result of setting/resetting the specified mask in the receiver."
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   964
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   965
    ^ aBoolean 
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   966
            ifTrue:  [self maskSet:integerMask]
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   967
            ifFalse: [self maskClear:integerMask]
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   968
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   969
    "turn on the 1-bit:
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   970
         |v|
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   971
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   972
         v := 2r0100.
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   973
         v mask:1 set:true   
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   974
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   975
     turn off the 1-bit:
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   976
         |v|
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   977
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   978
         v := 2r0101.
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   979
         v mask:1 set:false   
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
   980
    "
6875
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   981
!
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   982
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   983
printStringRadix:aRadix padTo:sz
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   984
    ^ self printStringRadix:aRadix size:sz fill:$0
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   985
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   986
    "
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   987
     1024 printStringRadix:16 size:4 fill:$0
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   988
     1024 printStringRadix:2 size:16 fill:$.
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   989
     1024 printStringRadix:16 size:8 fill:(Character space)
d064224376bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6858
diff changeset
   990
    "
6907
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   991
!
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   992
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   993
| aNumber
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   994
    ^ self bitOr:aNumber
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   995
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   996
    "
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   997
     14 | 1 
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   998
     9 & 8 
a77451f0c14c dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6875
diff changeset
   999
    "
6249
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1000
! !
fa8cb1110cef dolphin
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1001
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7173
diff changeset
  1002
!Integer methodsFor:'Compatibility-Squeak'!
4867
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
  1003
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
  1004
atRandom
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
  1005
    "return a random number between 1 amd myself"
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
  1006
8573
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1007
    ^ self atRandom:Random.
8572
3e649d2e3167 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8461
diff changeset
  1008
3e649d2e3167 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8461
diff changeset
  1009
    "
3e649d2e3167 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8461
diff changeset
  1010
     100 atRandom
3e649d2e3167 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8461
diff changeset
  1011
     1000 atRandom
3e649d2e3167 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8461
diff changeset
  1012
    "
4867
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
  1013
!
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1014
8573
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1015
atRandom:aRandomGenerator
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1016
    "return a random number between 1 amd myself"
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1017
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1018
    self < 1 ifTrue:[^ self].
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1019
    ^ aRandomGenerator nextIntegerBetween:1 and:self
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1020
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1021
    "
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1022
     100 atRandom:(Random new)
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1023
     1000 atRandom:(Random new)
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1024
    "
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1025
!
240f0fa27d20 +atRandom:
Claus Gittinger <cg@exept.de>
parents: 8572
diff changeset
  1026
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1027
printStringBase:base
4867
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
  1028
    "return my printString in a base;
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
  1029
     same as printStringRadix:"
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
  1030
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1031
    ^ self printStringRadix:base
9068
ad6967cff1b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9057
diff changeset
  1032
!
ad6967cff1b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9057
diff changeset
  1033
9125
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1034
printStringHex
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1035
    "return my printString in base 16;
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1036
     same as printStringRadix:"
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1037
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1038
    ^ self printStringRadix:16
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1039
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1040
    "
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1041
     4096 printStringHex
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1042
    "
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1043
!
73e759072361 i pushed up;
Claus Gittinger <cg@exept.de>
parents: 9069
diff changeset
  1044
9068
ad6967cff1b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9057
diff changeset
  1045
printStringRoman
9069
e5ba0dc84a53 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9068
diff changeset
  1046
    "return my roman printString;
e5ba0dc84a53 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9068
diff changeset
  1047
     almost the same as romanPrintString:"
e5ba0dc84a53 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9068
diff changeset
  1048
9068
ad6967cff1b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9057
diff changeset
  1049
    "funny - although the romans did not have negative numbers - squeak has"
ad6967cff1b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9057
diff changeset
  1050
    self negative ifTrue:[
ad6967cff1b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9057
diff changeset
  1051
        ^ '-' , self negated romanPrintString
ad6967cff1b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9057
diff changeset
  1052
    ].
ad6967cff1b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9057
diff changeset
  1053
    ^ self romanPrintString
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1054
! !
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1055
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7173
diff changeset
  1056
!Integer methodsFor:'Compatibility-V''Age'!
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1057
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1058
<< aNumber
7155
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1059
    "V'Age compatibility: left shift"
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1060
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1061
    ^ self bitShift:aNumber
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1062
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1063
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1064
     1 << 5 
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1065
     64 << -5 
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1066
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1067
!
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1068
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1069
>> aNumber
7155
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1070
    "V'Age compatibility: right shift"
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1071
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1072
    ^ self bitShift:(aNumber negated)
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1073
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1074
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1075
     1 >> -5  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1076
     64 >> 5  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1077
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1078
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1079
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1080
!Integer methodsFor:'bcd conversion'!
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1081
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1082
decodeFromBCD
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1083
    "return a number representing the value of the BCD encoded receiver."
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1084
4424
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
  1085
    |v rslt multiplier nibble|
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1086
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1087
    v := self.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1088
    rslt := 0.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1089
    multiplier := 1.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1090
    [v > 0] whileTrue:[
4424
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
  1091
        nibble := v bitAnd:16r0F.
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
  1092
        nibble > 9 ifTrue:[
4996
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
  1093
            ^ BCDConversionErrorSignal 
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
  1094
                    raiseRequestWith:self
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
  1095
                    errorString:'bad BCD coded value'
4424
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
  1096
        ].
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
  1097
        rslt := rslt + (nibble * multiplier).
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1098
        multiplier := multiplier * 10.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1099
        v := v bitShift:-4.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1100
    ].
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1101
    ^ rslt
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1102
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1103
    "
4949
4c0462bd4d6a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4921
diff changeset
  1104
     16r1234567890123 decodeFromBCD
4921
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1105
     16r1073741823 decodeFromBCD       
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1106
     16r1073741824 decodeFromBCD       
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1107
     16r1073741825 decodeFromBCD       
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1108
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1109
     16r55 decodeFromBCD   
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1110
     16r127 decodeFromBCD    
4921
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1111
     16r800000 decodeFromBCD    
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1112
     16r8000000 decodeFromBCD    
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1113
     16r80000000 decodeFromBCD    
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1114
     16r800000000 decodeFromBCD    
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1115
     16r127567890 decodeFromBCD    
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1116
     16r1234567890 decodeFromBCD     
4424
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
  1117
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
  1118
     16r5A decodeFromBCD   
4996
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
  1119
     16rFF decodeFromBCD   
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1120
    "
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1121
4996
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
  1122
    "Modified: / 15.11.1999 / 20:37:20 / cg"
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1123
!
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1124
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1125
encodeAsBCD
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1126
    "return a BCD encoded number representing the same value as the
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1127
     receiver."
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1128
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1129
    |v rslt shift|
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1130
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1131
    v := self.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1132
    rslt := shift := 0.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1133
    [v > 0] whileTrue:[
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1134
        rslt := rslt + ((v \\ 10) bitShift:shift).
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1135
        shift := shift + 4.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1136
        v := v // 10.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1137
    ].
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1138
    ^ rslt
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1139
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1140
    "
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1141
     55 encodeAsBCD hexPrintString 
4921
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1142
     127 encodeAsBCD hexPrintString        
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1143
     127 encodeAsBCD hexPrintString        
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1144
     8912345 encodeAsBCD hexPrintString        
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1145
     89123456 encodeAsBCD hexPrintString        
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1146
     891234567 encodeAsBCD hexPrintString        
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1147
     900000000 encodeAsBCD hexPrintString        
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1148
     1073741823 encodeAsBCD hexPrintString       
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1149
     1073741824 encodeAsBCD hexPrintString       
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1150
     1073741825 encodeAsBCD hexPrintString       
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1151
     1891234567 encodeAsBCD hexPrintString        
9fd46bc1b6ab comments
Claus Gittinger <cg@exept.de>
parents: 4918
diff changeset
  1152
     8912345678 encodeAsBCD hexPrintString        
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1153
     1234567890 encodeAsBCD hexPrintString  
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1154
    "
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1155
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1156
! !
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
  1157
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1158
!Integer methodsFor:'bit operators'!
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1159
4643
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1160
allMask:anInteger
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1161
    "return true if all 1-bits in anInteger are also 1 in the receiver"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1162
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1163
    ^ (self bitAnd:anInteger) == anInteger
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1164
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1165
    "2r00001111 allMask:2r00000001"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1166
    "2r00001111 allMask:2r00011110"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1167
    "2r00001111 allMask:2r00000000"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1168
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1169
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1170
anyMask:anInteger
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1171
    "return true if any 1-bits in anInteger is also 1 in the receiver.
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1172
     (somewhat incorrect, if the mask is zero)"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1173
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1174
    ^ (self bitAnd:anInteger) ~~ 0
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1175
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1176
    "2r00001111 anyMask:2r00000001"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1177
    "2r00001111 anyMask:2r11110000"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1178
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1179
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1180
bitAnd:anInteger
357
claus
parents: 356
diff changeset
  1181
    "return the bitwise-and of the receiver and the argument, anInteger.
claus
parents: 356
diff changeset
  1182
     This is a general and slow implementation, walking over the bytes of
claus
parents: 356
diff changeset
  1183
     the receiver and the argument."
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1184
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1185
    |n "{ Class: SmallInteger }"
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1186
     result byte|
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1187
6358
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1188
    anInteger isInteger ifFalse:[
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1189
        ^ anInteger bitAndFromInteger:self.
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1190
    ].
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1191
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1192
    n := (anInteger digitLength) min:(self digitLength).
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1193
    result := self class basicNew numberOfDigits:n.
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1194
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1195
    1 to:n do:[:index |
6358
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1196
        byte := (anInteger digitAt:index) bitAnd:(self digitAt:index).
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1197
        result digitAt:index put:byte.
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1198
    ].
3162
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1199
    (byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[
6358
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1200
        ^ result compressed
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1201
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1202
    ^ result
357
claus
parents: 356
diff changeset
  1203
claus
parents: 356
diff changeset
  1204
    "
claus
parents: 356
diff changeset
  1205
     (16r112233445566778899 bitAnd:16rFF                ) printStringRadix:16 
claus
parents: 356
diff changeset
  1206
     (16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 
claus
parents: 356
diff changeset
  1207
     (16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 
claus
parents: 356
diff changeset
  1208
     (16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 
claus
parents: 356
diff changeset
  1209
    "
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  1210
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  1211
    "Modified: 5.11.1996 / 14:06:26 / cg"
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1212
!
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  1213
85
claus
parents: 77
diff changeset
  1214
bitAt:index
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1215
    "return the value of the index's bit (index starts at 1).
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  1216
     Notice: the result of bitAt: on negative receivers is not 
5496
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1217
             defined in the language standard (since the implementation
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1218
             is free to choose any internal representation for integers)"
85
claus
parents: 77
diff changeset
  1219
claus
parents: 77
diff changeset
  1220
    |i "{Class: SmallInteger}"|
claus
parents: 77
diff changeset
  1221
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  1222
    i := index - 1.
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  1223
    i < 0 ifTrue:[
5496
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1224
        ^ SubscriptOutOfBoundsSignal 
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1225
                raiseRequestWith:index
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1226
                errorString:'index out of bounds'
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1227
    ].
85
claus
parents: 77
diff changeset
  1228
    ^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1)
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1229
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1230
    "
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1231
     1 bitAt:1                     => 1 
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1232
     1 bitAt:2                     => 0
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1233
     1 bitAt:0                     index error
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1234
     2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1235
     2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1236
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1237
     (1 bitShift:1000) bitAt:1000  => 0
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1238
     (1 bitShift:1000) bitAt:1001  => 1
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1239
     (1 bitShift:1000) bitAt:1002  => 0
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1240
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1241
     (1 bitShift:30) bitAt:30    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1242
     (1 bitShift:30) bitAt:31    
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1243
     (1 bitShift:30) bitAt:32     
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1244
     (1 bitShift:31) bitAt:31     
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1245
     (1 bitShift:31) bitAt:32     
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1246
     (1 bitShift:31) bitAt:33    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1247
     (1 bitShift:32) bitAt:32    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1248
     (1 bitShift:32) bitAt:33    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1249
     (1 bitShift:32) bitAt:34    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1250
     (1 bitShift:64) bitAt:64     
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1251
     (1 bitShift:64) bitAt:65     
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1252
     (1 bitShift:64) bitAt:66     
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1253
    "
85
claus
parents: 77
diff changeset
  1254
!
claus
parents: 77
diff changeset
  1255
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1256
bitClear:anInteger
3884
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1257
    "return the bitwise-and of the receiver and the complement of argument, anInteger.
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1258
     This is a general and slow implementation, walking over the bytes of
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1259
     the receiver and the argument."
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1260
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1261
    |n "{ Class: SmallInteger }"
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1262
     result byte|
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1263
3884
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1264
    n := (anInteger digitLength) max:(self digitLength).
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1265
    result := self class basicNew numberOfDigits:n.
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1266
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1267
    1 to:n do:[:index |
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1268
        byte :=  (self digitAt:index) bitClear:(anInteger digitAt:index).
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1269
        result digitAt:index put:byte.
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1270
    ].
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1271
    (byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1272
        ^ result compressed
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1273
    ].
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
  1274
    ^ result
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1275
!
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1276
3455
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1277
bitInvert
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1278
    "return a new integer, where all bits are complemented.
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1279
     This does not really make sense for negative largeIntegers,
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1280
     since the digits are stored as absolute value."
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1281
4614
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1282
"/    ^ -1 - self
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1283
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1284
    |n "{ Class: SmallInteger }"
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1285
     result byte|
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1286
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1287
    n := self digitLength.
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1288
    result := self class basicNew numberOfDigits:n.
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1289
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1290
    1 to:n do:[:index |
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1291
        byte := self digitAt:index.
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1292
        result digitAt:index put:(byte bitInvert bitAnd:16rFF).
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1293
    ].
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1294
    (byte == 255 or:[n <= SmallInteger maxBytes]) ifTrue:[
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1295
        ^ result compressed
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1296
    ].
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1297
    ^ result
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1298
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1299
     "
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1300
      16rffffffff bitInvert     
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1301
      16rff00ff00 bitInvert hexPrintString  
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1302
     "
3455
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1303
!
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
  1304
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1305
bitInvert32
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1306
    "return a new integer, where the low32 bits are complemented."
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1307
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1308
    ^ self bitXor: 16rFFFFFFFF
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1309
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1310
    "
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1311
     16r80000000 bitInvert32 hexPrintString  
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1312
     16r7FFFFFFF bitInvert32 hexPrintString  
4614
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1313
     16rFFFFFFFF bitInvert32 hexPrintString  
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
  1314
     0 bitInvert32 hexPrintString   
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1315
    "
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1316
!
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
  1317
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1318
bitOr:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1319
    "return the bitwise-or of the receiver and the argument, anInteger.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1320
     This is a general and slow implementation, walking over the bytes of
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1321
     the receiver and the argument."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1322
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1323
    |n "{ Class: SmallInteger }"
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1324
     result byte|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1325
6358
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1326
    anInteger isInteger ifFalse:[
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1327
        ^ anInteger bitOrFromInteger:self.
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1328
    ].
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1329
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1330
    n := (anInteger digitLength) max:(self digitLength).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1331
    result := self class basicNew numberOfDigits:n.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1332
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1333
    1 to:n do:[:index |
6358
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1334
        byte := (anInteger digitAt:index) bitOr:(self digitAt:index).
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1335
        result digitAt:index put:byte.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1336
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1337
"/ no need to normalize - if the operands were correct
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1338
"/    byte == 0 ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1339
"/        ^ result normalize
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1340
"/    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1341
    ^ result
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1342
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1343
85
claus
parents: 77
diff changeset
  1344
bitShift:shiftCount
claus
parents: 77
diff changeset
  1345
    "return the value of the receiver shifted by shiftCount bits;
claus
parents: 77
diff changeset
  1346
     leftShift if shiftCount > 0; rightShift otherwise.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1347
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1348
     Notice: the result of bitShift: on negative receivers is not 
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1349
             defined in the language standard (since the implementation
7155
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1350
             is free to choose any internal representation for integers)
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1351
             However, ST/X preserves the sign."
85
claus
parents: 77
diff changeset
  1352
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1353
    |result 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1354
     prev       "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1355
     next       "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1356
     byte       "{ Class: SmallInteger }"
359
claus
parents: 357
diff changeset
  1357
     byte2      "{ Class: SmallInteger }"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1358
     bitShift   "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1359
     revShift   "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1360
     digitShift "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1361
     n          "{ Class: SmallInteger }" 
3044
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
  1362
     nn         "{ Class: SmallInteger }"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
  1363
     nDigits    "{ Class: SmallInteger }" |
85
claus
parents: 77
diff changeset
  1364
6358
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1365
    shiftCount isInteger ifFalse:[
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1366
        ^ shiftCount bitShiftFromInteger:self.
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1367
    ].
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1368
85
claus
parents: 77
diff changeset
  1369
    shiftCount > 0 ifTrue:[
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1370
        "left shift"
85
claus
parents: 77
diff changeset
  1371
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1372
        digitShift := shiftCount // 8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1373
        bitShift := shiftCount \\ 8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1374
        n := self digitLength.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1375
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1376
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1377
         modulo 8 shifts can be done faster ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1378
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1379
        bitShift == 0 ifTrue:[
5017
09d6baf12523 dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents: 5003
diff changeset
  1380
            n := n + digitShift.
09d6baf12523 dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents: 5003
diff changeset
  1381
            result := self class basicNew numberOfDigits:n.
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1382
            result sign:self sign.
5017
09d6baf12523 dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents: 5003
diff changeset
  1383
            result digitBytes replaceFrom:(digitShift + 1) to:n with:self digitBytes.
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1384
            "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1385
             no normalize needed, since receiver was already normalized
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1386
            "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1387
            ^ result
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1388
        ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1389
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1390
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1391
         less-than-8 shifts can be done faster ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1392
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1393
        digitShift == 0 ifTrue:[
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1394
            nn := n+1.
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1395
            result := self class basicNew numberOfDigits:nn.
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1396
            result sign:self sign.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1397
            prev := 0.
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1398
            1 to:n do:[:index |
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1399
                byte := self digitAt:index.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1400
                byte := (byte bitShift:bitShift) bitOr:prev.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1401
                result digitAt:index put:(byte bitAnd:16rFF).
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1402
                prev := byte bitShift:-8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1403
            ].
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1404
            result digitAt:nn put:prev.
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1405
            "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1406
             might have stored a 0-byte ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1407
            "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1408
            prev == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1409
                ^ result compressed
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1410
            ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1411
            ^ result.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1412
        ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1413
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1414
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1415
         slow case ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1416
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1417
        n := n + digitShift + 1.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1418
        result := self class basicNew numberOfDigits:n.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1419
        result sign:self sign.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1420
        byte := self digitAt:1.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1421
        byte := (byte bitShift:bitShift) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1422
        result digitAt:(digitShift + 1) put:byte.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1423
        revShift := -8 + bitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1424
        nDigits := self digitLength.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1425
        2 to:nDigits do:[:index |
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1426
            byte := self digitAt:index.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1427
            byte2 := self digitAt:index-1.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1428
            byte := byte bitShift:bitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1429
            byte2 := byte2 bitShift:revShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1430
            byte := (byte bitOr:byte2) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1431
            result digitAt:(index + digitShift) put:byte.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1432
        ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1433
        byte2 := self digitAt:nDigits.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1434
        byte2 := (byte2 bitShift:revShift) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1435
        result digitAt:(nDigits + digitShift + 1) put:byte2.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1436
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1437
         might have stored a 0-byte ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1438
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1439
        byte2 == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1440
            ^ result compressed
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1441
        ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1442
        ^ result
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1443
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1444
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1445
    shiftCount < 0 ifTrue:[
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1446
        "right shift"
85
claus
parents: 77
diff changeset
  1447
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1448
        digitShift := shiftCount negated // 8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1449
        bitShift := shiftCount negated \\ 8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1450
        n := self digitLength.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1451
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1452
        digitShift >= n ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1453
            ^ 0
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1454
        ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1455
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1456
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1457
         modulo 8 shifts can be done faster ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1458
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1459
        bitShift == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1460
            n := n-digitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1461
            result := self class basicNew numberOfDigits:n.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1462
            result sign:self sign.
4830
879f09ec443a Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents: 4803
diff changeset
  1463
            result digitBytes replaceFrom:1 to:n with:self digitBytes startingAt:(digitShift + 1) .
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1464
            n <= SmallInteger maxBytes ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1465
                ^ result compressed
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1466
            ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1467
            ^ result
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1468
        ].
85
claus
parents: 77
diff changeset
  1469
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1470
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1471
         less-than-8 shifts can be done faster ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1472
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1473
        digitShift == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1474
            result := self class basicNew numberOfDigits:n.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1475
            result sign:self sign.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1476
            prev := 0.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1477
            bitShift := bitShift negated.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1478
            revShift := 8 + bitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1479
            n to:1 by:-1 do:[:index |
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1480
                byte := self digitAt:index.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1481
                next := (byte bitShift:revShift) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1482
                byte := (byte bitShift:bitShift) bitOr:prev.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1483
                result digitAt:index put:(byte bitAnd:16rFF).
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1484
                prev := next.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1485
            ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1486
            (n <= 5) ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1487
                ^ result compressed
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1488
            ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1489
            ^ result
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1490
        ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1491
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1492
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1493
         slow case ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1494
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1495
        nn := n-digitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1496
        result := self class basicNew numberOfDigits:nn.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1497
        result sign:self sign.
85
claus
parents: 77
diff changeset
  1498
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1499
        prev := 0.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1500
        bitShift := bitShift negated.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1501
        revShift := 8 + bitShift.
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1502
        nn := digitShift + 1.
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1503
        n to:nn by:-1 do:[:index |
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1504
            byte := self digitAt:index.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1505
            next := (byte bitShift:revShift) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1506
            byte := (byte bitShift:bitShift) bitOr:prev.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1507
            result digitAt:(index - digitShift) put:byte.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1508
            prev := next.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1509
        ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1510
        "the last stored byte ..."
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1511
        ^ result compressed
85
claus
parents: 77
diff changeset
  1512
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1513
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1514
    ^ self "no shift"
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  1515
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
  1516
    "Modified: / 8.7.1998 / 12:45:24 / cg"
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1517
    "Modified: / 5.5.1999 / 16:05:05 / stefan"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1518
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1519
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1520
bitTest:anInteger
5782
dcc037e5bdbf comment change
Claus Gittinger <cg@exept.de>
parents: 5496
diff changeset
  1521
    "return true, if any bit from aMask is set in the receiver.
dcc037e5bdbf comment change
Claus Gittinger <cg@exept.de>
parents: 5496
diff changeset
  1522
     I.e. true, if the bitwise-AND of the receiver and the argument, anInteger
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1523
     is non-0, false otherwise.
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1524
     This is a general and slow implementation, walking over the bytes of
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1525
     the receiver and the argument."
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1526
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1527
    |n "{ Class: SmallInteger }"
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1528
     byte|
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1529
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1530
    n := (anInteger digitLength) min:(self digitLength).
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1531
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1532
    1 to:n do:[:index |
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1533
        byte := (anInteger digitAt:index) bitAnd:(self digitAt:index).
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1534
        byte ~~ 0 ifTrue:[^ true].
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1535
    ].
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1536
    ^ false
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1537
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1538
    "
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1539
     16r112233445566778899 bitTest:16rFF  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1540
     16r112233445566778800 bitTest:16rFF  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1541
     16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1542
     16r112233445566778899 bitTest:16rFF0000000000000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1543
     16r112233445566778899 bitTest:16r00000000000000FFFF  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1544
     16r1234567800000000 bitTest:16r8000000000000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1545
     16r8765432100000000 bitTest:16r8000000000000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1546
     16r12345678 bitTest:16r80000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1547
     16r87654321 bitTest:16r80000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1548
    "
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1549
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1550
    "Modified: / 6.6.1999 / 15:10:33 / cg"
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1551
!
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
  1552
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1553
bitXor:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1554
    "return the bitwise-or of the receiver and the argument, anInteger.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1555
     This is a general and slow implementation, walking over the bytes of
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1556
     the receiver and the argument."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1557
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1558
    |n "{ Class: SmallInteger }"
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1559
     result byte|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1560
6358
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1561
    anInteger isInteger ifFalse:[
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1562
        ^ anInteger bitXorFromInteger:self.
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1563
    ].
706234560a0b allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents: 6249
diff changeset
  1564
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1565
    n := (anInteger digitLength) max:(self digitLength).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1566
    result := self class basicNew numberOfDigits:n.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1567
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1568
    1 to:n do:[:index |
3993
a5a09b2d36cd oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents: 3908
diff changeset
  1569
        byte := (anInteger digitAt:index) bitXor:(self digitAt:index).
a5a09b2d36cd oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents: 3908
diff changeset
  1570
        result digitAt:index put:byte.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1571
    ].
3993
a5a09b2d36cd oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents: 3908
diff changeset
  1572
    (byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[
a5a09b2d36cd oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents: 3908
diff changeset
  1573
        ^ result compressed
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1574
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1575
    ^ result
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1576
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1577
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1578
     (16r112233445566778899 bitXor:16rFF                ) printStringRadix:16 '112233445566778866' 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1579
     (16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799'
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1580
     (16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899'
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1581
     (16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1582
    "
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  1583
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  1584
    "Modified: 5.11.1996 / 14:06:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1585
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1586
5210
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1587
changeBit:index to:aBooleanOrNumber
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1588
    "return a new number where the specified bit is on or off,
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1589
     sepending on aBooleanOrNumber.
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1590
     Bits are counted from 1 starting with the least significant.
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1591
     The methods name may be missleading: the receiver is not changed,
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1592
     but a new number is returned. Should be named #withBitChanged:to:"
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1593
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1594
    (aBooleanOrNumber == 0 or:[aBooleanOrNumber == false]) ifTrue:[
8289
b0798ee8027f oops - bitClear/clearBit consusion
werner
parents: 7727
diff changeset
  1595
        ^ self clearBit:index
5210
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1596
    ].
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1597
    ^ self setBit:index
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1598
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1599
    "
8289
b0798ee8027f oops - bitClear/clearBit consusion
werner
parents: 7727
diff changeset
  1600
     (16r3fffffff changeBit:31 to:1) hexPrintString   
b0798ee8027f oops - bitClear/clearBit consusion
werner
parents: 7727
diff changeset
  1601
     (16r3fffffff asLargeInteger setBit:31) hexPrintString   
5210
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1602
    "
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1603
!
f56e448396f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5209
diff changeset
  1604
3683
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1605
clearBit:index
8937
4f2508548327 comment
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1606
    "return a new integer where the specified bit is off.
5209
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1607
     Bits are counted from 1 starting with the least significant.
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1608
     The methods name may be missleading: the receiver is not changed,
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1609
     but a new number is returned. Should be named #withBitCleared:"
3683
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1610
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1611
    |n         "{ Class: SmallInteger }"
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1612
     byteIndex "{ Class: SmallInteger }"
8937
4f2508548327 comment
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1613
     bitIndex  "{ Class: SmallInteger }"
3683
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1614
     result byte|
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1615
5496
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1616
    index <= 0 ifTrue:[
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1617
        ^ SubscriptOutOfBoundsSignal 
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1618
                raiseRequestWith:index
9057
13bd29f579c6 Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents: 8937
diff changeset
  1619
                errorString:'bit index out of bounds'
5496
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1620
    ].
3683
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1621
    byteIndex := ((index - 1) // 8) + 1.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1622
    n := self digitLength.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1623
    byteIndex > n ifTrue:[
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1624
        ^ self
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1625
    ].
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1626
9057
13bd29f579c6 Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents: 8937
diff changeset
  1627
    result := self simpleDeepCopy.
3683
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1628
    bitIndex := ((index - 1) \\ 8) + 1.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1629
    byte := (result digitAt:byteIndex) clearBit:bitIndex.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1630
    result digitAt:byteIndex put:byte.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1631
    (byte == 0 or:[n == byteIndex and:[n <= SmallInteger maxBytes]]) ifTrue:[
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1632
        ^ result compressed
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1633
    ].
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1634
    ^ result
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1635
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1636
    "
9057
13bd29f579c6 Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents: 8937
diff changeset
  1637
     3111111111 clearBit:1
3683
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1638
    "
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1639
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1640
    "Modified: / 28.7.1998 / 18:35:50 / cg"
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1641
!
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
  1642
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1643
even
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1644
    "return true if the receiver is even"
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1645
9401
65f884297c68 speed up #even and #odd
Stefan Vogel <sv@exept.de>
parents: 9316
diff changeset
  1646
    ^ (self bitAt:1) == 0
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1647
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1648
    "
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1649
     16r112233445566778899 even  
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1650
     16r112233445566778800 even  
5435
ad6612cc6848 oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1651
     1 even  
ad6612cc6848 oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1652
     2 even  
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1653
    "
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1654
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1655
    "Created: / 6.6.1999 / 15:00:40 / cg"
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1656
!
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1657
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1658
highBit
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1659
    "return the bitIndex of the highest bit set. The returned bitIndex
6481
d5c94e93067c highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6480
diff changeset
  1660
     starts at 1 for the least significant bit. 
d5c94e93067c highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6480
diff changeset
  1661
     Returns 0 if no bit is set."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1662
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1663
    |byteNr highByte|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1664
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1665
    byteNr := self digitLength.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1666
    byteNr == 0 ifTrue:[
6481
d5c94e93067c highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6480
diff changeset
  1667
        ^ 0
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1668
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1669
    highByte := self digitAt:byteNr.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1670
    ^ (byteNr - 1) * 8 + highByte highBit
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1671
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1672
    "
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1673
     0 highBit
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1674
     -1 highBit
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1675
     (1 bitShift:1) highBit     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1676
     (1 bitShift:30) highBit    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1677
     (1 bitShift:31) highBit    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1678
     (1 bitShift:32) highBit    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1679
     (1 bitShift:33) highBit    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1680
     (1 bitShift:64) highBit     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1681
     (1 bitShift:1000) highBit   
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1682
     (1 bitShift:1000) negated highBit   
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1683
     ((1 bitShift:64)-1) highBit  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1684
    "
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1685
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1686
    "Modified: / 3.5.1999 / 09:20:57 / stefan"
2429
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1687
!
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1688
5496
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1689
invertBit:index
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1690
    "return a new number where the specified bit is inverted.
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1691
     Bits are counted from 1 starting with the least significant.
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1692
     The methods name may be missleading: the receiver is not changed,
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1693
     but a new number is returned. Should be named #withBitInverted:"
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1694
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1695
    index <= 0 ifTrue:[
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1696
        ^ SubscriptOutOfBoundsSignal 
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1697
                raiseRequestWith:index
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1698
                errorString:'index out of bounds'
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1699
    ].
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1700
    ^ self bitXor:(1 bitShift:index-1)
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1701
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1702
    "
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1703
     0 invertBit:3         => 4 (2r100)
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1704
     0 invertBit:48        => 140737488355328 (2r1000.....000)
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1705
     ((0 invertBit:99) invertBit:100) printStringRadix:2  
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1706
    "
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1707
!
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1708
5209
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1709
isBitSet:index
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1710
    "return true if the index' bit is set; false otherwise.
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1711
     Bits are counted from 1 starting with the least significant."
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1712
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1713
    ^ (self bitAt:index) ~~ 0
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1714
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1715
    "
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1716
     5 isBitSet:3       => true
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1717
     2r0101 isBitSet:2  => false
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1718
     2r0101 isBitSet:1  => true
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1719
     2r0101 isBitSet:0  index error
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1720
    "
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1721
!
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1722
2429
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1723
lowBit
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1724
    "return the bitIndex of the lowest bit set. The returned bitIndex
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6358
diff changeset
  1725
     starts at 1 for the least significant bit. 
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6358
diff changeset
  1726
     Returns 0 if no bit is set."
2429
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1727
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1728
    |maxBytes "{ Class: SmallInteger }"
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1729
     byte|
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1730
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1731
    maxBytes := self digitLength.
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1732
    1 to:maxBytes do:[:byteIndex |
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6358
diff changeset
  1733
        byte := self digitAt:byteIndex.
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6358
diff changeset
  1734
        byte ~~ 0 ifTrue:[
7545
bf682add17b7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7444
diff changeset
  1735
            ^ (byteIndex-1)*8 + (byte lowBit)
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6358
diff changeset
  1736
        ].
2429
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1737
    ].
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6358
diff changeset
  1738
    ^ 0 "/ should not happen
2429
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1739
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1740
    "
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1741
     0 lowBit                  
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1742
     1 lowBit                  
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1743
     (1 bitShift:1) lowBit     
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1744
     (1 bitShift:1) highBit     
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1745
     (1 bitShift:30) lowBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1746
     (1 bitShift:30) highBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1747
     (1 bitShift:31) lowBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1748
     (1 bitShift:31) highBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1749
     (1 bitShift:32) lowBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1750
     (1 bitShift:32) highBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1751
     (1 bitShift:33) lowBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1752
     (1 bitShift:33) highBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1753
     (1 bitShift:64) lowBit     
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1754
     (1 bitShift:64) highBit     
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1755
     (1 bitShift:1000) lowBit   
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1756
     (1 bitShift:1000) highBit   
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1757
     ((1 bitShift:64)-1) lowBit  
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1758
     ((1 bitShift:64)-1) highBit  
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1759
    "
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1760
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1761
    "Modified: 1.3.1997 / 16:54:23 / cg"
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1762
!
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1763
4643
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1764
maskClear:aMask
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1765
    ^ self bitAnd:aMask bitInvert
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1766
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1767
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1768
maskSet:aMask
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1769
    ^ self bitOr:aMask
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1770
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1771
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1772
noMask:anInteger
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1773
    "return true if no 1-bit in anInteger is 1 in the receiver"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1774
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1775
    ^ (self bitAnd:anInteger) == 0
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1776
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1777
    "2r00001111 noMask:2r00000001"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1778
    "2r00001111 noMask:2r11110000"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1779
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1780
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1781
odd
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1782
    "return true if the receiver is odd"
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1783
9401
65f884297c68 speed up #even and #odd
Stefan Vogel <sv@exept.de>
parents: 9316
diff changeset
  1784
    ^ (self bitAt:1) == 1
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1785
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1786
    "
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1787
     16r112233445566778899 odd  
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1788
     16r112233445566778800 odd  
5435
ad6612cc6848 oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1789
     1 odd  
ad6612cc6848 oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1790
     2 odd  
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1791
    "
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1792
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1793
    "Created: / 6.6.1999 / 15:00:55 / cg"
5209
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1794
!
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1795
7155
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1796
rightShift:shiftCount
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1797
    "return the value of the receiver shifted right by shiftCount bits;
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1798
     rightShift if shiftCount > 0; leftShift otherwise.
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1799
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1800
     Notice: the result of bitShift: on negative receivers is not 
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1801
             defined in the language standard (since the implementation
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1802
             is free to choose any internal representation for integers)
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1803
             However, ST/X preserves the sign."
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1804
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1805
    ^ self bitShift:(shiftCount negated)
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1806
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1807
    "
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1808
     16r100000000 rightShift:1              
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1809
     16r100000000 negated rightShift:1      
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1810
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1811
     16r100000000 rightShift:2              
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1812
     16r100000000 negated rightShift:2      
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1813
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1814
     16r100000000 rightShift:3              
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1815
     16r100000000 negated rightShift:3      
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1816
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1817
     ((16r100000000 rightShift:1) rightShift:1) rightShift:1     
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1818
     ((16r100000000 negated rightShift:1) rightShift:1) rightShift:1  
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1819
    "
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1820
!
8112e11f850a +rightShift:
Claus Gittinger <cg@exept.de>
parents: 7093
diff changeset
  1821
5209
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1822
setBit:index
8937
4f2508548327 comment
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1823
    "return a new integer, where the specified bit is on.
5209
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1824
     Bits are counted from 1 starting with the least significant.
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1825
     The methods name may be missleading: the receiver is not changed,
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1826
     but a new number is returned. Should be named #withBitCleared:"
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1827
5496
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1828
    index <= 0 ifTrue:[
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1829
        ^ SubscriptOutOfBoundsSignal 
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1830
                raiseRequestWith:index
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1831
                errorString:'index out of bounds'
338c81a04468 index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents: 5469
diff changeset
  1832
    ].
5209
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1833
    ^ self bitOr:(1 bitShift:index-1)
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1834
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1835
    "
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1836
     0 setBit:3         => 4 (2r100)
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1837
     0 setBit:48        => 140737488355328 (2r1000.....000)
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1838
     ((0 setBit:99) setBit:100) printStringRadix:2  
3a52e2f97ec4 setBit/clearBit.
Claus Gittinger <cg@exept.de>
parents: 5186
diff changeset
  1839
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1840
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1841
3890
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1842
!Integer methodsFor:'byte access'!
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1843
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1844
digitByteLength
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1845
    "return the number bytes required for a 2's complement
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1846
     binary representation of this Integer.
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1847
     For positive receivers, thats the same as the digitLength."
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1848
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1849
    "
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1850
     check if there is a 0-byte ...
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1851
     this allows to ask unnormalized LargeIntegers 
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1852
     for their digitLength
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1853
    "
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1854
    |l "{ Class: SmallInteger }" |
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1855
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1856
    self >= 0 ifTrue:[^ self digitLength].
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1857
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1858
    l := self negated digitLength.
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1859
    (self digitByteAt:l) == 16rFF ifTrue:[
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1860
        ^ (l - 1) max:1
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1861
    ].
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1862
    ^ l
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1863
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1864
    "
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1865
     -129 digitByteLength 
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1866
    "
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1867
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1868
    "Created: / 25.10.1998 / 14:58:09 / cg"
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1869
    "Modified: / 25.10.1998 / 19:20:33 / cg"
10488
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1870
!
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1871
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1872
digitBytes
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1873
    "return a byteArray filled with the receivers bits
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1874
     (8 bits of the absolute value per element),
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1875
     least significant byte is first"
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1876
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1877
    ^ self subclassResponsibility
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1878
!
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1879
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1880
digitBytesMSB:msbFlag
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1881
    "return a byteArray filled with the receivers bits
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1882
     (8 bits of the absolute value per element),
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1883
     if msbflag = true, most significant byte is first,
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1884
     otherwise least significant byte is first"
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1885
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1886
    msbFlag ifTrue:[
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1887
        ^ self digitBytes copyReverse.  "digitBytes may be shared - reverse a copy!!"
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1888
    ].
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1889
    ^ self digitBytes
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1890
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1891
    "
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1892
      16r12 digitBytesMSB:true
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1893
      16r1234 digitBytesMSB:true
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1894
      16r1234 digitBytesMSB:false
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1895
      16r12345678 digitBytesMSB:true
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1896
      16r12345678 digitBytesMSB:false
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  1897
    "
3890
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1898
! !
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1899
5238
f7a816a660a3 categories
Claus Gittinger <cg@exept.de>
parents: 5210
diff changeset
  1900
!Integer methodsFor:'coercing & converting'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1901
1888
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1902
asFixedPoint
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1903
    "return the receiver as a fixedPoint number"
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1904
6640
2de8925ad2cd fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents: 6639
diff changeset
  1905
    ^ FixedPoint basicNew
2de8925ad2cd fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents: 6639
diff changeset
  1906
        setNumerator:self denominator:1 scale:1
1888
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1907
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1908
    "
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1909
     100 asFixedPoint      
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1910
     100 asFixedPoint + 0.1 asFixedPoint    
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1911
    "
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1912
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1913
    "Modified: 5.11.1996 / 15:13:17 / cg"
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1914
!
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1915
2141
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1916
asFixedPoint:scale
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1917
    "return the receiver as fixedPoint number, with the given number
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1918
     of post-decimal-point digits."
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1919
6639
d88a51b081ae fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents: 6635
diff changeset
  1920
    ^ FixedPoint basicNew
d88a51b081ae fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents: 6635
diff changeset
  1921
        setNumerator:self denominator:1 scale:scale
2141
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1922
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1923
    "
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1924
     100 asFixedPoint:2      
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1925
     100 asFixedPoint + (0.1 asFixedPoint:2)    
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1926
    "
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1927
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1928
    "Modified: 10.1.1997 / 20:00:08 / cg"
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1929
!
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1930
7444
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1931
asFloat
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1932
    "return a Float with same value as myself.
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1933
     Since floats have a limited precision, you usually loose bits when
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1934
     doing this."
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1935
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1936
    ^ Float fromInteger:self
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1937
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1938
    "
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1939
     1234567890 asFloat                     
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1940
     1234567890 asFloat asInteger                    
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1941
     12345678901234567890 asFloat           
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1942
     12345678901234567890 asFloat asInteger   
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1943
    "
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1944
!
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1945
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1946
asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1947
    "return a Fraction with same value as receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1948
2794
862ee34a4821 when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1949
    ^ Fraction basicNew setNumerator:self denominator:1
862ee34a4821 when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1950
862ee34a4821 when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1951
    "Modified: 28.7.1997 / 19:26:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1952
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1953
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1954
asInteger
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1955
    "return the receiver truncated towards zero - 
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1956
     for integers this is self"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1957
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1958
    ^ self
1337
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1959
!
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1960
7444
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1961
asLargeFloat
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1962
    "return a LargeFloat with same value as myself.
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1963
     Since largeFloats have a limited precision, you usually loose bits when
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1964
     doing this."
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1965
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1966
    ^ LargeFloat fromInteger:self
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1967
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1968
    "
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1969
     1234567890 asLargeFloat                     
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1970
     1234567890 asLargeFloat asInteger                    
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1971
     12345678901234567890 asLargeFloat           
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1972
     12345678901234567890 asLargeFloat asInteger   
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1973
    "
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1974
!
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1975
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1976
asLongFloat
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1977
    "return a LongFloat with same value as myself.
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1978
     Since longFloats have a limited precision, you usually loose bits when
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1979
     doing this."
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1980
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1981
    ^ LongFloat fromInteger:self
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1982
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1983
    "
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1984
     1234567890 asFloat                     
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1985
     1234567890 asFloat asInteger                    
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1986
     12345678901234567890 asFloat           
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1987
     12345678901234567890 asFloat asInteger   
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1988
    "
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1989
!
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1990
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1991
asModuloNumber
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1992
    "return a precomputed modulo number"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1993
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1994
    ^ ModuloNumber modulus:self.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1995
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1996
    "Created: / 3.5.1999 / 14:48:03 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1997
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1998
7444
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1999
asShortFloat
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  2000
    "return a ShortFloat with same value as receiver"
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  2001
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  2002
    ^ ShortFloat fromInteger:self
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  2003
!
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  2004
1337
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2005
signExtendedByteValue
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2006
    "return a smallInteger from sign-extending the 8'th bit.
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2007
     May be useful for communication interfaces"
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2008
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2009
    ^ (self bitAnd:16rFF) signExtendedByteValue
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2010
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2011
    "Created: 7.5.1996 / 09:31:52 / cg"
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2012
!
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2013
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2014
signExtendedShortValue
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2015
    "return a smallInteger from sign-extending the 16'th bit.
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2016
     May be useful for communication interfaces"
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2017
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2018
    ^ (self bitAnd:16rFFFF) signExtendedShortValue
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2019
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  2020
    "Modified: 7.5.1996 / 09:31:57 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2021
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2022
1976
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2023
!Integer methodsFor:'comparing'!
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2024
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2025
hash
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2026
    "redefined to return smallInteger hashValues"
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2027
3298
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  2028
    ^ self bitAnd:16r3FFFFFFF.
1976
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2029
3298
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  2030
    "
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  2031
        -20000000000000 hash 
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  2032
         20000000000000 hash
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  2033
    "
1976
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2034
3298
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  2035
    "Created: / 14.11.1996 / 12:12:27 / cg"
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  2036
    "Modified: / 24.2.1998 / 10:07:29 / stefan"
1976
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2037
! !
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  2038
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2039
!Integer methodsFor:'double dispatching'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2040
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2041
differenceFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2042
    "sent when a fraction does not know how to subtract the receiver, an integer"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2043
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2044
    |d|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2045
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2046
    d := aFraction denominator.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2047
    ^ aFraction class
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2048
        numerator:(aFraction numerator - (self * d))
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2049
        denominator:d
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  2050
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  2051
    "Modified: 28.7.1997 / 19:08:30 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2052
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2053
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2054
equalFromFraction:aFraction
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2055
    "that should never be invoked, as fractions are always normalized to integers
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2056
     if resulting from an arithmetic operation.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2057
     However, this implementation is for subclasses (i.e. fixed point) and also
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2058
     allows comparing unnormalized fractions as might appear within the fraction class"
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2059
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2060
    |denominator numerator|
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2061
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2062
    denominator := aFraction denominator.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2063
    numerator := aFraction numerator.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2064
    (denominator == 1) ifFalse:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2065
        ^ numerator = (self * denominator)
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2066
    ].
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2067
    ^ numerator = self
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2068
!
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2069
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2070
productFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2071
    "sent when a fraction does not know how to multiply the receiver, an integer"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2072
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2073
    ^ aFraction class
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2074
        numerator:(self * aFraction numerator)
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2075
        denominator:aFraction denominator
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  2076
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  2077
    "Modified: 28.7.1997 / 19:08:27 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2078
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2079
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2080
quotientFromFraction:aFraction
7444
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  2081
    "Return the quotient of the argument, aFraction and the receiver.
e228b1ba6d6a double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  2082
     Sent when aFraction does not know how to divide by the receiver."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2083
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2084
    ^ aFraction class
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2085
        numerator:aFraction numerator
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2086
        denominator:(self * aFraction denominator)
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  2087
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  2088
    "Modified: 28.7.1997 / 19:08:23 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2089
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2090
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2091
sumFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2092
    "sent when a fraction does not know how to add the receiver, an integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2093
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2094
    |d|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2095
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2096
    d := aFraction denominator.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2097
    ^ aFraction class
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2098
        numerator:(aFraction numerator + (self * d))
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2099
        denominator:d
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  2100
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  2101
    "Modified: 28.7.1997 / 19:08:11 / cg"
10681
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2102
!
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2103
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2104
sumFromTimestamp:aTimestamp
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2105
    "I am to be interpreted as seconds, return the timestamp this number of seconds
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2106
     after aTimestamp"
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2107
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2108
    ^ aTimestamp addSeconds:self.
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2109
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2110
    "
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2111
     Timestamp now addSeconds:100
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2112
     100 sumFromTimestamp:Timestamp now
835be355e638 adding timeDurations to a timeStamp (double dispatch)
sr
parents: 10488
diff changeset
  2113
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2114
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2115
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2116
!Integer methodsFor:'helpers'!
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2117
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2118
gcd_helper:anInteger 
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2119
    "a helper for the greatest common divisor of the receiver and anInteger.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2120
     Knuth's algorithm for large positive integers, with receiver being
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2121
     larger than the arg."
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2122
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2123
    | selfAbs argAbs selfLowBit argLowBit shift t |
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2124
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2125
    selfAbs := self.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2126
    argAbs := anInteger.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2127
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2128
    selfLowBit := selfAbs lowBit - 1.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2129
    argLowBit := argAbs lowBit - 1.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2130
    shift := selfLowBit min:argLowBit.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2131
    argAbs := argAbs bitShift:(argLowBit negated).
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2132
    [selfAbs = 0] whileFalse:[
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2133
	selfAbs := selfAbs bitShift:(selfLowBit negated).
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2134
	selfAbs < argAbs ifTrue:[
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2135
	    t := selfAbs. selfAbs := argAbs. argAbs := t
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2136
	].
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2137
	selfAbs := selfAbs - argAbs.
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2138
	selfLowBit := selfAbs lowBit - 1.
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2139
    ].
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2140
    ^ argAbs bitShift:shift
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2141
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2142
    "Created: 1.3.1997 / 16:38:17 / cg"
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2143
    "Modified: 1.3.1997 / 16:45:17 / cg"
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2144
! !
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2145
10027
36281068b212 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9401
diff changeset
  2146
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2147
!Integer methodsFor:'misc math'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2148
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2149
acker:n
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2150
    "return the value of acker(self, n).
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2151
      ;-) Do not try with receivers > 3"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2152
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2153
    (self == 0) ifTrue:[^ n + 1].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2154
    (n == 0) ifTrue:[^ (self - 1) acker: 1].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2155
    ^ (self - 1) acker:(self acker:(n - 1))
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2156
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2157
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2158
     3 acker:2  
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2159
     3 acker:7  
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2160
    "
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2161
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2162
    "Modified: 18.7.1996 / 13:08:16 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2163
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2164
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2165
divMod:aNumber
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2166
    "return an array filled with self // aNumber and
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2167
     self \\ aNumber. 
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2168
     The result is only defined for positive receiver and argument.
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2169
     This may be redefined in some integer classes for
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2170
     more performance (where the remainder is generated as a side effect of division)"
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2171
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2172
    ^ Array
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2173
	with:(self // aNumber)
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2174
	with:(self \\ aNumber)
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2175
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2176
    "  
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2177
     10 divMod:3  
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2178
     100 factorial divMod:103
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2179
    "
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2180
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2181
    "Modified: 29.10.1996 / 21:18:58 / cg"
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2182
!
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2183
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2184
extendedEuclid:tb
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2185
    "return the solution of 'ax + by = gcd(a,b)'.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2186
     An array containing x, y and gcd(a,b) is returned."
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2187
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2188
    |a b gcd gcd1 u u1 v v1 tmp t swap shift "{SmallInteger}"|
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2189
5001
732ea614d36d Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 4996
diff changeset
  2190
    self < tb ifTrue:[
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2191
        a := self.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2192
        b := tb.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2193
        swap := false.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2194
    ] ifFalse:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2195
        a := tb.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2196
        b := self.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2197
        swap := true.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2198
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2199
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2200
    shift := ((a lowBit) min:(b lowBit))-1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2201
    shift > 0 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2202
        tmp := shift negated.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2203
        a := a bitShift:tmp.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2204
        b := b bitShift:tmp.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2205
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2206
9057
13bd29f579c6 Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents: 8937
diff changeset
  2207
    gcd  := a.
13bd29f579c6 Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents: 8937
diff changeset
  2208
    gcd1 := b.        
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2209
    u := 1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2210
    u1 := 0.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2211
    v := 0.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2212
    v1 := 1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2213
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2214
    [
5001
732ea614d36d Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 4996
diff changeset
  2215
"/      The following condition is true:
732ea614d36d Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 4996
diff changeset
  2216
"/        (a * u1) + (b * v1) ~= gcd1 ifTrue:[self halt].
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2217
        t := gcd1 divMod:gcd.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2218
        gcd1 := gcd.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2219
        gcd := t at:2.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2220
        t := t at:1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2221
        tmp := v.
5003
304be9a3a226 Add some debug in #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 5001
diff changeset
  2222
"/v1 - (v * t) - v1 + (v * t) ~= 0 ifTrue:[self halt].
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2223
        v := v1 - (v * t).
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2224
        v1 := tmp.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2225
        tmp := u.
5003
304be9a3a226 Add some debug in #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 5001
diff changeset
  2226
"/u1 - (u * t) - u1 + (u * t) ~= 0 ifTrue:[self halt].
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2227
        u := u1 - (u * t).
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2228
        u1 := tmp.
9057
13bd29f579c6 Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents: 8937
diff changeset
  2229
    gcd > 0] whileTrue.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2230
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2231
    shift > 0 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2232
        gcd1 := gcd1 bitShift:shift.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2233
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2234
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2235
    swap ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2236
        ^ Array with:v1 with:u1 with:gcd1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2237
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2238
    ^ Array with:u1 with:v1 with:gcd1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2239
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2240
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2241
    " 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2242
     14 extendedEuclid:5
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2243
     14 extendedEuclid:2  
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2244
     25 extendedEuclid:15
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2245
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2246
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2247
    "Created: / 27.4.1999 / 15:19:22 / stefan"
5001
732ea614d36d Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 4996
diff changeset
  2248
    "Modified: / 18.11.1999 / 16:19:24 / stefan"
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2249
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2250
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2251
factorial
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2252
    "return fac(self) (i.e. 1*2*3...*self) using an iterative algorithm.
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2253
     This is slightly faster than the recursive algorithm, and does not
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2254
     suffer from stack overflow problems (with big receivers)"
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2255
2745
bdf76e380d27 errorString to DomainError
Claus Gittinger <cg@exept.de>
parents: 2458
diff changeset
  2256
    |p i|
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2257
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2258
    (self < 2) ifTrue:[
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2259
        self < 0 ifTrue:[
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2260
            "/
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2261
            "/ requested factorial of a negative number
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2262
            "/
3395
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  2263
            ^ self class
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  2264
                raise:#domainErrorSignal
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  2265
                receiver:self
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  2266
                selector:#factorial
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2267
                arguments:#()
3395
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  2268
                errorString:'factorial of negative number'
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2269
        ].
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2270
        ^ 1
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2271
    ].
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2272
    p := 2.
2745
bdf76e380d27 errorString to DomainError
Claus Gittinger <cg@exept.de>
parents: 2458
diff changeset
  2273
    i := 3.
bdf76e380d27 errorString to DomainError
Claus Gittinger <cg@exept.de>
parents: 2458
diff changeset
  2274
    [i <= self] whileTrue:[
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2275
        p := p * i.
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2276
        i := i + 1.
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2277
    ].
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2278
    ^ p
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2279
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2280
    "  
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2281
     10 factorial
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2282
     1000 factorial
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2283
     10000 factorial
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2284
     10000 factorialR
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2285
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2286
     Time millisecondsToRun:[2000 factorial] 
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2287
     Time millisecondsToRun:[2000 factorialR] 
2745
bdf76e380d27 errorString to DomainError
Claus Gittinger <cg@exept.de>
parents: 2458
diff changeset
  2288
     -1 factorial
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2289
    "
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2290
10027
36281068b212 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9401
diff changeset
  2291
    "Modified: / 04-10-2006 / 14:31:12 / cg"
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2292
!
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2293
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2294
factorialR
3908
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  2295
    "return fac(self) (i.e. 1*2*3...*self) using a recursive algorithm.
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  2296
     This is included to demonstration purposes - if you really need
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  2297
     factorial numbers, use the iterative #factorial, which is slightly
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  2298
     faster and does not suffer from stack overflow problems (with big receivers)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2299
356
claus
parents: 345
diff changeset
  2300
    (self >= 2) ifTrue:[
3908
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  2301
        ^ self * (self - 1) factorialR
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2302
    ].
356
claus
parents: 345
diff changeset
  2303
    ^ 1
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
  2304
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
  2305
    "
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2306
     10 factorialR
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2307
     1000 factorialR
4158
e04b61500e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2308
     Time millisecondsToRun:[10000 factorial]
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
  2309
    "
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  2310
3908
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  2311
    "Created: / 18.7.1996 / 12:48:36 / cg"
4158
e04b61500e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2312
    "Modified: / 8.5.1999 / 18:40:13 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2313
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2314
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2315
gcd:anInteger 
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2316
    "return the greatest common divisor of the receiver and anInteger.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2317
     Euclids & Knuths algorithm."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2318
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  2319
    |selfAbs argAbs t|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2320
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2321
    selfAbs := self abs.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2322
    argAbs := anInteger abs.
1888
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  2323
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2324
    selfAbs < argAbs ifTrue:[
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2325
	t := selfAbs.
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2326
	selfAbs := argAbs.
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2327
	argAbs := t.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2328
    ].
52
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  2329
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2330
    argAbs = 0 ifTrue: [^ selfAbs].
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2331
    selfAbs := selfAbs \\ argAbs.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2332
    selfAbs = 0 ifTrue:[^ argAbs].
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  2333
    ^ argAbs gcd_helper:selfAbs
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2334
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  2335
    "                     
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  2336
     3141589999999999 gcd:1000000000000000
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  2337
    "
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  2338
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  2339
    "Modified: / 25.10.1997 / 16:08:45 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2340
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2341
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2342
integerReciprocal
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2343
    "return an integer representing 1/self * 2**n.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2344
     Where an integer is one bit longer than self."
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2345
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2346
    |b rem result|                                   
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2347
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2348
    b := self highBit.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2349
    rem := 1 bitShift:b.
10488
8c254af93816 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 10030
diff changeset
  2350
    result := LargeInteger basicNew numberOfDigits:(b // 8)+1.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2351
    b := b+1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2352
    [b > 0] whileTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2353
        rem >= self ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2354
            rem := rem -= self.
4830
879f09ec443a Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents: 4803
diff changeset
  2355
            result digitBytes bitSetAt:b.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2356
        ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2357
        rem := rem mul2.               
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2358
        b := b - 1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2359
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2360
    ^ result compressed.          
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2361
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2362
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2363
    "                                     
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2364
     333 integerReciprocal                (2 raisedTo:18) // 333
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2365
     393 integerReciprocal          
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2366
     8 integerReciprocal              
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2367
     15 integerReciprocal 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2368
     15112233445566 integerReciprocal 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2369
     10239552311579 integerReciprocal
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2370
   "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2371
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2372
    "Modified: / 3.5.1999 / 14:27:18 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2373
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2374
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2375
inverseMod:n
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2376
    "find the modular inverse for myself to n.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2377
     This is defined as the solution of: '1 = (self * x) mod n"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2378
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2379
    |e ret|
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2380
5001
732ea614d36d Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 4996
diff changeset
  2381
    "the following expression returns #(x y (self gcd:n)), the solution of the equation
732ea614d36d Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 4996
diff changeset
  2382
     (self * x) + (n * y) = self gcd:n"
732ea614d36d Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 4996
diff changeset
  2383
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2384
    e := self extendedEuclid:n.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2385
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2386
    (e at:3) == 1 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2387
        ret := e at:1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2388
        ret negative ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2389
            ^ ret + n
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2390
        ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2391
        ^ ret.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2392
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2393
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2394
    ^ 0
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2395
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2396
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2397
     14 inverseMod:5      -> 4
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2398
     5 inverseMod:14      -> 3
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2399
     14 inverseMod:11     -> 4                (4 * 14) \\ 11 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2400
     11 inverseMod:14     -> 9                (9 * 11) \\ 14 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2401
     79 inverseMod:3220   -> 1019
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2402
     3220 inverseMod:79   -> 54               (54 * 3220) \\ 79
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2403
     1234567891 inverseMod:1111111111119          
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2404
                          -> 148726663534     (148726663534*1234567891) \\ 1111111111119
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2405
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2406
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2407
     14 extendedEuclid:11 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2408
     5 extendedEuclid:14 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2409
     14 extendedEuclid:2
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2410
     3220 extendedEuclid:79    
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2411
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2412
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2413
    "Created: / 27.4.1999 / 15:19:22 / stefan"
5001
732ea614d36d Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents: 4996
diff changeset
  2414
    "Modified: / 18.11.1999 / 16:21:37 / stefan"
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2415
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  2416
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2417
lcm:anInteger
52
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  2418
    "return the least common multiple (using gcd:)"
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  2419
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  2420
    ^ (self * anInteger) abs // (self gcd: anInteger)
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  2421
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2422
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2423
     65 lcm:15
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2424
     3 lcm:15
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2425
    "
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2426
!
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2427
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2428
raisedTo:exp mod:mod
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2429
    "return the modulu (remainder) of 
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2430
     the receiver raised to exp (an Integer) and mod (another Integer)"
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2431
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2432
    |result m t 
5816
ce15ce5294b4 Cleanup unused method vars
Stefan Vogel <sv@exept.de>
parents: 5799
diff changeset
  2433
"/     eI   "{Class: SmallInteger}"
ce15ce5294b4 Cleanup unused method vars
Stefan Vogel <sv@exept.de>
parents: 5799
diff changeset
  2434
     bits "{Class: SmallInteger}"|
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2435
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2436
    "use the addition chaining algorithm"
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2437
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2438
    exp == 0 ifTrue:[
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2439
        ^ 1
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2440
    ].
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2441
    exp == 1 ifTrue:[
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2442
        ^ self \\ mod.
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2443
    ].
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2444
    exp negative ifTrue:[
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2445
        ^ self class
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2446
            raise:#domainErrorSignal
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2447
            receiver:self
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2448
            selector:#raisedTo:mod:
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2449
            arguments:(Array with:exp with:mod)
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2450
            errorString:'modulo arithmethic with negative exponent'
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2451
    ].
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2452
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2453
    m := mod asModuloNumber.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2454
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2455
    result := 1.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2456
    t := m modulusOf:self.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2457
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2458
    "/ to be validated ...
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2459
"/    (exp class == SmallInteger) ifTrue:[
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2460
"/        eI := exp.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2461
"/        [eI ~~ 0] whileTrue:[
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2462
"/            (eI bitAnd:1) == 1 ifTrue:[
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2463
"/                result := m modulusOf:(result * t).
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2464
"/            ].
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2465
"/            t := m modulusOf:(t * t).
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2466
"/            eI := eI bitShift:-1.
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2467
"/        ].
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2468
"/        ^ result.
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2469
"/    ].
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2470
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2471
    bits := exp highBit.
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2472
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2473
    1 to:bits-1 do:[:i|
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2474
        (exp bitAt:i) == 1 ifTrue:[
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2475
            result := m modulusOf:(result * t).
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2476
        ].
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2477
        t := m modulusOf:(t * t).
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2478
    ].
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2479
    result := m modulusOf:(result * t).
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2480
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2481
    ^ result
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2482
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2483
    "                                       
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2484
     2 raisedTo:2 mod:3  
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2485
      20000000000000 raisedTo:200 mod:190  ->  30
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2486
     (20000000000000 raisedTo:200) \\ 190 
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2487
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2488
      Time millisecondsToRun:[10000 timesRepeat:[
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2489
                                20000000000000 raisedTo:200 mod:190
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2490
                              ]
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2491
                             ]      
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2492
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  2493
     Time millisecondsToRun:[1000 timesRepeat:[
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2494
                                (20000000000000 raisedTo:200) \\ 190
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2495
                             ]
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2496
                            ]    
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2497
    "
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2498
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2499
    "Created: / 27.4.1999 / 15:19:22 / stefan"
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2500
    "Modified: / 5.5.1999 / 11:18:20 / stefan"
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6055
diff changeset
  2501
    "Modified: / 16.11.2001 / 14:15:21 / cg"
4973
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2502
!
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2503
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2504
raisedToCrtModP:p q:q ep:ep eq:eq u:u
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2505
    "Application of the Chinese Remainder Theorem (CRT).
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2506
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2507
     This is a faster modexp for moduli with a known factorisation into two
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2508
     relatively prime factors p and q, and an input relatively prime to the
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2509
     modulus, the Chinese Remainder Theorem to do the computation mod p and
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2510
     mod q, and then combine the results.  This relies on a number of
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2511
     precomputed values, but does not actually require the modulus n or the
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2512
     exponent e.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2513
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2514
     expout = expin ^ e mod (p*q).
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2515
     We form this by evaluating
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2516
     p2 = (expin ^ e) mod p and
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2517
     q2 = (expin ^ e) mod q
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2518
     and then combining the two by the CRT.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2519
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2520
     Two optimisations of this are possible.  First, we can reduce expin
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2521
     modulo p and q before starting.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2522
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2523
     Second, since we know the factorisation of p and q (trivially derived
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2524
     from the factorisation of n = p*q), and expin is relatively prime to
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2525
     both p and q, we can use Euler's theorem, expin^phi(m) = 1 (mod m),
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2526
     to throw away multiples of phi(p) or phi(q) in e.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2527
     Letting ep = e mod phi(p) and
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2528
              eq = e mod phi(q)
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2529
     then combining these two speedups, we only need to evaluate
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2530
     p2 = ((expin mod p) ^ ep) mod p and
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2531
     q2 = ((expin mod q) ^ eq) mod q.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2532
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2533
     Now we need to apply the CRT.  Starting with
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2534
     expout = p2 (mod p) and
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2535
     expout = q2 (mod q)
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2536
     we can say that expout = p2 + p * k, and if we assume that 0 <= p2 < p,
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2537
     then 0 <= expout < p*q for some 0 <= k < q.  Since we want expout = q2
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2538
     (mod q), then p*k = q2-p2 (mod q).  Since p and q are relatively prime,
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2539
     p has a multiplicative inverse u mod q.  In other words, u = 1/p (mod q).
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2540
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2541
     Multiplying by u on both sides gives k = u*(q2-p2) (mod q).
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2542
     Since we want 0 <= k < q, we can thus find k as
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2543
     k = (u * (q2-p2)) mod q.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2544
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2545
     Once we have k, evaluating p2 + p * k is easy, and
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2546
     that gives us the result
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2547
    "  
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2548
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2549
    |result t mp mq|
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2550
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2551
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2552
    mp := p asModuloNumber.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2553
    t := mp modulusOf:self.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2554
    result := t raisedTo:ep mod:mp.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2555
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2556
    mq := q asModuloNumber.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2557
    t := mq modulusOf:self.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2558
    t := t raisedTo:eq mod:mq.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2559
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2560
    "now p2 is in result, q2 in t"
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2561
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2562
    t := t -= result.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2563
    t < 0 ifTrue:[
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2564
        t := t + q.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2565
    ].
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2566
    t := t *= u.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2567
    t := mq modulusOf:t.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2568
    t := t *= p.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2569
    result := result += t.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2570
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2571
    ^ result.
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2572
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2573
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2574
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2575
    "                                       
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2576
     2 raisedTo:2 mod:3
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2577
      20000000000000 raisedTo:200 mod:190
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2578
     (20000000000000 raisedTo:200) \\ 190
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2579
    "
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2580
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2581
    "Created: / 30.4.1999 / 15:53:15 / stefan"
2bbd09250179 Fix CRT
Stefan Vogel <sv@exept.de>
parents: 4972
diff changeset
  2582
    "Modified: / 5.5.1999 / 11:01:15 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2583
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2584
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2585
!Integer methodsFor:'printing & storing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2586
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2587
asBCDBytes
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2588
    "return a byteArray containing the receiver in BCD encoding.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2589
     The byteArray will contain the BCD encoded decimal string,
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2590
     starting with the most significant digits first.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2591
     This conversion is useful for some communication protocols,
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2592
     or control systems, which represent big numbers this way...
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2593
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2594
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2595
    |s rest twoDigits hi lo|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2596
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2597
    self == 0 ifTrue:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2598
        ^ #[ 16r00 ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2599
    ].
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2600
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2601
    "/ a very rough estimate on the final size ...
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2602
    s := (ByteArray new:(self digitLength * 2)) writeStream.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2603
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2604
    rest := self.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2605
    [rest > 0] whileTrue:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2606
        twoDigits := rest \\ 100.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2607
        rest := rest // 100.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2608
        hi := twoDigits \\ 10.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2609
        lo := twoDigits // 10.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2610
        s nextPut:(lo bitShift:4)+hi
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2611
    ].
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2612
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2613
    ^ s contents reverse
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2614
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2615
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2616
     12345678 asBCDBytes 
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2617
     12345678901234567890 asBCDBytes
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2618
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2619
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2620
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2621
!
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2622
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2623
displayString
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2624
    "return a string for displaying in a view (as in inspector).
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2625
     The output radix is usually 10, but can be changed by setting
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2626
     DefaultDisplayRadix (see Integer>>displayRadix:)"
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2627
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
  2628
    (DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  2629
	^ self printString
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
  2630
    ].
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2631
    ^ self radixPrintStringRadix:DefaultDisplayRadix
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2632
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2633
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2634
     Integer displayRadix:16. 12345 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2635
     Integer displayRadix:2.  12345 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2636
     Integer displayRadix:10. 12345
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2637
    "
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2638
!
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  2639
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2640
errorPrintHex
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2641
    "print the receiver as a hex number on the standard error stream"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2642
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2643
     (self printStringRadix:16) errorPrint
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2644
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2645
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2646
hexPrintString
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2647
    "return a hex string representation of the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2648
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2649
    ^ self printStringRadix:16
3874
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  2650
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  2651
    "
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  2652
     127 hexPrintString
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  2653
     -1 hexPrintString
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  2654
    "
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  2655
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  2656
    "Modified: / 11.10.1998 / 01:15:43 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2657
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2658
2408
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  2659
hexPrintString:size
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  2660
    "return a hex string representation of the receiver,
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  2661
     padded to size characters"
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  2662
5799
547c417b23cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5782
diff changeset
  2663
"/    ^ (self printStringRadix:16) leftPaddedTo:size with:$0
547c417b23cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5782
diff changeset
  2664
    ^ self printStringRadix:16 size:size fill:$0
547c417b23cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5782
diff changeset
  2665
547c417b23cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5782
diff changeset
  2666
    "
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2667
     12345 hexPrintString:4   
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2668
     123 hexPrintString:4   
5799
547c417b23cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5782
diff changeset
  2669
    "
2408
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  2670
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  2671
    "Created: 18.2.1997 / 13:32:33 / cg"
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  2672
!
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  2673
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2674
printHex
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2675
    "print the receiver as a hex number on the standard output stream"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2676
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2677
     (self printStringRadix:16) print
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2678
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2679
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2680
printOn:aStream
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2681
    "append a printed description of the receiver to aStream"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2682
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2683
    self printOn:aStream base:10
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2684
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2685
    "Modified: / 20.1.1998 / 14:10:45 / stefan"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2686
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2687
4522
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  2688
printOn:aStream base:b
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2689
    "return a string representation of the receiver in the specified
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2690
     radix (without the initial XXr)"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2691
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2692
    ^ self printOn:aStream base:b showRadix:false
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2693
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2694
    "
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2695
     10 printOn:Transcript base:3
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2696
     31 printOn:Transcript base:3
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2697
     -20 printOn:Transcript base:16
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2698
     -20 printOn:Transcript base:10
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2699
     3000 factorial printOn:Transcript base:10
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2700
    "
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2701
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2702
    "Modified: / 20.1.1998 / 18:05:02 / stefan"
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2703
    "Modified: / 7.9.2001 / 13:52:17 / cg"
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2704
!
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2705
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2706
printOn:aStream base:b showRadix:showRadix
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2707
    "append a string representation of the receiver in the specified numberBase to aStream
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2708
     (if showRadix is true, with initial XXr)
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2709
     The radix argument should be between 2 and 36."
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2710
4522
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  2711
    |base num s divMod mod r r2 r4 nD numN|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2712
4522
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  2713
    base := b.
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2714
    (base between:2 and:36) ifFalse:[
4522
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  2715
        self error:'invalid base' mayProceed:true.
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  2716
        base := 10.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
  2717
    ].
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
  2718
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2719
    showRadix ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2720
        base printOn:aStream.
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2721
        aStream nextPutAll:'r'.    
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2722
    ].
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2723
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2724
    (self = 0) ifTrue:[aStream nextPut:$0. ^ self].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2725
    (self < 0) ifTrue:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2726
        aStream nextPut:$- . 
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2727
        num := self negated.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2728
    ] ifFalse:[
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2729
        num := self.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2730
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2731
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2732
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2733
     claus: changed it from a recursive algorithm;
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2734
     (it used to trigger stack-overflow exceptions when printing
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2735
      3000 factorial ...)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2736
    "
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2737
"/    leftPart := num // base.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2738
"/    (leftPart ~= 0) ifTrue:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2739
"/        leftPart printOn:aStream base:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2740
"/        aStream nextPut:(Character digitValue:(num \\ base).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2741
"/        ^ self
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2742
"/    ].
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2743
"/    aStream nextPut:(Character digitValue:num).
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  2744
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2745
    "/ instead of computing the quotient and remainder
3136
91dbf1ea13a6 print in junks of 6 digits (instead of 8 digits)
Claus Gittinger <cg@exept.de>
parents: 3135
diff changeset
  2746
    "/ against radix, do it in junks of 5 or 6 digits.
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2747
    "/ This reduces the number of LargeInt-divisions
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2748
    "/ by that factor (turning them into smallInt divisions
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2749
    "/ within that junk) and speeds up the conversions noticably.
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2750
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2751
    r2 := base*base.   "/ radix^2
3138
1cd38a2d1924 oops - printString
ca
parents: 3136
diff changeset
  2752
    r4 := r2*r2.             "/ radix^4
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2753
    base <= 10 ifTrue:[
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2754
        r := r4*r2.        "/ radix^6
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2755
        nD := 6.
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2756
    ] ifFalse:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2757
        r := r4*base.    "/ radix^5
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2758
        nD := 5.
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2759
    ].
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2760
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2761
    "get a Stream with space for the digits we are going to print.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2762
     We need ((num log:base) ceiling) digits, which is equivalent
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2763
     to ((num log:2)/(base log:2) ceiling)
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2764
    "
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2765
    s := WriteStream on:(String new:((num highBit // base highBit - 1) + 1)).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2766
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  2767
    [num >= r] whileTrue:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2768
        "/
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2769
        "/ chop off nD digits.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2770
        "/
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2771
        divMod := num divMod:r.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2772
        num := divMod at:1.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2773
        numN := divMod at:2.
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  2774
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2775
        "/ process them
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2776
        nD timesRepeat:[
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2777
            divMod := numN divMod:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2778
            numN := divMod at:1.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2779
            mod := divMod at:2.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2780
            s nextPut:(Character digitValue:mod).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2781
        ].
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  2782
    ].
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2783
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2784
    [num ~= 0] whileTrue:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2785
        divMod := num divMod:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2786
        num := divMod at:1.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2787
        mod := divMod at:2.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2788
        s nextPut:(Character digitValue:mod).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2789
    ].
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2790
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2791
    aStream nextPutAll:(s contents reverse).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2792
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2793
    "
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2794
        3000 factorial printOn:Transcript base:10
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2795
        10 printOn:Transcript base:3
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2796
        31 printOn:Transcript base:3
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2797
        -20  printOn:Transcript base:16
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2798
        -20  printOn:Transcript base:10
6515
f35f8c519c30 comment
Claus Gittinger <cg@exept.de>
parents: 6481
diff changeset
  2799
        Time millisecondsToRun:[10000 factorial printString]   7650
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2800
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2801
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2802
    "Modified: / 20.1.1998 / 18:05:02 / stefan"
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2803
    "Created: / 7.9.2001 / 13:51:33 / cg"
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2804
    "Modified: / 7.9.2001 / 13:54:50 / cg"
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2805
!
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2806
7173
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2807
printOn:aStream base:baseInteger size:sz fill:fillCharacter
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2808
    "print a string representation of the receiver in the specified
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2809
     base. The string is padded on the left with fillCharacter to make
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2810
     its size as specified in sz."
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2811
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2812
    |stream string actualSize|
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2813
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2814
    stream := '' writeStream.
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2815
    self printOn:stream base:baseInteger showRadix:false.
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2816
    string := stream contents.
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2817
    actualSize := string size.
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2818
    actualSize < sz ifTrue:[
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2819
        aStream next:sz-actualSize put:fillCharacter.
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2820
    ].
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2821
    aStream nextPutAll:string.
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2822
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2823
    "
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2824
     1024 printOn:Transcript base:16 size:4 fill:$0
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2825
     1024 printOn:Transcript base:2 size:16 fill:$.
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2826
     1024 printOn:Transcript base:16 size:8 fill:Character space.
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2827
    "
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2828
!
a1d65d39dbfb *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 7155
diff changeset
  2829
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2830
printOn:aStream radix:base
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2831
    "append a printed description of the receiver to aStream.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2832
     The receiver is printed in radix base (instead of the default, 10).
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2833
     This method is obsoleted by #printOn:base:, which is ST-80 & ANSI compatible."
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2834
8897
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
  2835
    <resource: #obsolete>
da8d205a93aa Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents: 8892
diff changeset
  2836
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2837
    self printOn:aStream base:base
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2838
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2839
    "Modified: / 20.1.1998 / 14:10:45 / stefan"
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2840
    "Modified: / 7.9.2001 / 13:58:29 / cg"
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2841
!
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2842
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2843
printRomanOn:aStream
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2844
    "print the receiver as roman number to the receiver, aStream.
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2845
     This converts correct (i.e. prefix notation for 4,9,40,90, etc.)."
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2846
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2847
    ^ self printRomanOn:aStream naive:false
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2848
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2849
    "
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2850
     1 to:10 do:[:i | i printRomanOn:Transcript. Transcript cr.].
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2851
     1999 printRomanOn:Transcript. Transcript cr.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2852
     Date today year printRomanOn:Transcript. Transcript cr.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2853
    "
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2854
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2855
    "test all between 1 and 9999:
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2856
      1 to:9999 do:[:n |
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2857
        |romanString|
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2858
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2859
        romanString := String streamContents:[:stream | n printRomanOn:stream].
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2860
        (Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2861
     ]
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2862
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2863
!
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2864
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2865
printRomanOn:aStream naive:naive
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2866
    "print the receiver as roman number to the receiver, aStream.
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2867
     The naive argument controls if the conversion is
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
  2868
     correct (i.e. subtracting prefix notation for 4,9,40,90, etc.),
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
  2869
     or naive (i.e. print 4 as IIII and 9 as VIIII); also called simple.
6673
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2870
     The naive version is often used for page numbers in documents."
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2871
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2872
    |restValue spec|
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2873
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2874
    restValue := self.
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2875
    restValue > 0 ifFalse:[self error:'negative roman'].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2876
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2877
    naive ifTrue:[
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2878
        spec := #(
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2879
                " value string repeat "    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2880
                   1000 'M'    true
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2881
                    500 'D'    false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2882
                    100 'C'    true
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2883
                     50 'L'    false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2884
                     10 'X'    true
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2885
                      5 'V'    false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2886
                      1 'I'    true
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2887
                 ).
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2888
    ] ifFalse:[
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2889
        spec := #(
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2890
                " value string repeat "    
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2891
                   1000 'M'    true
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2892
                    900 'CM'   false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2893
                    500 'D'    false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2894
                    400 'CD'   false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2895
                    100 'C'    true
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2896
                     90 'XC'   false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2897
                     50 'L'    false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2898
                     40 'XL'   false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2899
                     10 'X'    true
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2900
                      9 'IX'   false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2901
                      5 'V'    false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2902
                      4 'IV'   false
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2903
                      1 'I'    true
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2904
                 ).
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2905
    ].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2906
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2907
    spec 
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2908
        inGroupsOf:3 
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2909
        do:[:rValue :rString :repeatFlag |
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2910
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2911
            [
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2912
                (restValue >= rValue) ifTrue:[
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2913
                    aStream nextPutAll:rString.
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2914
                    restValue := restValue - rValue.
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2915
                ].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2916
            ] doWhile:[ repeatFlag and:[ restValue >= rValue] ].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2917
        ].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2918
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2919
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2920
     1 to:10 do:[:i | i printRomanOn:Transcript naive:false. Transcript cr.].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2921
     1 to:10 do:[:i | i printRomanOn:Transcript naive:true. Transcript cr.].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2922
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2923
     1999 printRomanOn:Transcript. Transcript cr.
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2924
     Date today year printRomanOn:Transcript. Transcript cr.
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2925
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2926
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2927
    "test all between 1 and 9999:
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2928
      1 to:9999 do:[:n |
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2929
        |romanString|
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2930
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2931
        romanString := String streamContents:[:stream | n printRomanOn:stream naive:false].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2932
        (Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt].
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2933
     ]
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2934
    "
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2935
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2936
    "test naive all between 1 and 9999:
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2937
      1 to:9999 do:[:n |
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2938
        |romanString|
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2939
34547f06c5c7 roman numbers revisited
Claus Gittinger <cg@exept.de>
parents: 6640
diff changeset
  2940
        romanString := String streamContents:[:stream | n printRomanOn:stream naive:true].
6682
9a97f4deb836 more on roman numbers
Claus Gittinger <cg@exept.de>
parents: 6673
diff changeset
  2941
        (Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt].
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2942
     ]
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2943
    "
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2944
!
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  2945
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2946
printStringRadix:base
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2947
    "return a string representation of the receiver in the specified
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2948
     base; does NOT prepend XXr to the string.
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2949
     See also: radixPrintStringRadix:
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2950
               printOn:base:showRadix:"
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2951
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2952
    |s|
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2953
7685
535a69a7cd69 String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents: 7551
diff changeset
  2954
    s := WriteStream on:(String basicNew:20).
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2955
    self printOn:s base:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2956
    ^ s contents
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2957
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2958
    "Created: / 19.1.1998 / 17:20:58 / stefan"
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2959
    "Modified: / 20.1.1998 / 14:10:54 / stefan"
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2960
    "Modified: / 7.9.2001 / 13:58:13 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2961
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2962
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2963
printStringRadix:aRadix size:sz fill:fillCharacter
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2964
    "return a string representation of the receiver in the specified
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2965
     radix. The string is padded on the left with fillCharacter to make
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2966
     its size as specified in sz."
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2967
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  2968
    |s actualSize|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2969
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2970
    s := self printStringRadix:aRadix.
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  2971
    actualSize := s size.
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  2972
    actualSize < sz ifTrue:[
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  2973
	s := ((String new:(sz - actualSize)) atAllPut:fillCharacter) , s
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2974
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2975
    ^ s
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2976
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2977
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2978
     1024 printStringRadix:16 size:4 fill:$0
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2979
     1024 printStringRadix:2 size:16 fill:$.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2980
     1024 printStringRadix:16 size:8 fill:(Character space)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2981
    "
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  2982
!
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  2983
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2984
radixPrintStringRadix:radix
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2985
    "return a string representation of the receiver in the specified
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2986
     base; prepend XXr to the string"
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2987
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2988
    |s|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2989
7685
535a69a7cd69 String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents: 7551
diff changeset
  2990
    s := WriteStream on:(String basicNew:20).
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2991
    self printOn:s base:radix showRadix:true.
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2992
    ^ s contents
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  2993
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2994
    "
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2995
     31 radixPrintStringRadix:2   
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2996
     31 radixPrintStringRadix:3    
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2997
     31 radixPrintStringRadix:10    
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2998
     31 radixPrintStringRadix:16    
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  2999
     31 radixPrintStringRadix:36   
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3000
    "
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  3001
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  3002
    "Created: / 19.1.1998 / 17:38:00 / stefan"
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  3003
    "Modified: / 20.1.1998 / 14:11:03 / stefan"
5988
Claus Gittinger <cg@exept.de>
parents: 5882
diff changeset
  3004
    "Modified: / 7.9.2001 / 13:57:00 / cg"
6635
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3005
!
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3006
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3007
romanPrintString
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3008
    "return a roman number representation of the receiver as a string"
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3009
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3010
    ^ String streamContents:[:stream | self printRomanOn:stream].
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3011
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3012
    "
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3013
     1999 romanPrintString.    
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3014
     Date today year romanPrintString.
3b5525d83d5d roman numbers
Claus Gittinger <cg@exept.de>
parents: 6534
diff changeset
  3015
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  3016
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  3017
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3018
!Integer methodsFor:'queries'!
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  3019
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3020
digitAt:n
6055
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3021
    "return the n-th byte of the binary representation."
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3022
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3023
    self subclassResponsibility
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3024
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3025
    "This is a very stupid implementation, and should be redefined in
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3026
     concrete subclasses."
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  3027
6055
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3028
"/    |num count|
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3029
"/
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3030
"/    num := self.
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3031
"/    count := n.
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3032
"/    [count > 1] whileTrue:[
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3033
"/        num := num // 256.
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3034
"/        count := count - 1
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3035
"/    ].
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3036
"/    ^ num \\ 256
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  3037
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  3038
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3039
     16r44332211 digitAt:1     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3040
     16r44332211 digitAt:2    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3041
     16r44332211 digitAt:3     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3042
     16r44332211 digitAt:4     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3043
     16r44332211 digitAt:5     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3044
     16r00332211 digitAt:4     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3045
     16r00332211 digitAt:5     
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  3046
    "
6055
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3047
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3048
    "Modified: / 26.9.2001 / 21:21:21 / cg"
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3049
!
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3050
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3051
digitByteAt:n
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3052
    "return 8 bits of my signed value, starting at byte index.
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3053
     For positive receivers, this is the same as #digitAt:;
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3054
     for negative ones, the actual bit representation is returned."
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3055
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3056
    self subclassResponsibility
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3057
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3058
    "Created: / 26.9.2001 / 21:18:43 / cg"
e4d5016bad38 no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents: 5988
diff changeset
  3059
    "Modified: / 26.9.2001 / 21:20:19 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  3060
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  3061
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3062
digitLength
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3063
    "return the number of bytes needed for the binary representation
2816
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  3064
     of the receiver.
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  3065
     This method is redefined in concrete classes - the fallback here is
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  3066
     never really used."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3067
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3068
    ^ (self log:256) ceiling asInteger
2816
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  3069
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  3070
    "Modified: 31.7.1997 / 13:19:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3071
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3072
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3073
isInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3074
    "return true, if the receiver is some kind of integer number"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3075
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3076
    ^ true
3036
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3077
!
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3078
9134
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3079
isPowerOf:p
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3080
    "return true, if the receiver is a power of p"
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3081
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3082
    p == 2 ifTrue:[ ^ self isPowerOfTwo].
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3083
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3084
    "/ the following is a q&d hack, using existing code.
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3085
    ^ (Integer
9136
90f12ada7068 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9134
diff changeset
  3086
        readFromString:(self printStringRadix:p)
9134
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3087
        radix:2
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3088
        onError:-1) isPowerOfTwo
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3089
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3090
    "
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3091
     16r0000000000000000 isPowerOf:2  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3092
     16r0000004000000000 isPowerOf:2   
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3093
     16r0000004000000001 isPowerOf:2  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3094
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3095
     16r0000000000000001 isPowerOf:2  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3096
     16r0000000000000002 isPowerOf:2  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3097
     16r0000000000000004 isPowerOf:2  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3098
     16r0000000000000008 isPowerOf:2  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3099
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3100
     16r0000000000000001 isPowerOf:4  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3101
     16r0000000000000002 isPowerOf:4  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3102
     16r0000000000000004 isPowerOf:4  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3103
     16r0000000000000008 isPowerOf:4  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3104
     16r0000000000000010 isPowerOf:4  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3105
     16r0000000000000020 isPowerOf:4  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3106
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3107
     3r0000000000000001 isPowerOf:3  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3108
     3r0000000000000010 isPowerOf:3  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3109
     3r0000000000000100 isPowerOf:3  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3110
     3r0000000000001000 isPowerOf:3  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3111
     3r0000000000001001 isPowerOf:3  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3112
     3r0000000000002000 isPowerOf:3  
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3113
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3114
     10 isPowerOf:10           
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3115
     20 isPowerOf:10           
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3116
     100 isPowerOf:10          
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3117
     110 isPowerOf:10          
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3118
     200 isPowerOf:10          
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3119
     1000 isPowerOf:10         
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3120
     10000 isPowerOf:10        
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3121
     100000 isPowerOf:10       
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3122
     100001 isPowerOf:10       
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3123
 "
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3124
!
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3125
3036
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3126
isPowerOfTwo
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3127
    "return true, if the receiver is a power of 2"
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3128
9134
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3129
"/ mhmh: how about the following
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3130
"/    self == 0 ifTrue:[^ false].
98d5bfd1cc51 +isPowerOf:
Claus Gittinger <cg@exept.de>
parents: 9126
diff changeset
  3131
3036
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3132
    ^ (self bitAnd:(self - 1)) == 0
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3133
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3134
    "
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3135
     0 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3136
     1 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3137
     2 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3138
     3 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3139
     4 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3140
     16r8000000000000000 isPowerOfTwo
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3141
     16r8000000000000001 isPowerOfTwo
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3142
    "
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3143
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  3144
    "Modified: 15.10.1997 / 18:43:49 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3145
! !
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  3146
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3147
!Integer methodsFor:'special bit operators'!
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3148
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3149
bitAnd_32:anInteger
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3150
    "return a C-semantic 32bit locical-and of the receiver and
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3151
     the argument. Both must be either Small- or LargeIntegers.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3152
     This (nonstandard) specialized method is provided to allow simulation of
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3153
     and operations with C semantics."
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3154
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3155
%{  /* NOCONTEXT */
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3156
    int val1, val2, rslt;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3157
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3158
    if (__isSmallInteger(self)) {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3159
	val1 = __intVal(self);
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3160
    } else if (__isLargeInteger(self)) {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3161
	val1 = __longIntVal(self);
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3162
	if (!val1) goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3163
    } else {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3164
	goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3165
    }
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3166
    if (__isSmallInteger(anInteger)) {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3167
	val2 = __intVal(anInteger);
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3168
    } else if (__isLargeInteger(anInteger)) {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3169
	val2 = __longIntVal(anInteger);
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3170
	if (!val2) goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3171
    } else {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3172
	goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3173
    }
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3174
    rslt = val1 & val2;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3175
    RETURN(__MKINT(rslt));
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3176
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3177
  bad: ;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3178
%}.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3179
    self primitiveFailed.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3180
!
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3181
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3182
bitOr_32:anInteger
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3183
    "return a C-semantic 32bit locical-or of the receiver and
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3184
     the argument. Both must be either Small- or LargeIntegers.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3185
     This (nonstandard) specialized method is provided to allow simulation of
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3186
     and operations with C semantics."
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3187
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3188
%{  /* NOCONTEXT */
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3189
    int val1, val2, rslt;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3190
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3191
    if (__isSmallInteger(self)) {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3192
	val1 = __intVal(self);
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3193
    } else if (__isLargeInteger(self)) {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3194
	val1 = __longIntVal(self);
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3195
	if (!val1) goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3196
    } else {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3197
	goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3198
    }
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3199
    if (__isSmallInteger(anInteger)) {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3200
	val2 = __intVal(anInteger);
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3201
    } else if (__isLargeInteger(anInteger)) {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3202
	val2 = __longIntVal(anInteger);
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3203
	if (!val2) goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3204
    } else {
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  3205
	goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3206
    }
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3207
    rslt = val1 | val2;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3208
    RETURN(__MKINT(rslt));
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3209
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3210
  bad: ;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3211
%}.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3212
    self primitiveFailed.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3213
! !
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  3214
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3215
!Integer methodsFor:'testing'!
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3216
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3217
isLiteral
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3218
    "return true, if the receiver can be used as a literal constant in ST syntax
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3219
     (i.e. can be used in constant arrays)"
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3220
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3221
    ^ true
9316
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3222
!
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3223
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3224
isPrime
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3225
    "return true if I am a prime Number"
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3226
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3227
    self even ifTrue:[^ false].
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3228
    3 to:(self sqrt + 1) by:2 do:[:i |
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3229
        (self \\ i) == 0 ifTrue:[ ^ false ].
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3230
    ].                   
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3231
    ^ true
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3232
!
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3233
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3234
nextPrime
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3235
    "return the next prime after the receiver"
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3236
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3237
    |num|
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3238
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3239
    num := self + 1.
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3240
    num even ifTrue:[
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3241
        num := num + 1
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3242
    ].
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3243
    [num isPrime] whileFalse:[
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3244
        num := num + 2
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3245
    ].
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3246
    ^ num
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3247
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3248
    "
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3249
     22 nextPrime 
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3250
     37 nextPrime 
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3251
     36 nextPrime 
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3252
     3456737 nextPrime
dd88002fb60c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9261
diff changeset
  3253
    "
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3254
! !
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3255
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3256
!Integer methodsFor:'tracing'!
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3257
4682
4158042a9c8c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4653
diff changeset
  3258
traceInto:aRequestor level:level from:referrer
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3259
    "double dispatch into tracer, passing my type implicitely in the selector"
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3260
4682
4158042a9c8c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4653
diff changeset
  3261
    ^ aRequestor traceInteger:self level:level from:referrer
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3262
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3263
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3264
! !
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  3265
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3266
!Integer methodsFor:'truncation & rounding'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3267
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3268
ceiling
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3269
    "return the smallest integer which is larger or equal to the receiver.
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3270
     For integers, this is the receiver itself."
345
claus
parents: 328
diff changeset
  3271
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3272
    ^ self
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3273
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3274
    "Modified: 18.7.1996 / 12:44:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3275
!
345
claus
parents: 328
diff changeset
  3276
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3277
compressed
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3278
    "if the receiver can be represented as a SmallInteger, return
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3279
     a SmallInteger with my value; otherwise return self with leading
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3280
     zeros removed. This method is redefined in LargeInteger."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3281
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3282
    ^ self
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3283
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3284
    "Modified: 5.11.1996 / 14:07:41 / cg"
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  3285
!
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  3286
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3287
floor
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3288
    "return the largest integer which is smaller or equal to the receiver.
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3289
     For integers, this is the receiver itself."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3290
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3291
    ^ self
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3292
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3293
    "Modified: 18.7.1996 / 12:44:00 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3294
!
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  3295
1866
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3296
fractionPart
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3297
    "return a number with value from digits after the decimal point.
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3298
     (i.e. the receiver minus its truncated value)
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3299
     Since integers have no fraction, return 0 here."
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3300
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3301
    ^ 0
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3302
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3303
    "
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3304
     1234.56789 fractionPart      
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3305
     1.2345e6 fractionPart        
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3306
     1000 fractionPart              
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3307
     10000000000000000 fractionPart 
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3308
    "
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3309
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3310
    "Modified: 4.11.1996 / 20:27:44 / cg"
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3311
!
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3312
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3313
integerPart
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3314
    "return a number with value from digits before the decimal point.
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3315
     (i.e. the receivers truncated value)
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3316
     Since integers have no fraction, return the receiver here."
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3317
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3318
    ^ self
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3319
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3320
    "
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3321
     1234.56789 integerPart      
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3322
     1.2345e6 integerPart        
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3323
     1000 integerPart              
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3324
     10000000000000000 integerPart 
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3325
    "
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3326
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3327
    "Modified: 4.11.1996 / 20:28:22 / cg"
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3328
!
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  3329
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3330
normalize
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3331
    "if the receiver can be represented as a SmallInteger, return
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3332
     a SmallInteger with my value; otherwise return self with leading
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3333
     zeros removed.
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3334
     This method is left for backward compatibility - it has been
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3335
     renamed to #compressed for ST-80 compatibility."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  3336
9261
275c3c6392ed mark obsolete method
Stefan Vogel <sv@exept.de>
parents: 9141
diff changeset
  3337
    <resource: #obsolete>
275c3c6392ed mark obsolete method
Stefan Vogel <sv@exept.de>
parents: 9141
diff changeset
  3338
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3339
    ^ self compressed
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3340
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  3341
    "Modified: 5.11.1996 / 14:08:24 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3342
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  3343
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3344
rounded
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3345
    "return the receiver rounded toward the next Integer -
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3346
     for integers this is the receiver itself."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3347
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3348
    ^ self
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3349
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3350
    "Modified: 18.7.1996 / 12:44:24 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3351
!
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  3352
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3353
truncated
10824
3246284b2ddb comment
Claus Gittinger <cg@exept.de>
parents: 10681
diff changeset
  3354
    "return the receiver truncated towards zero as Integer 
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3355
     for integers this is the receiver itself."
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  3356
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3357
    ^ self
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3358
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  3359
    "Modified: 18.7.1996 / 12:44:33 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3360
! !
569
7134eb78cf48 readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3361
8394
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8335
diff changeset
  3362
!Integer methodsFor:'visiting'!
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8335
diff changeset
  3363
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8335
diff changeset
  3364
acceptVisitor:aVisitor with:aParameter
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8335
diff changeset
  3365
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8335
diff changeset
  3366
    ^ aVisitor visitInteger:self with:aParameter
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8335
diff changeset
  3367
! !
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 8335
diff changeset
  3368
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3369
!Integer::ModuloNumber class methodsFor:'documentation'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3370
4803
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3371
copyright
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3372
"
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3373
 COPYRIGHT (c) 1999 by eXept Software AG
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3374
              All Rights Reserved
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3375
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3376
 This software is furnished under a license and may be used
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3377
 only in accordance with the terms of that license and with the
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3378
 inclusion of the above copyright notice.   This software may not
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3379
 be provided or otherwise made available to, or used by, any
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3380
 other person.  No title to or ownership of the software is
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3381
 hereby transferred.
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3382
"
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3383
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3384
!
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3385
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3386
documentation
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3387
"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3388
    This is a helper class to perform fast computation of the modulus.
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  3389
    (with big numbers, this does make a difference)
4803
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3390
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3391
    WARNING: this does only work with numbers which have no common
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3392
             divisor (which is true for cryptographic applications).
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  3393
             So, use this only if you know what you are doing ...
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3394
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3395
    [author:]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3396
        Stefan Vogel
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3397
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3398
    [see also:]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3399
        Integer SmallInteger LargeInsteger
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3400
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3401
    [instance variables:]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3402
        modulus       the modulus
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3403
        reciprocal    reciprocal of the modulus
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3404
        shift         shift count to cut off some bits
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3405
"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3406
!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3407
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3408
examples
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3409
"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3410
                                                                [exBegin]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3411
        17 asModuloNumber modulusOf:38
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3412
                                                                [exEnd]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3413
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3414
                                                                [exBegin]
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  3415
        38 \\ 17        
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3416
                                                                [exEnd]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3417
"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3418
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3419
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3420
!Integer::ModuloNumber class methodsFor:'instance creation'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3421
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3422
modulus:anInteger
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3423
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3424
    ^ self new modulus:anInteger
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3425
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3426
    "Created: / 3.5.1999 / 11:13:15 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3427
    "Modified: / 3.5.1999 / 11:18:37 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3428
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3429
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3430
!Integer::ModuloNumber methodsFor:'accessing'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3431
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3432
modulus
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3433
    "return the modulus"
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3434
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3435
    ^ modulus
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3436
!
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3437
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3438
modulus:n
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3439
    "set the modulus"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3440
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3441
    modulus := n.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3442
    reciprocal := n integerReciprocal.
4971
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3443
    shift := n highBit negated.
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3444
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3445
    "Created: / 3.5.1999 / 10:02:39 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3446
    "Modified: / 3.5.1999 / 14:30:49 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3447
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3448
5238
f7a816a660a3 categories
Claus Gittinger <cg@exept.de>
parents: 5210
diff changeset
  3449
!Integer::ModuloNumber methodsFor:'arithmetic'!
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3450
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3451
modulusOf:aNumber
4971
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3452
    "compute the aNumber modulo myself.
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3453
     The shortcut works only, if aNumber is < modulo * modulo
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3454
     (When doing arithmethic modulo something).
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3455
     Otherwise do it the long way"
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3456
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3457
    |e t cnt|
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3458
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3459
    "throw off low nbits(modulus)"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3460
4971
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3461
    e := (aNumber bitShift:shift).
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3462
    e := e * reciprocal.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3463
    e := (e bitShift:shift).
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3464
    e := e * modulus.
4971
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3465
    e := aNumber - e.
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3466
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3467
    "this subtract is done max 2 times"
4971
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3468
    cnt := 2.
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3469
    [(t := e - modulus) >= 0] whileTrue:[
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3470
        e := t.
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3471
        cnt == 0 ifTrue:[
8335
e7fa7779b1b9 Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents: 8289
diff changeset
  3472
            "shortcut didn't work, do it the long way"
4971
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3473
            ^ e \\ modulus.
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3474
        ].
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3475
        cnt := cnt - 1.
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3476
    ].
4971
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3477
    ^ e.
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3478
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3479
    "
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3480
     |m|
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3481
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3482
     m := self new modulus:7.
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3483
     m modulusOf:55.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3484
    "
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3485
4971
aed19001173f Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents: 4952
diff changeset
  3486
    "Shortcut does not work: (size of divisor vs. dividend):
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3487
     |m|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3488
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3489
     m := self new modulus:7.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3490
     m modulusOf:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3491
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3492
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3493
    "SLOW (using standard \\ operation):
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3494
     |m|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3495
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3496
     m := 123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3497
     Time millisecondsToRun:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3498
        1000 timesRepeat:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3499
            874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658 \\ m
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3500
        ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3501
     ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3502
    "            
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3503
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3504
    "fast (using moduloNumber with almost same-sized dividend and divisor):
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3505
     |m|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3506
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3507
     m := self new modulus:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3508
     Time millisecondsToRun:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3509
        1000 timesRepeat:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3510
            m modulusOf:874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3511
        ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3512
     ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3513
    "            
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  3514
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3515
    "Modified: / 3.5.1999 / 14:30:32 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3516
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3517
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3518
!Integer::ModuloNumber methodsFor:'converting'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3519
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3520
asModuloNumber
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3521
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3522
    ^ self
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3523
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3524
    "Created: / 3.5.1999 / 14:48:27 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3525
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  3526
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  3527
!Integer class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3528
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  3529
version
11095
b3998b045684 printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11067
diff changeset
  3530
    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.196 2008-07-24 09:58:22 cg Exp $'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  3531
! !
6858
13919a7544b6 raisedToInteger pushed up
Claus Gittinger <cg@exept.de>
parents: 6682
diff changeset
  3532
4996
c3716cebe47b added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents: 4973
diff changeset
  3533
Integer initialize!