Numbers

sections in this chapter:
[ Number ] copy
[ Number ] > [ Number ]
[ Number ] >=: [ Number ]
[ Number ] < [ Number ]
[ Number ] <=: [ Number ]
[ Number ] = [ Number ]
[ Number ] !=: [ Number ]
[ Number ] between: [ Number ] and: [ Number ]
[ Number ] odd?
[ Number ] even?
[ Number ] + [ Number ]
[ Number ] add: [ Number ]
[ Number ] - [ Number ]
[ Number ] subtract: [ Number ]
[ Number ] * [ Number ]
[ Number ] multiply by: [ Number ]
[ Number ] / [ Number ]
[ Number ] divide-by: [ Number ]
[ Number ] modulo: [ modulo ]
[ Number ] power: [ Number ]
[ Number ] positive?
[ Number ] negative?
[ Number ] floor
[ Number ] [ String ]
[ Number ] qualifier
[ Number ] ceil
[ Number ] round
[ Number ] abs
[ Number ] sqrt
[ Number ] sin
[ Number ] cos
[ Number ] tan
[ Number ] atan
[ Number ] log
[ Number ] & [ Number ]
[ Number ] | [ Number ]
[ Number ] ^ [ Number ]
[ Number ] string
[ Number ] plain
[ Number ] boolean
[ Int64 ] new
[ Int64 ] from-string: [ String ]
[ Int64 ] / [ Int64 ]
[ Int64 ] modulo: [ Int64 ]
[ Int64 ] divide-by: [ Int64 ]
[ Int64 ] * [ Int64 ]
[ Int64 ] multiply-by: [ Int64 ]
[ Int64 ] + [ Int64 ]
[ Int64 ] add: [ Int64 ]
[ Int64 ] - [ Int64 ]
[ Int64 ] subtract: [ Int64 ]
[ Int64 ] = [ Int64 ]
[ Int64 ] !=: [ Int64 ]
[ Int64 ] < [ Int64 ]
[ Int64 ] > [ Int64 ]
[ Int64 ] <=: [ Int64 ]
[ Int64 ] >=: [ Int64 ]
[ Hx ] [ String ]
[ Oct ] [ String ]
[ Bin ] [ String ]

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
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

3
4
automatically generated from test: #597

In 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

7
automatically generated from test: #305

[ Number ] copy

Example

>> x := 1.
>> y := x.
>> z := x copy.
x add: 1.
Out write: x, stop.
Out write: y, stop.
Out write: z, stop.

Result

2
2
1
automatically generated from test: #394

[ Number ] > [ Number ]

Example


Out write: 8 > 7, stop.
Out write: 7 > 8, stop.

Result

True
False
automatically generated from test: #418

[ Number ] >=: [ Number ]

Example


>> x := ( 8 >=: 7 ).
Out write: x, stop.

Result

True
automatically generated from test: #419

[ Number ] < [ Number ]

Example


>> x := 8 < 7.
>> y := 7 < 8.
Out write: x, stop.
Out write: y, stop.

Result

False
True
automatically generated from test: #420

[ Number ] <=: [ Number ]

Example


>> x := ( 8 <=: 7 ).
>> y := 7 ≤ 8.
Out write: x, stop.
Out write: y, stop.

Result

False
True
automatically generated from test: #421

[ Number ] = [ Number ]

Example


>> x := 8 = 8.
>> y := 8 = 9.
Out write: x, stop.
Out write: y, stop.

Result

True
False
automatically generated from test: #422

[ Number ] !=: [ Number ]

Example


>> x := ( 8 !=: 8 ).
>> y := 8 ≠ 9.
Out write: x, stop.
Out write: y, stop.

Result

False
True
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

True
automatically generated from test: #424

[ Number ] odd?

Example


Out write: 2 odd?, stop.
Out write: 3 odd?, stop.

Result

False
True
automatically generated from test: #425

[ Number ] even?

Example


Out write: 2 even?, stop.
Out write: 3 even?, stop.

Result

True
False
automatically generated from test: #426

[ Number ] + [ Number ]

Example


>> x := 2 + 2.
>> y := x + 0.5.
Out write: x, stop.
Out write: y, stop.

Result

4
4.5
automatically generated from test: #427

[ Number ] add: [ Number ]

Example


>> x := 1.
x add: 2.
Out write: x, stop.

Result

3
automatically generated from test: #428

[ Number ] - [ Number ]

Example


>> x := 9.
>> y := x - 4.
Out write: y, stop.

Result

5
automatically generated from test: #429

[ Number ] subtract: [ Number ]

Example


>> x := 3.
x subtract: 1.
Out write: x, stop.

Result

2
automatically generated from test: #430

[ Number ] * [ Number ]

Example


>> x := 3 * 3.
Out write: x, stop.

Result

9
automatically generated from test: #431

[ Number ] multiply by: [ Number ]

Example


>> x := 5.
x multiply-by: 2.
Out write: x, stop.

Result

10
automatically generated from test: #434

[ Number ] / [ Number ]

Example


Out write: 10 / 2, stop.

Result

5
automatically generated from test: #435

[ Number ] divide-by: [ Number ]

Example


>> x := 10.
x divide-by: 2.
Out write: x, stop.

Result

5
automatically generated from test: #436

[ Number ] modulo: [ modulo ]

Example


>> x := 11 modulo: 3.
Out write: x, stop.

Result

2
automatically generated from test: #437

[ Number ] power: [ Number ]

Example


Out write: (2 power: 3).

Result

8
automatically generated from test: #438

[ Number ] positive?

Example

{ :i
Out write: ( i - 5 ) positive?, stop.
} × 10.

Result

False
False
False
False
False
False
True
True
True
True
automatically generated from test: #439

[ Number ] negative?

Example

{ :i
Out write: ( i - 5 ) negative?, stop.
} × 10.

Result

True
True
True
True
True
False
False
False
False
False
automatically generated from test: #440

[ Number ] floor

Example


>> x := 4.5.
Out write: x floor, stop.

Result

4
automatically generated from test: #441

[ Number ] [ String ]

Example


>> x := 3 dollars.
Out write: x qualifier.

Result

dollars
automatically generated from test: #442

[ Number ] qualifier

Example


>> x := 3 dollars.
Out write: x qualifier.

Result

dollars
automatically generated from test: #443

[ Number ] ceil

Example


>> x := 4.5.
Out write: x ceil, stop.

Result

5
automatically generated from test: #444

[ Number ] round

Example


>> x := 5.5.
Out write: x round, stop.

Result

6
automatically generated from test: #445

[ Number ] abs

Example


>> x := -7.
Out write: x abs, stop.

Result

7
automatically generated from test: #446

[ Number ] sqrt

Example


>> x := 49.
>> y := x sqrt.
Out write: y, stop.

Result

7
automatically generated from test: #447

[ Number ] sin

Example


Out write: 123 sin, stop.

Result

-0.4599034907
automatically generated from test: #448

[ Number ] cos

Example


Out write: 123 cos, stop.

Result

-0.8879689067
automatically generated from test: #449

[ Number ] tan

Example


Out write: 123 tan, stop.

Result

0.5179274716
automatically generated from test: #450

[ Number ] atan

Example


Out write: 123 atan, stop.

Result

1.5626664246
automatically generated from test: #451

[ Number ] log

Example


Out write: 123 log, stop.

Result

4.8121843554
automatically generated from test: #452

[ Number ] & [ Number ]

Example


Out write: (8 & 10), stop.

Result

8
automatically generated from test: #453

[ Number ] | [ Number ]

Example


Out write: (8 | 10), stop.

Result

10
automatically generated from test: #454

[ Number ] ^ [ Number ]

Example


Out write: (8 ^ 10), stop.

Result

2
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

123
1.23
2 apples
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

123
1.23
2
automatically generated from test: #457

[ Number ] boolean

Example

{ :i
Out write: (i - 1) bool, stop.
} × 10.

Result

True
False
True
True
True
True
True
True
True
True
automatically generated from test: #458

[ Int64 ] new

Example

>> a := Int64 new.
Out write: a, stop.

Result

0
automatically generated from test: #761

[ 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

123
41
246
124
121
True
True
True
True
True
True
automatically generated from test: #627

[ Int64 ] / [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: a / (Int64 from-string: ['3']), stop.

Result

41
automatically generated from test: #765

[ Int64 ] modulo: [ Int64 ]

Example

>> a := Int64 from-string: ['5'].
Out write: ( a modulo: (Int64 from-string: ['4'])), stop.

Result

1
automatically generated from test: #772

[ Int64 ] divide-by: [ Int64 ]

Example

>> a := Int64 from-string: ['25'].
Out write: a, stop.
a divide-by: ( Int64 from-string: ['10'] ).
Out write: a, stop.

Result

25
2
automatically generated from test: #760

[ Int64 ] * [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: a * (Int64 from-string: ['2']), stop.

Result

246
automatically generated from test: #764

[ Int64 ] multiply-by: [ Int64 ]

Example

>> a := Int64 from-string: ['25'].
Out write: a, stop.
a multiply-by: ( Int64 from-string: ['10'] ).
Out write: a, stop.

Result

25
250
automatically generated from test: #759

[ Int64 ] + [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: a + (Int64 from-string: ['1']), stop.

Result

124
automatically generated from test: #762

[ Int64 ] add: [ Int64 ]

Example

>> a := Int64 new.
Out write: a, stop.
a add: ( Int64 from-string: ['10'] ).
Out write: a, stop.

Result

0
10
automatically generated from test: #757

[ Int64 ] - [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: a - (Int64 from-string: ['2']), stop.

Result

121
automatically generated from test: #763

[ Int64 ] subtract: [ Int64 ]

Example

>> a := Int64 new.
Out write: a, stop.
a subtract: ( Int64 from-string: ['10'] ).
Out write: a, stop.

Result

0
-10
automatically generated from test: #758

[ Int64 ] = [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: ( a = (Int64 from-string: ['123'])), stop.

Result

True
automatically generated from test: #771

[ Int64 ] !=: [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: ( a !=: (Int64 from-string: ['124'])), stop.

Result

True
automatically generated from test: #770

[ Int64 ] < [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: ( a <   (Int64 from-string: ['124'])), stop.

Result

True
automatically generated from test: #769

[ Int64 ] > [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: ( a >   (Int64 from-string: ['122'])), stop.

Result

True
automatically generated from test: #768

[ Int64 ] <=: [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: ( a <=: (Int64 from-string: ['123'])), stop.

Result

True
automatically generated from test: #767

[ Int64 ] >=: [ Int64 ]

Example

>> a := Int64 from-string: ['123'].
Out write: ( a >=: (Int64 from-string: ['123'])), stop.

Result

True
automatically generated from test: #766

[ Hx ] [ String ]

Example

# print a hex number as decimal
Out write: Hx xFF, stop.

Result

255
automatically generated from test: #658

[ Oct ] [ String ]

Example

# octal to decimal
Out write: Oct o600, stop.

Result

384
automatically generated from test: #659

[ Bin ] [ String ]

Example

# binary to decimal
Out write: Bin b10000000, stop.

Result

128
automatically generated from test: #660