Number API Functions: Unterschied zwischen den Versionen
Cg (Diskussion | Beiträge) |
Cg (Diskussion | Beiträge) |
||
Zeile 122: | Zeile 122: | ||
Numbers can print themself on an output stream, |
Numbers can print themself on an output stream, |
||
''aNumber'' <code>printOn:</code>''aStream'' |
|||
or convert themself to a string: |
or convert themself to a string: |
||
''aNumber'' <code>printString</code> => String |
|||
The above generate a standard format, which should fit most needs. |
The above generate a standard format, which should fit most needs. |
||
More control is available via the printf functions, which offer a range of options to fill left or right, to control printing of the sign and to fill with zeros. |
More control is available via the printf functions, which offer a range of options to fill left or right, to control printing of the sign and to fill with zeros. |
||
<br>The printf:on: function: |
<br>The printf:on: function: |
||
''formatString'' <code>printf:</code> { ''aNumber'' } <code>on:</code> ''aStream'' |
|||
takes an argument Vector as first Argument; thus, multiple values are printed as in the following example: |
takes an argument Vector as first Argument; thus, multiple values are printed as in the following example: |
||
'%04x %3d %+4.3f\n' printf:{ 123 . 4 . 3.14159 } on:Transcript |
'%04x %3d %+4.3f\n' printf:{ 123 . 4 . 3.14159 } on:Transcript |
||
Individual conversions can be done with: |
Individual conversions can be done with: |
||
''aNumber'' <code>printfPrintString:</code>''formatString'' => String |
|||
Notice that printf accepts C-style character escapes (which is not the case in general with Smalltalk) |
Notice that printf accepts C-style character escapes (which is not the case in general with Smalltalk) |
Version vom 26. September 2020, 02:25 Uhr
This document lists most useful (and most often needed) functions. Be aware, that there are many more to be found in either the class references or via the builtin class browser.
Reference: Integer Float Fraction FixedPoint Complex which all inherit from Number
Inhaltsverzeichnis
Literals (i.e. Constant Numbers)[Bearbeiten]
1234
16rFFAA or 0xaffe
2r0101 or 0b101010
NrXXX
- integer constant; decimal, hex, binary or any base N; integers may be arbitrary long
0.5
1e5
-1.2e-3
- float constant; actually double precision; use "q" for extra precision
(4/3)
- a fraction (numerator / denominator)
(4+3i)
- complex number (real part / imaginary part)
Testing[Bearbeiten]
aNumber isFinite
=> Boolean
- Check if number is not infinity and not NaN:
aNumber isInfinite
=> Boolean
- Check if number is either positive infinity (INF) or negative infinity (-INF):
aNumber isNaN
=> Boolean
- Check if number is NaN ("Not a Number"):
aNumber negative
=> Boolean
- same as
aNumber < 0
- same as
aNumber positive
=> Boolean
- same as
aNumber >= 0
- same as
aNumber strictlyPositive
=> Boolean
- same as
aNumber > 0
- same as
anInteger isPrime
=> Boolean
- true if the number is a prime number
anInteger isPowerOf2
=> Boolean
- true if there exists an n, such that 2ˆn equals the number
anInteger isPowerOf:
b => Boolean
- true if there exists an n, such that bˆn equals the number
anInteger nextPrime
=> anInteger
anInteger nextPowerOf2
=> anInteger
Arithmetic[Bearbeiten]
The usual arithmetic operators.
Notice, that they are evaluated left to right, without special precedences.
Thus you should always use parentheses to group expressions when there are two or more operators in an arithmetic expression.
aNumber +
aNumber => Number
aNumber -
aNumber => Number
aNumber *
aNumber => Number
aNumber /
aNumber => Number
aNumber //
aNumber => Integer
aNumber \\
aNumber => Integer
- Truncated result and remainder (towards the next smaller integer i.e. towards negative infinity):
- Examples:
100 // 3 => 33
-100 // 3 => -34
aNumber quo:
aNumber => Integer
aNumber rem:
aNumber => Integer
- Truncated result and remainder (towards zero)
Mathematical and Trigonometric Functions[Bearbeiten]
The usual operations are provided as unary messages to the number:
aNumber ln
=> Number
aNumber log10
=> Number
aNumber log2
=> Number
- logarithm; by default, an error is reported for negative numbers;
to get a complex result, use "Complex trapImaginary:[ aNumber ln ]".
- logarithm; by default, an error is reported for negative numbers;
aNumber sqrt
=> Number
- square root; by default, an error is reported for negative numbers;
to get a complex result, use "Complex trapImaginary:[ aNumber sort ]".
- square root; by default, an error is reported for negative numbers;
aNumber cbrt
=> Number
- cubic root.
aNumber exp
=> Number
number1 raisedTo: number2 => Number
- exponentiation
aNumber sin
=> Number
aNumber cos
=> Number
aNumber tan
=> Number
aNumber arcSin
=> Number
aNumber arcCos
=> Number
aNumber arcTan
=> Number
aNumber arcTan2
=> Number
aNumber sinh
=> Number
aNumber cosh
=> Number
aNumber tanh
=> Number
- trigonometric / hyperbolic functions
Bitwise Operators[Bearbeiten]
ínteger1 bitAnd:
integer2 => Integer
ínteger1 bitOr:
integer2 => Integer
ínteger1 bitXor:
integer2 => Integer
ínteger1 bitShift:
count => Integer
ínteger1 leftShift:
count => Integer
ínteger1 rightShift:
count => Integer
ínteger1 bitTest:
integer2 => Boolean
Printing[Bearbeiten]
Numbers can print themself on an output stream,
aNumber printOn:
aStream
or convert themself to a string:
aNumber printString
=> String
The above generate a standard format, which should fit most needs.
More control is available via the printf functions, which offer a range of options to fill left or right, to control printing of the sign and to fill with zeros.
The printf:on: function:
formatString printf:
{ aNumber } on:
aStream
takes an argument Vector as first Argument; thus, multiple values are printed as in the following example:
'%04x %3d %+4.3f\n' printf:{ 123 . 4 . 3.14159 } on:Transcript
Individual conversions can be done with:
aNumber printfPrintString:
formatString => String
Notice that printf accepts C-style character escapes (which is not the case in general with Smalltalk)