Integer.st
author Claus Gittinger <cg@exept.de>
Tue, 19 Oct 1999 12:09:43 +0200
changeset 4917 662e7f3315fb
parent 4867 3bb80fe9bb8f
child 4918 c89047114a1e
permissions -rw-r--r--
comments
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
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
Number subclass:#Integer
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
    14
	instanceVariableNames:''
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
    15
	classVariableNames:'DefaultDisplayRadix'
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
    16
	poolDictionaries:''
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
    17
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    20
Object subclass:#ModuloNumber
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    21
	instanceVariableNames:'modulus reciprocal shift'
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    22
	classVariableNames:''
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    23
	poolDictionaries:''
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    24
	privateIn:Integer
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    25
!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
    26
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
    27
!Integer class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    29
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    30
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    31
 COPYRIGHT (c) 1988 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
    32
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    33
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    34
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    35
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    36
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    37
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    38
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    39
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    40
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    41
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    42
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    43
documentation
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    44
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    45
    abstract superclass for all integer numbers.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    46
    See details in concrete subclasses LargeInteger and SmallInteger.
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
    47
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1888
diff changeset
    48
    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
    49
	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
    50
	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
    51
	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
    52
	int op float       -> float
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1888
diff changeset
    53
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
    54
    [Class variables:]
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
    55
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    56
	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
    57
				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
    58
				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
    59
				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
    60
				(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
    61
				 - I know - I am lazy ;-). Default is 10.
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
    62
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
    63
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
    64
    [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
    65
	Claus Gittinger
1556
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    66
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    67
    [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
    68
	Number
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
    69
	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
    70
	Float ShortFloat Fraction FixedPoint
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    71
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    72
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    73
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
    74
!Integer class methodsFor:'instance creation'!
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    75
3044
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    76
byte1:b1 byte2:b2 byte3:b3 byte4:b4
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    77
    "Squeak compatibility:
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    78
     Return an Integer given four value bytes.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    79
     The returned integer is either a Small- or a LargeInteger"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    80
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    81
    |t|
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    82
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    83
    t := b4.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    84
    t := (t bitShift:8) + b3.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    85
    t := (t bitShift:8) + b2.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    86
    ^ (t bitShift:8) + b1.
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    87
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
     (Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    90
     (Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    91
    "
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
    "Created: 19.10.1997 / 18:08:52 / cg"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    94
    "Modified: 19.10.1997 / 18:09:04 / cg"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    95
!
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
    96
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
    97
fastFromString:aString at:startIndex
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
    98
    "return the next unsigned Integer from the string 
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
    99
     as a decimal number, starting at startIndex.
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   100
     The number must be in the native machines int range
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   101
     (i.e. 63bit on alpha / 31 on all others);
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   102
     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
   103
     No spaces are skipped.
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   104
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   105
     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
   106
     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
   107
     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
   108
     numbers."
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   109
%{
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   110
     if (__isString(aString) && __isSmallInteger(startIndex)) {
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   111
        char *cp = (char *)(__stringVal(aString));
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   112
        int idx = __intVal(startIndex) - 1;
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   113
        unsigned INT val;
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   114
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   115
        if ((unsigned)idx < __stringSize(aString)) {
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   116
            val = atoi(cp + idx);
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   117
            if (val <= _MAX_INT) {
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   118
                RETURN(__MKSMALLINT(val));
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   119
            }
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   120
            RETURN (__MKUINT(val));
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   121
        }
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
     self primitiveFailed.
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   125
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
     Integer fastFromString:'12345' at:1  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   128
     Integer fastFromString:'12345' at:2  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   129
     Integer fastFromString:'12345' at:3  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   130
     Integer fastFromString:'12345' at:4  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   131
     Integer fastFromString:'12345' at:5  
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   132
     Integer fastFromString:'1234512345' at:1  
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   133
     Integer fastFromString:'2147483647' at:1  
4611
5d77a2fa73ba comment
Claus Gittinger <cg@exept.de>
parents: 4610
diff changeset
   134
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   135
     Integer fastFromString:'4294967295' at:1 
4611
5d77a2fa73ba comment
Claus Gittinger <cg@exept.de>
parents: 4610
diff changeset
   136
     Integer fastFromString:'12345' at:6  
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   137
     Integer fastFromString:'12345' at:0  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   138
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   139
     Time millisecondsToRun:[
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   140
        100000 timesRepeat:[
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   141
            Integer readFrom:'12345'
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   142
        ]
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   143
     ]  
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   144
    "
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
     Time millisecondsToRun:[
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   148
        100000 timesRepeat:[
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   149
            Integer fastFromString:'12345' at:1  
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   150
        ]
4610
742650733761 comment
Claus Gittinger <cg@exept.de>
parents: 4609
diff changeset
   151
     ]   
3639
c4fbb700a569 added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents: 3638
diff changeset
   152
    "
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
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   155
fromBCDBytes:aByteArray
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   156
    "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
   157
     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
   158
     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
   159
     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
   160
     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
   161
    "
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
    |val|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   164
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   165
    val := 0.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   166
    aByteArray do:[:twoDigits |
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   167
        |hi lo|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   168
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   169
        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
   170
        lo := twoDigits bitAnd:16r0F.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   171
        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
   172
    ].
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   173
    ^ val
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   174
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
     Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   177
     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
   178
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   179
!
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
   180
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   181
new:numberOfBytes neg:negative
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   182
    "for ST-80 compatibility:
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   183
     Return an empty Integer (uninitialized value) with space for
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   184
     numberOfBytes bytes (= digitLength). The additional argument
132
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   185
     negative specifies if the result should be a negative number.
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   186
     The digits can be stored byte-wise into the result, using digitAt:put:"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   187
132
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   188
    ^ (LargeInteger basicNew numberOfDigits:numberOfBytes) 
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
   189
		sign:(negative ifTrue:[-1] ifFalse:[1])
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   190
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   191
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   192
readFrom:aStringOrStream 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   193
    "return the next unsigned Integer from the (character-)stream aStream 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   194
     as decimal number. 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   195
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   196
     NOTICE:   
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   197
       This behaves different from the default readFrom:, in returning
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   198
       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
   199
       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
   200
       - depending on the upcoming ANSI standard, this may change."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   201
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   202
    ^ self readFrom:aStringOrStream onError:0
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   203
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   204
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   205
     Integer readFrom:(ReadStream on:'foobar')     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   206
     Integer readFrom:(ReadStream on:'foobar') onError:nil  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   207
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   208
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   209
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   210
readFrom:aStringOrStream onError:exceptionBlock
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   211
    "return the next Integer from the (character-)stream aStream,
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   212
     handling initial XXr for arbitrary radix numbers and initial sign.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   213
     Also, all initial whitespace is skipped.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   214
     If the string does not represent a valid integer number,
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   215
     return the value of exceptionBlock."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   216
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   217
    |value|
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   218
1700
25223802076c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1560
diff changeset
   219
    ErrorSignal handle:[:ex |
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   220
        ^ exceptionBlock value
1700
25223802076c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1560
diff changeset
   221
    ] do:[
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   222
        |str nextChar negative|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   223
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   224
        str := aStringOrStream readStream.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   225
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   226
        nextChar := str skipSeparators.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   227
        (nextChar == $-) ifTrue:[
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   228
            negative := true.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   229
            str next.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   230
            nextChar := str peekOrNil
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   231
        ] ifFalse:[
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   232
            negative := false
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   233
        ].
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   234
        (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
   235
            "
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   236
             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
   237
            "
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   238
            ^ exceptionBlock value
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   239
        ].
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   240
        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
   241
        nextChar := str peekOrNil.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   242
        ((nextChar == $r) or:[ nextChar == $R]) ifTrue:[
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   243
            "-xxr<number> is invalid; should be xxr-<val>"
1700
25223802076c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1560
diff changeset
   244
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   245
            negative ifTrue:[
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   246
                '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
   247
                negative := false
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   248
            ].
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   249
            str next.
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   250
            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
   251
        ].
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   252
        negative ifTrue:[
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   253
            value := value negated
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   254
        ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   255
    ].
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   256
    ^ value
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   257
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   258
    "
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   259
     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
   260
     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
   261
     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
   262
     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
   263
     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
   264
     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
   265
     Integer readFrom:'foo'                          
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   266
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   267
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   268
    "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
   269
    "Modified: / 14.4.1998 / 19:17:28 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   270
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   271
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   272
readFrom:aStringOrStream radix:radix
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   273
    "return the next unsigned Integer from the (character-)stream aStream 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   274
     in radix; (assumes that the initial XXr has already been read).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   275
     No whitespace-skipping is done.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   276
     Returns 0 if no number available.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   277
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   278
     NOTICE:   
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   279
       This behaves different from the default readFrom:, in returning
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   280
       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
   281
       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
   282
       - depending on the upcoming ANSI standard, this may change."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   283
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   284
    ^ self readFrom:aStringOrStream radix:radix onError:0
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   285
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   286
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   287
readFrom:aStringOrStream radix:radix onError:exceptionBlock
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   288
    "return the next unsigned Integer from the (character-)stream aStream 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   289
     in radix; (assumes that the initial XXr has already been read).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   290
     No whitespace-skipping is done.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   291
     Returns the value of exceptionBlock, if no number is available."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   292
4169
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   293
    |str nextChar nextChar2 value 
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   294
     r     "{ Class: SmallInteger }"
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   295
     r2    "{ Class: SmallInteger }"|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   296
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   297
    str := aStringOrStream readStream.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   298
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   299
    nextChar := str peekOrNil.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   300
    (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
   301
        ^ exceptionBlock value
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   302
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   303
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   304
    value := nextChar digitValue.
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   305
    str next.
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   306
    nextChar := str peekOrNil.
4169
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   307
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   308
"/ OLD code
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   309
"/    [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   310
"/        str next.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   311
"/        value := value * radix + nextChar digitValue.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   312
"/        nextChar := str peekOrNil.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   313
"/    ].
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   314
"/    ^ value.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   315
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   316
    "/ 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
   317
    "/ converted number is large
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   318
    "/ (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
   319
    "/ It should not be slower for smallIntegers.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   320
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   321
    r := radix.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   322
    r2 := r * r.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   323
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   324
    [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
   325
        str next.
4169
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   326
        nextChar2 := str peekOrNil.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   327
        (nextChar2 notNil and:[nextChar2 isDigitRadix:r]) ifTrue:[
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   328
            str next.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   329
            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
   330
            nextChar := str peekOrNil.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   331
        ] ifFalse:[
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   332
            ^ (value * r) + nextChar digitValue.
7079cb06963b tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents: 4158
diff changeset
   333
        ]
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   334
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   335
    ^ value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   336
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   337
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   338
     Integer readFrom:(ReadStream on:'12345') radix:10  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   339
     Integer readFrom:(ReadStream on:'FFFF') radix:16  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   340
     Integer readFrom:(ReadStream on:'1010') radix:2   
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   341
     Integer readFrom:(ReadStream on:'foobar') radix:10   
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   342
     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
   343
     Integer readFrom:'gg' radix:10 onError:0                      
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   344
    "
3374
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   345
6897f584accb faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3298
diff changeset
   346
    "Modified: / 14.4.1998 / 19:16:46 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   347
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   348
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
   349
!Integer class methodsFor:'constants'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   350
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   351
unity
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   352
    "return the neutral element for multiplication (1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   353
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   354
    ^ 1
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   355
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   356
    "Modified: 18.7.1996 / 12:26:43 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   357
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   358
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   359
zero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   360
    "return the neutral element for addition (0)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   361
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   362
    ^ 0
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   363
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1431
diff changeset
   364
    "Modified: 18.7.1996 / 12:26:38 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   365
! !
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   366
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
   367
!Integer class methodsFor:'misc'!
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   368
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   369
displayRadix:aNumber
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
   370
    "being tired of always sending #printStringRadix: in the inspectors,
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   371
     this allows you to change the default print radix for the displayString
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   372
     method."
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   373
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   374
    DefaultDisplayRadix := aNumber
132
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   375
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   376
    "
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   377
     Integer displayRadix:16. 123456 inspect
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   378
     Integer displayRadix:10. 123456 inspect
ab2cfccd218c *** empty log message ***
claus
parents: 92
diff changeset
   379
    "
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   380
! !
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
   381
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   382
!Integer class methodsFor:'queries'!
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   383
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   384
hasSharedInstances
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   385
    "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
   386
     with the same value are identical.
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   387
     Although not always shared (LargeIntegers), these should be treated
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   388
     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
   389
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   390
    ^ true
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   391
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   392
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   393
! !
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
   394
4867
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   395
!Integer methodsFor:'Compatibility - Squeak'!
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   396
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   397
atRandom
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   398
    "return a random number between 1 amd myself"
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   399
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   400
    self < 1 ifTrue:[^ self].
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   401
    ^ (1 to:self) atRandom
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   402
!
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   403
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   404
printStringBase:base
4867
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   405
    "return my printString in a base;
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   406
     same as printStringRadix:"
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   407
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   408
    ^ self printStringRadix:base
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   409
! !
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   410
4867
3bb80fe9bb8f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4830
diff changeset
   411
!Integer methodsFor:'Compatibility - VisualAge'!
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   412
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   413
<< aNumber
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   414
    "left shift"
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   415
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   416
    ^ self bitShift:aNumber
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   417
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   418
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   419
     1 << 5 
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   420
     64 << -5 
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   421
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   422
!
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   423
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   424
>> aNumber
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   425
    "right shift"
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   426
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   427
    ^ self bitShift:(aNumber negated)
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   428
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   429
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   430
     1 >> -5  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   431
     64 >> 5  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   432
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   433
!
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   434
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   435
isBitSet:index
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   436
    "return true if the index' bit is set; false otherwise.
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   437
     Bits are counted from 1 starting with the least significant."
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   438
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   439
    ^ (self bitAt:index) ~~ 0
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   440
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   441
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   442
     5 isBitSet:3       => true
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   443
     2r0101 isBitSet:2  => false
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   444
     2r0101 isBitSet:1  => true
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   445
     2r0101 isBitSet:0  index error
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   446
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   447
!
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   448
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   449
setBit:index
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   450
    "return a new number where the specified bit is on.
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   451
     Bits are counted from 1 starting with the least significant."
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   452
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   453
    ^ self bitOr:(1 bitShift:index-1)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   454
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   455
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   456
     0 setBit:3         => 4 (2r100)
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   457
     0 setBit:48        => 140737488355328 (2r1000.....000)
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   458
     ((0 setBit:99) setBit:100) printStringRadix:2  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   459
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   460
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   461
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   462
!Integer methodsFor:'bcd conversion'!
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   463
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   464
decodeFromBCD
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   465
    "return a number representing the value of the BCD encoded receiver."
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   466
4424
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
   467
    |v rslt multiplier nibble|
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   468
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   469
    v := self.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   470
    rslt := 0.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   471
    multiplier := 1.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   472
    [v > 0] whileTrue:[
4424
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
   473
        nibble := v bitAnd:16r0F.
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
   474
        nibble > 9 ifTrue:[
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
   475
            ^ self error:'bad BCD coded value'
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
   476
        ].
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
   477
        rslt := rslt + (nibble * multiplier).
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   478
        multiplier := multiplier * 10.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   479
        v := v bitShift:-4.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   480
    ].
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   481
    ^ rslt
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   482
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   483
    "
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   484
     16r55 decodeFromBCD   
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   485
     16r127 decodeFromBCD    
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   486
     16r1234567890 decodeFromBCD     
4424
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
   487
f0f658ed8995 bcd support
Claus Gittinger <cg@exept.de>
parents: 4423
diff changeset
   488
     16r5A decodeFromBCD   
4423
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   489
    "
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   490
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   491
!
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   492
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   493
encodeAsBCD
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   494
    "return a BCD encoded number representing the same value as the
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   495
     receiver."
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   496
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   497
    |v rslt shift|
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   498
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   499
    v := self.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   500
    rslt := shift := 0.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   501
    [v > 0] whileTrue:[
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   502
        rslt := rslt + ((v \\ 10) bitShift:shift).
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   503
        shift := shift + 4.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   504
        v := v // 10.
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   505
    ].
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   506
    ^ rslt
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   507
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   508
    "
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   509
     55 encodeAsBCD hexPrintString 
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   510
     127 encodeAsBCD hexPrintString 
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   511
     1234567890 encodeAsBCD hexPrintString  
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   512
    "
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   513
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   514
! !
2a478882f765 bcd support
Claus Gittinger <cg@exept.de>
parents: 4376
diff changeset
   515
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   516
!Integer methodsFor:'bit operators'!
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   517
4643
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   518
allMask:anInteger
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   519
    "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
   520
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   521
    ^ (self bitAnd:anInteger) == anInteger
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   522
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   523
    "2r00001111 allMask:2r00000001"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   524
    "2r00001111 allMask:2r00011110"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   525
    "2r00001111 allMask:2r00000000"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   526
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   527
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   528
anyMask:anInteger
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   529
    "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
   530
     (somewhat incorrect, if the mask is zero)"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   531
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   532
    ^ (self bitAnd:anInteger) ~~ 0
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   533
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   534
    "2r00001111 anyMask:2r00000001"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   535
    "2r00001111 anyMask:2r11110000"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   536
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
   537
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   538
bitAnd:anInteger
357
claus
parents: 356
diff changeset
   539
    "return the bitwise-and of the receiver and the argument, anInteger.
claus
parents: 356
diff changeset
   540
     This is a general and slow implementation, walking over the bytes of
claus
parents: 356
diff changeset
   541
     the receiver and the argument."
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   542
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   543
    |n "{ Class: SmallInteger }"
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   544
     result byte|
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   545
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   546
    n := (anInteger digitLength) min:(self digitLength).
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   547
    result := self class basicNew numberOfDigits:n.
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   548
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   549
    1 to:n do:[:index |
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
   550
	byte := (anInteger digitAt:index) bitAnd:(self digitAt:index).
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
   551
	result digitAt:index put:byte.
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   552
    ].
3162
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
   553
    (byte == 0 or:[n <= SmallInteger maxBytes]) 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
   554
	^ result compressed
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   555
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   556
    ^ result
357
claus
parents: 356
diff changeset
   557
claus
parents: 356
diff changeset
   558
    "
claus
parents: 356
diff changeset
   559
     (16r112233445566778899 bitAnd:16rFF                ) printStringRadix:16 
claus
parents: 356
diff changeset
   560
     (16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 
claus
parents: 356
diff changeset
   561
     (16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 
claus
parents: 356
diff changeset
   562
     (16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 
claus
parents: 356
diff changeset
   563
    "
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
   564
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
   565
    "Modified: 5.11.1996 / 14:06:26 / cg"
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   566
!
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
   567
85
claus
parents: 77
diff changeset
   568
bitAt:index
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   569
    "return the value of the index's bit (index starts at 1).
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   570
     Notice: the result of bitShift: on negative receivers is not 
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
   571
	     defined in the language standard (since the implementation
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
   572
	     is free to choose any internal representation for integers)"
85
claus
parents: 77
diff changeset
   573
claus
parents: 77
diff changeset
   574
    |i "{Class: SmallInteger}"|
claus
parents: 77
diff changeset
   575
claus
parents: 77
diff changeset
   576
    i := index - 1.
claus
parents: 77
diff changeset
   577
    ^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1)
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   578
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   579
    "
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   580
     1 bitAt:1                     => 1 
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   581
     1 bitAt:2                     => 0
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   582
     1 bitAt:0                     index error
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   583
     2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   584
     2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0  
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   585
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   586
     (1 bitShift:1000) bitAt:1000  => 0
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   587
     (1 bitShift:1000) bitAt:1001  => 1
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   588
     (1 bitShift:1000) bitAt:1002  => 0
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   589
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   590
     (1 bitShift:30) bitAt:30    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   591
     (1 bitShift:30) bitAt:31    
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   592
     (1 bitShift:30) bitAt:32     
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   593
     (1 bitShift:31) bitAt:31     
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   594
     (1 bitShift:31) bitAt:32     
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   595
     (1 bitShift:31) bitAt:33    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   596
     (1 bitShift:32) bitAt:32    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   597
     (1 bitShift:32) bitAt:33    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   598
     (1 bitShift:32) bitAt:34    
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   599
     (1 bitShift:64) bitAt:64     
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   600
     (1 bitShift:64) bitAt:65     
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   601
     (1 bitShift:64) bitAt:66     
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   602
    "
85
claus
parents: 77
diff changeset
   603
!
claus
parents: 77
diff changeset
   604
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
   605
bitClear:anInteger
3884
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   606
    "return the bitwise-and of the receiver and the complement of argument, anInteger.
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   607
     This is a general and slow implementation, walking over the bytes of
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   608
     the receiver and the argument."
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   609
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   610
    |n "{ Class: SmallInteger }"
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   611
     result byte|
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
   612
3884
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   613
    n := (anInteger digitLength) max:(self digitLength).
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   614
    result := self class basicNew numberOfDigits:n.
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   615
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   616
    1 to:n do:[:index |
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   617
        byte :=  (self digitAt:index) bitClear:(anInteger digitAt:index).
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   618
        result digitAt:index put:byte.
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   619
    ].
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   620
    (byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   621
        ^ result compressed
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   622
    ].
ddda4144e331 fixed bitClear
ca
parents: 3874
diff changeset
   623
    ^ result
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
   624
!
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
   625
3455
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
   626
bitInvert
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
   627
    "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
   628
     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
   629
     since the digits are stored as absolute value."
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
   630
4614
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   631
"/    ^ -1 - self
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   632
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   633
    |n "{ Class: SmallInteger }"
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   634
     result byte|
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   635
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   636
    n := self digitLength.
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   637
    result := self class basicNew numberOfDigits:n.
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   638
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   639
    1 to:n do:[:index |
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   640
        byte := self digitAt:index.
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   641
        result digitAt:index put:(byte bitInvert bitAnd:16rFF).
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   642
    ].
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   643
    (byte == 255 or:[n <= SmallInteger maxBytes]) ifTrue:[
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   644
        ^ result compressed
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   645
    ].
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   646
    ^ result
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   647
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   648
     "
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   649
      16rffffffff bitInvert     
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   650
      16rff00ff00 bitInvert hexPrintString  
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   651
     "
3455
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
   652
!
2b19e1e70adc added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents: 3395
diff changeset
   653
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   654
bitInvert32
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   655
    "return a new integer, where the low32 bits are complemented."
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   656
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   657
    ^ self bitXor: 16rFFFFFFFF
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   658
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   659
    "
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   660
     16r80000000 bitInvert32 hexPrintString  
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   661
     16r7FFFFFFF bitInvert32 hexPrintString  
4614
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   662
     16rFFFFFFFF bitInvert32 hexPrintString  
14d5ce4bf117 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4611
diff changeset
   663
     0 bitInvert32 hexPrintString   
4376
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   664
    "
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   665
!
8df0935ac5b1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   666
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   667
bitOr:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   668
    "return the bitwise-or of the receiver and the argument, anInteger.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   669
     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
   670
     the receiver and the argument."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   671
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   672
    |n "{ Class: SmallInteger }"
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   673
     result byte|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   674
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   675
    n := (anInteger digitLength) max:(self digitLength).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   676
    result := self class basicNew numberOfDigits:n.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   677
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   678
    1 to:n do:[:index |
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   679
	byte := (anInteger digitAt:index) bitOr:(self digitAt:index).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   680
	result digitAt:index put:byte.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   681
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   682
"/ no need to normalize - if the operands were correct
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   683
"/    byte == 0 ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   684
"/        ^ result normalize
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   685
"/    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   686
    ^ result
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   687
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   688
85
claus
parents: 77
diff changeset
   689
bitShift:shiftCount
claus
parents: 77
diff changeset
   690
    "return the value of the receiver shifted by shiftCount bits;
claus
parents: 77
diff changeset
   691
     leftShift if shiftCount > 0; rightShift otherwise.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   692
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   693
     Notice: the result of bitShift: on negative receivers is not 
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   694
             defined in the language standard (since the implementation
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   695
             is free to choose any internal representation for integers)"
85
claus
parents: 77
diff changeset
   696
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   697
    |result 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   698
     prev       "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   699
     next       "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   700
     byte       "{ Class: SmallInteger }"
359
claus
parents: 357
diff changeset
   701
     byte2      "{ Class: SmallInteger }"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   702
     bitShift   "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   703
     revShift   "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   704
     digitShift "{ Class: SmallInteger }"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   705
     n          "{ Class: SmallInteger }" 
3044
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
   706
     nn         "{ Class: SmallInteger }"
a0bbac91639b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3036
diff changeset
   707
     nDigits    "{ Class: SmallInteger }" |
85
claus
parents: 77
diff changeset
   708
claus
parents: 77
diff changeset
   709
    shiftCount > 0 ifTrue:[
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   710
        "left shift"
85
claus
parents: 77
diff changeset
   711
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   712
        digitShift := shiftCount // 8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   713
        bitShift := shiftCount \\ 8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   714
        n := self digitLength.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   715
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   716
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   717
         modulo 8 shifts can be done faster ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   718
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   719
        bitShift == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   720
            result := self class basicNew numberOfDigits:n + digitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   721
            result sign:self sign.
4830
879f09ec443a Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents: 4803
diff changeset
   722
            result digitBytes replaceFrom:(digitShift + 1) with:self digitBytes.
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   723
            "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   724
             no normalize needed, since receiver was already normalized
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   725
            "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   726
            ^ result
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   727
        ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   728
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   729
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   730
         less-than-8 shifts can be done faster ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   731
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   732
        digitShift == 0 ifTrue:[
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
   733
            nn := n+1.
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
   734
            result := self class basicNew numberOfDigits:nn.
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   735
            result sign:self sign.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   736
            prev := 0.
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
   737
            1 to:n do:[:index |
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   738
                byte := self digitAt:index.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   739
                byte := (byte bitShift:bitShift) bitOr:prev.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   740
                result digitAt:index put:(byte bitAnd:16rFF).
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   741
                prev := byte bitShift:-8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   742
            ].
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
   743
            result digitAt:nn put:prev.
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   744
            "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   745
             might have stored a 0-byte ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   746
            "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   747
            prev == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   748
                ^ result compressed
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   749
            ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   750
            ^ result.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   751
        ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   752
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   753
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   754
         slow case ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   755
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   756
        n := n + digitShift + 1.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   757
        result := self class basicNew numberOfDigits:n.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   758
        result sign:self sign.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   759
        byte := self digitAt:1.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   760
        byte := (byte bitShift:bitShift) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   761
        result digitAt:(digitShift + 1) put:byte.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   762
        revShift := -8 + bitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   763
        nDigits := self digitLength.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   764
        2 to:nDigits do:[:index |
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   765
            byte := self digitAt:index.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   766
            byte2 := self digitAt:index-1.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   767
            byte := byte bitShift:bitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   768
            byte2 := byte2 bitShift:revShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   769
            byte := (byte bitOr:byte2) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   770
            result digitAt:(index + digitShift) put:byte.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   771
        ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   772
        byte2 := self digitAt:nDigits.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   773
        byte2 := (byte2 bitShift:revShift) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   774
        result digitAt:(nDigits + digitShift + 1) put:byte2.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   775
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   776
         might have stored a 0-byte ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   777
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   778
        byte2 == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   779
            ^ result compressed
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   780
        ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   781
        ^ result
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   782
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   783
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   784
    shiftCount < 0 ifTrue:[
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   785
        "right shift"
85
claus
parents: 77
diff changeset
   786
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   787
        digitShift := shiftCount negated // 8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   788
        bitShift := shiftCount negated \\ 8.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   789
        n := self digitLength.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   790
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   791
        digitShift >= n ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   792
            ^ 0
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   793
        ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   794
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   795
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   796
         modulo 8 shifts can be done faster ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   797
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   798
        bitShift == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   799
            n := n-digitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   800
            result := self class basicNew numberOfDigits:n.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   801
            result sign:self sign.
4830
879f09ec443a Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents: 4803
diff changeset
   802
            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
   803
            n <= SmallInteger maxBytes ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   804
                ^ result compressed
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   805
            ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   806
            ^ result
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   807
        ].
85
claus
parents: 77
diff changeset
   808
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   809
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   810
         less-than-8 shifts can be done faster ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   811
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   812
        digitShift == 0 ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   813
            result := self class basicNew numberOfDigits:n.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   814
            result sign:self sign.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   815
            prev := 0.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   816
            bitShift := bitShift negated.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   817
            revShift := 8 + bitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   818
            n to:1 by:-1 do:[:index |
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   819
                byte := self digitAt:index.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   820
                next := (byte bitShift:revShift) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   821
                byte := (byte bitShift:bitShift) bitOr:prev.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   822
                result digitAt:index put:(byte bitAnd:16rFF).
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   823
                prev := next.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   824
            ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   825
            (n <= 5) ifTrue:[
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   826
                ^ result compressed
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   827
            ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   828
            ^ result
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   829
        ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   830
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   831
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   832
         slow case ...
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   833
        "
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   834
        nn := n-digitShift.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   835
        result := self class basicNew numberOfDigits:nn.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   836
        result sign:self sign.
85
claus
parents: 77
diff changeset
   837
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   838
        prev := 0.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   839
        bitShift := bitShift negated.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   840
        revShift := 8 + bitShift.
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
   841
        nn := digitShift + 1.
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
   842
        n to:nn by:-1 do:[:index |
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   843
            byte := self digitAt:index.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   844
            next := (byte bitShift:revShift) bitAnd:16rFF.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   845
            byte := (byte bitShift:bitShift) bitOr:prev.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   846
            result digitAt:(index - digitShift) put:byte.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   847
            prev := next.
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   848
        ].
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   849
        "the last stored byte ..."
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   850
        ^ result compressed
85
claus
parents: 77
diff changeset
   851
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   852
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   853
    ^ self "no shift"
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
   854
3638
edf314b220d2 fixed #bitShift: (right)
Claus Gittinger <cg@exept.de>
parents: 3455
diff changeset
   855
    "Modified: / 8.7.1998 / 12:45:24 / cg"
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
   856
    "Modified: / 5.5.1999 / 16:05:05 / stefan"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   857
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   858
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   859
bitTest:anInteger
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   860
    "return true, if the bitwise-and of the receiver and the argument, anInteger
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   861
     is non-0, false otherwise.
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   862
     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
   863
     the receiver and the argument."
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   864
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   865
    |n "{ Class: SmallInteger }"
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   866
     byte|
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   867
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   868
    n := (anInteger digitLength) min:(self digitLength).
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   869
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   870
    1 to:n do:[:index |
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   871
        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
   872
        byte ~~ 0 ifTrue:[^ true].
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   873
    ].
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   874
    ^ false
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   875
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   876
    "
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   877
     16r112233445566778899 bitTest:16rFF  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   878
     16r112233445566778800 bitTest:16rFF  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   879
     16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   880
     16r112233445566778899 bitTest:16rFF0000000000000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   881
     16r112233445566778899 bitTest:16r00000000000000FFFF  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   882
     16r1234567800000000 bitTest:16r8000000000000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   883
     16r8765432100000000 bitTest:16r8000000000000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   884
     16r12345678 bitTest:16r80000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   885
     16r87654321 bitTest:16r80000000  
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   886
    "
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   887
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   888
    "Modified: / 6.6.1999 / 15:10:33 / cg"
1407
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   889
!
6c278dcd5570 added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents: 1337
diff changeset
   890
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   891
bitXor:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   892
    "return the bitwise-or of the receiver and the argument, anInteger.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   893
     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
   894
     the receiver and the argument."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   895
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   896
    |n "{ Class: SmallInteger }"
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   897
     result byte|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   898
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   899
    n := (anInteger digitLength) max:(self digitLength).
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   900
    result := self class basicNew numberOfDigits:n.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   901
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   902
    1 to:n do:[:index |
3993
a5a09b2d36cd oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents: 3908
diff changeset
   903
        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
   904
        result digitAt:index put:byte.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   905
    ].
3993
a5a09b2d36cd oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents: 3908
diff changeset
   906
    (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
   907
        ^ result compressed
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   908
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   909
    ^ result
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   910
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   911
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   912
     (16r112233445566778899 bitXor:16rFF                ) printStringRadix:16 '112233445566778866' 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   913
     (16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799'
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   914
     (16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899'
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   915
     (16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   916
    "
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
   917
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
   918
    "Modified: 5.11.1996 / 14:06:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   919
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   920
3683
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   921
clearBit:index
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   922
    "return a new number where the specified bit is off.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   923
     Bits are counted from 1 starting with the least significant."
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   924
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   925
    |n         "{ Class: SmallInteger }"
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   926
     byteIndex "{ Class: SmallInteger }"
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   927
     bitIndex "{ Class: SmallInteger }"
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   928
     result byte|
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   929
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   930
    byteIndex := ((index - 1) // 8) + 1.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   931
    n := self digitLength.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   932
    byteIndex > n ifTrue:[
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   933
        ^ self
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   934
    ].
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   935
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   936
    result := self copy.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   937
    bitIndex := ((index - 1) \\ 8) + 1.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   938
    byte := (result digitAt:byteIndex) clearBit:bitIndex.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   939
    result digitAt:byteIndex put:byte.
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   940
    (byte == 0 or:[n == byteIndex and:[n <= SmallInteger maxBytes]]) ifTrue:[
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   941
        ^ result compressed
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   942
    ].
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   943
    ^ result
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   944
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   945
    "
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   946
     3111111111 clearBit:1 
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   947
    "
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   948
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   949
    "Modified: / 28.7.1998 / 18:35:50 / cg"
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   950
!
2eac599d04c1 fixed clearBit:
Claus Gittinger <cg@exept.de>
parents: 3639
diff changeset
   951
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   952
even
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   953
    "return true if the receiver is even"
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   954
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   955
    ^ self bitTest:1
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   956
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   957
    "
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   958
     16r112233445566778899 even  
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   959
     16r112233445566778800 even  
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   960
    "
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   961
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   962
    "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
   963
!
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
   964
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   965
highBit
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   966
    "return the bitIndex of the highest bit set. The returned bitIndex
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   967
     starts at 1 for the least significant bit. Returns -1 if no bit is set."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   968
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   969
    |byteNr highByte|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   970
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   971
    byteNr := self digitLength.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
   972
    byteNr == 0 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
   973
        ^ -1
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
   974
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   975
    highByte := self digitAt:byteNr.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   976
    ^ (byteNr - 1) * 8 + highByte highBit
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   977
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   978
    "
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
   979
     0 highBit
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
   980
     -1 highBit
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   981
     (1 bitShift:1) highBit     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   982
     (1 bitShift:30) highBit    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   983
     (1 bitShift:31) highBit    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   984
     (1 bitShift:32) highBit    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   985
     (1 bitShift:33) highBit    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   986
     (1 bitShift:64) highBit     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   987
     (1 bitShift:1000) highBit   
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
   988
     (1 bitShift:1000) negated highBit   
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   989
     ((1 bitShift:64)-1) highBit  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   990
    "
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
   991
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
   992
    "Modified: / 3.5.1999 / 09:20:57 / stefan"
2429
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
   993
!
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
   994
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
   995
lowBit
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
   996
    "return the bitIndex of the lowest bit set. The returned bitIndex
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
   997
     starts at 1 for the least significant bit. Returns -1 if no bit is set."
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
   998
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
   999
    |maxBytes "{ Class: SmallInteger }"
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1000
     bitIndex "{ Class: SmallInteger }" 
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1001
     byte|
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1002
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1003
    maxBytes := self digitLength.
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1004
    bitIndex := 0.
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1005
    1 to:maxBytes do:[:byteIndex |
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1006
	byte := self digitAt:byteIndex.
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1007
	byte ~~ 0 ifTrue:[
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1008
	    ^ bitIndex + byte lowBit
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1009
	].
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1010
	bitIndex := bitIndex + 8
2429
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1011
    ].
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1012
    ^ -1
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1013
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1014
    "
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1015
     0 lowBit                  
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1016
     1 lowBit                  
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1017
     (1 bitShift:1) lowBit     
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1018
     (1 bitShift:1) highBit     
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1019
     (1 bitShift:30) lowBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1020
     (1 bitShift:30) highBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1021
     (1 bitShift:31) lowBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1022
     (1 bitShift:31) highBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1023
     (1 bitShift:32) lowBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1024
     (1 bitShift:32) highBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1025
     (1 bitShift:33) lowBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1026
     (1 bitShift:33) highBit    
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1027
     (1 bitShift:64) lowBit     
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1028
     (1 bitShift:64) highBit     
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1029
     (1 bitShift:1000) lowBit   
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1030
     (1 bitShift:1000) highBit   
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1031
     ((1 bitShift:64)-1) lowBit  
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1032
     ((1 bitShift:64)-1) highBit  
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1033
    "
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1034
1aeeadaa162e added #lowBit
Claus Gittinger <cg@exept.de>
parents: 2428
diff changeset
  1035
    "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
  1036
!
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1037
4643
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1038
maskClear:aMask
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1039
    ^ self bitAnd:aMask bitInvert
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1040
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1041
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1042
maskSet:aMask
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1043
    ^ self bitOr:aMask
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1044
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1045
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1046
noMask:anInteger
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1047
    "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
  1048
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1049
    ^ (self bitAnd:anInteger) == 0
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1050
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1051
    "2r00001111 noMask:2r00000001"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1052
    "2r00001111 noMask:2r11110000"
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1053
!
48ee5b949c81 moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents: 4614
diff changeset
  1054
4278
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1055
odd
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1056
    "return true if the receiver is odd"
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1057
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1058
    ^ (self bitTest:1) not
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1059
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1060
    "
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1061
     16r112233445566778899 odd  
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1062
     16r112233445566778800 odd  
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1063
    "
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1064
78c34fcd9952 added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 4169
diff changeset
  1065
    "Created: / 6.6.1999 / 15:00:55 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1066
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1067
3890
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1068
!Integer methodsFor:'byte access'!
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1069
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1070
digitByteLength
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1071
    "return the number bytes required for a 2's complement
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1072
     binary representation of this Integer.
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1073
     For positive receivers, thats the same as the digitLength."
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1074
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1075
    "
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1076
     check if there is a 0-byte ...
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1077
     this allows to ask unnormalized LargeIntegers 
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1078
     for their digitLength
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1079
    "
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1080
    |l "{ Class: SmallInteger }" |
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1081
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1082
    self >= 0 ifTrue:[^ self digitLength].
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1083
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1084
    l := self negated digitLength.
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1085
    (self digitByteAt:l) == 16rFF ifTrue:[
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1086
        ^ (l - 1) max:1
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1087
    ].
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1088
    ^ l
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1089
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1090
    "
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1091
     -129 digitByteLength 
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1092
    "
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1093
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1094
    "Created: / 25.10.1998 / 14:58:09 / cg"
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1095
    "Modified: / 25.10.1998 / 19:20:33 / cg"
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1096
! !
a024811ed304 added #digitByteLength
Claus Gittinger <cg@exept.de>
parents: 3884
diff changeset
  1097
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1098
!Integer methodsFor:'coercing and converting'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1099
1888
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1100
asFixedPoint
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1101
    "return the receiver as a fixedPoint number"
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1102
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1103
    ^ FixedPoint numerator:self denominator:1 scale:1
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1104
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1105
    "
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1106
     100 asFixedPoint      
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1107
     100 asFixedPoint + 0.1 asFixedPoint    
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1108
    "
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1109
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1110
    "Modified: 5.11.1996 / 15:13:17 / cg"
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1111
!
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1112
2141
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1113
asFixedPoint:scale
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1114
    "return the receiver as fixedPoint number, with the given number
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1115
     of post-decimal-point digits."
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1116
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1117
    ^ FixedPoint numerator:self denominator:1 scale:scale
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1118
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1119
    "
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1120
     100 asFixedPoint:2      
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1121
     100 asFixedPoint + (0.1 asFixedPoint:2)    
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1122
    "
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1123
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1124
    "Modified: 10.1.1997 / 20:00:08 / cg"
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1125
!
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 2133
diff changeset
  1126
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1127
asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1128
    "return a Fraction with same value as receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1129
2794
862ee34a4821 when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1130
    ^ 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
  1131
862ee34a4821 when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1132
    "Modified: 28.7.1997 / 19:26:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1133
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1134
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1135
asInteger
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1136
    "return the receiver truncated towards zero - 
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1137
     for integers this is self"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1138
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1139
    ^ self
1337
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1140
!
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1141
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1142
asModuloNumber
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1143
    "return a precomputed modulo number"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1144
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1145
    ^ ModuloNumber modulus:self.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1146
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1147
    "Created: / 3.5.1999 / 14:48:03 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1148
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1149
1337
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1150
signExtendedByteValue
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1151
    "return a smallInteger from sign-extending the 8'th bit.
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1152
     May be useful for communication interfaces"
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1153
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1154
    ^ (self bitAnd:16rFF) signExtendedByteValue
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1155
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1156
    "Created: 7.5.1996 / 09:31:52 / cg"
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1157
!
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1158
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1159
signExtendedShortValue
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1160
    "return a smallInteger from sign-extending the 16'th bit.
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1161
     May be useful for communication interfaces"
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1162
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1163
    ^ (self bitAnd:16rFFFF) signExtendedShortValue
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1164
e5e00eecd4c9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  1165
    "Modified: 7.5.1996 / 09:31:57 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1166
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1167
1976
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  1168
!Integer methodsFor:'comparing'!
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  1169
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  1170
hash
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  1171
    "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
  1172
3298
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  1173
    ^ self bitAnd:16r3FFFFFFF.
1976
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  1174
3298
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  1175
    "
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  1176
        -20000000000000 hash 
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  1177
         20000000000000 hash
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  1178
    "
1976
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  1179
3298
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  1180
    "Created: / 14.11.1996 / 12:12:27 / cg"
bf837fee7a15 Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents: 3188
diff changeset
  1181
    "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
  1182
! !
f6e7094cd3e4 Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  1183
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1184
!Integer methodsFor:'double dispatching'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1185
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1186
differenceFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1187
    "sent when a fraction does not know how to subtract the receiver, an integer"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1188
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1189
    |d|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1190
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1191
    d := aFraction denominator.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1192
    ^ (Fraction numerator:(aFraction numerator - (self * d))
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1193
	      denominator:d)
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1194
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1195
    "Modified: 28.7.1997 / 19:08:30 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1196
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1197
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1198
productFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1199
    "sent when a fraction does not know how to multiply the receiver, an integer"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1200
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1201
    ^ (Fraction numerator:(self * aFraction numerator)
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1202
	      denominator:aFraction denominator)
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1203
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1204
    "Modified: 28.7.1997 / 19:08:27 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1205
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1206
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1207
quotientFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1208
    "sent when a fraction does not know how to divide the receiver, an integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1209
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1210
    ^ Fraction 
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1211
	numerator:aFraction numerator
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1212
	denominator:(self * aFraction denominator)
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1213
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1214
    "Modified: 28.7.1997 / 19:08:23 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1215
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1216
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1217
sumFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1218
    "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
  1219
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1220
    |d|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1221
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1222
    d := aFraction denominator.
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1223
    ^ Fraction numerator:(aFraction numerator + (self * d))
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1224
	      denominator:d
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1225
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2745
diff changeset
  1226
    "Modified: 28.7.1997 / 19:08:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1227
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1228
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1229
!Integer methodsFor:'helpers'!
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1230
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1231
gcd_helper:anInteger 
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1232
    "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
  1233
     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
  1234
     larger than the arg."
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1235
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1236
    | selfAbs argAbs selfLowBit argLowBit shift t |
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1237
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1238
    selfAbs := self.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1239
    argAbs := anInteger.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1240
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1241
    selfLowBit := selfAbs lowBit - 1.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1242
    argLowBit := argAbs lowBit - 1.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1243
    shift := selfLowBit min:argLowBit.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1244
    argAbs := argAbs bitShift:(argLowBit negated).
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1245
    [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
  1246
	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
  1247
	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
  1248
	    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
  1249
	].
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1250
	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
  1251
	selfLowBit := selfAbs lowBit - 1.
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1252
    ].
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1253
    ^ argAbs bitShift:shift
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1254
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1255
    "Created: 1.3.1997 / 16:38:17 / cg"
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1256
    "Modified: 1.3.1997 / 16:45:17 / cg"
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1257
! !
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1258
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1259
!Integer methodsFor:'misc math'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1260
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1261
acker:n
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1262
    "return the value of acker(self, n).
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1263
      ;-) Do not try with receivers > 3"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1264
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1265
    (self == 0) ifTrue:[^ n + 1].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1266
    (n == 0) ifTrue:[^ (self - 1) acker: 1].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1267
    ^ (self - 1) acker:(self acker:(n - 1))
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1268
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1269
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1270
     3 acker:2  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1271
    "
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1272
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1273
    "Modified: 18.7.1996 / 13:08:16 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1274
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1275
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1276
divMod:aNumber
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1277
    "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
  1278
     self \\ aNumber. 
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1279
     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
  1280
     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
  1281
     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
  1282
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1283
    ^ 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
  1284
	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
  1285
	with:(self \\ aNumber)
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1286
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1287
    "  
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1288
     10 divMod:3  
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1289
     100 factorial divMod:103
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1290
    "
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1291
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1292
    "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
  1293
!
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1294
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1295
extendedEuclid:tb
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1296
    "return the solution of 'ax + by = gcd(a,b)'.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1297
     An array conatining x, y and gcd(a,b) is returned."
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1298
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1299
    |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
  1300
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1301
    self > tb ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1302
        a := self.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1303
        b := tb.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1304
        swap := false.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1305
    ] ifFalse:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1306
        a := tb.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1307
        b := self.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1308
        swap := true.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1309
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1310
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1311
    shift := ((a lowBit) min:(b lowBit))-1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1312
    shift > 0 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1313
        tmp := shift negated.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1314
        a := a bitShift:tmp.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1315
        b := b bitShift:tmp.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1316
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1317
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1318
    gcd  := a copy.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1319
    gcd1 := b copy.        
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1320
    u := 1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1321
    u1 := 0.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1322
    v := 0.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1323
    v1 := 1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1324
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1325
    [
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1326
        t := gcd1 divMod:gcd.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1327
        gcd1 := gcd.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1328
        gcd := t at:2.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1329
        t := t at:1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1330
        tmp := v.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1331
        v := v1 - (v * t).
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1332
        v1 := tmp.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1333
        tmp := u.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1334
        u := u1 - (u * t).
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1335
        u1 := tmp.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1336
    ] doWhile:[gcd > 0].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1337
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1338
    shift > 0 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1339
        gcd1 := gcd1 bitShift:shift.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1340
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1341
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1342
    swap ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1343
        ^ Array with:v1 with:u1 with:gcd1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1344
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1345
    ^ Array with:u1 with:v1 with:gcd1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1346
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1347
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1348
    " 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1349
     14 extendedEuclid:5
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1350
     14 extendedEuclid:2  
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1351
     25 extendedEuclid:15
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1352
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1353
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1354
    "Created: / 27.4.1999 / 15:19:22 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1355
    "Modified: / 3.5.1999 / 07:30:02 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1356
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1357
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1358
factorial
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1359
    "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
  1360
     This is slightly faster than the recursive algorithm, and does not
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1361
     suffer from stack overflow problems (with big receivers)"
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1362
2745
bdf76e380d27 errorString to DomainError
Claus Gittinger <cg@exept.de>
parents: 2458
diff changeset
  1363
    |p i|
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1364
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1365
    (self < 2) 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
  1366
	self < 0 ifTrue:[
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1367
	    "/
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1368
	    "/ requested factorial of a negative number
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1369
	    "/
3395
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  1370
            ^ self class
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  1371
                raise:#domainErrorSignal
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  1372
                receiver:self
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  1373
                selector:#factorial
02f47135d80f pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents: 3374
diff changeset
  1374
                errorString:'factorial of negative number'
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1375
	].
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1376
	^ 1
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1377
    ].
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1378
    p := 2.
2745
bdf76e380d27 errorString to DomainError
Claus Gittinger <cg@exept.de>
parents: 2458
diff changeset
  1379
    i := 3.
bdf76e380d27 errorString to DomainError
Claus Gittinger <cg@exept.de>
parents: 2458
diff changeset
  1380
    [i <= self] whileTrue:[
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1381
	p := p * i.
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1382
	i := i + 1.
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1383
    ].
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1384
    ^ p
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1385
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1386
    "  
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1387
     10 factorial
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1388
     1000 factorial
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1389
     10000 factorial
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1390
     10000 factorialR
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1391
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1392
     Time millisecondsToRun:[2000 factorial] 
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1393
     Time millisecondsToRun:[2000 factorialR] 
2745
bdf76e380d27 errorString to DomainError
Claus Gittinger <cg@exept.de>
parents: 2458
diff changeset
  1394
     -1 factorial
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1395
    "
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1396
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1397
    "Modified: 29.10.1996 / 20:58:43 / cg"
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1398
!
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1399
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1400
factorialR
3908
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  1401
    "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
  1402
     This is included to demonstration purposes - if you really need
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  1403
     factorial numbers, use the iterative #factorial, which is slightly
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  1404
     faster and does not suffer from stack overflow problems (with big receivers)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1405
356
claus
parents: 345
diff changeset
  1406
    (self >= 2) ifTrue:[
3908
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  1407
        ^ self * (self - 1) factorialR
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1408
    ].
356
claus
parents: 345
diff changeset
  1409
    ^ 1
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
  1410
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
  1411
    "
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1412
     10 factorialR
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1413
     1000 factorialR
4158
e04b61500e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  1414
     Time millisecondsToRun:[10000 factorial]
212
3edd10edefaf *** empty log message ***
claus
parents: 202
diff changeset
  1415
    "
1560
f5ffcc5c3834 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1559
diff changeset
  1416
3908
b893f399f517 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3890
diff changeset
  1417
    "Created: / 18.7.1996 / 12:48:36 / cg"
4158
e04b61500e51 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  1418
    "Modified: / 8.5.1999 / 18:40:13 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1419
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1420
3162
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1421
fib
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1422
    "compute the fibionacci number for the receiver.
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1423
     (same result as returned by #recursiveFib.
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1424
      This method shows how a changed algorithm can
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1425
      change things much more drastic than tuning ...
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1426
      ... compare 30 recursiveFib with 30 fib;
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1427
      and dont even try 60 recursiveFib)"
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1428
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1429
    |a1 a2 an|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1430
3162
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1431
    a1 := 1.
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1432
    a2 := 0.
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1433
    an := 0.
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1434
    self timesRepeat:[
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1435
	an := a1 + a2.
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1436
	a2 := a1.
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1437
	a1 := an
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1438
    ].
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1439
    ^ an
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1440
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1441
    "
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1407
diff changeset
  1442
     Transcript showCR:(Time millisecondsToRun:[30 fib]) 
3162
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1443
     Transcript showCR:(Time millisecondsToRun:[30 recursiveFib]) 
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1444
     Transcript showCR:(Time millisecondsToRun:[60 fib])
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1445
     Transcript showCR:(Time millisecondsToRun:[200 fib])
c7c8af356ff2 dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents: 3138
diff changeset
  1446
     Transcript showCR:(Time millisecondsToRun:[1500 fib])
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1447
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1448
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1449
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1450
gcd:anInteger 
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1451
    "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
  1452
     Euclids & Knuths algorithm."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1453
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1454
    |selfAbs argAbs t|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1455
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1456
    selfAbs := self abs.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1457
    argAbs := anInteger abs.
1888
463d49ca22e3 fixedPoint support
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  1458
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1459
    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
  1460
	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
  1461
	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
  1462
	argAbs := t.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1463
    ].
52
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  1464
2428
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1465
    argAbs = 0 ifTrue: [^ selfAbs].
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1466
    selfAbs := selfAbs \\ argAbs.
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1467
    selfAbs = 0 ifTrue:[^ argAbs].
d54efc515824 better gcd for large integers
Claus Gittinger <cg@exept.de>
parents: 2408
diff changeset
  1468
    ^ argAbs gcd_helper:selfAbs
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  1469
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1470
    "                     
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1471
     3141589999999999 gcd:1000000000000000
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1472
    "
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1473
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3044
diff changeset
  1474
    "Modified: / 25.10.1997 / 16:08:45 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1475
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1476
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1477
integerReciprocal
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1478
    "return an integer representing 1/self * 2**n.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1479
     Where an integer is one bit longer than self."
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1480
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1481
    |b rem result|                                   
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1482
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1483
    b := self highBit.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1484
    rem := 1 bitShift:b.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1485
    result := 0 asLargeInteger numberOfDigits:(b // 8)+1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1486
    b := b+1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1487
    [b > 0] whileTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1488
        rem >= self ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1489
            rem := rem -= self.
4830
879f09ec443a Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents: 4803
diff changeset
  1490
            result digitBytes bitSetAt:b.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1491
        ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1492
        rem := rem mul2.               
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1493
        b := b - 1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1494
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1495
    ^ result compressed.          
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1496
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1497
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1498
    "                                     
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1499
     333 integerReciprocal                (2 raisedTo:18) // 333
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1500
     393 integerReciprocal          
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1501
     8 integerReciprocal              
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1502
     15 integerReciprocal 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1503
     15112233445566 integerReciprocal 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1504
     10239552311579 integerReciprocal
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1505
   "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1506
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1507
    "Modified: / 3.5.1999 / 14:27:18 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1508
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1509
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1510
inverseMod:n
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1511
    "find the modular inverse for myself to n.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1512
     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
  1513
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1514
    |e ret|
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1515
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1516
    e := self extendedEuclid:n.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1517
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1518
    (e at:3) == 1 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1519
        ret := e at:1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1520
        ret negative ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1521
            ^ ret + n
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1522
        ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1523
        ^ ret.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1524
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1525
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1526
    ^ 0
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1527
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1528
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1529
     14 inverseMod:5      -> 4
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1530
     5 inverseMod:14      -> 3
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1531
     14 inverseMod:11     -> 4                (4 * 14) \\ 11 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1532
     11 inverseMod:14     -> 9                (9 * 11) \\ 14 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1533
     79 inverseMod:3220   -> 1019
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1534
     3220 inverseMod:79   -> 54               (54 * 3220) \\ 79
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1535
     1234567891 inverseMod:1111111111119          
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1536
                          -> 148726663534     (148726663534*1234567891) \\ 1111111111119
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1537
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1538
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1539
     14 extendedEuclid:11 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1540
     5 extendedEuclid:14 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1541
     14 extendedEuclid:2
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1542
     3220 extendedEuclid:79    
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1543
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1544
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1545
    "Created: / 27.4.1999 / 15:19:22 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1546
    "Modified: / 30.4.1999 / 19:08:01 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1547
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1548
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1549
lcm:anInteger
52
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  1550
    "return the least common multiple (using gcd:)"
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  1551
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  1552
    ^ (self * anInteger) abs // (self gcd: anInteger)
cf23b8901602 *** empty log message ***
claus
parents: 49
diff changeset
  1553
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1554
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1555
     65 lcm:15
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1556
     3 lcm:15
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1557
    "
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1558
!
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1559
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1560
raisedTo:exp mod:mod
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1561
    "return the modulu (remainder) of 
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1562
     the receiver raised to exp (an Integer) and mod (another Integer)"
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1563
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1564
    |result m t bits "{Class: SmallInteger}"|
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1565
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1566
    "use the addition chaining algorithm"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1567
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1568
    exp negative ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1569
        ^ self class
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1570
            raise:#domainErrorSignal
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1571
            receiver:self
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1572
            selector:#raisedTo:mod:
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1573
            errorString:'modulo arithmethic with negative exponent'
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1574
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1575
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1576
    m := mod asModuloNumber.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1577
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1578
    result := 1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1579
    t := self \\ mod.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1580
    bits := exp highBit.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1581
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1582
    1 to:bits do:[:i|
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1583
        (exp bitAt:i) == 1 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1584
            result := m modulusOf:(result * t).
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1585
        ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1586
        t := m modulusOf:(t * t).
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1587
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1588
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1589
    ^ result
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1590
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1591
    "                                       
4917
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1592
     2 raisedTo:2 mod:3  
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1593
      20000000000000 raisedTo:200 mod:190 
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1594
     (20000000000000 raisedTo:200) \\ 190 
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1595
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1596
      Time millisecondsToRun:[100 timesRepeat:[
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1597
                                20000000000000 raisedTo:200 mod:190
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1598
                              ]
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1599
                             ]      
4917
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1600
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1601
     Time millisecondsToRun:[100 timesRepeat:[
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1602
                                (20000000000000 raisedTo:200) \\ 190
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1603
                             ]
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  1604
                            ]    
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1605
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1606
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1607
    "Created: / 27.4.1999 / 15:19:22 / stefan"
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1608
    "Modified: / 5.5.1999 / 11:18:20 / stefan"
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1609
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1610
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1611
raisedToCrtModP:p q:q ep:ep eq:eq u:u
4917
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1612
    "Application of the Chinese Remainder Theorem (CRT).
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1613
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1614
     This is a faster modexp for moduli with a known factorisation into two
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1615
     relatively prime factors p and q, and an input relatively prime to the
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1616
     modulus, the Chinese Remainder Theorem to do the computation mod p and
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1617
     mod q, and then combine the results.  This relies on a number of
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1618
     precomputed values, but does not actually require the modulus n or the
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1619
     exponent e.
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1620
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1621
     expout = expin ^ e mod (p*q).
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1622
     We form this by evaluating
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1623
     p2 = (expin ^ e) mod p and
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1624
     q2 = (expin ^ e) mod q
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1625
     and then combining the two by the CRT.
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1626
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1627
     Two optimisations of this are possible.  First, we can reduce expin
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1628
     modulo p and q before starting.
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1629
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1630
     Second, since we know the factorisation of p and q (trivially derived
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1631
     from the factorisation of n = p*q), and expin is relatively prime to
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1632
     both p and q, we can use Euler's theorem, expin^phi(m) = 1 (mod m),
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1633
     to throw away multiples of phi(p) or phi(q) in e.
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1634
     Letting ep = e mod phi(p) and
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1635
              eq = e mod phi(q)
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1636
     then combining these two speedups, we only need to evaluate
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1637
     p2 = ((expin mod p) ^ ep) mod p and
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1638
     q2 = ((expin mod q) ^ eq) mod q.
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1639
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1640
     Now we need to apply the CRT.  Starting with
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1641
     expout = p2 (mod p) and
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1642
     expout = q2 (mod q)
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1643
     we can say that expout = p2 + p * k, and if we assume that 0 <= p2 < p,
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1644
     then 0 <= expout < p*q for some 0 <= k < q.  Since we want expout = q2
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1645
     (mod q), then p*k = q2-p2 (mod q).  Since p and q are relatively prime,
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1646
     p has a multiplicative inverse u mod q.  In other words, u = 1/p (mod q).
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1647
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1648
     Multiplying by u on both sides gives k = u*(q2-p2) (mod q).
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1649
     Since we want 0 <= k < q, we can thus find k as
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1650
     k = (u * (q2-p2)) mod q.
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1651
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1652
     Once we have k, evaluating p2 + p * k is easy, and
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1653
     that gives us the result
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1654
    "  
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1655
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1656
    |result t|
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1657
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1658
    t := self \\ p.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1659
    result := t raisedTo:ep mod:p.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1660
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1661
    t := self \\ q.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1662
    t := t raisedTo:eq mod:q.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1663
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1664
    "now p2 is in result, q2 in t"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1665
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1666
    t := t -= result.
4141
09df9029a84c Fix chinese remainder stuff.
Stefan Vogel <sv@exept.de>
parents: 4136
diff changeset
  1667
    t := t \\ q.
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1668
    t := t *= u.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1669
    t := t \\ q.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1670
    t := t *= p.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1671
    result := result += t.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1672
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1673
    ^ result.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1674
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1675
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1676
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1677
    "                                       
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1678
     2 raisedTo:2 mod:3
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1679
      20000000000000 raisedTo:200 mod:190
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1680
     (20000000000000 raisedTo:200) \\ 190
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1681
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1682
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1683
    "Created: / 30.4.1999 / 15:53:15 / stefan"
4151
6d6971c27567 Some speedups.
Stefan Vogel <sv@exept.de>
parents: 4142
diff changeset
  1684
    "Modified: / 5.5.1999 / 11:01:15 / stefan"
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
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1687
raisedToInteger:exp 
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1688
    "return the receiver raised to exp"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1689
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1690
    |result e t bits "{SmallInteger}"|
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1691
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1692
    "use the addition chaining algorithm"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1693
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1694
    result := 1.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1695
    t := self.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1696
    exp < 0 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1697
        e := exp negated.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1698
    ] ifFalse:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1699
        e := exp.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1700
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1701
    bits := e highBit.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1702
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1703
    1 to:bits do:[:i|
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1704
        (e bitAt:i) == 1 ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1705
            result := result * t.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1706
        ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1707
        t := t * t.
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1708
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1709
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1710
    (exp < 0) ifTrue:[
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1711
        ^ 1 / result
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1712
    ].
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1713
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1714
    ^ result
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1715
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1716
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1717
    "
4917
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1718
     (2 raisedTo:216) asFloat     
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  1719
     (2 raisedTo:500) raisedTo:500    
4136
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1720
      2 raisedTo:10
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1721
     -2 raisedTo:10
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1722
     2 raisedTo:9
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1723
     -2 raisedTo:9
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1724
     10 raisedTo:-10
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1725
     2 raisedTo:0
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1726
    "
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1727
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1728
    "Created: / 27.4.1999 / 15:19:22 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1729
    "Modified: / 27.4.1999 / 16:16:11 / stefan"
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1730
!
8c8bfa5808a3 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 4039
diff changeset
  1731
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1732
recursiveFib
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1733
    "compute the fibionacci number for the receiver.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1734
     WARNING:
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1735
         Dont use this method if you need fibionacci numbers -
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1736
         this method is for benchmarking purposes only.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1737
         (use #fib instead and dont ever try 60 recursiveFib ...)"
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1738
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1739
    (self > 1) ifTrue:[
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1740
	^ (self - 1) recursiveFib + (self - 2) recursiveFib
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1741
    ].
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1742
    ^ 1
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1743
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1744
    "
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1745
     30 fib
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1746
     30 recursiveFib
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1747
     Transcript showCR:(Time millisecondsToRun:[30 recursiveFib])
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1748
     Transcript showCR:(Time millisecondsToRun:[30 fib])
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1749
    "
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1750
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1751
    "Modified: 18.7.1996 / 12:47:19 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1752
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1753
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1754
!Integer methodsFor:'printing & storing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1755
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1756
asBCDBytes
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1757
    "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
  1758
     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
  1759
     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
  1760
     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
  1761
     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
  1762
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1763
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1764
    |s rest twoDigits hi lo|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1765
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1766
    self == 0 ifTrue:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1767
        ^ #[ 16r00 ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1768
    ].
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1769
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1770
    "/ 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
  1771
    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
  1772
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1773
    rest := self.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1774
    [rest > 0] whileTrue:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1775
        twoDigits := rest \\ 100.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1776
        rest := rest // 100.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1777
        hi := twoDigits \\ 10.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1778
        lo := twoDigits // 10.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1779
        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
  1780
    ].
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1781
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1782
    ^ s contents reverse
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1783
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1784
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1785
     12345678 asBCDBytes 
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1786
     12345678901234567890 asBCDBytes
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1787
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1788
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1789
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1790
!
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  1791
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1792
displayString
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1793
    "return a string for displaying in a view (as in inspector).
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1794
     The output radix is usually 10, but can be changed by setting
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1795
     DefaultDisplayRadix (see Integer>>displayRadix:)"
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1796
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
  1797
    (DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  1798
	^ self printString
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
  1799
    ].
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1800
    ^ self radixPrintStringRadix:DefaultDisplayRadix
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1801
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1802
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1803
     Integer displayRadix:16. 12345 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1804
     Integer displayRadix:2.  12345 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1805
     Integer displayRadix:10. 12345
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1806
    "
49
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1807
!
f1c2d75f2eb6 *** empty log message ***
claus
parents: 44
diff changeset
  1808
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1809
errorPrintHex
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1810
    "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
  1811
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1812
     (self printStringRadix:16) errorPrint
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1813
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1814
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1815
hexPrintString
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1816
    "return a hex string representation of the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1817
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1818
    ^ self printStringRadix:16
3874
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  1819
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  1820
    "
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  1821
     127 hexPrintString
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  1822
     -1 hexPrintString
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  1823
    "
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  1824
a04715e25340 comment
Claus Gittinger <cg@exept.de>
parents: 3683
diff changeset
  1825
    "Modified: / 11.10.1998 / 01:15:43 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1826
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1827
2408
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1828
hexPrintString:size
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1829
    "return a hex string representation of the receiver,
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1830
     padded to size characters"
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1831
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1832
    ^ (self printStringRadix:16) leftPaddedTo:size with:$0
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1833
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1834
    "Created: 18.2.1997 / 13:32:33 / cg"
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1835
!
fd31506f31ef added #hexPrintString:
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
  1836
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1837
printHex
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1838
    "print the receiver as a hex number on the standard output stream"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1839
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1840
     (self printStringRadix:16) print
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1841
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1842
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1843
printOn:aStream
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1844
    "append a printed description of the receiver to aStream"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1845
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1846
    self printOn:aStream base:10
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1847
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1848
    "Modified: / 20.1.1998 / 14:10:45 / stefan"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1849
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1850
4522
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  1851
printOn:aStream base:b
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1852
    "return a string representation of the receiver in the specified
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1853
     radix (without the initial XXr)"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1854
4522
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  1855
    |base num s divMod mod r r2 r4 nD numN|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1856
4522
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  1857
    base := b.
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1858
    (base between:2 and:36) ifFalse:[
4522
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  1859
        self error:'invalid base' mayProceed:true.
29a6625dc7b1 error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents: 4424
diff changeset
  1860
        base := 10.
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
  1861
    ].
06dbdeeed4f9 *** empty log message ***
claus
parents: 52
diff changeset
  1862
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1863
    (self = 0) ifTrue:[aStream nextPut:$0. ^ self].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1864
    (self < 0) ifTrue:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1865
        aStream nextPut:$- . 
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1866
        num := self negated.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1867
    ] ifFalse:[
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1868
        num := self.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1869
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1870
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1871
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1872
     claus: changed it from a recursive algorithm;
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1873
     (it used to trigger stack-overflow exceptions when printing
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1874
      3000 factorial ...)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1875
    "
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1876
"/    leftPart := num // base.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1877
"/    (leftPart ~= 0) ifTrue:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1878
"/        leftPart printOn:aStream base:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1879
"/        aStream nextPut:(Character digitValue:(num \\ base).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1880
"/        ^ self
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1881
"/    ].
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1882
"/    aStream nextPut:(Character digitValue:num).
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  1883
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1884
    "/ 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
  1885
    "/ 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
  1886
    "/ 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
  1887
    "/ 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
  1888
    "/ 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
  1889
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1890
    r2 := base*base.   "/ radix^2
3138
1cd38a2d1924 oops - printString
ca
parents: 3136
diff changeset
  1891
    r4 := r2*r2.             "/ radix^4
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1892
    base <= 10 ifTrue:[
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1893
        r := r4*r2.        "/ radix^6
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1894
        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
  1895
    ] ifFalse:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1896
        r := r4*base.    "/ radix^5
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1897
        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
  1898
    ].
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1899
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1900
    "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
  1901
     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
  1902
     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
  1903
    "
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1904
    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
  1905
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1906
    [num >= r] whileTrue:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1907
        "/
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1908
        "/ chop off nD digits.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1909
        "/
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1910
        divMod := num divMod:r.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1911
        num := divMod at:1.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1912
        numN := divMod at:2.
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  1913
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1914
        "/ process them
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1915
        nD timesRepeat:[
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1916
            divMod := numN divMod:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1917
            numN := divMod at:1.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1918
            mod := divMod at:2.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1919
            s nextPut:(Character digitValue:mod).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1920
        ].
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  1921
    ].
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1922
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1923
    [num ~= 0] whileTrue:[
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1924
        divMod := num divMod:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1925
        num := divMod at:1.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1926
        mod := divMod at:2.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1927
        s nextPut:(Character digitValue:mod).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1928
    ].
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1929
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1930
    aStream nextPutAll:(s contents reverse).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1931
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1932
    "
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1933
        3000 factorial printOn:Transcript base:10
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1934
        10 printOn:Transcript base:3
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1935
        31 printOn:Transcript base:3
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1936
        -20  printOn:Transcript base:16
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1937
        -20  printOn:Transcript base:10
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1938
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  1939
3121
91de0211651c even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1940
    "Modified: / 17.11.1997 / 16:27:45 / cg"
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1941
    "Modified: / 20.1.1998 / 18:05:02 / stefan"
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1942
!
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1943
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1944
printOn:aStream radix:base
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1945
    "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
  1946
     The receiver is printed in radix base (instead of the default, 10).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1947
     This method is obsoleted by #printOn:base:, which is ST-80 compatible."
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1948
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1949
    self printOn:aStream base:base
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1950
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1951
    "Modified: / 20.5.1996 / 11:54:10 / cg"
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1952
    "Modified: / 20.1.1998 / 14:10:45 / stefan"
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1953
!
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1954
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1955
printStringRadix:base
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1956
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1957
    |s|
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1958
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1959
    s := WriteStream on:(String new:20).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1960
    self printOn:s base:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1961
    ^ s contents
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1962
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1963
    "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
  1964
    "Modified: / 20.1.1998 / 14:10:54 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1965
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1966
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1967
printStringRadix:aRadix size:sz fill:fillCharacter
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1968
    "return a string representation of the receiver in the specified
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1969
     radix. The string is padded on the left with fillCharacter to make
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1970
     its size as specified in sz."
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1971
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  1972
    |s actualSize|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1973
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1974
    s := self printStringRadix:aRadix.
2951
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  1975
    actualSize := s size.
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  1976
    actualSize < sz ifTrue:[
256e90eee7f9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  1977
	s := ((String new:(sz - actualSize)) atAllPut:fillCharacter) , s
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1978
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1979
    ^ s
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1980
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1981
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1982
     1024 printStringRadix:16 size:4 fill:$0
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1983
     1024 printStringRadix:2 size:16 fill:$.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1984
     1024 printStringRadix:16 size:8 fill:(Character space)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1985
    "
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  1986
!
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  1987
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1988
radixPrintStringRadix:base
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1989
    "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
  1990
     base; prepend XXr to the string"
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1991
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1992
    |s|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1993
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1994
    s := WriteStream on:(String new:20).
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1995
    base printOn:s.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1996
    s nextPut:$r.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1997
    self printOn:s base:base.
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  1998
    ^ s contents
216
a8abff749575 *** empty log message ***
claus
parents: 213
diff changeset
  1999
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2000
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2001
     31 radixPrintStringRadix:2
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2002
     31 radixPrintStringRadix:3
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2003
     31 radixPrintStringRadix:36
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2004
    "
3188
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2005
301eaf46b9e4 Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents: 3162
diff changeset
  2006
    "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
  2007
    "Modified: / 20.1.1998 / 14:11:03 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2008
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2009
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2010
!Integer methodsFor:'queries'!
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  2011
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2012
digitAt:n
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2013
    "return the n-th byte of the binary representation.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2014
     This is a very stupid implementation, and should be redefined in
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2015
     concrete subclasses."
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  2016
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2017
    |num count|
569
7134eb78cf48 readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2018
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2019
    num := self.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2020
    count := n.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2021
    [count > 1] whileTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2022
	num := num // 256.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2023
	count := count - 1
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  2024
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2025
    ^ num \\ 256
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  2026
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  2027
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2028
     16r44332211 digitAt:1     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2029
     16r44332211 digitAt:2    
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2030
     16r44332211 digitAt:3     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2031
     16r44332211 digitAt:4     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2032
     16r44332211 digitAt:5     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2033
     16r00332211 digitAt:4     
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2034
     16r00332211 digitAt:5     
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  2035
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2036
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2037
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2038
digitLength
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2039
    "return the number of bytes needed for the binary representation
2816
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  2040
     of the receiver.
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  2041
     This method is redefined in concrete classes - the fallback here is
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  2042
     never really used."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2043
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2044
    ^ (self log:256) ceiling asInteger
2816
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  2045
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2794
diff changeset
  2046
    "Modified: 31.7.1997 / 13:19:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2047
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2048
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2049
isInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2050
    "return true, if the receiver is some kind of integer number"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2051
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2052
    ^ true
3036
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2053
!
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2054
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2055
isPowerOfTwo
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2056
    "return true, if the receiver is a power of 2"
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2057
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2058
    ^ (self bitAnd:(self - 1)) == 0
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2059
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2060
    "
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2061
     0 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2062
     1 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2063
     2 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2064
     3 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2065
     4 isPowerOfTwo     
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2066
     16r8000000000000000 isPowerOfTwo
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2067
     16r8000000000000001 isPowerOfTwo
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2068
    "
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2069
f2d332547db7 added #isPowerOf2
Claus Gittinger <cg@exept.de>
parents: 2951
diff changeset
  2070
    "Modified: 15.10.1997 / 18:43:49 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2071
! !
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  2072
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2073
!Integer methodsFor:'special bit operators'!
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2074
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2075
bitAnd_32:anInteger
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2076
    "return a C-semantic 32bit locical-and of the receiver and
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2077
     the argument. Both must be either Small- or LargeIntegers.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2078
     This (nonstandard) specialized method is provided to allow simulation of
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2079
     and operations with C semantics."
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2080
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2081
%{  /* NOCONTEXT */
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2082
    int val1, val2, rslt;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2083
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2084
    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
  2085
	val1 = __intVal(self);
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2086
    } 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
  2087
	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
  2088
	if (!val1) goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2089
    } 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
  2090
	goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2091
    }
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2092
    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
  2093
	val2 = __intVal(anInteger);
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2094
    } 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
  2095
	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
  2096
	if (!val2) goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2097
    } 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
  2098
	goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2099
    }
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2100
    rslt = val1 & val2;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2101
    RETURN(__MKINT(rslt));
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2102
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2103
  bad: ;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2104
%}.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2105
    self primitiveFailed.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2106
!
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2107
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2108
bitOr_32:anInteger
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2109
    "return a C-semantic 32bit locical-or of the receiver and
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2110
     the argument. Both must be either Small- or LargeIntegers.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2111
     This (nonstandard) specialized method is provided to allow simulation of
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2112
     and operations with C semantics."
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2113
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2114
%{  /* NOCONTEXT */
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2115
    int val1, val2, rslt;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2116
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2117
    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
  2118
	val1 = __intVal(self);
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2119
    } 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
  2120
	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
  2121
	if (!val1) goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2122
    } 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
  2123
	goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2124
    }
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2125
    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
  2126
	val2 = __intVal(anInteger);
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2127
    } 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
  2128
	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
  2129
	if (!val2) goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2130
    } 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
  2131
	goto bad;
2458
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2132
    }
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2133
    rslt = val1 | val2;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2134
    RETURN(__MKINT(rslt));
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2135
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2136
  bad: ;
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2137
%}.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2138
    self primitiveFailed.
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2139
! !
4f29a9dd12fe checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2457
diff changeset
  2140
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2141
!Integer methodsFor:'testing'!
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2142
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2143
isLiteral
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2144
    "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
  2145
     (i.e. can be used in constant arrays)"
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2146
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2147
    ^ true
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2148
! !
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2149
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2150
!Integer methodsFor:'tracing'!
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2151
4682
4158042a9c8c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4653
diff changeset
  2152
traceInto:aRequestor level:level from:referrer
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2153
    "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
  2154
4682
4158042a9c8c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4653
diff changeset
  2155
    ^ aRequestor traceInteger:self level:level from:referrer
4653
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2156
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2157
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2158
! !
b3d38149fbde added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents: 4643
diff changeset
  2159
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2160
!Integer methodsFor:'truncation & rounding'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2161
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2162
ceiling
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2163
    "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
  2164
     For integers, this is the receiver itself."
345
claus
parents: 328
diff changeset
  2165
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2166
    ^ self
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2167
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2168
    "Modified: 18.7.1996 / 12:44:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2169
!
345
claus
parents: 328
diff changeset
  2170
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2171
compressed
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2172
    "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
  2173
     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
  2174
     zeros removed. This method is redefined in LargeInteger."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2175
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2176
    ^ self
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2177
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2178
    "Modified: 5.11.1996 / 14:07:41 / cg"
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  2179
!
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  2180
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2181
floor
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2182
    "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
  2183
     For integers, this is the receiver itself."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2184
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2185
    ^ self
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2186
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2187
    "Modified: 18.7.1996 / 12:44:00 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2188
!
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
  2189
1866
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2190
fractionPart
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2191
    "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
  2192
     (i.e. the receiver minus its truncated value)
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2193
     Since integers have no fraction, return 0 here."
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2194
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2195
    ^ 0
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2196
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2197
    "
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2198
     1234.56789 fractionPart      
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2199
     1.2345e6 fractionPart        
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2200
     1000 fractionPart              
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2201
     10000000000000000 fractionPart 
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2202
    "
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2203
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2204
    "Modified: 4.11.1996 / 20:27:44 / cg"
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2205
!
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2206
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2207
integerPart
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2208
    "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
  2209
     (i.e. the receivers truncated value)
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2210
     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
  2211
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2212
    ^ self
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2213
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2214
    "
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2215
     1234.56789 integerPart      
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2216
     1.2345e6 integerPart        
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2217
     1000 integerPart              
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2218
     10000000000000000 integerPart 
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2219
    "
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2220
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2221
    "Modified: 4.11.1996 / 20:28:22 / cg"
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2222
!
225afe414934 added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
  2223
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2224
normalize
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2225
    "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
  2226
     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
  2227
     zeros removed.
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2228
     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
  2229
     renamed to #compressed for ST-80 compatibility."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2230
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2231
    ^ self compressed
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2232
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1866
diff changeset
  2233
    "Modified: 5.11.1996 / 14:08:24 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2234
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2235
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2236
rounded
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2237
    "return the receiver rounded toward the next Integer -
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2238
     for integers this is the receiver itself."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2239
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2240
    ^ self
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2241
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2242
    "Modified: 18.7.1996 / 12:44:24 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2243
!
69
4564b6328136 *** empty log message ***
claus
parents: 54
diff changeset
  2244
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2245
truncated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2246
    "return the receiver truncated towards zero - 
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2247
     for integers this is the receiver itself."
159
514c749165c3 *** empty log message ***
claus
parents: 132
diff changeset
  2248
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2249
    ^ self
1558
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2250
864bddaf96e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  2251
    "Modified: 18.7.1996 / 12:44:33 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2252
! !
569
7134eb78cf48 readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2253
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2254
!Integer::ModuloNumber class methodsFor:'documentation'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2255
4803
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2256
copyright
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2257
"
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2258
 COPYRIGHT (c) 1999 by eXept Software AG
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2259
              All Rights Reserved
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2260
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2261
 This software is furnished under a license and may be used
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2262
 only in accordance with the terms of that license and with the
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2263
 inclusion of the above copyright notice.   This software may not
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2264
 be provided or otherwise made available to, or used by, any
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2265
 other person.  No title to or ownership of the software is
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2266
 hereby transferred.
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2267
"
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2268
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2269
!
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2270
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2271
documentation
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2272
"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2273
    This is a helper class to perform fast computation of the modulus.
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  2274
    (with big numbers, this does make a difference)
4803
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2275
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2276
    WARNING: this does only work with numbers which have no common
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2277
             divisor (which is true for cryptographic applications).
47355eb08e99 comment
Claus Gittinger <cg@exept.de>
parents: 4801
diff changeset
  2278
             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
  2279
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2280
    [author:]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2281
        Stefan Vogel
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2282
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2283
    [see also:]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2284
        Integer SmallInteger LargeInsteger
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2285
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2286
    [instance variables:]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2287
        modulus       the modulus
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2288
        reciprocal    reciprocal of the modulus
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2289
        shift         shift count to cut off some bits
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2290
"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2291
!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2292
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2293
examples
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2294
"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2295
                                                                [exBegin]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2296
        17 asModuloNumber modulusOf:38
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2297
                                                                [exEnd]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2298
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2299
                                                                [exBegin]
4609
6a1819c7a743 comment
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
  2300
        38 \\ 17        
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2301
                                                                [exEnd]
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2302
"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2303
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2304
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2305
!Integer::ModuloNumber class methodsFor:'instance creation'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2306
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2307
modulus:anInteger
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2308
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2309
    ^ self new modulus:anInteger
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2310
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2311
    "Created: / 3.5.1999 / 11:13:15 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2312
    "Modified: / 3.5.1999 / 11:18:37 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2313
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2314
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2315
!Integer::ModuloNumber methodsFor:'accessing'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2316
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2317
modulus
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2318
    "return the modulus"
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2319
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2320
    ^ modulus
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2321
!
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2322
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2323
modulus:n
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2324
    "set the modulus"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2325
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2326
    modulus := n.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2327
    reciprocal := n integerReciprocal.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2328
    shift := (n highBit ) negated.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2329
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2330
    "Created: / 3.5.1999 / 10:02:39 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2331
    "Modified: / 3.5.1999 / 14:30:49 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2332
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2333
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2334
!Integer::ModuloNumber methodsFor:'arithmethic'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2335
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2336
modulusOf:aNumber
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2337
    |d e|
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2338
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2339
    d := aNumber.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2340
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2341
    "throw off low nbits(modulus)"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2342
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2343
    e := (d bitShift:shift).
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2344
    e := e * reciprocal.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2345
    e := (e bitShift:shift).
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2346
    e := e * modulus.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2347
    d := d - e.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2348
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2349
    "this subtract is done max 2 times"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2350
    [d > modulus] whileTrue:[
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2351
        d := d - modulus.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2352
    ].
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2353
    ^ d.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2354
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2355
    "
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2356
     |m|
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2357
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2358
     m := self new modulus:7.
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2359
     m modulusOf:55.
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2360
    "
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2361
4801
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2362
    "VERY SLOW (size of divisor vs. dividend):
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2363
     |m|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2364
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2365
     m := self new modulus:7.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2366
     m modulusOf:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2367
    "
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2368
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2369
    "SLOW (using standard \\ operation):
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2370
     |m|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2371
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2372
     m := 123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2373
     Time millisecondsToRun:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2374
        1000 timesRepeat:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2375
            874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658 \\ m
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2376
        ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2377
     ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2378
    "            
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2379
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2380
    "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
  2381
     |m|
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2382
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2383
     m := self new modulus:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2384
     Time millisecondsToRun:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2385
        1000 timesRepeat:[
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2386
            m modulusOf:874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2387
        ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2388
     ]
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2389
    "            
edf02501d5e2 comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2390
4142
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2391
    "Modified: / 3.5.1999 / 14:30:32 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2392
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2393
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2394
!Integer::ModuloNumber methodsFor:'converting'!
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2395
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2396
asModuloNumber
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2397
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2398
    ^ self
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2399
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2400
    "Created: / 3.5.1999 / 14:48:27 / stefan"
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2401
! !
a881cc4bfaec Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents: 4141
diff changeset
  2402
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1700
diff changeset
  2403
!Integer class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2404
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2405
version
4917
662e7f3315fb comments
Claus Gittinger <cg@exept.de>
parents: 4867
diff changeset
  2406
    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.114 1999-10-19 10:09:43 cg Exp $'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2407
! !