Number API Functions: Unterschied zwischen den Versionen

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen
Zeile 14: Zeile 14:
<br>16rFFAA or 0xaffe
<br>16rFFAA or 0xaffe
<br>2r0101 or 0b101010
<br>2r0101 or 0b101010
<br>8r377 or 0o377
<br>NrXXX
<br>NrXXX
::integer constant; decimal, hex, binary or any base N; integers may be arbitrary long
::integer constant; decimal, hex, binary, octal or any base N; integers may be arbitrary long
0.5
0.5
<br>1e5
<br>1e5

Version vom 26. September 2020, 15:05 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

Back to Useful API Functions

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

aNumber positive => Boolean

same as aNumber >= 0

aNumber strictlyPositive => Boolean

same as aNumber > 0

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.
Point right.png 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 => 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:
 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:
 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

logarithm; by default, an error is reported for negative numbers;
to get a complex result, use "Complex trapImaginary:[ aNumber ln ]".

aNumber sqrt => Number

square root; by default, an error is reported for negative numbers;
to get a complex result, use "Complex trapImaginary:[ aNumber sort ]".

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)



Copyright © 2014-2024 eXept Software AG