Math & logic


Most of Red's math and logic is usual except maybe the order of evaluation. Below I list the operators (words) used for calculations, adding notes that I find useful. Most of them have no need for a detailed description.


Math

The basics:

The following group have a both a functional (e.g. add) and an infix operator (e.g. "+") . They accept number! char! pair! tuple! or vector! as arguments (except power?).

Note that if you use the functional operator, it goes before the operands (e.g.: 3 + 4 <=> add 3 4).

add or +

subtract or -

multiply or *

divide or /

power or **

absolute

Evaluates an expression and returns the absolute value, that is, a positive number.

negate

Invert the signal of a value, that is: positive <=> negative

pi

3,141592...

random

Returns a random value of the same type as its argument.

If argument is an integer, returns an integer between 1 (inclusive) and the argument (inclusive).

If argument is a float, returns a float between 0 (inclusive) and the argument (inclusive).

If the argument is a series, it shuffles the elements.

>> random 10
== 2
>> random 33x33
== 13x23
>> random 1
== 1
>> random 1.0
== 0.07588539741741744
>> random "abcde"
== "cedab"
>> random 10:20:05
== 8:02:32.5867693

Refinements:

/seed - Restart or randomize. I think the use of this is if your random function is called many times within a program. In this case it may not be so random unless you reestart it with a seed.

/secure - Returns a cryptographically secure random number (from red-by-example.org, I'm not sure what it does)

/only - Pick a random value from a series.

>> random/only ["fly" "bee" "ant" "owl" "dog"]
== "fly"
>> random/only "aeiou"
== #"o"

round

Returns the nearest integer value. Halves (e.g. 0,5) are rounded away from zero by default.

>> round 2.3
== 2.0
>> round 2.5
== 3.0
>> round -2.3
== -2.0
>> round -2.5
== -3.0

Refinements:

/to - You supply the "precision" of your rounding:

>> round/to 6.8343278 0.1
== 6.8
>> round/to 6.8343278 0.01
== 6.83
>> round/to 6.8343278 0.001
== 6.834

/even - Halves (e.g. 0.5) are rounded not "up" as default, but towards the even integer.

>> round/even 2.5
== 2.0              ;not 3

/down - Simply truncates the number, but keeps the number a float!.

>> round/down 3.9876
== 3.0
>> round/down -3.876
== -3.0

/half-down - Halves round toward zero, not away from zero.

>> round/half-down 2.5
== 2.0
>> round/half-down -2.5
== -2.0

/floor - Round in negative direction.

>> round/floor 3.8
== 3.0
>> round/floor -3.8
== -4.0

/ceiling - Round in positive direction.

>> round/ceiling 2.2
== 3.0
>> round/ceiling -2.8
== -2.0

/half-ceiling - Halves round in positive direction.

>> round/half-ceiling 2.5
== 3.0
>> round/half-ceiling -2.5
== -2.0

square-root

Takes any number! as argument.


Remainders etc.:

remainder or //

Takes number! char! pair! tuple! and vector! as arguments. Returns the rest of dividing the first by the second value.

>> remainder 15 6
== 3
>> remainder -15 6
== -3
>> remainder 4.67 2
== 0.67
>> 17 // 5
== 2
>> 4.8 // 2.2
== 0.3999999999999995

%

Seems to me as the same as remainder ( "//" ).

modulo

Seems to me as the same as remainder for positive numbers, but it is different with negative numbers. I could not figure out exactly how it works.
red-by-example.org says: "The word modulo computes the non-negative remainder of the first argument divided by the second argument."

>> modulo 9 4
== 1
>> modulo -15 6
== 3
>> modulo -15 -6
== 3
>> modulo -15 7     ;?????
== 6
>> modulo -15 -7    ;?????
== 6

Logarithms etc.:

exp

Raises e (the natural number) to the power in the single argument.

log-10

Returns the logarithm base 10 of the argument.

log-2

Returns the logarithm base 2 of the argument.

log-e

Returns the logarithm base e of the argument.


Trigonometry:

All the trigonometric functions with long names (arccosine, cosine etc) use degrees as default, but accept the refinement /radians to use this unit. The short name versions (acos, cos etc.) take radians as arguments and require it to be a float!

acos or arccosine

asin or arcsine

atan or arctangent

atan2 or arctangent2

cos or cosine

sin or sine

tan or tangent


Extras:

max

Returns the greater of two arguments. Arguments may be scalar! or series!

I'm not sure how it selects the greater series, but is seems to choose the series with the first greater value from left to right.

>> max 8 12
== 12
>> max "abd" "abc"
== "abd"

>> max [1 2 3] [3 2 1]
== [3 2 1]

>> max [1 2 99] [3 2 1]
== [3 2 1]

In a pair! comparison, it returns the greater for each element:

>> max 12x6 7x34
== 12x34

min

Returns the smaller of two arguments. Notes of max apply here too.

odd?

Returns true if integer! argument is odd, and false otherwise.

even?

Returns true if integer! argument is even, and false otherwise.

positive?

true if greater than zero. Note: false if zero.

negative?

true if less than zero. Note: false if zero.

zero?

true only if zero.

math

Evaluates a block! using the normal mathematical rules of precedence, that is, divisions and multiplications are evaluated before additions and subtractions and so on.

within?

It has 3 arguments of the pair! type. The first is a point's coordinates (origin in the upper left corner). The other two describe a rectangle, the first is its upper left origin, and the second is the width and height. If the point is inside or at the edge, returns true!, otherwise returns false! . For examples, check red-by-example.org .

NaN?

Returns true if the argument is 'not a number',otherwise false.

NaN

?

a-an

?


Binaries, hexadecimals etc.


Logic

and~ or and (infix)

equal? or =

greater-or-equal? or >=

greater? or >

lesser-or-equal? or <=

lesser? or <

not

not-equal? or <>

or~ or or (infix)

same? or =?

Returns true it the arguments refer to the same data (object, string etc.), that is, it they both refer to the same space in memory.

>> a: [1 2 3]
== [1 2 3]
>> b: a          ; b points to the same data as a
== [1 2 3]
>> a =? b
== true          ; they are the same
>> c: [1 2 3]
== [1 2 3]
>> c =? a        ; c is equal to a, but is not the same data in memory.
== false

strict-equal? or ==

Returns true if the arguments are exactly equal, with same datatype same lower-case/uppercase (strings) etc.

>> a: "house"
>> b: "House"
>> a = b
== true
>> a == b
== false

results matching ""

    No results matching ""