Number API Functions: Unterschied zwischen den Versionen
Cg (Diskussion | Beiträge) |
Cg (Diskussion | Beiträge) |
||
Zeile 109: | Zeile 109: | ||
''aNumber'' <code>sqrt</code> => Number |
''aNumber'' <code>sqrt</code> => Number |
||
:: square root.<br>By default, an error is reported for negative numbers;<br>to get a complex result, use "<code>Complex trapImaginary:[ aNumber |
:: square root.<br>By default, an error is reported for negative numbers;<br>to get a complex result, use "<code>Complex trapImaginary:[ aNumber sqrt ]</code>". |
||
''aNumber'' <code>cbrt</code> => Number |
''aNumber'' <code>cbrt</code> => Number |
Version vom 27. September 2020, 08:30 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
8r377 or 0o377
NrXXX
- integer constant; decimal, hex, binary, octal 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]
aNumber +
aNumber => Number
aNumber -
aNumber => Number
aNumber *
aNumber => Number
aNumber /
aNumber => Number
- The usual arithmetic operators.
- All of the above allow operands of any number type, and will generate a result as appropriate. When dividing integers, an exact result is returned (either integer or fraction). If any of the arguments is inexact (i.e. a floating point number), the result will be inexact. If both are exact, the result will be exact. Fractional results are reduced to the greatest common divisor as denominator.
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.
- The usual arithmetic operators.
aNumber //
aNumber => Integer
aNumber \\
aNumber => Integer
- Truncated result and remainder (towards the next smaller integer i.e. towards negative infinity).
The following equation holds:(a // b) * b + (a \\ b) = a
- Examples:
- Truncated result and remainder (towards the next smaller integer i.e. towards negative infinity).
100 // 3 => 33 100 \\ 3 => 1
-100 // 3 => -34 -100 \\ 3 => 2
aNumber quo:
aNumber => Integer
aNumber rem:
aNumber => Integer
- Truncated result (towards zero) and corresponding remainder. For positive arguments, this is the same as the above. The equation is similar to above:
(a quo: b) * b + (a rem: b) = a
- Examples:
- Truncated result (towards zero) and corresponding remainder. For positive arguments, this is the same as the above. The equation is similar to above:
100 quo: 3 => 33 100 rem: 3 => 1
-100 quo: 3 => -33 -100 rem: 3 => -1
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
aNumber log:
base => Number
- logarithm (natural, base10, base2 or arbitrary base).
By default, an error is reported for negative numbers;
to get a complex result, use "Complex trapImaginary:[ aNumber ln ]
".
- logarithm (natural, base10, base2 or arbitrary base).
aNumber sqrt
=> Number
- square root.
By default, an error is reported for negative numbers;
to get a complex result, use "Complex trapImaginary:[ aNumber sqrt ]
".
- square root.
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:
x => 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, or convert themself to a string:
aNumber printOn:
aStream
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.
formatString printf:
{ aNumber } on:
aStream
- The printf:on: function 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)