Numbers
Each time you write a number, for example 9, -10, or 3,12, behind the screen, Xoscript will convert these numbers into a Number object. You can send messages to this Number object, or you could assign the number to a variable and send messages afterwards:
10 even?
>> tenner := 10.
tenner even?
Both notations are valid. The Number object responds to the following messages…
Most of these messages are self-explanatory and allow you to execute mathematical operations or comparisons.
Note that you may use ≤ ≥ ≠ × and ÷ instead their ASCII counterparts. This is a matter of preference.
The difference between binary mathematical messages (+) and their keyword variations (add:) is that the former will return a new number, which is the result of the operation, whereas with the latter the object itself will be modified. This is illustrated in the following example:
Example:
>> a := 1.
>> b := a + 3.
a add: 2.
Out write: a, stop.
Out write: b, stop.
Result:
automatically generated from test: #597In the above example, b = 4 and a = 3. With add: 2 the value of a is raised by 2, while + 3 creates a new number that is equal to a + 3. The same applies to other mathematical processes, e.g., multiplications. By using the multiplication symbol, you will receive a new object as answer. In case you use the message multiply-by:, you will multiply the number itself.
With the message between:and:, for example in: Number between: X and: Y, you will get a number between X and Y. In this way, any random number can be generated:
>> a := Number between: 1 and: 10.
Out write: a, stop.
As of version 1.4, the build-in generator of random numbers in xoscript is crypogrpahically secure to the best of our knowledge.
You can attach a qualifier to a number, for instance, 6 apples. Each message that does not get recognised by a number will be considered a qualifier. You can retrieve the qualifier of a number by means of the message qualifier:
A qualifier is basically a Text object that is stored with the Number object. The qualifier is also printed after the number on a write: assignment. Qualifiers could be used to add amounts in mixed currencies for example. On adding the amounts, you can ask for the qualifiers. The following program example illustrates this principle by using a historical currency calculator (as the exchange rate remains reasonably stable!).
Example:
Number learn: ['plus:'] means: ['+'].
Number on: ['+'] do: { :x
>> rate := 1.
>> currency := x qualifier.
(currency = ['euros']) true: {
rate := 2.
}. <- (self plus: (x × rate)).
}.
>> dollars := 3.
>> euros := 2.
dollars qualify: ['dollars'].
euros qualify: ['euros'].
>> money := dollars + euros.
Out write: money, stop.
Result:
automatically generated from test: #305[ Number ] > [ Number ]
Example:
Out write: 8 > 7, stop.
Out write: 7 > 8, stop.
Result:
automatically generated from test: #418
[ Number ] >=: [ Number ]
Example:
>> x := ( 8 >=: 7 ).
Out write: x, stop.
Result:
automatically generated from test: #419
[ Number ] < [ Number ]
Example:
>> x := 8 < 7.
>> y := 7 < 8.
Out write: x, stop.
Out write: y, stop.
Result:
automatically generated from test: #420
[ Number ] <=: [ Number ]
Example:
>> x := ( 8 <=: 7 ).
>> y := 7 ≤ 8.
Out write: x, stop.
Out write: y, stop.
Result:
automatically generated from test: #421
[ Number ] = [ Number ]
Example:
>> x := 8 = 8.
>> y := 8 = 9.
Out write: x, stop.
Out write: y, stop.
Result:
automatically generated from test: #422
[ Number ] !=: [ Number ]
Example:
>> x := ( 8 !=: 8 ).
>> y := 8 ≠ 9.
Out write: x, stop.
Out write: y, stop.
Result:
automatically generated from test: #423
[ Number ] between: [ Number ] and: [ Number ]
Example:
# draw a number between 0 and 10
# boundaries are inclusive
>> x := Number between: 0 and: 10.
Out write: ((x >=: 0) and: (x <=: 10)), stop.
Result:
automatically generated from test: #424
[ Number ] odd?
Example:
Out write: 2 odd?, stop.
Out write: 3 odd?, stop.
Result:
automatically generated from test: #425
[ Number ] even?
Example:
Out write: 2 even?, stop.
Out write: 3 even?, stop.
Result:
automatically generated from test: #426
[ Number ] + [ Number ]
Example:
>> x := 2 + 2.
>> y := x + 0.5.
Out write: x, stop.
Out write: y, stop.
Result:
automatically generated from test: #427
[ Number ] add: [ Number ]
Example:
>> x := 1.
x add: 2.
Out write: x, stop.
Result:
automatically generated from test: #428
[ Number ] - [ Number ]
Example:
>> x := 9.
>> y := x - 4.
Out write: y, stop.
Result:
automatically generated from test: #429
[ Number ] subtract: [ Number ]
Example:
>> x := 3.
x subtract: 1.
Out write: x, stop.
Result:
automatically generated from test: #430
[ Number ] * [ Number ]
Example:
>> x := 3 * 3.
Out write: x, stop.
Result:
automatically generated from test: #431
[ Number ] multiply by: [ Number ]
Example:
>> x := 5.
x multiply-by: 2.
Out write: x, stop.
Result:
automatically generated from test: #434
[ Number ] / [ Number ]
Example:
Out write: 10 / 2, stop.
Result:
automatically generated from test: #435
[ Number ] divide by: [ Number ]
Example:
>> x := 10.
x divide-by: 2.
Out write: x, stop.
Result:
automatically generated from test: #436
[ Number ] modulo: [ modulo ]
Example:
>> x := 11 modulo: 3.
Out write: x, stop.
Result:
automatically generated from test: #437
[ Number ] power: [ Number ]
Example:
Out write: (2 power: 3).
Result:
automatically generated from test: #438
[ Number ] positive?
Example:
{ :i
Out write: ( i - 5 ) positive?, stop.
} × 10.
Result:
automatically generated from test: #439
[ Number ] negative?
Example:
{ :i
Out write: ( i - 5 ) negative?, stop.
} × 10.
Result:
automatically generated from test: #440
[ Number ] floor
Example:
>> x := 4.5.
Out write: x floor, stop.
Result:
automatically generated from test: #441
[ Number ] [ String ]
Example:
>> x := 3 dollars.
Out write: x qualifier.
Result:
automatically generated from test: #442
[ Number ] qualifier.
Example:
>> x := 3 dollars.
Out write: x qualifier.
Result:
automatically generated from test: #443
[ Number ] ceil
Example:
>> x := 4.5.
Out write: x ceil, stop.
Result:
automatically generated from test: #444
[ Number ] round
Example:
>> x := 5.5.
Out write: x round, stop.
Result:
automatically generated from test: #445
[ Number ] abs
Example:
>> x := -7.
Out write: x abs, stop.
Result:
automatically generated from test: #446
[ Number ] sqrt
Example:
>> x := 49.
>> y := x sqrt.
Out write: y, stop.
Result:
automatically generated from test: #447
[ Number ] sin
Example:
Out write: 123 sin, stop.
Result:
automatically generated from test: #448
[ Number ] cos
Example:
Out write: 123 cos, stop.
Result:
automatically generated from test: #449
[ Number ] tan
Example:
Out write: 123 tan, stop.
Result:
automatically generated from test: #450
[ Number ] atan
Example:
Out write: 123 atan, stop.
Result:
automatically generated from test: #451
[ Number ] log
Example:
Out write: 123 log, stop.
Result:
automatically generated from test: #452
[ Number ] & [ Number ]
Example:
Out write: (8 & 10), stop.
Result:
automatically generated from test: #453
[ Number ] | [ Number ]
Example:
Out write: (8 | 10), stop.
Result:
automatically generated from test: #454
[ Number ] ^ [ Number ]
Example:
Out write: (8 ^ 10), stop.
Result:
automatically generated from test: #455
[ Number ] string
Example:
>> x := 123.
Out write: x, stop.
>> x := 1.23.
Out write: x, stop.
>> x := 2 apples.
Out write: x, stop.
Result:
automatically generated from test: #456
[ Number ] plain
Example:
>> x := 123.
Out write: x plain, stop.
>> x := 1.23.
Out write: x plain, stop.
>> x := 2 apples.
Out write: x plain, stop.
Result:
automatically generated from test: #457
[ Number ] boolean
Example:
{ :i
Out write: (i - 1) bool, stop.
} × 10.
Result:
automatically generated from test: #458
[ Int64 ] from-string: [ String ]
Example:
# create a 64-bit integer for exact calculations
>> a := Int64 from-string: ['123'].
# to string
Out write: a, stop.
# divide
Out write: a / (Int64 from-string: ['3']), stop.
# multiply
Out write: a * (Int64 from-string: ['2']), stop.
# add
Out write: a + (Int64 from-string: ['1']), stop.
# subtract
Out write: a - (Int64 from-string: ['2']), stop.
# compare
Out write: ( a = (Int64 from-string: ['123'])), stop.
Out write: ( a !=: (Int64 from-string: ['124'])), stop.
Out write: ( a < (Int64 from-string: ['124'])), stop.
Out write: ( a > (Int64 from-string: ['122'])), stop.
Out write: ( a >=: (Int64 from-string: ['123'])), stop.
Out write: ( a <=: (Int64 from-string: ['123'])), stop.
Result:
automatically generated from test: #627
[ Hx ] [ String ]
Example:
# print a hex number as decimal
Out write: Hx xFF, stop.
Result:
automatically generated from test: #658
[ Oct ] [ String ]
Example:
# octal to decimal
Out write: Oct o600, stop.
Result:
automatically generated from test: #659
[ Bin ] [ String ]
Example:
# binary to decimal
Out write: Bin b10000000, stop.
Result:
automatically generated from test: #660