# Operators

Operators are math statements to modify or create values. Several operators can be chained together to create complex logic in a single statement. A simple operation example is "heading = heading + 180" to add 180° to the current heading angle, like the program used in the Loop Forever example.

## Basic Operators

These operators can be used for basic math within a statment:

+ Adds two values

- Subtracts one value from the other

* Multiplies two values

/ Divides one value by another

^ Exponent multiplies a value by itself a given number of times

For example, to add 180° to the heading and then roll for 1s:

void startProgram()
{
backLed = 255;
controlSystemTargetYaw = (controlSystemTargetYaw + 180);
delay(1);
controlSystemTargetSpeed = 50;
delay(1);
controlSystemTargetSpeed = 0;
}

A more complex calculation on the heading could be to add 180° as done above, then divide by 3, then multiply by 6 to derrive a heading of 120°:

void startProgram()
{
backLed = 255;
// derrive a heading of 120°
controlSystemTargetYaw = (controlSystemTargetYaw + (180 / (3 * 2 )));
delay(1);
controlSystemTargetSpeed = 50;
delay(1);
controlSystemTargetSpeed = 0;
}

You can also embed sensor readings in operand statements instead of staic values; see the Spinning Top example on the Comparators page that operates on the gyroscope rate of rotation to generate RGB values. As you operate on values rember to follow order of operations rules.

## Randomness

You can create interesting logic by adding randomness to programs, such as used the logic used in the Fortune Teller program. You need to specificy one of the two value types that Oval supports:

### Random Float

// type: float // access: read & write // range: 0 - 1 // units: none

nextRandomFloat generates a random float value from 0 to 1. You can then use this to make your own custom range. For example, use (nextRandomFloat * 4 + 1) to get a random number from 1 to 5. The result can be any float number, such as 1.33333, 2.4545, etc..

### Random Int

// type: int // access:: read & write // range -2147483648 - 2147483647 // units: none

nextRandomInt generates a random integer value from -2147483648 to 2147483647. You can then use this to make your own custom range. For example, use use the modulo function _mod(nextRandomInt, 4) + 1 to set a random number from 1 to 5. The result can be any int value as a whole number, such as 1, 2, 3, 4, or 5.

## Operand Functions

These more advanced operators require a defined function to solve. You will need to review Functions to use these operators if you are not already familiar with them.

### Square Root

_sqrt returns the square root of a value.

float _sqrt(float value)
{
if (value <= 0)
{
return 0;
}
return value ^ 0.5;
}

For example, to derive a heading from the square root of 900 use:

float _sqrt(float value)
{
if (value <= 0)
{
return 0;
}
return value ^ 0.5;
}

void startProgram()
{
controlSystemTargetYaw = _sqrt(900);
delay(1);
controlSystemTargetSpeed = 50;
delay(1);
controlSystemTargetSpeed = 0;
}

### Round

_round rounds a value to the nearest integer value.

float _round(float value)
{
int round;
if (value > 0)
{
round = value + 0.5;
}
else
{
round = value - 0.5;
}
return round;
}


### Floor

_floor rounds a value down to the nearest integer value.

float _floor(float value)
{
int floorValue = value;
if (value < floorValue)
{
floorValue = floorValue - 1;
}
return floorValue;
}

### Ceiling

_ceiling rounds a value up to the nearest integer value.

float _ceiling(float value)
{
int ceilValue = value;
if (value > ceilValue)
{
ceilValue = ceilValue + 1;
}
return ceilValue;
}

### Absolute Value

_abs returns the magnitude of a value, removing its sign.

float _abs(float value)
{
if (value < 0)
{
value = -value;
}
return value;
}

### Sign

_sign returns +1 for positive values, -1 for negative values, and 0 for zero.

float _sign(float value)
{
if (value < 0)
{
return -1;
}
else if (value > 0)
{
return 1;
}
else
{
return 0;
}
}

### Modulo

_mod returns the remainder after division of one value by another.

float _mod(float dividend, float divisor)
{
if (divisor == 0)
{
return 0;
}

int quotient = dividend / divisor;
float remainder = dividend - (divisor * quotient);
return remainder;
}

### Minimum Value

_min returns the lesser of two values. Use this to set a lower bound on a value.

float _min(float a, float b)
{
if (a < b)
{
return a;
}
else
{
return b;
}
}

### Maximum Value

_max returns the greater of two values. Use this to set an upper bound on a value.

float _max(float a, float b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}

## Triginometry Functions

Enter 0-360° to get the ratio of sides in a right angle triangle. Notice that we convert radians to degrees for ease of use, but you could write your own trig functions to use radians if desired.

### Sine

sinInDegrees returns the sin of an angle; equal to Opposite / Hypotenuse.

float sinInDegrees(float degrees)
{
float pi = 3.14159265;
float radians = degrees * pi / 180;
}

### Cosine

cosInDegrees returns the cos of an angle; equal to Adjacent / Hypotenuse.

float cosInDegrees(float degrees)
{
float pi = 3.14159265;
float radians = degrees * pi / 180;
}

### Tangent

tanInDegrees returns the tan of an angle; equal to Opposite / Adjacent.

float tanInDegrees(float degrees)
{
float pi = 3.14159265;
float radians = degrees * pi / 180;
}

### A Sine

asinInDegrees returns an angle in degrees, given the ratio of opposite side to hypotenuse. In the below function, x = opposite / hypotenuse.

float asinInDegrees(float x)
{
float negate = 0;
if (x < 0)
{
x = x*-1;
negate = 1;
}
float pi = 3.14159265;
radians = pi * 0.5 - ((1.0 - x) ^ 0.5) * radians;
return radians * 180 / pi;
}

### A Cosine

acosInDegrees returns an angle in degrees, given the ratio of adjacent side to hypotenuse. In the below function, x = adjacent / hypotenuse.

float acosInDegrees(float x)
{
float negate = 0;
if (x < 0)
{
x = x*-1;
negate = 1;
}
float pi = 3.14159265;
float ret = -0.0187293;
ret = ret * x;
ret = ret + 0.0742610;
ret = ret * x;
ret = ret - 0.2121144;
ret = ret * x;
ret = ret + 1.5707288;
ret = ret * ((1.0 - x) ^ 0.5);
ret = ret - 2 * negate * ret;
ret = negate * pi + ret;
return ret * 180 / pi;
}

### A Tangent

atanInDegrees returns an angle in degrees, given the opposite and adjacent sides.

atanInDegrees(float opp, float adj)
{
float pi = 3.1415927;
return atan2(opp, adj) * 180.0 / pi;
}