Math
(→cos) |
(→randLong) |
||
| (12 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | + | <yambe:breadcrumb self="Math">General|General Programming</yambe:breadcrumb> | |
| − | + | <br /> | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | 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.''' | ||
| + | <br /> | ||
| − | { | + | {{tl|1|1}} |
| − | + | <br /> | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | {| | + | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
== abs == | == abs == | ||
| Line 44: | Line 21: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''input'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the absolute value of (can be: int, long, short, float). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 75: | Line 52: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''Cosine'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the arc-cosine of (in radians). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 108: | Line 85: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''Sine'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the arc-sine of (in radians). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 141: | Line 118: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''Tangent'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the arc-tangent of (in radians). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 154: | Line 131: | ||
float result = asin(param) * 180.0 / PI; // create floating point variable 'result' and | float result = asin(param) * 180.0 / PI; // create floating point variable 'result' and | ||
// set it equal to the arc-sine of 'param' in degrees (26.565) | // set it equal to the arc-sine of 'param' in degrees (26.565) | ||
| + | </syntaxhighlight> | ||
| + | |- | ||
| + | |} | ||
| + | |- | ||
| + | |} | ||
| + | <br /> | ||
| + | |||
| + | == atof == | ||
| + | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
| + | |- | ||
| + | | class="functionType"| | ||
| + | <span class="bigKeywordBI">float </span> | ||
| + | <span class="bigKeywordB">atof</span> | ||
| + | <span class="bigCodePunc">(</span> | ||
| + | <span class="bigKeywordBI">string </span> | ||
| + | <span class="bigCodeBasic">str</span> | ||
| + | <span class="bigCodePunc">)</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 a float representation of the string, <span class="codeBasic">str</span>. | ||
| + | |- | ||
| + | | | ||
| + | {| class="parameterTable" cellpadding="5%" width="100%" | ||
| + | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter | ||
| + | ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation | ||
| + | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
| + | |- | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''str'' | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The string to convert to a float. | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_string|string]] | ||
| + | |- | ||
| + | |} | ||
| + | |- | ||
| + | | | ||
| + | {| | ||
| + | |- | ||
| + | |<syntaxhighlight lang="ROBOTC"> | ||
| + | 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 | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |- | ||
| + | |} | ||
| + | |- | ||
| + | |} | ||
| + | <br /> | ||
| + | |||
| + | == atoi == | ||
| + | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
| + | |- | ||
| + | | class="functionType"| | ||
| + | <span class="bigKeywordBI">long </span> | ||
| + | <span class="bigKeywordB">atoi</span> | ||
| + | <span class="bigCodePunc">(</span> | ||
| + | <span class="bigKeywordBI">string </span> | ||
| + | <span class="bigCodeBasic">str</span> | ||
| + | <span class="bigCodePunc">)</span> | ||
| + | |- | ||
| + | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_long|long]]) Returns a long representation of the string, <span class="codeBasic">str</span>. | ||
| + | |- | ||
| + | | | ||
| + | {| class="parameterTable" cellpadding="5%" width="100%" | ||
| + | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter | ||
| + | ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation | ||
| + | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
| + | |- | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''str'' | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The string to convert to a long. | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_string|string]] | ||
| + | |- | ||
| + | |} | ||
| + | |- | ||
| + | | | ||
| + | {| | ||
| + | |- | ||
| + | |<syntaxhighlight lang="ROBOTC"> | ||
| + | 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 | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|- | |- | ||
| Line 174: | Line 235: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''input'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The floating point number to take the ceiling value of. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 205: | Line 266: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''fRadians'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the cosine of (in radians). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 230: | Line 291: | ||
|- | |- | ||
| 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 cosine of a number of degrees. | | 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 cosine of a number of degrees. | ||
| + | |- | ||
| | | | ||
{| class="parameterTable" cellpadding="5%" width="100%" | {| class="parameterTable" cellpadding="5%" width="100%" | ||
| Line 236: | Line 298: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''fDegrees'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the cosine of (in degrees). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 268: | Line 330: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''fDegrees'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number of degrees to convert into radians. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 300: | Line 362: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''input'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The floating point number to raise the constant 'e' to. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 332: | Line 394: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''input'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The floating point number to take the floor value of. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 363: | Line 425: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''input'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The floating point number to take the natural logarithm of. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 394: | Line 456: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''input'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The floating point number to take the base-10 logarithm of. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 454: | Line 516: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''base'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The floating point base to raise to the power of 'power'. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
| style="border-style: solid; border-width: 0px 0px 0px 0px"|''exponent'' | | style="border-style: solid; border-width: 0px 0px 0px 0px"|''exponent'' | ||
| Line 490: | Line 552: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''fRadians'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number of radians to convert into degrees. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 502: | Line 564: | ||
float degrees = radiansToDegrees(PI); // create a floating point variable 'degrees' | float degrees = radiansToDegrees(PI); // create a floating point variable 'degrees' | ||
// and set it equal to the amount of degrees in PI (180) | // and set it equal to the amount of degrees in PI (180) | ||
| + | </syntaxhighlight> | ||
| + | |- | ||
| + | |} | ||
| + | |- | ||
| + | |} | ||
| + | <br /> | ||
| + | |||
| + | == rand == | ||
| + | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
| + | |- | ||
| + | | class="functionType"| <span class="bigKeywordBI">word </span><span class="bigKeywordB">rand</span><span class="bigCodePunc">()</span> | ||
| + | |- | ||
| + | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_word|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. | ||
| + | |- | ||
| + | | | ||
| + | {| | ||
| + | |- | ||
| + | |<syntaxhighlight lang="ROBOTC"> | ||
| + | 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 | ||
| + | } | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |- | ||
| + | |} | ||
| + | |- | ||
| + | |} | ||
| + | <br /> | ||
| + | |||
| + | == randLong == | ||
| + | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
| + | |- | ||
| + | | class="functionType"| <span class="bigKeywordBI">long </span><span class="bigKeywordB">randLong</span><span class="bigCodePunc">()</span> | ||
| + | |- | ||
| + | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_long|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. | ||
| + | |- | ||
| + | | | ||
| + | {| | ||
| + | |- | ||
| + | |<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] | ||
| + | wait1Msec(500); // wait 500 milliseconds | ||
| + | } | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|- | |- | ||
| Line 522: | Line 664: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''input'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to test the sign of |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 555: | Line 697: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''fRadians'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the sine of (in radians). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 587: | Line 729: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''fDegrees'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the sine of (in degrees). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 599: | Line 741: | ||
float result = sinDegrees(180); // create a floating point variable 'result' and | float result = sinDegrees(180); // create a floating point variable 'result' and | ||
// set it equal to the sine of 180 degrees (0) | // set it equal to the sine of 180 degrees (0) | ||
| + | </syntaxhighlight> | ||
| + | |- | ||
| + | |} | ||
| + | |- | ||
| + | |} | ||
| + | <br /> | ||
| + | |||
| + | == srand == | ||
| + | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
| + | |- | ||
| + | | class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">srand</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">const long </span><span class="bigCodeBasic">nSeedValue</span><span class="bigCodePunc">)</span> | ||
| + | |- | ||
| + | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) The pseudo-random number generator is initialized using the argument passed as <span class="codeBasic">nSeedValue</span>. | ||
| + | |||
| + | 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. | ||
| + | |- | ||
| + | | | ||
| + | {| class="parameterTable" cellpadding="5%" width="100%" | ||
| + | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter | ||
| + | ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation | ||
| + | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
| + | |- | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''nSeedValue'' | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The seed for the pseudo-random number generator, <span class="codeBasic">rand</span><span class="codePunc">()</span> | ||
| + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_long|long]] | ||
| + | |- | ||
| + | |} | ||
| + | |- | ||
| + | | | ||
| + | {| | ||
| + | |- | ||
| + | |<syntaxhighlight lang="ROBOTC"> | ||
| + | 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 | ||
| + | } | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|- | |- | ||
| Line 619: | Line 808: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''input'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the square-root of. |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 651: | Line 840: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''fRadians'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the tangent of (in radians). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
| Line 682: | Line 871: | ||
! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type | ||
|- | |- | ||
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|''fDegrees'' |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|The number to take the tangent of (in degrees). |
| − | | style="border-style: solid; border-width: | + | | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_float|float]] |
|- | |- | ||
|} | |} | ||
Latest revision as of 08: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.
|
| |||||||
abs
| float abs(const float input) | ||||||
| (float) Returns the absolute value of a number. | ||||||
| ||||||
|
acos
| float acos(const float Cosine) | ||||||
| (float) Returns the arc-cosine of a number in radians. | ||||||
| ||||||
|
asin
| float asin(const float Sine) | ||||||
| (float) Returns the arc-sine of a number in radians. | ||||||
| ||||||
|
atan
| float atan(const float Tangent) | ||||||
| (float) Returns the arc-tangent of a number in radians. | ||||||
| ||||||
|
atof
|
float atof ( string str ) | ||||||
| (float) Returns a float representation of the string, str. | ||||||
| ||||||
|
atoi
|
long atoi ( string str ) | ||||||
| (long) Returns a long representation of the string, str. | ||||||
| ||||||
|
ceil
| float ceil(const float input) | ||||||
| (float) Returns the smallest integer value that is greater than or equal to input. | ||||||
| ||||||
|
cos
| float cos(const float fRadians) | ||||||
| (float) Returns the cosine of a number of radians. | ||||||
| ||||||
|
cosDegrees
| float cosDegrees(const float fDegrees) | ||||||
| (float) Returns the cosine of a number of degrees. | ||||||
| ||||||
|
degreesToRadians
| float degreesToRadians(const float fDegrees) | |||||||
| (float) Returns the radian equivalent of fDegrees. | |||||||
|
exp
| float exp(const float input) | ||||||
| (float) Returns the number 'e' rasied to the power of input. | ||||||
| ||||||
|
floor
| float floor(const float input) | ||||||
| (float) Returns the largest integer value that is less than or equal to input. | ||||||
| ||||||
|
log
| float log(const float input) | ||||||
| (float) Returns the natural logarithm (ln) of input. | ||||||
| ||||||
|
log10
| float log10(const float input) | ||||||
| (float) Returns the base-10 logarithm of input. | ||||||
| ||||||
|
PI
| const float PI = 3.14159265358979323846264338327950288419716939937510 | |
| (float) The constant π. | |
|
pow
| float pow(const float base, const float exponent) | |||||||||
| (float) Returns base to the power of exponent. | |||||||||
| |||||||||
|
radiansToDegrees
| short radiansToDegrees(const float fRadians) | ||||||
| (short) Returns the degree equivalent of fRadians. | ||||||
| ||||||
|
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. | |
|
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. | |
|
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. | ||||||
| ||||||
|
sin
| float sin(const float fRadians) | ||||||
| (float) Returns the sine of a number of radians. | ||||||
| ||||||
|
sinDegrees
| float sinDegrees(const float fDegrees) | ||||||
| (float) Returns the sine of a number of degrees. | ||||||
| ||||||
|
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. | ||||||
| ||||||
|
sqrt
| float sqrt(const float input) | ||||||
| (float) Returns the square-root of input. | ||||||
| ||||||
|