Number.st
author Claus Gittinger <cg@exept.de>
Thu, 11 Dec 2014 15:11:07 +0100
changeset 17207 75c41a799425
parent 17190 284d9c71385f
child 17224 43a8ca39cc90
permissions -rw-r--r--
class: Number comment/format in: #isAlmostEqualTo:nEpsilon: ref to documentation
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
15108
ffef3e5a13d5 class: Number
Claus Gittinger <cg@exept.de>
parents: 15100
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
"
5360
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
    12
"{ Package: 'stx:libbasic' }"
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
    13
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    14
ArithmeticValue subclass:#Number
15127
6278efcd484d class: Number
Stefan Vogel <sv@exept.de>
parents: 15108
diff changeset
    15
	instanceVariableNames:''
16630
cd7e20dfc977 class: Number
Stefan Vogel <sv@exept.de>
parents: 16477
diff changeset
    16
	classVariableNames:'DecimalPointCharacterForPrinting DecimalPointCharactersForReading'
15127
6278efcd484d class: Number
Stefan Vogel <sv@exept.de>
parents: 15108
diff changeset
    17
	poolDictionaries:''
6278efcd484d class: Number
Stefan Vogel <sv@exept.de>
parents: 15108
diff changeset
    18
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1701
diff changeset
    21
!Number class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    22
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    23
copyright
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    24
"
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    25
 COPYRIGHT (c) 1988 by Claus Gittinger
15108
ffef3e5a13d5 class: Number
Claus Gittinger <cg@exept.de>
parents: 15100
diff changeset
    26
              All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    27
88
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    28
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    29
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    31
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    32
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    33
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    34
"
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    35
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    36
88
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    37
documentation
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    39
    abstract superclass for all kinds of numbers
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1245
diff changeset
    40
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
    41
    [class variables:]
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
    42
        DecimalPointCharacterForPrinting          <Character>                     used when printing
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
    43
        DecimalPointCharactersForReading          <Collection of Character>       accepted as decimalPointChars when reading
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
    44
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1245
diff changeset
    45
    [author:]
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
    46
        Claus Gittinger
1628
da30f2f41db7 comment
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    47
da30f2f41db7 comment
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    48
    [see also:]
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
    49
        Integer LargeInteger SmallInteger
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
    50
        LimitedPrecisionReal Float ShortFloat
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
    51
        Fraction FixedPoint
88
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    52
"
81dacba7a63a *** empty log message ***
claus
parents: 56
diff changeset
    53
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    54
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1701
diff changeset
    55
!Number class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    56
11944
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    57
fastFromString:aString
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    58
    "return the next Float, Integer or ShortFloat from the string.
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    59
     No spaces are skipped.
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    60
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    61
     This is a specially tuned entry (using a low-level C-call), which
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    62
     returns garbage if the argument string is not a valid float number.
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    63
     It has been added to allow high speed string decomposition into numbers,
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    64
     especially for mass-data."
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    65
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    66
    ^ self fastFromString:aString at:1
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    67
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    68
    "
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    69
     Float fromString:'12345.0'
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    70
     Float fastFromString:'12345.0'
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    71
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    72
     Integer fromString:'12345'
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    73
     Integer fastFromString:'12345'
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    74
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    75
     should be roughly 10times faster than the general method:
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    76
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    77
     Time millisecondsToRun:[
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    78
        100000 timesRepeat:[ Float fromString:'12345.0' ]
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    79
     ].   
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    80
     Time millisecondsToRun:[
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    81
        100000 timesRepeat:[ Float fastFromString:'12345.0' ]
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    82
     ].   
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    83
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    84
     Time millisecondsToRun:[
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    85
        100000 timesRepeat:[ Integer fromString:'12345' ]
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    86
     ].  
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    87
     Time millisecondsToRun:[
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    88
        100000 timesRepeat:[ Integer fastFromString:'12345' ]
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    89
     ]. 
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    90
    "
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    91
!
6fb9813181cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11770
diff changeset
    92
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
    93
fromString:aString
17056
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
    94
    "return a number by reading from aString.
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
    95
     In contrast to readFrom:, no garbage is allowed after the number.
17056
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
    96
     I.e. the string must contain exactly one valid number (with optional separators around)"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
    97
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
    98
    ^ self
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
    99
        fromString:aString
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   100
        decimalPointCharacters:(self decimalPointCharactersForReading) 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   101
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   102
    "
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   103
     Number fromString:'12345'
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   104
     Number fromString:'abc'
17056
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   105
     Number fromString:'1abc'   -> raises an error
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   106
     Number readFrom:'1abc'     -> reads a 1    
17058
641d0f196c1e class: Number
Claus Gittinger <cg@exept.de>
parents: 17057
diff changeset
   107
     Number readFrom:'10/2'     -> reads a 10   
641d0f196c1e class: Number
Claus Gittinger <cg@exept.de>
parents: 17057
diff changeset
   108
     Number fromString:'10/2'   -> raises an error   
641d0f196c1e class: Number
Claus Gittinger <cg@exept.de>
parents: 17057
diff changeset
   109
     Number fromString:'(1/2)'  -> reads a fraction   
641d0f196c1e class: Number
Claus Gittinger <cg@exept.de>
parents: 17057
diff changeset
   110
     Number readFrom:'(1/2)'    -> reads a fraction   
641d0f196c1e class: Number
Claus Gittinger <cg@exept.de>
parents: 17057
diff changeset
   111
     Number readFrom:'(10/2)'   -> reads a 5   
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   112
     '12345' asNumber
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   113
    "
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   114
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   115
    "Modified: / 3.8.1998 / 20:05:11 / cg"
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   116
!
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   117
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   118
fromString:aString decimalPointCharacters:decimalPointCharacters
17056
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   119
    "return a number by reading from aString.
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   120
     In contrast to readFrom:, no garbage is allowed after the number.
17056
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   121
     I.e. the string must contain exactly one valid number (with optional separators around)"
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   122
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   123
    |s num|
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   124
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   125
    s := aString readStream.
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   126
    num := self readFrom:s decimalPointCharacters:decimalPointCharacters onError:[^ self error:'invalid number'].
17056
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   127
    s atEnd ifFalse:[
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   128
        s skipSeparators.
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   129
        s atEnd ifFalse:[
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   130
            ^ self error:'garbage at end of number'
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   131
        ].
dfbf6a7fd352 class: Number
Claus Gittinger <cg@exept.de>
parents: 16656
diff changeset
   132
    ].
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   133
    ^ num.
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   134
3716
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   135
    "
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   136
     Number fromString:'12345'
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   137
     Number fromString:'abc'
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   138
     Number fromString:'1abc'
3716
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   139
     '12345' asNumber
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   140
    "
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   141
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   142
    "Modified: / 3.8.1998 / 20:05:11 / cg"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   143
!
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   144
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   145
fromString:aString decimalPointCharacters:decimalPointCharacters onError:exceptionBlock
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   146
    "return a number by reading from aString.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   147
     In contrast to readFrom:, no garbage is allowed after the number.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   148
     I.e. the string must contain exactly one valid number (with optional separators around)"
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   149
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   150
    ^ self readFrom:aString decimalPointCharacters:decimalPointCharacters onError:exceptionBlock
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   151
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   152
    "
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   153
     Number fromString:'12345' onError:0
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   154
     Number fromString:'fooBarBaz' onError:0
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   155
    "
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   156
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   157
    "Modified: / 3.8.1998 / 20:05:34 / cg"
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   158
!
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   159
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   160
fromString:aString onError:exceptionBlock
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   161
    "return a number by reading from aString.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   162
     In contrast to readFrom:, no garbage is allowed after the number.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   163
     I.e. the string must contain exactly one valid number (with optional separators around)"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   164
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   165
    ^ self readFrom:aString onError:exceptionBlock
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   166
3716
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   167
    "
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   168
     Number fromString:'12345' onError:0
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   169
     Number fromString:'fooBarBaz' onError:0
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   170
    "
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   171
e7904bd50d9d comment
Claus Gittinger <cg@exept.de>
parents: 3634
diff changeset
   172
    "Modified: / 3.8.1998 / 20:05:34 / cg"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   173
!
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   174
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   175
readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   176
    "return the next Number from the (character-)stream aStream;
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   177
     skipping all whitespace first.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   178
     Return the value of exceptionBlock, if no number can be read.
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   179
     This method is less strict than the smalltalk number reader; it
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   180
     allows for prefixed + and also allows missing fractional part after eE.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   181
     It also allows garbage after the number - i.e. it reads what it can.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   182
     See #fromString: , which is more strict and does not allow garbage at the end."
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   183
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   184
    ^ self
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   185
        readFrom:aStringOrStream 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   186
        decimalPointCharacters:decimalPointCharacters 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   187
        onError:[self error:'conversion error for: ' , self name]
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   188
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   189
    "
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   190
     Number readFrom:'123.456' decimalPointCharacters:'.'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   191
     Number readFrom:'123,456' decimalPointCharacters:'.,'     
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   192
     Number readFrom:'123,456' decimalPointCharacters:'.'     
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   193
    "
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   194
!
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   195
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   196
readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters onError:exceptionBlock
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   197
    "return the next Number from the (character-)stream aStream;
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   198
     skipping all whitespace first.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   199
     Return the value of exceptionBlock, if no number can be read.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   200
     This method is less strict than the smalltalk number reader; it
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   201
     allows for prefixed + and also allows missing fractional part after eE.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   202
     It also allows garbage after the number - i.e. it reads what it can.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   203
     See #fromString: , which is more strict and does not allow garbage at the end."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   204
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   205
    ^ [
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   206
        |value intValue mantissaAndScale scale decimalMantissa str 
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   207
         nextChar radix sign signExp exp numerator denom|
3995
777470826394 no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 3900
diff changeset
   208
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   209
        str := aStringOrStream readStream.
1701
80d13adb2e77 better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1635
diff changeset
   210
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   211
        nextChar := str skipSeparators.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   212
        nextChar isNil ifTrue:[^ exceptionBlock value].
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   213
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   214
        (nextChar == $-) ifTrue:[
15197
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   215
            sign := -1.
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   216
            str next.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   217
            nextChar := str peekOrNil
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   218
        ] ifFalse:[
15197
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   219
            sign := 1.
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   220
            (nextChar == $+) ifTrue:[
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   221
                str next.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   222
                nextChar := str peekOrNil
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   223
            ]
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   224
        ].
15197
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   225
        nextChar = $( ifTrue:[
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   226
            "maybe a Fraction e.g. (1/3)"
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   227
            str next.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   228
            numerator := Integer readFrom:str onError:[^ exceptionBlock value].
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   229
            str skipSeparators.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   230
            nextChar := str peekOrNil.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   231
            nextChar = $/ ifTrue:[
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   232
                str next.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   233
                denom := Integer readFrom:str onError:[^ exceptionBlock value].
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   234
                str skipSeparators.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   235
                nextChar := str peekOrNil.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   236
            ].
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   237
            nextChar = $) ifFalse:[^ exceptionBlock value].
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   238
            str next.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   239
            value := Fraction numerator:numerator denominator:denom.
15197
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   240
            ^ value * sign
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   241
        ].
11526
474c8ba65e7a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11488
diff changeset
   242
        nextChar isNil ifTrue:[^ exceptionBlock value].    
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   243
        (nextChar isDigit or:[(decimalPointCharacters includes:nextChar)]) ifFalse:[
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   244
            ^ exceptionBlock value.
569
7134eb78cf48 readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   245
"/          value := super readFrom:str.
15197
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   246
"/          sign == -1 ifTrue:[value := value negated].
339
claus
parents: 329
diff changeset
   247
"/          ^ value
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   248
        ].
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   249
        (decimalPointCharacters includes:nextChar) ifTrue:[
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   250
            radix := 10.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   251
            value := 0.0.
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   252
            intValue := 0.
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   253
        ] ifFalse:[
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   254
            value := Integer readFrom:str radix:10.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   255
            nextChar := str peekOrNil.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   256
            ((nextChar == $r) or:[ nextChar == $R]) ifTrue:[
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   257
                str next.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   258
                radix := value.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   259
                value := Integer readFrom:str radix:radix.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   260
            ] ifFalse:[
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   261
                radix := 10
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   262
            ].
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   263
            intValue := value.
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   264
        ].
4637
814b3d8e3f72 allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents: 4558
diff changeset
   265
12112
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   266
        (self == Integer or:[self inheritsFrom:Integer]) ifFalse:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   267
            (decimalPointCharacters includes:nextChar) ifTrue:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   268
                str next.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   269
                nextChar := str peekOrNil.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   270
                decimalMantissa := 0.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   271
                (nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[
16450
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   272
                    |mantissa|
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   273
                    mantissaAndScale := self readMantissaAndScaleFrom:str radix:radix.
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   274
                    mantissa := mantissaAndScale first.
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   275
                    value := (mantissa coerce:value) + mantissa.
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   276
                    nextChar := str peekOrNil.
12112
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   277
                ]
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   278
            ].
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   279
            ('eEdDqQ' includes:nextChar) ifTrue:[
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   280
                str next.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   281
                nextChar := str peekOrNil.
12112
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   282
                signExp := 1.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   283
                (nextChar == $+) ifTrue:[
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   284
                    str next.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   285
                    nextChar := str peekOrNil.
12112
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   286
                ] ifFalse:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   287
                    (nextChar == $-) ifTrue:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   288
                        str next.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   289
                        nextChar := str peekOrNil.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   290
                        signExp := -1
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   291
                    ]
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   292
                ].
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   293
                ('qQ' includes:nextChar) ifTrue:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   294
                    value := value asLongFloat.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   295
                ] ifFalse:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   296
                    value := value asFloat.
16450
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   297
"/ future: (for now, always create Doubles for Dolphin,Squeak etc. compatibility)
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   298
"/                ('eE' includes:nextChar) ifTrue:[
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   299
"/                    value := value asShortFloat
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   300
"/                ]
9130
07e7a292aa76 fixedPoint reading fixed (explicit scale)
Claus Gittinger <cg@exept.de>
parents: 9127
diff changeset
   301
                ].
12112
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   302
                (nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   303
                    exp := (Integer readFrom:str radix:radix) * signExp.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   304
                    value := value * ((value class unity * 10.0) raisedToInteger:exp)
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   305
                ]
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   306
            ] ifFalse:[
16450
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   307
                ('sS' includes:nextChar) ifTrue:[
12112
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   308
                    str next.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   309
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   310
                    nextChar := str peekOrNil.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   311
                    (nextChar notNil and:[ nextChar isDigit]) ifTrue:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   312
                        scale := (Integer readFrom:str).
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   313
                    ].
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   314
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   315
                    mantissaAndScale isNil ifTrue:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   316
                        value := intValue asFixedPoint:(scale ? 0).
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   317
                    ] ifFalse:[
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   318
                        denom := 10 raisedTo:mantissaAndScale last.
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   319
                        value := FixedPoint 
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   320
                                    numerator:(intValue * denom) + (mantissaAndScale second)
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   321
                                    denominator:denom
16450
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   322
                                    scale:(scale ? mantissaAndScale third).
12112
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
   323
                    ].
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   324
                ] ifFalse:[
16450
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   325
                    (self inheritsFrom:LimitedPrecisionReal) ifTrue:[
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   326
                        "when requesting a specific Float instance, coerce it.
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   327
                         otherwise return a value without loosing precision"
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   328
                        value := self coerce:value.
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   329
                    ].
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   330
                ].
7456
42a1cbc55b6c care to not always return longFloats
Claus Gittinger <cg@exept.de>
parents: 7451
diff changeset
   331
            ].
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   332
        ].
15197
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   333
        sign == -1 ifTrue:[
6632
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   334
            value := value negated
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   335
        ].
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   336
        value.
65014fd967d9 Avoid creation of block in #readFrom:onError:
Stefan Vogel <sv@exept.de>
parents: 6584
diff changeset
   337
    ] on:Error do:exceptionBlock
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   338
329
claus
parents: 325
diff changeset
   339
    "
claus
parents: 325
diff changeset
   340
     Number readFrom:(ReadStream on:'54.32e-01')      
claus
parents: 325
diff changeset
   341
     Number readFrom:(ReadStream on:'12345678901234567890') 
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   342
     Number readFrom:(ReadStream on:'12345678901234567890.0') 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   343
     Number readFrom:(ReadStream on:'12345678901234567890.012345678901234567890') 
329
claus
parents: 325
diff changeset
   344
     Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') 
569
7134eb78cf48 readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   345
     Number readFrom:'16rAAAAFFFFAAAAFFFF' 
7134eb78cf48 readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   346
     Number readFrom:'0.000001'  
329
claus
parents: 325
diff changeset
   347
     '+00000123.45' asNumber  
15078
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   348
     Number readFrom:'(1/3)'      
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   349
     Number readFrom:'(-1/3)'      
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   350
     Number readFrom:'(1/-3)'      
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   351
     Number readFrom:'-(1/3)'      
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   352
     Number readFrom:'-(-1/3)'      
15078
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   353
     Number readFrom:'(-1/3'      
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   354
     Number readFrom:'99s'      
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   355
     Number readFrom:'99.00s'      
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   356
     Number readFrom:'99.0000000s'      
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   357
     Number readFrom:'.0000000s'      
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   358
     Number readFrom:'.0000000q'      
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   359
     Number readFrom:'.0000000f'      
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   360
     Number readFrom:'.0000000e'      
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   361
     Number readFrom:'.0000000s1'      
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   362
     Number readFrom:'.0000000q1'      
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   363
     Number readFrom:'.0000000f1'      
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   364
     Number readFrom:'.0000000e1'      
16450
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   365
     LongFloat readFrom:'.00000001'      
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   366
     Number readFrom:'.00000000000001'      
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   367
     Number readFrom:'.001'      
9a81e708b188 class: Number
Stefan Vogel <sv@exept.de>
parents: 16406
diff changeset
   368
     ShortFloat readFrom:'.001'      
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   369
     Number readFrom:'123garbage'      -> returns 123
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   370
     Number fromString:'123garbage'    -> raises an error  
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   371
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   372
     DecimalPointCharactersForReading := #( $. $, ).   
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   373
     Number readFrom:'99,00'      
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   374
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   375
     DecimalPointCharactersForReading := #( $. ).   
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   376
     Number readFrom:'99,00'      
329
claus
parents: 325
diff changeset
   377
    "
569
7134eb78cf48 readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   378
3375
7c64da3964e4 faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
   379
    "Modified: / 14.4.1998 / 19:22:50 / cg"
296
754358c5508a *** empty log message ***
claus
parents: 293
diff changeset
   380
!
754358c5508a *** empty log message ***
claus
parents: 293
diff changeset
   381
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   382
readFrom:aStringOrStream onError:exceptionBlock
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   383
    "return the next Number from the (character-)stream aStream;
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   384
     skipping all whitespace first; return the value of exceptionBlock,
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   385
     if no number can be read.
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   386
     This method is less strict than the smalltalk number reader; it
17057
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   387
     allows for prefixed + and also allows missing fractional part after eE.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   388
     It also allows garbage after the number - i.e. it reads what it can.
08f275c70437 class: Number
Claus Gittinger <cg@exept.de>
parents: 17056
diff changeset
   389
     See #fromString: , which is more strict and does not allow garbage at the end."
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   390
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   391
    ^ self 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   392
        readFrom:aStringOrStream 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   393
        decimalPointCharacters:(self decimalPointCharactersForReading) 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   394
        onError:exceptionBlock
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   395
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   396
    "
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   397
     Number readFrom:(ReadStream on:'54.32e-01')      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   398
     Number readFrom:(ReadStream on:'12345678901234567890') 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   399
     Number readFrom:(ReadStream on:'12345678901234567890.0') 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   400
     Number readFrom:(ReadStream on:'12345678901234567890.012345678901234567890') 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   401
     Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   402
     Number readFrom:'16rAAAAFFFFAAAAFFFF' 
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   403
     Number readFrom:'0.000001'  
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   404
     '+00000123.45' asNumber  
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   405
     Number readFrom:'99s'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   406
     Number readFrom:'99.00s'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   407
     Number readFrom:'99.0000000s'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   408
     Number readFrom:'.0000000s'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   409
     Number readFrom:'.0000000q'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   410
     Number readFrom:'.0000000f'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   411
     Number readFrom:'.0000000e'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   412
     Number readFrom:'.0000000s1'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   413
     Number readFrom:'.0000000q1'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   414
     Number readFrom:'.0000000f1'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   415
     Number readFrom:'.0000000e1'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   416
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   417
     DecimalPointCharactersForReading := #( $. $, ).   
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   418
     Number readFrom:'99,00'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   419
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   420
     DecimalPointCharactersForReading := #( $. ).   
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   421
     Number readFrom:'99,00'      
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   422
    "
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   423
!
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   424
296
754358c5508a *** empty log message ***
claus
parents: 293
diff changeset
   425
readSmalltalkSyntaxFrom:aStream
754358c5508a *** empty log message ***
claus
parents: 293
diff changeset
   426
    "ST-80 compatibility (thanks to a note from alpha testers)
754358c5508a *** empty log message ***
claus
parents: 293
diff changeset
   427
     read and return the next Number in smalltalk syntax from the 
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   428
     (character-) aStream.
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   429
     Returns nil if aStream contains no valid number."
329
claus
parents: 325
diff changeset
   430
15776
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   431
    ^ self readSmalltalkSyntaxFrom:aStream onError:nil.
296
754358c5508a *** empty log message ***
claus
parents: 293
diff changeset
   432
329
claus
parents: 325
diff changeset
   433
    "
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   434
     Number readSmalltalkSyntaxFrom:'99d'    
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   435
     Number readSmalltalkSyntaxFrom:'99.00d'    
15078
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   436
     Number readSmalltalkSyntaxFrom:'54.32e-01'    
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   437
     Number readSmalltalkSyntaxFrom:'12345678901234567890'
15776
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   438
     Number readSmalltalkSyntaxFrom:'16rAAAAFFFFAAAAFFFF'   
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   439
     Number readSmalltalkSyntaxFrom:'foobar'     
15078
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   440
     Number readSmalltalkSyntaxFrom:'(1/10)' 
15127
6278efcd484d class: Number
Stefan Vogel <sv@exept.de>
parents: 15108
diff changeset
   441
6278efcd484d class: Number
Stefan Vogel <sv@exept.de>
parents: 15108
diff changeset
   442
     Number readSmalltalkSyntaxFrom:'(1/0)' 
6278efcd484d class: Number
Stefan Vogel <sv@exept.de>
parents: 15108
diff changeset
   443
15197
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   444
     Number readFrom:'(1/3)' 
15078
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   445
     Number readFrom:'(-1/3)'
15197
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   446
     Number readFrom:'-(1/3)' 
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   447
     Number readFrom:'(1/-3)' 
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   448
     Number readFrom:'(-1/-3)' 
a78cec832ef3 class: Number
Claus Gittinger <cg@exept.de>
parents: 15127
diff changeset
   449
     Number readFrom:'-(-1/-3)'   
15078
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   450
     Number readSmalltalkSyntaxFrom:'+00000123.45'  
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
   451
     Number readFrom:'+00000123.45'  
5013
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   452
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   453
     |s|
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   454
     s := ReadStream on:'2.'.
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   455
     Number readSmalltalkSyntaxFrom:s.
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   456
     s next    
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   457
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   458
     |s|
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   459
     s := ReadStream on:'2.0.'.
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   460
     Number readSmalltalkSyntaxFrom:s.
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   461
     s next    
329
claus
parents: 325
diff changeset
   462
    "
3613
75629df7f224 fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents: 3394
diff changeset
   463
5013
e2aff65e6eba comment
Claus Gittinger <cg@exept.de>
parents: 4988
diff changeset
   464
    "Modified: / 19.11.1999 / 18:26:47 / cg"
15776
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   465
!
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   466
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   467
readSmalltalkSyntaxFrom:aStream onError:errorValue
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   468
    "ST-80 compatibility (thanks to a note from alpha testers)
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   469
     read and return the next Number in smalltalk syntax from the 
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   470
     (character-) aStream.
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   471
     Returns nil if aStream contains no valid number."
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   472
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   473
    |n|
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   474
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   475
    [
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   476
        n := Scanner scanNumberFrom:aStream.
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   477
    ] on:Error do:[:ex|
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   478
        n := nil
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   479
    ].
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   480
    n isNil ifTrue:[^ errorValue value].
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   481
    ^ n
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   482
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   483
    "
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   484
     Number readSmalltalkSyntaxFrom:'foo' onError:123   
e7f5200ad493 class: Number
Claus Gittinger <cg@exept.de>
parents: 15595
diff changeset
   485
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   486
! !
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   487
16656
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   488
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
   489
!Number class methodsFor:'constants'!
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
   490
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   491
decimalPointCharacter 
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   492
    "printed"
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   493
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   494
    <resource: #obsolete>
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   495
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   496
    ^ self decimalPointCharacterForPrinting
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   497
!
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   498
8303
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   499
decimalPointCharacter:aCharacter 
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   500
    "printed"
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   501
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   502
    <resource: #obsolete>
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   503
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   504
    self decimalPointCharacterForPrinting:aCharacter
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   505
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   506
    "
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   507
     1.5 printString
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   508
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   509
     Number decimalPointCharacter:$,.
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   510
     1.5 printString       
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   511
     Number decimalPointCharacter:$..
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   512
    "
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   513
!
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   514
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   515
decimalPointCharacterForPrinting 
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   516
    "printed"
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   517
16267
beef1cb9c801 class: Number
Claus Gittinger <cg@exept.de>
parents: 16070
diff changeset
   518
    ^ DecimalPointCharacterForPrinting ? $.
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   519
!
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   520
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   521
decimalPointCharacterForPrinting:aCharacter 
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   522
    "printed"
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   523
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   524
    DecimalPointCharacterForPrinting := aCharacter
8303
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   525
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   526
    "
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   527
     1.5 printString
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   528
9274
f3b231fb2ef7 comments
Claus Gittinger <cg@exept.de>
parents: 9140
diff changeset
   529
     Number decimalPointCharacterForPrinting:$,.
8303
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   530
     1.5 printString       
9274
f3b231fb2ef7 comments
Claus Gittinger <cg@exept.de>
parents: 9140
diff changeset
   531
     Number decimalPointCharacterForPrinting:$..
8303
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   532
    "
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   533
!
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   534
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   535
decimalPointCharacters 
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   536
    "accepted when converting from a string"
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   537
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   538
    <resource: #obsolete>
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   539
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   540
    ^ self decimalPointCharactersForReading
8303
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   541
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   542
    "
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   543
     1.5 printString
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   544
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   545
     Number decimalPointCharacters:#( $. $,) .
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   546
     Number fromString:'1.5'.     
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   547
     Number fromString:'1,5'.     
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   548
     Number decimalPointCharacters:#( $. ).
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   549
    "
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   550
!
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   551
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   552
decimalPointCharacters:aCollectionOfCharacters 
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   553
    "accepted when converting from a string"
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   554
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   555
    <resource: #obsolete>
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   556
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   557
    self decimalPointCharactersForReading:aCollectionOfCharacters 
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   558
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   559
    "
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   560
     Number decimalPointCharacters:#( $. $,) .
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   561
     Number fromString:'1.5'.     
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   562
     Number fromString:'1,5'.     
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   563
     Number decimalPointCharacters:#( $. ).
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   564
    "
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   565
!
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   566
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   567
decimalPointCharactersForReading 
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   568
    "default when converting from a string"
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   569
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   570
    "/ cg: changing the default leads to trouble in some
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   571
    "/ language processors (PrologScanner...)
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   572
    "/ PLEASE DO ONLY CHANGE THE DEFAULT BELOW FOR END-USER APPLICATIONS (if at all).
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   573
    "/ BETTER: pass the DecimalPointCharacterSet explicitly
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   574
    DecimalPointCharactersForReading isNil ifTrue:[
9333
c1d712705426 decimalPointCharacter stuff
Claus Gittinger <cg@exept.de>
parents: 9274
diff changeset
   575
        ^ #( $. )
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   576
    ].
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   577
    ^ DecimalPointCharactersForReading
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   578
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   579
    "
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   580
     1.5 printString
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   581
9274
f3b231fb2ef7 comments
Claus Gittinger <cg@exept.de>
parents: 9140
diff changeset
   582
     Number decimalPointCharactersForReading:#( $. $,) .
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   583
     Number fromString:'1.5'.     
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   584
     Number fromString:'1,5'.     
9274
f3b231fb2ef7 comments
Claus Gittinger <cg@exept.de>
parents: 9140
diff changeset
   585
     Number decimalPointCharactersForReading:#( $. ).
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   586
    "
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   587
!
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   588
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   589
decimalPointCharactersForReading:aCollectionOfCharacters 
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   590
    "accepted when converting from a string"
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   591
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   592
    DecimalPointCharactersForReading := aCollectionOfCharacters
8303
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   593
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   594
    "
9274
f3b231fb2ef7 comments
Claus Gittinger <cg@exept.de>
parents: 9140
diff changeset
   595
     Number decimalPointCharactersForReading:#( $. $,) .
8303
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   596
     Number fromString:'1.5'.     
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   597
     Number fromString:'1,5'.     
9274
f3b231fb2ef7 comments
Claus Gittinger <cg@exept.de>
parents: 9140
diff changeset
   598
     Number decimalPointCharactersForReading:#( $. ).
8303
837daadaab78 decimalPointCharacter definition
werner
parents: 8301
diff changeset
   599
    "
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   600
!
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
   601
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
   602
epsilon
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
   603
    "return the maximum relative spacing of instances of mySelf 
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
   604
     (i.e. the value-delta of the least significant bit)"
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
   605
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
   606
     ^ self subclassResponsibility
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
   607
!
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
   608
10727
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   609
epsilonForCloseTo
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   610
    "return the epsilon used in the closeTo: comparison.
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   611
     (useful would be something like self epsilon or epsilon*10,
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   612
      but for Squeak compatibility.... - sigh)"
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   613
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   614
    ^ 0.0001
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   615
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   616
    "
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   617
     Float epsilon     
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   618
     ShortFloat epsilon 
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   619
     Float epsilon10    
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   620
     ShortFloat epsilon10
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   621
    "
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   622
!
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   623
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
   624
pi
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
   625
    "return Pi in my representation (and accuracy)."
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
   626
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
   627
     ^ self subclassResponsibility
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
   628
! !
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
   629
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1701
diff changeset
   630
!Number class methodsFor:'error reporting'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   631
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   632
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arg:arg errorString:text 
3394
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   633
    "ST-80 compatible signal raising. Provided for PD numeric classes"
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   634
4558
94044dccefab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3995
diff changeset
   635
    <context: #return>
94044dccefab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3995
diff changeset
   636
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   637
    ^ self
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   638
        raise:aSignalSymbolOrErrorClass 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   639
        receiver:someNumber 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   640
        selector:sel 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   641
        arguments:(Array with:arg)
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   642
        errorString:text 
3394
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   643
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   644
    "
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   645
     Number 
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   646
        raise:#domainErrorSignal
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   647
        receiver:1.0
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   648
        selector:#sin
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   649
        arg:nil
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   650
        errorString:'foo bar test'
3394
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   651
    "
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   652
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   653
    "Modified: / 16.11.2001 / 14:12:50 / cg"
3394
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   654
!
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   655
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   656
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arguments:argArray errorString:text 
6194
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   657
    "ST-80 compatible signal raising. Provided for PD numeric classes.
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   658
     aSignalSymbolOrErrorClass is either an Error-subclass, or
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   659
     the selector which is sent to myself, to retrieve the Exception class / Signal."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   660
4558
94044dccefab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3995
diff changeset
   661
    <context: #return>
94044dccefab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3995
diff changeset
   662
6194
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   663
    |msg signalOrException|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   664
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   665
    msg := MessageSend
6194
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   666
                receiver:someNumber
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   667
                selector:sel
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   668
                arguments:argArray.
3394
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   669
6194
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   670
    aSignalSymbolOrErrorClass isSymbol ifTrue:[
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   671
        signalOrException := self perform:aSignalSymbolOrErrorClass.
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   672
    ] ifFalse:[
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   673
        signalOrException := aSignalSymbolOrErrorClass.    "/ assume its an Error-Subclass
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   674
    ].
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   675
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   676
    ^ signalOrException
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   677
         raiseRequestWith:msg 
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   678
         errorString:text 
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   679
         in:thisContext sender
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   680
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   681
    "
3394
33e8273b95f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3375
diff changeset
   682
     Number 
6194
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   683
        raise:#domainErrorSignal
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   684
        receiver:1.0
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   685
        selector:#foo 
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   686
        errorString:'foo bar test'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   687
    "
6194
e060dbb4f75c allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6155
diff changeset
   688
6195
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   689
    "Modified: / 16.11.2001 / 14:12:09 / cg"
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   690
!
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   691
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   692
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel errorString:text 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   693
    "ST-80 compatible signal raising. Provided for PD numeric classes.
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   694
     aSignalSymbolOrErrorClass is either an Error-subclass, or
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   695
     the selector which is sent to myself, to retrieve the Exception class / Signal."
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   696
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   697
    <context: #return>
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   698
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   699
    ^ self
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   700
        raise:aSignalSymbolOrErrorClass 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   701
        receiver:someNumber 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   702
        selector:sel 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   703
        arguments:#()
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   704
        errorString:text 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   705
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   706
    "
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   707
     Number 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   708
        raise:#domainErrorSignal
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   709
        receiver:1.0
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   710
        selector:#foo 
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   711
        errorString:'foo bar test'
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   712
    "
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   713
9e60c38d1d61 #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents: 6194
diff changeset
   714
    "Modified: / 16.11.2001 / 14:13:16 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   715
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   716
16656
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   717
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1701
diff changeset
   718
!Number class methodsFor:'private'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   719
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   720
readMantissaAndScaleFrom:aStream radix:radix
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   721
    "helper for readFrom: -
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   722
     return the mantissa (post-decimal-point digits) from the (character-)stream aStream;
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   723
     In addition, the scale (number of postDecimalPoint digits) is returned 
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   724
     (to support reading fixedPoint numbers).
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   725
     No whitespace is skipped.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   726
     Errs if no number is available on aStream."
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   727
10321
c07d199084fc avoid double-send of digitValue
Claus Gittinger <cg@exept.de>
parents: 10320
diff changeset
   728
    |nextChar value factor intMantissa scale digit|
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   729
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   730
    value := 0.0.
10320
a573647dad4f avoid longfloats if possible
Claus Gittinger <cg@exept.de>
parents: 10191
diff changeset
   731
    factor := 1.0 / radix.
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   732
    scale := 0.
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   733
    intMantissa := 0.
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   734
    nextChar := aStream peekOrNil.
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   735
    [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
10321
c07d199084fc avoid double-send of digitValue
Claus Gittinger <cg@exept.de>
parents: 10320
diff changeset
   736
        digit := nextChar digitValue.
c07d199084fc avoid double-send of digitValue
Claus Gittinger <cg@exept.de>
parents: 10320
diff changeset
   737
        value := value + (digit * factor).
c07d199084fc avoid double-send of digitValue
Claus Gittinger <cg@exept.de>
parents: 10320
diff changeset
   738
        intMantissa := (intMantissa * radix) + digit.
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   739
        factor := factor / radix.
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   740
        scale := scale + 1.
10320
a573647dad4f avoid longfloats if possible
Claus Gittinger <cg@exept.de>
parents: 10191
diff changeset
   741
        scale > 6 ifTrue:[
a573647dad4f avoid longfloats if possible
Claus Gittinger <cg@exept.de>
parents: 10191
diff changeset
   742
            factor := factor asLongFloat.
a573647dad4f avoid longfloats if possible
Claus Gittinger <cg@exept.de>
parents: 10191
diff changeset
   743
            value := value asLongFloat.
a573647dad4f avoid longfloats if possible
Claus Gittinger <cg@exept.de>
parents: 10191
diff changeset
   744
        ].
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   745
        aStream next.
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   746
        nextChar := aStream peekOrNil
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   747
    ].
7388
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
   748
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   749
    ^ (Array with:value with:intMantissa with:scale).
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   750
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   751
    "
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   752
     Number readMantissaAndScaleFrom:'234'    readStream radix:10. 
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   753
     Number readMantissaAndScaleFrom:'2'      readStream radix:10. 
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   754
     Number readMantissaAndScaleFrom:'234567' readStream radix:10. 
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   755
     Number readMantissaAndScaleFrom:'234000' readStream radix:10. 
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   756
     Number readMantissaAndScaleFrom:'234'    readStream radix:10. 
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   757
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
   758
     Number readMantissaAndScaleFrom:'12345678901234567890' readStream radix:10. 
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   759
    "
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   760
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   761
    "Modified: / 14.4.1998 / 18:47:47 / cg"
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   762
!
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   763
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   764
readMantissaFrom:aStream radix:radix
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   765
    "helper for readFrom: -
6692
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   766
     return the mantissa (post-decimal-point digits)
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   767
     from the (character-)stream aStream;
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   768
     No whitespace is skipped.
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   769
     Errs if no number available."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   770
6899
9ed8494c9f10 support for fixedPoint numbers (s-exponentCharacter)
Claus Gittinger <cg@exept.de>
parents: 6702
diff changeset
   771
    ^ (self readMantissaAndScaleFrom:aStream radix:radix) first
3375
7c64da3964e4 faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
   772
6692
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   773
    "
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   774
     Number readMantissaFrom:'234'    readStream radix:10.
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   775
     Number readMantissaFrom:'2'      readStream radix:10.
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   776
     Number readMantissaFrom:'234567' readStream radix:10.
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   777
    "
314f88dcbbd1 comment
Claus Gittinger <cg@exept.de>
parents: 6637
diff changeset
   778
3375
7c64da3964e4 faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
   779
    "Modified: / 14.4.1998 / 18:47:47 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   780
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   781
8892
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8844
diff changeset
   782
!Number class methodsFor:'queries'!
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8844
diff changeset
   783
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8844
diff changeset
   784
isAbstract
11224
a9a66cee404f comment
Claus Gittinger <cg@exept.de>
parents: 11136
diff changeset
   785
    "Return if this class is an abstract class.
a9a66cee404f comment
Claus Gittinger <cg@exept.de>
parents: 11136
diff changeset
   786
     True is returned for Number here; false for subclasses.
a9a66cee404f comment
Claus Gittinger <cg@exept.de>
parents: 11136
diff changeset
   787
     Abstract subclasses must redefine again."
a9a66cee404f comment
Claus Gittinger <cg@exept.de>
parents: 11136
diff changeset
   788
8892
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8844
diff changeset
   789
    ^ self == Number
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8844
diff changeset
   790
! !
5d05a7f150a5 +isAbstract
Claus Gittinger <cg@exept.de>
parents: 8844
diff changeset
   791
16656
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   792
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7214
diff changeset
   793
!Number methodsFor:'Compatibility-Squeak'!
4988
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   794
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   795
asSmallAngleDegrees
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   796
    "Return the receiver normalized to lie within the range (-180, 180)"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   797
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   798
    | pos |
4988
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   799
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   800
    pos := self \\ 360.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   801
    pos > 180 ifTrue: [pos := pos - 360].
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   802
    ^ pos
4988
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   803
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   804
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   805
     #(-500 -300 -150 -5 0 5 150 300 500 1200) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   806
        collect: [:n | n asSmallAngleDegrees]
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   807
    "
6072
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
   808
!
4988
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   809
6335
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   810
closeFrom:aNumber
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   811
    "are these two numbers close?"
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   812
10727
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   813
    ^ self closeFrom:aNumber withEpsilon:(self class epsilonForCloseTo)
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   814
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   815
    "
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   816
     9.0 closeTo: 8.9999     
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   817
     9.9 closeTo: 9          
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   818
     (9/3) closeTo: 2.9999      
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   819
     1 closeTo: 0.9999      
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   820
     1 closeTo: 1.0001      
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   821
     1 closeTo: 1.001       
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   822
     1 closeTo: 0.999       
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   823
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   824
     0.9999 closeTo: 1      
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   825
     1.0001 closeTo: 1      
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   826
     1.001 closeTo: 1     
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   827
     0.999 closeTo: 1
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   828
     Float NaN closeTo:Float NaN
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   829
     Float infinity closeTo:Float infinity
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   830
    "
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   831
!
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   832
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   833
closeFrom:aNumber withEpsilon:eps
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   834
    "are these two numbers close?"
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   835
6335
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   836
    | fuzz |
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   837
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   838
    self isNaN == aNumber isNaN ifFalse: [^ false]. 
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   839
    self isInfinite == aNumber isInfinite ifFalse: [^ false].
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   840
10727
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   841
    fuzz := (self abs max:aNumber abs) * eps. 
6335
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   842
    ^ (self - aNumber) abs <= fuzz
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   843
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   844
    "
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   845
     9.0 closeTo: 8.9999     
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   846
     9.9 closeTo: 9          
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   847
     (9/3) closeTo: 2.9999      
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   848
     1 closeTo: 0.9999      
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   849
     1 closeTo: 1.0001      
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   850
     1 closeTo: 1.001       
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   851
     1 closeTo: 0.999       
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   852
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   853
     0.9999 closeTo: 1      
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   854
     1.0001 closeTo: 1      
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   855
     1.001 closeTo: 1     
6345
cfe5db4fe391 Only comments
Stefan Vogel <sv@exept.de>
parents: 6335
diff changeset
   856
     0.999 closeTo: 1
cfe5db4fe391 Only comments
Stefan Vogel <sv@exept.de>
parents: 6335
diff changeset
   857
     Float NaN closeTo:Float NaN
cfe5db4fe391 Only comments
Stefan Vogel <sv@exept.de>
parents: 6335
diff changeset
   858
     Float infinity closeTo:Float infinity
6335
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   859
    "
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   860
!
8286bc57d05e closeTo: generalized for all numbers
Claus Gittinger <cg@exept.de>
parents: 6258
diff changeset
   861
6143
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
   862
closeTo:num
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
   863
    "are these two numbers close to each other?"
6143
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
   864
10727
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   865
    ^ self closeTo:num withEpsilon:(self class epsilonForCloseTo)
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   866
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   867
    "
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   868
     1 closeTo:1.0000000001 
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   869
     1 closeTo:1.001
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   870
     1 closeTo:1.001 withEpsilon:0.001
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   871
    "
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   872
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   873
    "Created: / 5.11.2001 / 18:07:26 / cg"
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   874
!
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   875
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   876
closeTo:num withEpsilon:eps
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   877
    "are these two numbers close to each other?"
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   878
6374
fad888313ce3 closeTo: fix
Claus Gittinger <cg@exept.de>
parents: 6345
diff changeset
   879
    num isNumber ifFalse:[^false].
10727
4e6346eb48a9 closeTo: with epsilon parameter
sr
parents: 10556
diff changeset
   880
    ^ num closeFrom:self withEpsilon:eps
6143
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
   881
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
   882
    "
14495
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   883
     1 closeTo:1.0000000001        
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   884
     1 closeTo:1.001               
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   885
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   886
     1 closeTo:1.001 withEpsilon:0.1 
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   887
     1 closeTo:1.201 withEpsilon:0.1 
12979
917a726bf375 comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents: 12762
diff changeset
   888
917a726bf375 comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents: 12762
diff changeset
   889
     3.14 closeTo:(3.14 asFixedPoint:2)
917a726bf375 comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents: 12762
diff changeset
   890
     (3.14 asFixedPoint:2) closeTo:3.14
6143
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
   891
    "
6155
00eaf2fa55bc isReal comment
Claus Gittinger <cg@exept.de>
parents: 6154
diff changeset
   892
12979
917a726bf375 comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents: 12762
diff changeset
   893
    "Created: / 05-11-2001 / 18:07:26 / cg"
917a726bf375 comment/format in: #closeTo:withEpsilon:
Claus Gittinger <cg@exept.de>
parents: 12762
diff changeset
   894
    "Modified: / 02-08-2010 / 13:27:22 / cg"
6143
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
   895
!
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
   896
9139
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   897
degreeCos
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   898
    "Return the cosine of the receiver taken as an angle in degrees."
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   899
    
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   900
    ^ self degreesToRadians cos
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   901
!
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   902
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   903
degreeSin
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   904
    "Return the sine of the receiver taken as an angle in degrees."
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   905
    
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   906
    ^ self degreesToRadians sin
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   907
!
94df665b3260 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9138
diff changeset
   908
11712
b83c3734b1ce added #degreeTan
sr
parents: 11660
diff changeset
   909
degreeTan
b83c3734b1ce added #degreeTan
sr
parents: 11660
diff changeset
   910
    "Return the cosine of the receiver taken as an angle in degrees."
b83c3734b1ce added #degreeTan
sr
parents: 11660
diff changeset
   911
    
b83c3734b1ce added #degreeTan
sr
parents: 11660
diff changeset
   912
    ^ self degreesToRadians tan
b83c3734b1ce added #degreeTan
sr
parents: 11660
diff changeset
   913
!
b83c3734b1ce added #degreeTan
sr
parents: 11660
diff changeset
   914
17127
f2611d8d0ce0 class: Number
Claus Gittinger <cg@exept.de>
parents: 17060
diff changeset
   915
isEqual: aNumber within: accuracy 
f2611d8d0ce0 class: Number
Claus Gittinger <cg@exept.de>
parents: 17060
diff changeset
   916
        ^(self - aNumber) abs < accuracy
f2611d8d0ce0 class: Number
Claus Gittinger <cg@exept.de>
parents: 17060
diff changeset
   917
!
f2611d8d0ce0 class: Number
Claus Gittinger <cg@exept.de>
parents: 17060
diff changeset
   918
14495
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   919
rounded:n
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   920
    "Answer the float rounded with n digits of precision"
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   921
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   922
    | mult |
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   923
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   924
    mult := 10 raisedTo: n.
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   925
    ^ (((self * mult) rounded) asFloat / mult).
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   926
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   927
    "
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   928
     7 rounded:2   
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   929
     7.1 rounded:2    
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   930
     7.2345 rounded:2 
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   931
     7.2385 rounded:2 
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   932
     7.2341 rounded:3   
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   933
     7.2345 rounded:3   
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   934
     7.2348 rounded:3   
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   935
    "
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   936
!
d5afe5bc91f7 added: #rounded:
Claus Gittinger <cg@exept.de>
parents: 14009
diff changeset
   937
4988
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   938
stringForReadout
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   939
    ^ self rounded printString
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   940
! !
ac426684e852 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4895
diff changeset
   941
16406
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
   942
9127
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   943
!Number methodsFor:'coercing & converting'!
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   944
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   945
i
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   946
    "return a complex number, with the receiver as imaginary part, 0 as real part"
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   947
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   948
    ^ Complex
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   949
        real:0
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   950
        imaginary:self
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   951
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   952
    "
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   953
     3i          
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   954
     (1+1i)      
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   955
    "
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   956
! !
04cd80c64a30 decimapPointCharacter stuff;
Claus Gittinger <cg@exept.de>
parents: 8979
diff changeset
   957
16656
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   958
!Number methodsFor:'comparing'!
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   959
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   960
isAlmostEqualTo:aNumber nEpsilon:nE 
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   961
    "return true, if the argument, aNumber represents almost the same numeric value
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   962
     as the receiver, false otherwise.
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   963
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   964
     nE is the number of minimal float distances, that the numbers may differ and
17207
75c41a799425 class: Number
Claus Gittinger <cg@exept.de>
parents: 17190
diff changeset
   965
     still be considered equal. See documentation in LimitedPrecisionReal for more detail.
16656
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   966
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   967
     For background information why floats need this 
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   968
     read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   969
    "
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   970
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   971
    |f1 f2 diff scaledEpsilon largest|
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   972
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   973
    diff := (self - aNumber) abs.
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   974
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   975
    scaledEpsilon := nE * diff class epsilon.
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   976
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   977
    diff <= scaledEpsilon ifTrue:[
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   978
        "compare for really close values near 0"
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   979
        ^ true.
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   980
    ] ifFalse:[
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   981
        "scaled comparison for larger values"
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   982
        f1 := self abs.
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   983
        f2 := aNumber abs.
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   984
        largest := f1 > f2 ifTrue:[f1] ifFalse:[f2].
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   985
        ^ (diff <= (scaledEpsilon * largest)).
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   986
    ].
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   987
! !
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
   988
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   989
!Number methodsFor:'converting'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
   990
3634
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   991
% aNumber 
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   992
    "Return a complex number with the receiver as the real part and 
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   993
     aNumber as the imaginary part"
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   994
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   995
    ^ Complex real:self imaginary:aNumber
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   996
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   997
    "Modified: / 9.7.1998 / 10:18:12 / cg"
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   998
!
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
   999
11458
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1000
+/- anError
14009
a2b68a9c9719 comment
Claus Gittinger <cg@exept.de>
parents: 13898
diff changeset
  1001
    "return a MeasurementValue with a given error."
11458
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1002
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1003
    ^ MeasurementValue value:self minValue:(self-anError) maxValue:(self+anError)
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1004
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1005
    "
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1006
     (100 +/- 5) * 2            
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1007
     (100 +/- 5) * (100 +/- 10)  
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1008
     (100 +/- 5) + (100 +/- 10)  
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1009
     (100 +/- 5) - (100 +/- 10)  
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1010
    "
14009
a2b68a9c9719 comment
Claus Gittinger <cg@exept.de>
parents: 13898
diff changeset
  1011
a2b68a9c9719 comment
Claus Gittinger <cg@exept.de>
parents: 13898
diff changeset
  1012
    "Modified (comment): / 14-02-2012 / 14:17:36 / cg"
11458
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1013
!
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1014
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1015
@ aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1016
    "return a Point with the receiver as x-coordinate and the argument
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1017
     as y-coordinate"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1018
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1019
%{  /* NOCONTEXT */
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1020
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1021
    /*
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1022
     * I cannot tell if this special code is worth anything
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1023
     */
5322
411b6c0f7250 renamed sone structures;
Claus Gittinger <cg@exept.de>
parents: 5299
diff changeset
  1024
    if (__CanDoQuickNew(sizeof(struct __Point))) {      /* OBJECT ALLOCATION */
7857
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1025
        OBJ newPoint;
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1026
        int spc;
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1027
7857
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1028
        __qCheckedAlignedNew(newPoint, sizeof(struct __Point));
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1029
        __InstPtr(newPoint)->o_class = @global(Point);
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1030
        __qSTORE(newPoint, @global(Point));
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1031
        __PointInstPtr(newPoint)->p_x = self;
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1032
        __PointInstPtr(newPoint)->p_y = aNumber;
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1033
        if (! __bothSmallInteger(self, aNumber)) {
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1034
            spc = __qSpace(newPoint);
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1035
            __STORE_SPC(newPoint, aNumber, spc);
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1036
            __STORE_SPC(newPoint, self, spc);
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1037
        }
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1038
        RETURN ( newPoint );
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1039
    }
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1040
%}
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1041
.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1042
    ^ Point x:self y:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1043
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1044
3634
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1045
asComplex
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1046
    "Return a complex number with the receiver as the real part and 
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1047
     zero as the imaginary part"
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1048
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  1049
    ^ Complex real:self
3634
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1050
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1051
    "Modified: / 9.7.1998 / 10:18:16 / cg"
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1052
!
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1053
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1054
asMetaNumber
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1055
    ^ SomeNumber new realNumber:self
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1056
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1057
7214
2315f1d48b84 *** empty log message ***
martin
parents: 6899
diff changeset
  1058
asNumber
17060
7b39761eb1ce class: Number
Claus Gittinger <cg@exept.de>
parents: 17058
diff changeset
  1059
    "I am a number, so return myself"
7214
2315f1d48b84 *** empty log message ***
martin
parents: 6899
diff changeset
  1060
2315f1d48b84 *** empty log message ***
martin
parents: 6899
diff changeset
  1061
    ^ self.
2315f1d48b84 *** empty log message ***
martin
parents: 6899
diff changeset
  1062
!
2315f1d48b84 *** empty log message ***
martin
parents: 6899
diff changeset
  1063
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1064
asPoint
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1065
    "return a new Point with the receiver as all coordinates;  
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1066
     often used to supply the same value in two dimensions, as with 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1067
     symmetrical gridding or scaling."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1068
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1069
%{  /* NOCONTEXT */
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1070
5322
411b6c0f7250 renamed sone structures;
Claus Gittinger <cg@exept.de>
parents: 5299
diff changeset
  1071
    if (__CanDoQuickNew(sizeof(struct __Point))) {      /* OBJECT ALLOCATION */
7857
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1072
        OBJ newPoint;
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1073
7857
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1074
        __qCheckedAlignedNew(newPoint, sizeof(struct __Point));
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1075
        __InstPtr(newPoint)->o_class = @global(Point);
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1076
        __qSTORE(newPoint, @global(Point));
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1077
        __PointInstPtr(newPoint)->p_x = self;
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1078
        __PointInstPtr(newPoint)->p_y = self;
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1079
        __STORE(newPoint, self);
51971c337c26 Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents: 7729
diff changeset
  1080
        RETURN ( newPoint );
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1081
    }
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1082
%}.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1083
    ^ Point x:self y:self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1084
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1085
13898
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1086
asTimeDuration
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1087
    "return an TimeDuration object from the receiver, taking the receiver
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1088
     as number of seconds"
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1089
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1090
    ^ TimeDuration seconds:self 
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1091
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1092
    "
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1093
     5 asTimeDuration   
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1094
     50.25 asTimeDuration 
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1095
     3600 asTimeDuration 
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1096
    "
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1097
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1098
    "Created: / 08-01-2012 / 19:04:04 / cg"
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1099
!
42fd67913187 added: #asTimeDuration
Claus Gittinger <cg@exept.de>
parents: 13631
diff changeset
  1100
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1101
degreesToRadians
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1102
    "interpreting the receiver as radians, return the degrees"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1103
8203
666bfa7bcb97 Fix C cmpiler warnings.
Stefan Vogel <sv@exept.de>
parents: 7857
diff changeset
  1104
    ^ self * (Float pi / 180.0)
7388
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1105
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1106
    "
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1107
     180 degreesToRadians
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1108
     Float pi radiansToDegrees
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1109
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1110
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1111
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1112
literalArrayEncoding
1245
c8afea3d5af0 commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
  1113
    "encode myself as an array literal, from which a copy of the receiver
c8afea3d5af0 commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
  1114
     can be reconstructed with #decodeAsLiteralArray."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1115
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1116
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1117
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1118
    "Modified: 1.9.1995 / 02:25:26 / claus"
1245
c8afea3d5af0 commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
  1119
    "Modified: 22.4.1996 / 13:00:27 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1120
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1121
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1122
radiansToDegrees
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1123
    "interpreting the receiver as degrees, return the radians"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1124
8203
666bfa7bcb97 Fix C cmpiler warnings.
Stefan Vogel <sv@exept.de>
parents: 7857
diff changeset
  1125
    ^ self * (180.0 / Float pi)
7388
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1126
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1127
    "
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1128
     180 degreesToRadians     
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1129
     Float pi radiansToDegrees
bb89a53e2682 fixed conversion from string to not loose precision which is later
Claus Gittinger <cg@exept.de>
parents: 7356
diff changeset
  1130
    "
6637
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1131
!
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1132
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1133
withScale:newScale
8203
666bfa7bcb97 Fix C cmpiler warnings.
Stefan Vogel <sv@exept.de>
parents: 7857
diff changeset
  1134
    "return a fixedPoint number representing the same value as the receiver, 
6637
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1135
     with newScale number of post-decimal digits"
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1136
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1137
    ^ self asFixedPoint:newScale
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1138
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1139
    "
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1140
     1234 withScale:2 
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1141
     1234.1 withScale:2 
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1142
     1234.12 withScale:2 
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1143
     1234.123 withScale:2 
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1144
     (1/7) withScale:2
b6ee031f3096 withScale moved up.
Claus Gittinger <cg@exept.de>
parents: 6632
diff changeset
  1145
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1146
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1147
11008
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1148
!Number methodsFor:'converting-times'!
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1149
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1150
days
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1151
    "return a TimeDuration representing this number of days"
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1152
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1153
    ^ TimeDuration days:self
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1154
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1155
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1156
     1000 milliseconds 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1157
     10 seconds 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1158
     10 minutes 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1159
     1 days 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1160
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1161
!
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1162
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1163
hours
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1164
    "return a TimeDuration representing this number of hours"
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1165
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1166
    ^ TimeDuration hours:self
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1167
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1168
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1169
     1000 milliseconds 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1170
     10 seconds 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1171
     10 minutes 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1172
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1173
!
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1174
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1175
milliseconds
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1176
    "return a TimeDuration representing this number of milliseconds"
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1177
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1178
    ^ TimeDuration fromMilliseconds:self
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1179
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1180
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1181
     1000 milliseconds 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1182
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1183
!
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1184
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1185
minutes
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1186
    "return a TimeDuration representing this number of minutes"
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1187
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1188
    ^ TimeDuration minutes:self
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1189
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1190
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1191
     1000 milliseconds 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1192
     10 seconds 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1193
     10 minutes 
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1194
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1195
!
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1196
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1197
seconds
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1198
    "return a TimeDuration representing this number of seconds"
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1199
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1200
    ^ TimeDuration seconds:self
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1201
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1202
    "
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1203
     1000 milliseconds 
11770
37bd8fc58d59 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11712
diff changeset
  1204
     10 seconds      
37bd8fc58d59 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11712
diff changeset
  1205
     10 minutes      
11008
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1206
    "
13631
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1207
!
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1208
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1209
weeks
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1210
    "return a TimeDuration representing this number of weeks"
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1211
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1212
    ^ TimeDuration weeks:self
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1213
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1214
    "
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1215
     1000 milliseconds 
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1216
     10 seconds 
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1217
     10 minutes 
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1218
     1 days 
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1219
     1 weeks 
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1220
    "
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1221
82875f651ca6 added: #weeks
Claus Gittinger <cg@exept.de>
parents: 12979
diff changeset
  1222
    "Created: / 05-09-2011 / 11:17:59 / cg"
11008
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1223
! !
5a92714a522b timeDuration conversions added
Michael Beyl <mb@exept.de>
parents: 10727
diff changeset
  1224
16406
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1225
!Number methodsFor:'double dispatching'!
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1226
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1227
differenceFromTimestamp:aTimestamp
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1228
    "I am to be interpreted as seconds, return the timestamp this number of seconds
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1229
     before aTimestamp"
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1230
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1231
    ^ aTimestamp subtractMilliseconds:(self * 1000) truncated.
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1232
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1233
    "
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1234
     100.0 differenceFromTimestamp:Timestamp now 
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1235
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1236
     |t1 t2|
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1237
     t1 := Timestamp now. 
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1238
     t2 := 1.5 differenceFromTimestamp:t1.
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1239
     t1 inspect. t2 inspect.
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1240
    "
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1241
! !
967b6c76169a class: Number
Stefan Vogel <sv@exept.de>
parents: 16267
diff changeset
  1242
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1243
!Number methodsFor:'intervals'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1244
6072
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1245
downTo:stop
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1246
    "return an interval from receiver down to the argument, incrementing by -1"
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1247
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1248
    ^ self to:stop by:-1
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1249
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1250
    "
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1251
     (10 downTo:1) do:[:i | Transcript showCR:i].
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1252
    "
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1253
!
55858717097a added #downTo:
Claus Gittinger <cg@exept.de>
parents: 5565
diff changeset
  1254
15595
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1255
downTo:stop by:step
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1256
    "return an interval from receiver down to the argument, decrementing by step"
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1257
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1258
    ^ self to:stop by:step negated
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1259
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1260
    "
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1261
     (10 downTo:1 by:0.5) do:[:i | Transcript showCR:i].
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1262
    "
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1263
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1264
    "Created: / 01-08-2013 / 14:36:56 / cg"
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1265
!
fa5010435cda class: Number
Claus Gittinger <cg@exept.de>
parents: 15197
diff changeset
  1266
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1267
to:stop
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1268
    "return an interval from receiver up to the argument, incrementing by 1"
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
    ^ Interval from:self to:stop
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1271
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1272
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1273
to:stop by:step
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1274
    "return an interval from receiver up to the argument, incrementing by step"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1275
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1276
    ^ Interval from:self to:stop by:step
6154
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1277
!
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1278
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1279
to:stop byFactor:factor
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1280
    "return a geometric series from receiver up to the argument;
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1281
     elements have a constant factor in between"
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1282
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1283
    ^ GeometricSeries from:self to:stop byFactor:factor
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1284
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1285
    "
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1286
     (1 to:256 byFactor:2)
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1287
     (256 to:1 byFactor:1/2)     
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  1288
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1289
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1290
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1291
!Number methodsFor:'iteration'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1292
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1293
timesRepeat:aBlock
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1294
    "evaluate the argument, aBlock self times"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1295
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1296
    |count|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1297
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1298
    count := self.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1299
    [count > 0] whileTrue:[
15108
ffef3e5a13d5 class: Number
Claus Gittinger <cg@exept.de>
parents: 15100
diff changeset
  1300
        aBlock value.
ffef3e5a13d5 class: Number
Claus Gittinger <cg@exept.de>
parents: 15100
diff changeset
  1301
        count := count - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1302
    ]
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1303
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1304
3634
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1305
!Number methodsFor:'mathematical functions'!
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1306
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1307
cbrt
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1308
    "return the cubic root of the receiver"
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1309
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1310
    (self isLimitedPrecisionReal not
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1311
    or:[self generality < 1.0 generality]) ifTrue:[
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1312
        ^ self asFloat cbrt.
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1313
    ].
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1314
    ^ self cbrt_withAccuracy:self epsilon
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1315
!
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1316
3634
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1317
conjugated
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1318
    "Return the complex conjugate of this Number."
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1319
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1320
    ^ self
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1321
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1322
    "Modified: / 9.7.1998 / 10:17:31 / cg"
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1323
!
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1324
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1325
exp
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1326
    "compute e**x of the receiver"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1327
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1328
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1329
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1330
        ^ self asFloat exp.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1331
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1332
    ^ self exp_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1333
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1334
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1335
floorLog:radix
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1336
    "return the logarithm truncated as an integer"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1337
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1338
    ^ (self log:radix) floor
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1339
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1340
3634
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1341
imaginary
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1342
    "Return the imaginary part of this Number."
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1343
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1344
    ^ 0
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1345
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1346
    "Modified: / 9.7.1998 / 10:17:24 / cg"
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1347
!
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1348
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1349
ln
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1350
    "compute ln of the receiver"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1351
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1352
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1353
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1354
        ^ self asFloat ln.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1355
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1356
    ^ self ln_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1357
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1358
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1359
log
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1360
    "return log base 10 of the receiver.
12584
d32f98e9b219 comment/format in: #log
Claus Gittinger <cg@exept.de>
parents: 12112
diff changeset
  1361
     Alias for log:10."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1362
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1363
    ^ self log10
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1364
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1365
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1366
log10
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1367
    "return log base 10 of the receiver"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1368
10550
0f2a07595d1c Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents: 10325
diff changeset
  1369
    (self isLimitedPrecisionReal not
0f2a07595d1c Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents: 10325
diff changeset
  1370
    or:[self generality < 1.0 generality]) ifTrue:[
0f2a07595d1c Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents: 10325
diff changeset
  1371
        ^ self asFloat log10.
0f2a07595d1c Fix bug 641: changed #log10 to be more accurate
Stefan Vogel <sv@exept.de>
parents: 10325
diff changeset
  1372
    ].
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1373
    ^ self log:10
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1374
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1375
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1376
log:aNumber
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1377
    "return log base aNumber of the receiver"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1378
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1379
    ^ self ln / aNumber ln
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1380
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1381
    "
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1382
      1000 log:10
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1383
      (1000 log:10) floor
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1384
    "
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1385
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1386
7468
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1387
raisedTo:aNumber
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1388
    "return the receiver raised to aNumber"
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1389
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1390
    aNumber = 0 ifTrue:[^ 1].
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1391
    aNumber = 1 ifTrue:[^ self].
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1392
    aNumber isInteger ifTrue:[
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1393
        ^ self raisedToInteger:aNumber
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1394
    ].
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1395
    ^ self asFloat raisedTo:aNumber
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1396
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1397
    "
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1398
     2 raisedTo: 4    
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1399
     10 raisedTo: 4    
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1400
    "
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1401
!
61c8a3053bf4 moved math functions to number
Claus Gittinger <cg@exept.de>
parents: 7456
diff changeset
  1402
3634
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1403
real
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1404
    "Return the real part of this Number."
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1405
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1406
    ^ self
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1407
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1408
    "Modified: / 9.7.1998 / 10:17:17 / cg"
6258
435600a44e73 added timesTwoPower.
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1409
!
435600a44e73 added timesTwoPower.
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1410
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1411
sqrt
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1412
    "return the square root of the receiver"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1413
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1414
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1415
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1416
        ^ self asFloat sqrt.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1417
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1418
    ^ self sqrt_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1419
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1420
10191
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1421
sqrtWithErrorLessThan:epsilon
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1422
    "compute the square root, using the Newton method.
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1423
     The approximated return value has an error less than the given epsilon."
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1424
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1425
    |y yN|
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1426
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1427
    yN := self / 2.
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1428
    [
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1429
       y := yN.
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1430
       yN := ( y + (self / y) ) / 2.
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1431
    ] doUntil:[ (yN - y) abs < epsilon ].
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1432
    ^ yN.
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1433
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1434
    "
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1435
     (2 asFixedPoint:4) sqrtWithErrorLessThan:0.001   
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1436
    "
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1437
!
4974027704a5 sqrtWithErrorLessThan:
Claus Gittinger <cg@exept.de>
parents: 9333
diff changeset
  1438
6258
435600a44e73 added timesTwoPower.
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1439
timesTwoPower:anInteger
12762
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1440
    "Return the receiver multiplied by 2 raised to the power of the argument.
6258
435600a44e73 added timesTwoPower.
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1441
     For protocol completeness wrt. Squeak and ST80."
435600a44e73 added timesTwoPower.
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1442
12762
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1443
    anInteger >= 0 ifTrue:[
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1444
        ^ self * (1 bitShift:anInteger)
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1445
    ].
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1446
    ^ self / (1 bitShift:anInteger negated)
6258
435600a44e73 added timesTwoPower.
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1447
435600a44e73 added timesTwoPower.
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1448
    "
12762
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1449
     123 timesTwoPower:0   -> 123
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1450
     123 timesTwoPower:1   -> 246
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1451
     123 timesTwoPower:2   -> 492
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1452
     123 timesTwoPower:3   -> 984
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1453
a41a496d8c61 changed: #timesTwoPower:
Claus Gittinger <cg@exept.de>
parents: 12759
diff changeset
  1454
     (2 timesTwoPower: -150) timesTwoPower: 150  -> 2
6258
435600a44e73 added timesTwoPower.
Claus Gittinger <cg@exept.de>
parents: 6195
diff changeset
  1455
    "
3634
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1456
! !
bbf443dce0cf complex conversion stuff
Claus Gittinger <cg@exept.de>
parents: 3613
diff changeset
  1457
11458
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1458
!Number methodsFor:'measurement values'!
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1459
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1460
maxValue
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1461
    "the maximum possible value taking me as a measurement with possible error; 
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1462
     as I am exact, thats myself"
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1463
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1464
    ^ self
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1465
!
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1466
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1467
minValue
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1468
    "the minimum possible value taking me as a measurement with possible error; 
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1469
     as I am exact, thats myself"
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1470
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1471
    ^ self
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1472
! !
d668f83e44d1 measurement support
Claus Gittinger <cg@exept.de>
parents: 11224
diff changeset
  1473
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1474
!Number methodsFor:'printing & storing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1475
5299
92b363dbd833 added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1476
printOn:aStream paddedWith:padCharacter to:size base:radix
92b363dbd833 added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1477
    |s|
92b363dbd833 added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1478
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
  1479
    radix == 10 ifTrue:[
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
  1480
        s := self printString.
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
  1481
    ] ifFalse:[
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
  1482
        s := self printStringRadix:radix.
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
  1483
    ].
5299
92b363dbd833 added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1484
    s printOn: aStream leftPaddedTo:size with: padCharacter
11098
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1485
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1486
    "
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1487
     100 printOn:Transcript paddedWith:$0 to:10 base:10.     Transcript cr.
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1488
     100 printOn:Transcript paddedWith:$0 to:10 base:16.     Transcript cr.
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1489
     100 printOn:Transcript paddedWith:(Character space) to:10 base:16.     Transcript cr.
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1490
     100 printOn:Transcript paddedWith:(Character space) to:10 base:2.     Transcript cr.
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1491
    "
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
  1492
!
5299
92b363dbd833 added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1493
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1494
printOn:aStream thousandsSeparator:thousandsSeparator
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1495
    "print the receiver as business number with thousands separator to aStream.
11136
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1496
     thousandsSeparator is locale specific and is usualy a single quote ('), a comma or period."
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1497
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1498
    |rest|
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1499
11594
5440d97af571 #printStringWithThousandsSeparator fix for non-integers
Stefan Vogel <sv@exept.de>
parents: 11526
diff changeset
  1500
    self >= 1000 ifTrue:[
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1501
        (self // 1000) printOn:aStream thousandsSeparator:thousandsSeparator.
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1502
        thousandsSeparator printOn:aStream.
11136
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1503
        rest := self \\ 1000.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1504
        rest < 100 ifTrue:[
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1505
            aStream nextPut:$0.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1506
            rest < 10 ifTrue:[
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1507
                aStream nextPut:$0.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1508
            ].
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1509
        ].
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1510
        rest printOn:aStream.
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1511
        ^ self.
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1512
    ].
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1513
    self printOn:aStream.
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1514
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1515
    "
11097
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1516
     swiss style:
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1517
     1000000 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1518
     12345678 printOn:Transcript thousandsSeparator:$'.     Transcript cr.  
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1519
     1234567 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1520
     123456 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
11136
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1521
     123056 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
11097
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1522
     12345 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1523
     1234 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1524
     123 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1525
11136
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1526
     (12345678.12 asFixedPoint:2) printOn:Transcript thousandsSeparator:$'.     Transcript cr.  
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1527
     1234567.12 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1528
     123456.12 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1529
     123056.12 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1530
     12345.12 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1531
     1234.12 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1532
     123.12 printOn:Transcript thousandsSeparator:$'.     Transcript cr.
ac25ee31e086 fix printOn:thousandsSeparator when there is a 0 behind a separator
Stefan Vogel <sv@exept.de>
parents: 11098
diff changeset
  1533
11097
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1534
     us style:
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1535
     1000000 printOn:Transcript thousandsSeparator:$,.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1536
     12345678 printOn:Transcript thousandsSeparator:$,.     Transcript cr.  
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1537
     1234567 printOn:Transcript thousandsSeparator:$,.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1538
     123456 printOn:Transcript thousandsSeparator:$,.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1539
     12345 printOn:Transcript thousandsSeparator:$,.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1540
     1234 printOn:Transcript thousandsSeparator:$,.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1541
     123 printOn:Transcript thousandsSeparator:$,.     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1542
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1543
     german (european ?) style
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1544
     1000000 printOn:Transcript thousandsSeparator:$..     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1545
     12345678 printOn:Transcript thousandsSeparator:$..     Transcript cr.  
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1546
     1234567 printOn:Transcript thousandsSeparator:$..     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1547
     123456 printOn:Transcript thousandsSeparator:$..     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1548
     12345 printOn:Transcript thousandsSeparator:$..     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1549
     1234 printOn:Transcript thousandsSeparator:$..     Transcript cr.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1550
     123 printOn:Transcript thousandsSeparator:$..     Transcript cr.
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1551
    "
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1552
!
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1553
11660
62a726d91255 new: #printStringFormat:
Stefan Vogel <sv@exept.de>
parents: 11594
diff changeset
  1554
printStringFormat:formatString
62a726d91255 new: #printStringFormat:
Stefan Vogel <sv@exept.de>
parents: 11594
diff changeset
  1555
    ^ self printfPrintString:formatString
62a726d91255 new: #printStringFormat:
Stefan Vogel <sv@exept.de>
parents: 11594
diff changeset
  1556
!
62a726d91255 new: #printStringFormat:
Stefan Vogel <sv@exept.de>
parents: 11594
diff changeset
  1557
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
  1558
printStringRadix:radix
11098
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1559
    "only implemented for integers"
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1560
8301
74d9ee379019 decimalPointCharacter definition
werner
parents: 8203
diff changeset
  1561
    self subclassResponsibility
11098
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1562
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1563
    "
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1564
     100 printStringRadix:10.     
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1565
     100 printStringRadix:16.     
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1566
     100 printStringRadix:2.     
bfc609a33e11 comments
Claus Gittinger <cg@exept.de>
parents: 11097
diff changeset
  1567
    "
5299
92b363dbd833 added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1568
!
92b363dbd833 added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents: 5238
diff changeset
  1569
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1570
printStringWithThousandsSeparator
11097
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1571
    "print the receiver as swiss business number with thousands separator to aStream.
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1572
     Caveat: Should use the separator from the locale here"
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1573
11097
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1574
    ^ self printStringWithThousandsSeparator:$'.
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1575
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1576
    "
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1577
     1000000 printStringWithThousandsSeparator
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1578
     12345678 printStringWithThousandsSeparator
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1579
     1234567 printStringWithThousandsSeparator
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1580
     123456 printStringWithThousandsSeparator
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1581
     12345 printStringWithThousandsSeparator
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1582
     1234 printStringWithThousandsSeparator
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1583
     123 printStringWithThousandsSeparator
11097
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1584
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1585
     1000000 asFixedPoint printStringWithThousandsSeparator
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1586
     12345678 asFixedPoint printStringWithThousandsSeparator
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1587
     1234567 asFixedPoint printStringWithThousandsSeparator  
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1588
     123456 asFixedPoint printStringWithThousandsSeparator
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1589
     12345 asFixedPoint printStringWithThousandsSeparator 
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1590
     1234 asFixedPoint printStringWithThousandsSeparator 
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1591
     123 asFixedPoint printStringWithThousandsSeparator
11594
5440d97af571 #printStringWithThousandsSeparator fix for non-integers
Stefan Vogel <sv@exept.de>
parents: 11526
diff changeset
  1592
     ((9999999//10000) asFixedPoint:9) printStringWithThousandsSeparator 
11097
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1593
    "
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1594
!
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1595
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1596
printStringWithThousandsSeparator:thousandsSeparator
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1597
    "print the receiver as business number with a thousands separator to aStream.
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1598
     Notice:
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1599
        americans use comma
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1600
        germans (europeans ?) use a dot
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1601
        swiss people (business people ?) use a single quote
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1602
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1603
     Caveat: Should use the separator from the locale here"
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1604
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1605
    ^ String streamContents:[:s | self printOn:s thousandsSeparator:thousandsSeparator].
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1606
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1607
    "
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1608
     Transcript showCR:(1000000 printStringWithThousandsSeparator:$').
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1609
     Transcript showCR:(12345678 printStringWithThousandsSeparator:$').
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1610
     Transcript showCR:(1234567 printStringWithThousandsSeparator:$').
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1611
     Transcript showCR:(123456 printStringWithThousandsSeparator:$').
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1612
     Transcript showCR:(12345 printStringWithThousandsSeparator:$').
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1613
     Transcript showCR:(1234 printStringWithThousandsSeparator:$').
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1614
     Transcript showCR:(123 printStringWithThousandsSeparator:$').
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1615
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1616
     Transcript showCR:(1000000 printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1617
     Transcript showCR:(12345678 printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1618
     Transcript showCR:(1234567 printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1619
     Transcript showCR:(123456 printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1620
     Transcript showCR:(12345 printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1621
     Transcript showCR:(1234 printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1622
     Transcript showCR:(123 printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1623
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1624
     Transcript showCR:((1000000 asFixedPoint:2) printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1625
     Transcript showCR:((12345678 asFixedPoint:2) printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1626
     Transcript showCR:((1234567 asFixedPoint:2) printStringWithThousandsSeparator:$,).  
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1627
     Transcript showCR:((123456 asFixedPoint:2) printStringWithThousandsSeparator:$,).
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1628
     Transcript showCR:((12345 asFixedPoint:2) printStringWithThousandsSeparator:$,). 
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1629
     Transcript showCR:((1234 asFixedPoint:2) printStringWithThousandsSeparator:$,). 
5e4961639e41 printStringWithThousandsSep comments and refactored
Claus Gittinger <cg@exept.de>
parents: 11096
diff changeset
  1630
     Transcript showCR:((123 asFixedPoint:2) printStringWithThousandsSeparator:$,).
11096
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1631
    "
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1632
!
19d6c993e67f printStringWithThousandsSep moved up (also valid for FixedPoint Decimals)
Claus Gittinger <cg@exept.de>
parents: 11008
diff changeset
  1633
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1634
storeOn:aStream
11460
146fb82b5523 comment
Claus Gittinger <cg@exept.de>
parents: 11458
diff changeset
  1635
    "append a string for storing the receiver onto the argument, aStream
146fb82b5523 comment
Claus Gittinger <cg@exept.de>
parents: 11458
diff changeset
  1636
     - since numbers are literals,they store as they print."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1637
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1638
    ^ self printOn:aStream
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1639
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1640
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1641
storeString
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1642
    "return a string for storing 
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1643
     - since numbers are literals, they store as they print."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1644
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1645
    ^ self printString
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1646
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  1647
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1648
!Number methodsFor:'taylor series'!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1649
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1650
arcSin_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1651
    "compute the arcSine of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1652
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1653
    "/ uses taylor series:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1654
    "/                 1*x^3   1*3 * x^5   
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1655
    "/    arcSin = x + ----- + ---------- + ...
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1656
    "/                 2* 3    2*4 *  5    
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1657
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1658
    |x2 num numf den denf approx delta|
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1659
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1660
    ((self < -1) or:[self > 1]) ifTrue:[ 
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1661
        ^ self class
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1662
            raise:#domainErrorSignal
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1663
            receiver:self
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1664
            selector:#arcSin
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1665
            arguments:#()
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1666
            errorString:'bad receiver in arcSin'
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1667
    ].
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1668
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1669
    x2 := self * self.
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1670
    num := x2 * self.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1671
    approx := self + (num / 6).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1672
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1673
    numf := 3.
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1674
    denf := 4.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1675
    den := 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1676
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1677
    [
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1678
        num := (num * x2) * numf.   numf := numf + 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1679
        den := den * denf.          denf := denf + 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1680
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1681
        delta := num / (den * numf).
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1682
        approx := approx + delta.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1683
    ] doUntil:[delta abs <= epsilon].
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1684
    ^ approx
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1685
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1686
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1687
     0.5 arcSin                                    0.523599
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1688
     0.5q arcSin                        0.523598776
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1689
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1690
     0.5q arcSin_withAccuracy:1         0.520833333
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1691
     0.5q arcSin_withAccuracy:0.1       0.520833333
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1692
     0.5q arcSin_withAccuracy:0.01      0.523177083
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1693
     0.5q arcSin_withAccuracy:0.001     0.523525856
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1694
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1695
     0.5q arcSin_withAccuracy:1e-20     0.523598776
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1696
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1697
     0.5 asLargeFloat arcSin_withAccuracy:1e-30    -- not yet 
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1698
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1699
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1700
     0.1 arcSin                                    0.100167
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1701
     0.1q arcSin                        0.100167421
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1702
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1703
     0.1q arcSin_withAccuracy:1         0.100166667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1704
     0.1q arcSin_withAccuracy:0.1       0.100166667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1705
     0.1q arcSin_withAccuracy:0.01      0.100166667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1706
     0.1q arcSin_withAccuracy:0.001     0.100166667
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1707
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1708
     0.1q arcSin_withAccuracy:1e-20     0.100167421
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1709
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1710
     0.1 asLargeFloat arcSin_withAccuracy:1e-30    -- not yet 
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1711
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1712
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1713
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1714
arcTan_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1715
    "compute the arcTangent of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1716
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1717
    "/ uses taylor series:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1718
    "/                 x^3   x^5   x^7
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1719
    "/ arcTan(x) = x - --- + --- - --- ...
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1720
    "/                  3     5    7
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1721
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1722
    |x2 num den approx delta|
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1723
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1724
    x2 := self * self.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1725
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1726
    num := (x2 * self) negated.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1727
    den := 3.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1728
    approx := self + (num / den).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1729
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1730
    [
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1731
        den := den + 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1732
        num := (num * x2) negated.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1733
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1734
        delta := num / den.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1735
        approx := approx + delta.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1736
    ] doUntil:[delta abs <= epsilon].
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1737
    ^ approx
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1738
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1739
    "
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1740
     1.0 arcTan                       0.785398
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1741
     1q arcTan                        0.785398163
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1742
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1743
     1q arcTan_withAccuracy:1         0.666666667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1744
     1q arcTan_withAccuracy:0.1       0.744011544
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1745
     1q arcTan_withAccuracy:0.01      0.790299653
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1746
     1q arcTan_withAccuracy:0.001     0.785897165
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1747
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1748
     1q arcTan_withAccuracy:1e-8      0.785398168     
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1749
     1q arcTan_withAccuracy:1e-20     -- not yet, converges very slow 
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1750
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1751
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1752
     0.5 arcTan                         0.463648
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1753
     0.5q arcTan                        0.463647609
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1754
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1755
     0.5q arcTan_withAccuracy:1         0.458333333
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1756
     0.5q arcTan_withAccuracy:0.1       0.458333333
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1757
     0.5q arcTan_withAccuracy:0.01      0.464583333
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1758
     0.5q arcTan_withAccuracy:0.001     0.463684276
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1759
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1760
     0.5q arcTan_withAccuracy:1e-20     0.463647609
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1761
     0.5 asLargeFloat arcTan_withAccuracy:1e-30    -- not yet  
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1762
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1763
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1764
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1765
cbrt_withAccuracy:epsilon 
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1766
    "compute cubic root of the receiver using a newton approx."
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1767
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1768
    "
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1769
      Use Newton's method:
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1770
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1771
                 2*x_n + (a / x_n^2)
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1772
        x_n+1 =  ---------------
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1773
                      3
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1774
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1775
        cbrt(a) = x_n
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1776
    "
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1777
    
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1778
    |approx|
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1779
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1780
    self = 0 ifTrue:[
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1781
        ^ self
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1782
    ].
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1783
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1784
    approx := 1.
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1785
    [
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1786
        |lastApprox|
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1787
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1788
        lastApprox := approx.
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1789
        approx := ((approx * 2) + (self / approx / approx)) / 3.
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1790
        (approx - lastApprox) abs > epsilon
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1791
    ] whileTrue.
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1792
    ^ approx
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1793
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1794
    "
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1795
     8q cbrt                                 2.0
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1796
     8q cbrt_withAccuracy:0.01               2.000004911675504018
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1797
     8q cbrt_withAccuracy:0.0001             2.000000000012062239
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1798
     8q cbrt_withAccuracy:0.0000001          2.0 
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1799
     8q cbrt_withAccuracy:0.0000000001       2.0
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1800
     8q cbrt_withAccuracy:0.000000000001     2.0
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1801
     8q cbrt_withAccuracy:LongFloat epsilon  2.0
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1802
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1803
     27q cbrt_withAccuracy:0.01              3.000000541064176501
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1804
     27q cbrt_withAccuracy:LongFloat epsilon  3.0
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1805
     -27q cbrt_withAccuracy:LongFloat epsilon -3.0
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1806
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1807
     MessageTally spyOn:[ |arg|
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1808
        arg := 2 asLongFloat.
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1809
        1000000 timesRepeat:[
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1810
             arg cbrt_withAccuracy:0.000000000001
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1811
        ]
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1812
     ]
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1813
     Time millisecondsToRun:[ |arg|
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1814
        arg := 2 asLongFloat.
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1815
        1000000 timesRepeat:[
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1816
             arg cbrt_withAccuracy:0.000000000001
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1817
        ]
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1818
     ]
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1819
    "
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1820
!
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1821
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1822
cos_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1823
    "compute the cosine of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1824
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1825
    "/ uses taylor series:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1826
    "/               x^2   x^4   x^6
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1827
    "/  cos(x) = 1 - --- + --- - --- ...
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1828
    "/                2!!    4!!    6!!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1829
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1830
    |x2 facN num den approx lastApprox|
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1831
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1832
    x2 := self * self.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1833
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1834
    num := x2 negated.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1835
    den := 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1836
    facN := 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1837
    approx := 1 + (num / den).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1838
    lastApprox := 1.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1839
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1840
    [ (lastApprox - approx) abs > epsilon ] whileTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1841
        facN := facN + 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1842
        den := den * (facN - 1) * facN.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1843
        num := (num * x2) negated.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1844
        lastApprox := approx.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1845
        approx := approx + (num / den).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1846
    ].
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1847
    ^ approx
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1848
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1849
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1850
     1.0 cos                                    0.540302
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1851
     1.0 asLongFloat cos_withAccuracy:1         0.5
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1852
     1.0 asLongFloat cos_withAccuracy:0.1       0.541666667 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1853
     1.0 asLongFloat cos_withAccuracy:0.01      0.540277778
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1854
     1.0 asLongFloat cos_withAccuracy:0.001     0.540302579
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1855
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1856
     1.0 asLongFloat cos_withAccuracy:1e-40     0.540302306    
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1857
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1858
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1859
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1860
cosh_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1861
    "compute the hyperbolic cosine of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1862
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1863
    "/ uses taylor series:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1864
    "/               x^2   x^4   x^6
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1865
    "/    cosh = x + --- + --- + --- ...
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1866
    "/                2!!    4!!    6!!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1867
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1868
    |x2 facN num den approx delta|
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1869
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1870
    x2 := self * self.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1871
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1872
    num := x2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1873
    den := 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1874
    facN := 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1875
    approx := self + (num / den).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1876
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1877
    [
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1878
        facN := facN + 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1879
        den := den * (facN - 1) * facN.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1880
        num := num * x2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1881
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1882
        delta := num / den.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1883
        approx := approx + delta.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1884
    ] doUntil:[delta <= epsilon].
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1885
    ^ approx
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1886
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1887
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1888
     1.0 cosh                                    1.54308
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1889
     1.0q cosh_withAccuracy:1         1.5 
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1890
     1.0q cosh_withAccuracy:0.1       1.54308 
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1891
     1.0q cosh_withAccuracy:0.01      1.54308 
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1892
     1.0q cosh_withAccuracy:0.001     1.54308 
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1893
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1894
     1.0q cosh_withAccuracy:1e-40   -> 1.543080    
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1895
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1896
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1897
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1898
epsilon
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1899
    "return the maximum relative spacing of instances of mySelf 
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1900
     (i.e. the value-delta of the least significant bit)"
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1901
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1902
    ^ self class epsilon
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1903
!
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  1904
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1905
exp_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1906
    "compute e**x of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1907
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1908
    "/ uses taylor series:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1909
    "/             x    x^2   x^3
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1910
    "/  e^x = 1 + --- + --- + --- ...
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1911
    "/             1!!    2!!    3!!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1912
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1913
    |x2 facN num den approx delta|
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1914
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  1915
    x2 := self * self.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1916
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1917
    num := x2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1918
    den := 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1919
    facN := 2.
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1920
    approx := self + 1 + (num / den).
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1921
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1922
    [
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1923
        facN := facN + 1.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1924
        den := den * facN.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1925
        num := num * self.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1926
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1927
        delta := num / den.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1928
        approx := approx + delta.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1929
    ] doUntil:[delta abs <= epsilon].
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1930
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1931
    ^ approx
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1932
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1933
    "
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1934
     -1 exp
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1935
     1.0 exp                                    2.71828
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1936
     1q exp                                     2.71828183
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1937
     2q exp                                     7.3890561                        
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1938
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1939
     1q exp_withAccuracy:1                      2.66666667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1940
     1q exp_withAccuracy:0.1                    2.70833333
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1941
     1q exp_withAccuracy:0.01                   2.71666667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1942
     1q exp_withAccuracy:0.001                  2.71825397
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1943
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1944
     2q exp_withAccuracy:LongFloat epsilon      7.3890561
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1945
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1946
     1.0 asLongFloat exp_withAccuracy:1e-40     2.71828183 
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1947
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1948
     5 exp_withAccuracy:1e-40      
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1949
     (1 exp_withAccuracy:1e-100) asFixedPoint:100
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1950
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1951
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1952
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1953
ln_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  1954
    "compute ln of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1955
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1956
    "uses taylor series:
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1957
                 u^2   u^3
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1958
        ln = u - --- + --- ...
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1959
                  2    3
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1960
      where:
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1961
             u = x - 1    and: x < 1
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1962
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1963
     Now we use modified taylor, which converges faster:
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1964
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1965
                   1+y        1   1       1
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1966
        ln(x) = ln --- = 2y ( - + - y^2 + - y^4 + ....)
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1967
                   1-y        1   3       5
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1968
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1969
        where y = (x-1) / (x+1)  and x > 0
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1970
    "
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1971
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1972
    |denominator approx y y2 exp delta|
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1973
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1974
    self <= 0 ifTrue:[
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1975
        ^ self class
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1976
            raise:#domainErrorSignal
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1977
            receiver:self
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1978
            selector:#ln
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1979
            arguments:#()
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1980
            errorString:'bad receiver in ln'
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1981
    ].
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1982
        
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1983
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1984
    y := (self - 1)/(self + 1).
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1985
    exp := y2 := y * y.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1986
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1987
    approx := 1.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1988
    denominator := 3.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1989
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1990
    [
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1991
        delta := exp / denominator.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1992
        approx := approx + delta.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1993
        exp := exp * y2.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1994
        denominator := denominator + 2.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1995
    ] doUntil:[delta <= epsilon].
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1996
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1997
    ^ y * 2 * approx.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  1998
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  1999
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2000
    "
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2001
     2.0 ln                         0.693147
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2002
     2.0q ln                        0.693147181
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2003
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2004
     2.0q ln_withAccuracy:1         0.691358025     
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2005
     2.0q ln_withAccuracy:0.1       0.691358025       
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2006
     2.0q ln_withAccuracy:0.01      0.693004115    
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2007
     2.0q ln_withAccuracy:0.0000001 0.69314718    
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2008
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2009
     2.0q ln_withAccuracy:1e-10     
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2010
     2.0q ln_withAccuracy:1e-20     
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2011
     2.0q ln_withAccuracy:1e-40     0.693147181
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2012
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2013
     2 ln_withAccuracy:1e-40                   
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2014
     0 ln_withAccuracy:1e-40                   
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2015
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2016
     (2 ln_withAccuracy:1e-100) asFixedPoint:100
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2017
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2018
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2019
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2020
sin_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2021
    "compute the sine of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2022
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2023
    "/ uses taylor series:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2024
    "/               x^3   x^5   x^7
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2025
    "/  sin(x) = x - --- + --- - --- ...
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2026
    "/                3!!    5!!    7!!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2027
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2028
    |x2 facN num den approx delta|
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2029
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2030
    x2 := self * self.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2031
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2032
    num := (x2 * self) negated.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2033
    den := 2*3.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2034
    facN := 3.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2035
    approx := self + (num / den).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2036
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2037
    [
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2038
        facN := facN + 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2039
        den := den * (facN - 1) * facN.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2040
        num := (num * x2) negated.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2041
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2042
        delta := num / den.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2043
        approx := approx + delta.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2044
    ] doUntil:[delta abs <= epsilon].
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2045
    ^ approx
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2046
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2047
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2048
     1.0 sin                                    0.841471
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2049
     1.0q sin                        0.841470985
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2050
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2051
     1.0q sin_withAccuracy:1         0.833333333
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2052
     1.0q sin_withAccuracy:0.1       0.841666667 
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2053
     1.0q sin_withAccuracy:0.01      0.841666667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2054
     1.0q sin_withAccuracy:0.001     0.841468254
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2055
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2056
     1.0q sin_withAccuracy:1e-40     0.841470985
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2057
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2058
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2059
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2060
sinh_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2061
    "compute the hyperbolic sine of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2062
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2063
    "/ uses taylor series:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2064
    "/               x^3   x^5   x^7
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2065
    "/ sinh(x) = x + --- + --- + --- ...
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2066
    "/                3!!    5!!    7!!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2067
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2068
    |x2 facN num den approx delta|
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2069
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2070
    x2 := self * self.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2071
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2072
    num := x2 * self.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2073
    den := 2*3.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2074
    facN := 3.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2075
    approx := self + (num / den).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2076
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2077
    [
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2078
        facN := facN + 2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2079
        den := den * (facN - 1) * facN.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2080
        num := num * x2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2081
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2082
        delta := num / den.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2083
        approx := approx + delta.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2084
    ] doUntil:[delta abs <= epsilon].
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2085
    ^ approx
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2086
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2087
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2088
     1.0 sinh                                    1.1752
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2089
     1q sinh                        1.17520119
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2090
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2091
     1q sinh_withAccuracy:1         1.16666667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2092
     1q sinh_withAccuracy:0.1       1.175 
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2093
     1q sinh_withAccuracy:0.01      1.175
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2094
     1q sinh_withAccuracy:0.001     1.17519841
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2095
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2096
     1q sinh_withAccuracy:1e-40     1.17520119 
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2097
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2098
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2099
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2100
sqrt_withAccuracy:epsilon 
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2101
    "compute square root of the receiver using newtom/heron algorithm"
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2102
    "
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2103
      Use the Heron algorithm:
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2104
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2105
                 x_n + (a / x_n)
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2106
        x_n+1 =  ---------------
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2107
                      2
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2108
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2109
        sqrt(a) = x_n
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2110
    "
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2111
    
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2112
    |approx|
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2113
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2114
    self <= 0 ifTrue:[
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2115
        self = 0 ifTrue:[
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2116
            ^ self
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2117
        ].
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2118
        ^ self class
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2119
            raise:#imaginaryResultSignal
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2120
            receiver:self
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2121
            selector:#sqrt
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2122
            arguments:#()
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2123
            errorString:'bad (negative) receiver in sqrt'
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2124
    ].
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2125
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2126
    approx := 1.
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2127
    [
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2128
        |lastApprox|
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2129
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2130
        lastApprox := approx.
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2131
        approx := ((self / approx) + approx) / 2.
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2132
        (approx - lastApprox) abs > epsilon
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2133
    ] whileTrue.
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2134
    ^ approx
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2135
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2136
    "
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2137
     2 sqrt                                  1.4142135623731
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2138
     2q sqrt                                 1.414213562373095049
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2139
     2q sqrt_withAccuracy:0.01               1.414215686274509804
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2140
     2q sqrt_withAccuracy:0.0001             1.414213562374689911
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2141
     2q sqrt_withAccuracy:0.0000001          1.414213562373095049
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2142
     2q sqrt_withAccuracy:0.0000000001       1.414213562373095049
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2143
     2q sqrt_withAccuracy:0.000000000001     1.414213562373095049
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2144
     2q sqrt_withAccuracy:LongFloat epsilon  1.414213562373095049
7729
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2145
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2146
     (4 sqrt_withAccuracy:Integer epsilon) asFloat
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2147
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2148
     MessageTally spyOn:[ |arg|
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2149
        arg := 2 asLongFloat.
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2150
        1000000 timesRepeat:[
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2151
             arg sqrt_withAccuracy:0.000000000001
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2152
        ]
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2153
     ]
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2154
     Time millisecondsToRun:[ |arg|
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2155
        arg := 2 asLongFloat.
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2156
        1000000 timesRepeat:[
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2157
             arg sqrt_withAccuracy:0.000000000001
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2158
        ]
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2159
     ]
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2160
    "
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2161
!
bbd20db09d1b Fix senders of #xxxx_withAccuracy: to use #epsilon instead of #precision.
Stefan Vogel <sv@exept.de>
parents: 7468
diff changeset
  2162
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2163
tan_withAccuracy:epsilon
17190
284d9c71385f class: Number
Claus Gittinger <cg@exept.de>
parents: 17127
diff changeset
  2164
    "compute the tangens of the receiver using a taylor series approx."
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2165
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2166
    "/ uses taylor series:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2167
    "/                x^3     x^5      x^7      x^9     2^2n * ( 2^2n - 1) * B2n * x^(2n-1)
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2168
    "/    tan = x + 1*--- + 2*--- + 17*--- + 62*----... ----------------------------------...
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2169
    "/                 3       15      315      2835                 (2n)!!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2170
    "/ where Bi is the ith bernoulli number.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2171
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2172
    |factors idx x2 num t approx lastApprox delta|
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2173
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2174
    "/    (1 to:20) collect:[:n| |num den|
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2175
    "/        num := (2 raisedTo:(2*n)) * ((2 raisedTo:(2*n))-1) * ((n*2) bernoulli).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2176
    "/        den := (2*n) factorial.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2177
    "/        num / den
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2178
    "/    ]   
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2179
   factors := #(
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2180
        (1 3) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2181
        (2 15) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2182
        (17 315) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2183
        (62 2835)
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2184
        (1382 155925) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2185
        (21844 6081075) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2186
        (929569 638512875)
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2187
        (6404582 10854718875) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2188
        (443861162 1856156927625) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2189
        (18888466084 194896477400625) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2190
        (113927491862 2900518163668125) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2191
        (58870668456604 3698160658676859375) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2192
        (8374643517010684 1298054391195577640625) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2193
        (689005380505609448 263505041412702261046875) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2194
        (129848163681107301953 122529844256906551386796875) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2195
        (1736640792209901647222 4043484860477916195764296875) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2196
        (418781231495293038913922 2405873491984360136479756640625) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2197
        (56518638202982204522669764 801155872830791925447758961328125) 
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2198
        (32207686319158956594455462 1126482925555250126673224649609375)).
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2199
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2200
    x2 := self * self.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2201
8203
666bfa7bcb97 Fix C cmpiler warnings.
Stefan Vogel <sv@exept.de>
parents: 7857
diff changeset
  2202
    num := x2 * self.               "/ =  x^3
666bfa7bcb97 Fix C cmpiler warnings.
Stefan Vogel <sv@exept.de>
parents: 7857
diff changeset
  2203
    approx := self + (num / 3).     "/ do the first iteration
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2204
    lastApprox := self.
8203
666bfa7bcb97 Fix C cmpiler warnings.
Stefan Vogel <sv@exept.de>
parents: 7857
diff changeset
  2205
    idx := 2.
666bfa7bcb97 Fix C cmpiler warnings.
Stefan Vogel <sv@exept.de>
parents: 7857
diff changeset
  2206
    [
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2207
        t := factors at:idx ifAbsent:[].
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2208
        t isNil ifTrue:[
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2209
            self error:'too many iterations'.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2210
"/ Not enough bernoulli numbers for now...
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2211
"/            |tempNum tempDen|
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2212
"/            tempNum := 2 raisedTo:(2*idx).
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2213
"/            tempNum := tempNum * (tempNum-1) * ((2*idx) bernoulli).
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2214
"/            tempDen := (2*idx) factorial.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2215
"/            t := Array with:tempNum with:tempDen.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2216
        ].
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2217
        idx := idx + 1.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2218
        num := num * x2.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2219
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2220
        delta := num * t first / t second.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2221
        approx := approx + delta.
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2222
    ] doUntil:[delta abs <= epsilon].
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2223
    ^ approx
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2224
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2225
    "
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2226
     0.5 tan                         0.546302
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2227
     0.5q tan                        0.54630249
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2228
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2229
     0.5q tan_withAccuracy:1         0.541666667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2230
     0.5q tan_withAccuracy:0.1       0.541666667
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2231
     0.5q tan_withAccuracy:0.01      0.545833333
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2232
     0.5q tan_withAccuracy:0.001     0.54625496
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2233
     0.5q tan_withAccuracy:1e-15     0.54630249
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2234
10556
fb4ea1df1f1d Fix/speed up taylor computations
Stefan Vogel <sv@exept.de>
parents: 10550
diff changeset
  2235
     0.5q tan_withAccuracy:1e-40     -- too many iterations
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2236
    "
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2237
! !
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2238
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2239
!Number methodsFor:'testing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2240
5360
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2241
isDivisibleBy:aNumber
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2242
    "return true, if the receiver can be divided by the argument, aNumber without a remainder.
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2243
     Notice, that the result is only worth trusting, if the receiver is an integer."
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2244
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2245
    aNumber = 0 ifTrue: [^ false].
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2246
    aNumber isInteger ifFalse: [^ false].
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2247
    ^ (self \\ aNumber) = 0
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2248
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2249
    "
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2250
     3 isDivisibleBy:2     
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2251
     4 isDivisibleBy:2
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2252
     4.0 isDivisibleBy:2   
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2253
     4.5 isDivisibleBy:4.5 
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2254
     4.5 isDivisibleBy:1.0 
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2255
    "
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2256
!
1aef297d3cbe added #isDivisibleBy:
Claus Gittinger <cg@exept.de>
parents: 5322
diff changeset
  2257
6143
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
  2258
isNaN
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
  2259
    "return true, if the receiver is an invalid float (NaN - not a number)."
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
  2260
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
  2261
    ^ false
6155
00eaf2fa55bc isReal comment
Claus Gittinger <cg@exept.de>
parents: 6154
diff changeset
  2262
00eaf2fa55bc isReal comment
Claus Gittinger <cg@exept.de>
parents: 6154
diff changeset
  2263
    "Created: / 5.11.2001 / 18:07:26 / cg"
6143
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
  2264
!
695cc93a96b4 isNan, isInfinite and closeTo:
james
parents: 6087
diff changeset
  2265
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2266
isNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2267
    "return true, if the receiver is a kind of number"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2268
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2269
    ^ true
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2270
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2271
6087
826b87eab740 moved isReal
Claus Gittinger <cg@exept.de>
parents: 6072
diff changeset
  2272
isReal
826b87eab740 moved isReal
Claus Gittinger <cg@exept.de>
parents: 6072
diff changeset
  2273
    "return true, if the receiver is some kind of real number (as opposed to a complex);
826b87eab740 moved isReal
Claus Gittinger <cg@exept.de>
parents: 6072
diff changeset
  2274
     true is returned here - the method is redefined from Object."
826b87eab740 moved isReal
Claus Gittinger <cg@exept.de>
parents: 6072
diff changeset
  2275
826b87eab740 moved isReal
Claus Gittinger <cg@exept.de>
parents: 6072
diff changeset
  2276
    ^ true
826b87eab740 moved isReal
Claus Gittinger <cg@exept.de>
parents: 6072
diff changeset
  2277
!
826b87eab740 moved isReal
Claus Gittinger <cg@exept.de>
parents: 6072
diff changeset
  2278
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2279
isZero
1557
2c3c301cf48f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  2280
    "return true, if the receiver is zero"
2c3c301cf48f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  2281
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2282
    ^ self = 0
1557
2c3c301cf48f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  2283
2c3c301cf48f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  2284
    "Modified: 18.7.1996 / 12:40:49 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2285
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2286
4659
cdf37afc0e26 moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents: 4637
diff changeset
  2287
!Number methodsFor:'tracing'!
cdf37afc0e26 moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents: 4637
diff changeset
  2288
4682
4158042a9c8c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4659
diff changeset
  2289
traceInto:aRequestor level:level from:referrer
4659
cdf37afc0e26 moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents: 4637
diff changeset
  2290
    "double dispatch into tracer, passing my type implicitely in the selector"
cdf37afc0e26 moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents: 4637
diff changeset
  2291
4682
4158042a9c8c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4659
diff changeset
  2292
    ^ aRequestor traceNumber:self level:level from:referrer
4659
cdf37afc0e26 moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents: 4637
diff changeset
  2293
cdf37afc0e26 moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents: 4637
diff changeset
  2294
cdf37afc0e26 moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents: 4637
diff changeset
  2295
! !
cdf37afc0e26 moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents: 4637
diff changeset
  2296
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2297
!Number methodsFor:'trigonometric'!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2298
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2299
arcCos
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2300
    "return the arccosine of the receiver (in radians)"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2301
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2302
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2303
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2304
        ^ self asFloat arcCos.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2305
    ].
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2306
    ^ (self class pi / 2) - self arcSin
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2307
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2308
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2309
arcCosh
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2310
    "return the hyperbolic arccosine of the receiver."
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2311
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2312
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2313
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2314
        ^ self asFloat arcCosh.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2315
    ].
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2316
    ^ (self + (self*self-1) sqrt) ln.
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2317
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2318
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2319
arcSin
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2320
    "return the arcsine of the receiver (in radians)"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2321
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2322
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2323
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2324
        ^ self asFloat arcSin.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2325
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  2326
    ^ self arcSin_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2327
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2328
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2329
arcSinh
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2330
    "return the hyperbolic arcsine of the receiver."
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2331
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2332
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2333
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2334
        ^ self asFloat arcSinh.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2335
    ].
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2336
    ^ ( self + (self*self+1) sqrt ) ln
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2337
"/    ^ self arcSinh_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2338
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2339
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2340
arcTan
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2341
    "return the arctangent of the receiver (as radians)"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2342
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2343
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2344
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2345
        ^ self asFloat arcTan.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2346
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  2347
    ^ self arcTan_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2348
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2349
11488
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2350
arcTan2:x
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2351
    "return atan2(self,x) (as radians)"
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2352
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2353
    (self isLimitedPrecisionReal not
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2354
    or:[self generality < 1.0 generality]) ifTrue:[
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2355
        ^ self asFloat arcTan2:x.
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2356
    ].
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2357
    ^ self arcTan2_withAccuracy:self epsilon x:x
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2358
!
8d948de20326 +arcTan2:
Claus Gittinger <cg@exept.de>
parents: 11460
diff changeset
  2359
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2360
arcTanh
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2361
    "return the hyperbolic arctangent of the receiver."
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2362
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2363
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2364
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2365
        ^ self asFloat arcTanh.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2366
    ].
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2367
    ^ ((self + 1) ln / 2) - ((1 - self) ln / 2)
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2368
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2369
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2370
cos
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2371
    "return the cosine of the receiver (interpreted as radians)"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2372
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2373
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2374
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2375
        ^ self asFloat cos.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2376
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  2377
    ^ self cos_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2378
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2379
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2380
cosh
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2381
    "return the hyperbolic cosine of the receiver"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2382
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2383
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2384
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2385
        ^ self asFloat cosh.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2386
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  2387
    ^ self cosh_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2388
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2389
8844
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2390
cot
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2391
    "return the cotangent of the receiver"
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2392
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2393
    ^ 1 / self tan
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2394
!
21e7c41e277e hyperbolic functions fixed
Claus Gittinger <cg@exept.de>
parents: 8830
diff changeset
  2395
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2396
sin
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2397
    "return the sine of the receiver (interpreted as radians)"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2398
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2399
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2400
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2401
        ^ self asFloat sin.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2402
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  2403
    ^ self sin_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2404
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2405
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2406
sinh
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2407
    "return the hyperbolic sine of the receiver"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2408
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2409
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2410
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2411
        ^ self asFloat sinh.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2412
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  2413
    ^ self sinh_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2414
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2415
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2416
tan
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2417
    "return the tangens of the receiver (interpreted as radians)"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2418
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2419
    ^ self sin / self cos
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2420
!
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2421
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2422
tanh
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2423
    "return the hyperbolic tangens of the receiver"
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2424
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2425
    (self isLimitedPrecisionReal not
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2426
    or:[self generality < 1.0 generality]) ifTrue:[
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2427
        ^ self asFloat tanh.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2428
    ].
8634
2c838074e754 epsilon
Claus Gittinger <cg@exept.de>
parents: 8303
diff changeset
  2429
    ^ self tanh_withAccuracy:self epsilon
7449
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2430
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2431
"/ If a fast exp is available, the following might be better...
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2432
"/
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2433
"/    |exp nexp|
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2434
"/
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2435
"/    "/ tanh is:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2436
"/    "/      sinh(x)
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2437
"/    "/      -------
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2438
"/    "/      cosh(x)
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2439
"/    "/
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2440
"/    "/ which is:
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2441
"/    "/      (exp(x) - exp(-x)) / 2
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2442
"/    "/      ----------------------
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2443
"/    "/      (exp(x) + exp(-x)) / 2
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2444
"/    
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2445
"/    exp := self exp.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2446
"/    nexp := self negated exp.
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2447
"/
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2448
"/    ^ (exp - nexp) / (exp + nexp)
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2449
! !
192789c6e1c2 fallback code for trigonometric;
Claus Gittinger <cg@exept.de>
parents: 7388
diff changeset
  2450
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5360
diff changeset
  2451
!Number methodsFor:'truncation & rounding'!
3900
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2452
4895
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2453
detentBy: detent atMultiplesOf: grid snap: snap
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2454
    "Map all values that are within detent/2 of any multiple of grid 
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2455
     to that multiple.  
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2456
     Otherwise, if snap is true, return self, meaning that the values 
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2457
     in the dead zone will never be returned.  
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2458
     If snap is false, then expand the range between dead zones
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2459
     so that it covers the range between multiples of the grid, 
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2460
     and scale the value by that factor."
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2461
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2462
    | r1 r2 |
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2463
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2464
    r1 := self roundTo: grid.                    "Nearest multiple of grid"
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2465
    (self roundTo: detent) = r1 ifTrue: [^ r1].  "Snap to that multiple..."
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2466
    snap ifTrue: [^ self].                       "...or return self"
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2467
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2468
    r2 := self < r1                               "Nearest end of dead zone"
15108
ffef3e5a13d5 class: Number
Claus Gittinger <cg@exept.de>
parents: 15100
diff changeset
  2469
            ifTrue: [r1 - (detent asFloat/2)]
ffef3e5a13d5 class: Number
Claus Gittinger <cg@exept.de>
parents: 15100
diff changeset
  2470
            ifFalse: [r1 + (detent asFloat/2)].
4895
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2471
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2472
    "Scale values between dead zones to fill range between multiples"
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2473
    ^ r1 + ((self - r2) * grid asFloat / (grid - detent))
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2474
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2475
    "
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2476
     (170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: true]         
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2477
     (170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: false]
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2478
     (3.9 to: 4.1 by: 0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: true]    
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2479
     (-3.9 to: -4.1 by: -0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: false]
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2480
    "
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2481
!
cc546b082506 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4682
diff changeset
  2482
3900
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2483
fractionPart
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2484
    "return a float with value from digits after the decimal point.
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2485
     i.e. the receiver minus its truncated value,
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2486
     such that (self truncated + self fractionPart) = self"
3900
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2487
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2488
    ^ self - self truncated
3900
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2489
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2490
    "
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2491
     1234.56789 fractionPart
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2492
     1.2345e6 fractionPart  
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2493
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2494
     1.6 asLongFloat fractionPart + 1.6 asLongFloat truncated    
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
  2495
     -1.6 asLongFloat fractionPart + -1.6 asLongFloat truncated    
3900
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2496
    "
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2497
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2498
    "Modified: / 4.11.1996 / 20:26:54 / cg"
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2499
    "Created: / 28.10.1998 / 17:14:40 / cg"
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2500
!
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2501
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2502
integerPart
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2503
    "return a float with value from digits before the decimal point
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2504
     (i.e. the truncated value)"
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2505
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2506
    ^ self truncated asFloat
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2507
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2508
    "
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2509
     1234.56789 integerPart 
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2510
     1.2345e6 integerPart   
6154
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  2511
     12.5 integerPart 
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  2512
     -12.5 integerPart 
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  2513
     (5/3) integerPart  
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  2514
     (-5/3) integerPart 
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  2515
     (5/3) truncated  
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  2516
     (-5/3) truncated  
3900
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2517
    "
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2518
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2519
    "Created: / 28.10.1998 / 17:14:56 / cg"
6154
6cb999c5ace3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6143
diff changeset
  2520
    "Modified: / 5.11.2001 / 17:54:22 / cg"
3900
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2521
! !
c3bc76767cfc added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3770
diff changeset
  2522
16656
9d93ee3916f6 class: Number
Stefan Vogel <sv@exept.de>
parents: 16630
diff changeset
  2523
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1701
diff changeset
  2524
!Number class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2525
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2526
version
17207
75c41a799425 class: Number
Claus Gittinger <cg@exept.de>
parents: 17190
diff changeset
  2527
    ^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.158 2014-12-11 14:11:07 cg Exp $'
12112
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
  2528
!
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
  2529
949f9713c4a5 changed: #readFrom:decimalPointCharacters:onError:
Claus Gittinger <cg@exept.de>
parents: 11944
diff changeset
  2530
version_CVS
17207
75c41a799425 class: Number
Claus Gittinger <cg@exept.de>
parents: 17190
diff changeset
  2531
    ^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.158 2014-12-11 14:11:07 cg Exp $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 569
diff changeset
  2532
! !
15078
997a122ceeb5 class: Number
Stefan Vogel <sv@exept.de>
parents: 14495
diff changeset
  2533