Unofficial LSL Reference

[[functions:llcos]]


Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Function: llCos

float llCos(float arg)

Return the cosine of the argument. The argument should be an angle in radians.

Parameters

arg

A float with the angle whose cosine is to be found, in radians.

Return value

A float with the cosine of the argument.

Notes

  • Inputs of Infinity, -Infinity and NaN result in NaN.
  • If the angle is otherwise not between approx. -9.22337e18 and 9.22337e18, the argument is returned unchanged.
  • Otherwise, the return value is always between -1 and 1.

Short examples

float F;
F = llCos(1); // Sets F to approx. 0.540302, because that's the cosine of 1 radian.
F = llCos(-1); // Sets F to approx. 0.540302 too,
               // because the cosine of -1 radian equals the cosine of 1 radian.
F = llCos(0); // Sets F to 1, because that's the cosine of 0.
F = llCos(PI); // Sets F to -1, which is the cosine of PI.
F = llCos(1e20);  // Sets F to 1e20 because 1e20 is returned unchanged.

Complete examples

llCos is very frequently used together with llSin, as llCos gives the X component and llSin the Y component of a unit 2D vector at the given angle.

llSin-llCos-example.lsl
default
{
    state_entry()
    {
        llOwnerSay("Calculate a vector at an angle. Please say the angle in chat.");
        llListen(0, "", llGetOwner(), "");
    }

    listen(integer chan, string name, key id, string msg)
    {
        float angle = (float)msg * DEG_TO_RAD;
        llOwnerSay("Input angle: " + msg + "; resulting vector: ("
                   + (string)llCos(angle) + ", " + (string)llSin(angle) + ")");
    }
}

The opposite calculation, i.e. finding the angle of a 2D vector, can be performed with llAtan2.

See also

  • llSin calculates the sine of the argument.
  • llAcos calculates the inverse cosine of the argument.
  • llAtan2 calculates the angle of a 2D vector.

Other trigonometric functions

  • llAsin calculates the inverse sine of the argument.
  • llTan calculates the tangent of the argument.
  • float type and associated caveats and limitations.