REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Math: Standard Functions and Operators

Pending Revision

This document was written for R2 and has yet to be revised for R3.

This section describes the standard math functions and operators used in REBOL.

Contents

absolute

The expressions:

absolute value

abs value

return the absolute value of value.

Works with integer!, decimal!, money!, time!, pair! datatypes.

print absolute -10
10
print absolute -1.2
1.2
print absolute -$1.2
$1.20
print absolute -10:20
10:20
print absolute -10x-20
10x20

add

The expressions:

value1 + value2

add value1 value2

return the result of adding value1 to value2.

Works with integer!, decimal!, money!, time!, tuple!, pair!, date!, char! datatypes.

print 1 + 2
3
print 1.2 + 3.4
4.6
print 1.2.3 + 3.4.5
4.6.8
print $1 + $2
$3.00
print 1:20 + 3:40
5:00
print 10x20 + 30x40
40x60
print #"A" + 10
K
print add 1 2
3

complement

The expression:

complement value

returns the numeric complement (bitwise complement) of a value.

Works with integer!, decimal!, tuple! datatypes.

print complement 10
-11
print complement 10.5
-11
print complement 100.100.100
155.155.155

divide

The expressions:

value1 / value2

divide value1 value2

return the result of dividing value1 by value2.

Works with integer!, decimal!, money!, time!, tuple!, pair!, char! datatypes.

print 10 / 2
5
print 1.2 / 3
0.4
print 11.22.33 / 10
1.2.3
print $12.34 / 2
$6.17
print 1:20 / 2
0:40
print 10x20 / 2
5x10
print divide 10 2
5

multiply

The expressions:

value1 * value2

multiply value1 value2

return the result of multiplying value1 by value2.

Works with integer!, decimal!, money!, time!, tuple!, pair!, char! datatypes.

print 10 * 2
20
print 1.2 * 3.4
4.08
print 1.2.3 * 3.4.5
3.8.15
print $10 * 2
$20.00
print 1:20 * 3
4:00
print 10x20 * 3
30x60
print multiply 10 2
20

negate

The expressions:

- value

negate value

change the sign of the value.

Works with integer!, decimal!, money!, time!, pair!, char! datatypes.

<A name=_Toc487519923>print - 10
-10
print - 1.2
-1.2
print - $10
-$10.00
print - 1:20
-1:20
print - 10x20
-10x-20
print negate 10
-10

random

The expression:

random value

returns a random value that is less than or equal to value given.

Note that for integers random begins at 1, not 0, and is inclusive of the value given. This allows random to be used directly with functions like pick.

When a decimal is used the result is a decimal datatype rounded to an integer.

The /seed refinement restarts the random generator. Use the /seed refinement with random first it if you want unique random number generation. You can use the current date and time to make the seed more random:

random/seed now

Works with integer!, decimal!, money!, time!, tuple!, pair!, date!, char!, string!, block! datatypes.

print random 10
5
print random 10.5
2
print random 100.100.100
79.95.66
print random $100
$32.00
print random 10:30
6:37:33
print random 10x20
2x4
print random 30-Jun-2000
27-Dec-1171

remainder

The expressions:

value1 // value2

remainder value1 value2

return the remainder of dividing value1 by value2.

Works with integer!, decimal!, money!, time!, tuple!, pair! datatypes.

print 11 // 2
1
print 11.22.33 // 10
1.2.3
print 11x22 // 2
1x0
print remainder 11 2
1

subtract

The expressions:

value1 - value2

subtract value1 value2

return the result of subtracting value2 from value1.

Works with integer!, decimal!, money!, time!, tuple!, pair!, date!, char! datatypes.

print 2 - 1
1
print 3.4 - 1.2
2.2
print 3.4.5 - 1.2.3
2.2.2
print $2 - $1
$1.00
print 3:40 - 1:20
2:20
print 30x40 - 10x20
20x20
print #"Z" - 1
Y
print subtract 2 1
1


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin