ROBOTC 4.X has a powerful collection of math functions that can be used to create and solve complex mathematical formulas.


abs

float abs(const float input)
Parameter Explanation Data Type
Return Type The function returns a positive floating point decimal value. float
input The number to take the absolute value of. (can be: int, long, short, float)

 

  • Returns the absolute value of a number.

 

Code Example:

 	//create a variable 'G' and set it equal to -9.81
	float G = -9.81;

	//create and set variable 'downForce'
	//to the absolute value of 'G' (9.81)
	float downForce = abs(G);

 


acos

float acos(const float Cosine)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value. float
Cosine The number to take the arc-cosine of (in radians). float

 

  • Returns the arc-cosine of a number in radians.

 

Code Example:

	// create and set floating point variable 'param' to 0.5
        float param = 0.5;

/* create floating point variable 'result' and set it
equal to the arc-cosine of 'param' in degrees (60) */
float result = acos(param) * 180.0 / PI;

 


asin

float asin(const float Sine)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
Sine The number to take the arc-sine of (in radians). float

 

  • Returns the arc-sine of a number in radians.

 

Code Example:

// create and set floating point variable 'param' to 0.5
float param = 0.5; 

/* create floating point variable 'result' and set it 
equal to the arc-sine of 'param' in degrees (30) */
 float result = asin(param) * 180.0 / PI;  

 


atan

float atan(const float Tangent)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
Tangent The number to take the arc-tangent of (in radians). float

 

  • Returns the arc-tangent of a number in radians.

 

Code Example:

// create and set floating point variable 'param' to 0.5
float param = 0.5; 

/* create floating point variable 'result' and set it 
equal to the arc-sine of 'param' in degrees (26.565)*/
float result = asin(param) * 180.0 / PI;  

 


atof

float atof(string str)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
str The string to convert to a float. string

 

  • Returns a float representation of the string, str.

 

Code Example:

task main()
{
 string strPI = "3.14";    // string 'strPI' is set equal to "3.14"
 float test = atof(strPI); // convert the string value of 'strPI' to a float and set 'test' to that number (3.14)
 while(true){}              // keep the ROBOTC debugger alive so we can see the result
}

 


atoi

long atoi(string str)
Parameter Explanation Data Type
Return Type The function returns a long integer value long
str The string to convert to a long. string

 

  • Returns a long representation of the string, str.

 

Code Example:

task main()
{
 string strPI = "3.14";    // string 'strPI' is set equal to "3.14"
 long test = atoi(strPI);  // convert the string value of 'strPI' to a long and set 'test' to that number (3)
 while(true){}              // keep the ROBOTC debugger alive so we can see the result
}

 


ceil

float ceil(const float input)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
input The number to find the ceiling of (round up to nearest integer) float

 

  • Returns the smallest integer value that is greater than or equal to input.

 

Code Example:

 float E = 2.72;           // create a variable 'E' and set it equal to 2.72
 float ceiling = ceil(E);  // create and set variable 'ceiling' to the ceiling value of 'E' (3)

 


cos

float cos(const float fRadians)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
fRadians The number to take the cosine of (in radians). float

 

  • Returns the cosine of a number of radians.

 

Code Example:

// create a floating point variable 'result' and set it 
//equal to the cosine of PI (-1)

float result = cos(PI);  

 


cosDegrees

float cosDegrees(const float fDegrees)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
fDegrees The number to take the cosine of (in degrees). float

 

  • Returns the cosine of a number of degrees.

 

Code Example:

/* create a floating point variable 'result' and set it 
equal to the cosine of 180 degrees (-1) */
float result = cosDegrees(180);   

 


degreesToRadians

float degreesToRadians(const float fDegrees)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
fDegrees The number of degrees to convert into radians. int

 

  • Returns the radian equivalent of fDegrees.

 

Code Example:

// create a floating point variable 'radiansPerDegree ' and set it equal to 
//the amount of radians in 1.0 degrees (0.017)
float radiansPerDegree = degreesToRadians(1.0);  

 


exp

float exp(const float input)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
input The floating point number to raise the constant 'e' to. float

 

  • Returns the number 'e' rasied to the power of input.

 

Code Example:

 /* create floating point variable 'result' and set it 
equal to e raised to the 4th power (e^4 = 54.598) */
float result = exp(4);  

 


floor

float floor(const float input)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
input The floating point number to take the floor value of (round down to nearest integer) float

 

  • Returns the largest integer value that is less than or equal to input.

 

Code Example:

// create a variable 'E' and set it equal to 2.72
float E = 2.72;

// create and set variable 'floorValue' to the floor value of 'E' (2)
 float floorValue = floor(E);  

 


log

float log(const float input)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
input The floating point number to take the natural logarithm of. float

 

  • Returns the natural logarithm (ln) of input.

 

Code Example:

// create a variable 'E' and set it equal to 2.72
float E = 2.72;
 
// create and set variable 'ln' to the natural log of 'E' (1.00)
float ln = log(E);  

 


log10

float log10(const float input)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
input The floating point number to take the base-10 logarithm of. float

 

  • Returns the base-10 logarithm of input.

 

Code Example:

// create a variable 'E' and set it equal to 2.72
float E = 2.72;
            
// create and set variable 'logBTen' to the base-10 log of 'E' (0.43) 
float logBTen = log10(E);  

 


PI

const float PI
Variable Name Range of Values Data Type
PI 3.14159265358979323846264338327950288419716939937510 float
  • The constant π.

 

Code Example:

 float y = 0.0;                // create and set 'y' to 0.0
 float LCD_width = 100.0;      // create and set 'LCD_width' to 100.0 (width of NXT LCD in pixels)  
 drawLine(0, 31, 99, 31);   // draw a line across the center of the LCD  
 for(int x = 0; x < 100; x++)// loop from 0 to 99
 { 
  y = sin(x * (2.0 * PI) / LCD_width) * 25; // calculate y-coordinate of pixel to draw
  setPixel(x, y + 31);                   // draw pixel at (x, y+31)
  wait1Msec(100);                           // wait 100 milliseconds
 }

 


pow

float pow(const float base, const float exponent)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
base The floating point base to raise to the power of 'power'. float
exponent The floating point exponent to raise 'base' to. float

 

  • Returns base to the power of exponent.

 

Code Example:

// create floating point variable 'kilobyte' and set it equal to 10^3 or 1000
float kilobyte = pow(10, 3);  

 


radiansToDegrees

short radiansToDegrees(const float fRadians)
Parameter Explanation Data Type
Return Type The function returns a short integer value. short
fRadians The number of radians to convert into degrees. float

 

  • Returns the degree equivalent of fRadians.

 

Code Example:

/* create a floating point variable 'degrees' and set it 
equal to the amount of degrees in PI (180)*/
float degrees = radiansToDegrees(PI);  

 


rand

word rand()
Parameter Explanation Data Type
Return Type The function returns a word type integer value word

 

  • Returns a pseudo-random integral number in the range 0 to at least 32,767 (depending on platform).
  • A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range:

    • ( value % 100 ) is in the range 0 to 99

    • ( value % 100 + 1 ) is in the range 1 to 100
    • ( value % 30 + 1985 ) is in the range 1985 to 2014
  • Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

 

Code Example:

task main()
{
 int min = -100;   // create int variable 'min' and set it to -100
 int max = 100;    // create int variable 'max' and set it to 100
 srand(nSysTime);  // generate seed for rand() from current system time  
 while(true)// infinite loop:
 {
  motor[rightMotor] = (rand() % (max-min)) + min; // set 'rightMotor' to a random number in the range: [min, max]
  motor[leftMotor]  = (rand() % (max-min)) + min; // set 'leftMotor' to a random number in the range: [min, max]
  wait1Msec(500); // wait 500 milliseconds
 }
}

 


randLong

long randLong()
Parameter Explanation Data Type
Return Type The function returns a long type integer long

 

  • Returns a pseudo-random integral number in the range 0 to 2,147,483,647.
  • A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range:

    • ( value % 100 ) is in the range 0 to 99

    • ( value % 100 + 1 ) is in the range 1 to 100
    • ( value % 30 + 1985 ) is in the range 1985 to 2014
  • Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

 

Code Example:

task main()
{
 long min = -100;   // create long variable 'min' and set it to -100
 long max = 100;    // create long variable 'max' and set it to 100  
 srand(nSysTime);  // generate seed for rand() from current system time  
 while(true)// infinite loop:
 {
  motor[rightMotor] = (randLong() % (max-min)) + min; // set 'rightMotor' to a random number in the range: [min, max]
  motor[leftMotor]  = (randLong() % (max-min)) + min; // set 'leftMotor' to a random number in the range: [min, max]
  wait1Msec(500); // wait 500 milliseconds
 }
}

 


sgn

short sgn(const float input)
Parameter Explanation Data Type
Return Type The function returns a short integer value short
input The number to test the sign of float

 

  • Returns a value less than 0 if input is negative, and a value greater than 0 if input is positive.

 

Code Example:

 int res1 = sgn(-9.81);  // returns -1 to 'res1'
 int res2 = sgn(3.14);   // returns 1 to 'res2'
 int res3 = sgn(0);      // returns 0 to 'res3'

 


sin

float sin(const float fRadians)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
fRadians The number to take the sine of (in radians). float

 

  • Returns the sine of a number of radians.

 

Code Example:

// create a floating point variable 'result' and set it 
//equal to the cosine of PI (0)
float result = sin(PI);  

 


sinDegrees

float sinDegrees(const float fDegrees)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
fDegrees The number to take the sine of (in degrees). float

 

  • Returns the sine of a number of degrees.

 

Code Example:

 /* create a floating point variable 'result' and 
set it equal to the sine of 180 degrees (0) */
float result = sinDegrees(180);  

 


srand

void srand(const long nSeedValue)
Parameter Explanation Data Type
Return Type The function returns no value void
nSeedValue The seed for a pseudo-random number generator, rand() or randlong() long

 

  • The pseudo-random number generator is initialized using the argument passed as nSeedValue.
  • For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.

  • Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

 

Code Example:

task main()
{
 int min = -100;   // create int variable 'min' and set it to -100
 int max = 100;    // create int variable 'max' and set it to 100
 srand(nSysTime);  // generate seed for rand() from current system time  
 
 while(true)// infinite loop:
 {
  motor[rightMotor] = (rand() % (max-min)) + min; // set 'rightMotor' to a random number in the range: [min, max]
  motor[leftMotor]  = (rand() % (max-min)) + min; // set 'leftMotor' to a random number in the range: [min, max]
  wait1Msec(500); // wait 500 milliseconds
 }
 
}

 


sqrt

float sqrt(const float input)
Parameter Explanation Data Type
Return Type The function returns a floating point decimal value float
input The number to take the square-root of. float

 

 

Code Example:

// create a floating point variable 'result' and 
//set it equal to the square-root of 1764 (42)
float result = sqrt(1764);