Difference between revisions of "Math"

From ROBOTC API Guide
Jump to: navigation, search
(PI)
(PI)
Line 386: Line 386:
 
{
 
{
 
   y = sin(x * (2.0 * PI) / LCD_width) * 25; // calculate y-coordinate of pixel to draw
 
   y = sin(x * (2.0 * PI) / LCD_width) * 25; // calculate y-coordinate of pixel to draw
   nxtSetPixel(x, y + 31);                  // draw pixel at (x, y+31)
+
   nxtSetPixel(x, y + 31) π ;                  // draw pixel at (x, y+31)
 
   wait1Msec(100);                          // wait 100 milliseconds
 
   wait1Msec(100);                          // wait 100 milliseconds
 
}
 
}

Revision as of 20:24, 4 January 2012

Main >> NXT >> Functions >> Math

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.




abs

float abs(const float input)
Returns the absolute value of a number.


Parameter Explanation
input The number to take the absolute value of (can be: int, long, short, float).


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


acos

float acos(const float Cosine)
Returns the arc-cosine of a number in radians.


Parameter Explanation
Cosine The number to take the arc-cosine of (in radians).


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


asin

float asin(const float Sine)
Returns the arc-sine of a number in radians.


Parameter Explanation
Sine The number to take the arc-sine of (in radians).


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


atan

float atan(const float Tangent)
Returns the arc-tangent of a number in radians.


Parameter Explanation
Tangent The number to take the arc-tangent of (in radians).


float param = 0.5;                        // create and set floating point variable 'param' to 0.5
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)


ceil

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


Parameter Explanation
input The floating point number to take the ceiling value of.


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)
Returns the cosine of a number of radians.


Parameter Explanation
fRadians The number to take the cosine of (in radians).


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

cosDegrees

float cosDegrees(const float fDegrees)
Returns the cosine of a number of degrees.


Parameter Explanation
fDegrees The number to take the cosine of (in degrees).


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

degreesToRadians

float degreesToRadians(const float fDegrees)
Returns the radian equivalent of 'fDegrees'.


Parameter Explanation
fDegrees The number of degrees to convert into radians.


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


exp

float exp(const float input)
Returns the number 'e' rasied to the power of 'input'.


Parameter Explanation
input The floating point number to raise the constant 'e' to.


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

floor

float floor(const float input)
Returns the largest integer value that is less than or equal to 'input'.


Parameter Explanation
input The floating point number to take the floor value of.


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


log

float log(const float input)
Returns the natural logarithm (ln) of 'input'.


Parameter Explanation
input The floating point number to take the natural logarithm of.


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


log10

float log10(const float input)
Returns the base-10 logarithm of 'input'.


Parameter Explanation
input The floating point number to take the base-10 logarithm of.


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


PI

const float PI = 3.14159265358979323846264338327950288419716939937510
The constant π.


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)
 
nxtDrawLine(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
  nxtSetPixel(x, y + 31) π ;                   // draw pixel at (x, y+31)
  wait1Msec(100);                           // wait 100 milliseconds
}

pow

float pow(const float base, const float exponent)
Returns 'base' to the power of 'exponent'.


Parameter Explanation
base The floating point base to raise to the power of 'power'.
exponent The floating point exponent to raise 'base' to.


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


radiansToDegrees

short radiansToDegrees(const float fRadians)
Returns the degree equivalent of 'fRadians'.


Parameter Explanation
fRadians The number of radians to convert into degrees.


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


sgn

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


Parameter Explanation
input The number to test the sign of


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)
Returns the sine of a number of radians.


Parameter Explanation
fRadians The number to take the sine of (in radians).


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

sinDegrees

float sinDegrees(const float fDegrees)
Returns the sine of a number of degrees.


Parameter Explanation
fDegrees The number to take the sine of (in degrees).


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

sqrt

float sqrt(const float input)
Returns the square-root of 'input'.


Parameter Explanation
input The number to take the square-root of.


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