|ROBOTC has a powerful collection of useful math functions for the NXT, TETRIX, and VEX CORTEX platforms. '''The RCX and VEX PIC platforms do not have enough memory to store these more advanced math functions.'''

+

−

|-

+

−

|}

+

−

+

−

----

+

ROBOTC has a powerful collection of useful math functions for the NXT, TETRIX, VEX CORTEX, and Arduino MEGA-based platforms. '''The RCX, VEX PIC and Arduino 328P-based platforms do not have enough memory to store these more advanced math functions or support floating point numbers.'''

| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Returns the number 'e' rasied to the power of <span class="codeBasic">input</span>.

+

| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_float|float]]) Returns the number 'e' rasied to the power of <span class="codeBasic">input</span>.

| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Returns the '''largest integer''' value that is less than or equal to <span class="codeBasic">input</span>.

+

| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_float|float]]) Returns the '''largest integer''' value that is less than or equal to <span class="codeBasic">input</span>.

| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Returns a value less than 0 if <span class="codeBasic">input</span> is negative, and a value greater than 0 if <span class="codeBasic">input</span> is positive.

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.

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.

+

|-

+

|

+

{|

+

|-

+

|<syntaxhighlight lang="ROBOTC">

+

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]

| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Returns a value less than 0 if <span class="codeBasic">input</span> is negative, and a value greater than 0 if <span class="codeBasic">input</span> is positive.

Latest revision as of 15:23, 18 May 2012

ROBOTC has a powerful collection of useful math functions for the NXT, TETRIX, VEX CORTEX, and Arduino MEGA-based platforms. The RCX, VEX PIC and Arduino 328P-based platforms do not have enough memory to store these more advanced math functions or support floating point numbers.

atof

taskmain(){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

taskmain(){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)

(float) Returns the smallest integer value that is greater than or equal to input.

radiansToDegrees

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

rand

word rand()

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

taskmain(){int min = -100; // create int variable 'min' and set it to -100int max = 100; // create int variable 'max' and set it to 100srand(nSysTime); // generate seed for rand() from current system timewhile(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()

(long) Returns a pseudo-random integral number in the range 0 to at least 2,147,483,647 (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.

taskmain(){long min = -100; // create long variable 'min' and set it to -100long max = 100; // create long variable 'max' and set it to 100srand(nSysTime); // generate seed for rand() from current system timewhile(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)

(short) Returns a value less than 0 if input is negative, and a value greater than 0 if input is positive.

sinDegrees

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

srand

void srand(const long nSeedValue)

(void) 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.