Skip to content

Core\Math::sin

Returns the sine of an angle. The angle is in radians, and Core\Math::toRadians converts degrees to radians. The result is always from -1.0 to 1.0. An infinity or NaN (a value that means "not a number") gives NaN.

In plain words: draw a line of length 1 from the centre of a circle, at the angle you give. The sine is how far the end of that line is above the centre.

Good to know: the result is not always exact. The sine of 180 degrees is zero, but the result is a tiny number close to zero. Round the result when you show it.

The examples below show the sines of a few angles, then angles in degrees and rounding, then how high a ramp rises.

Parameters

  • $radiansfloatThe angle, in radians. Core\Math::toRadians converts degrees to radians.

Return value

Returns float. A number from -1.0 to 1.0. An infinity or NaN gives NaN.

Examples

Sines of angles

<?nvs
// Shows the sines of a few angles in radians.
// The sine of 0 is 0. This prints 0.
echo Core\Math::sin(0.0), "\n";
// `Core\Math::PI` radians is half a circle, so `PI / 2` is a quarter circle. This prints 1.
echo Core\Math::sin(Core\Math::PI / 2.0), "\n";
// A negative angle gives the same result with the other sign.
// These lines print 0.8414709848079 and -0.8414709848079.
echo Core\Math::sin(1.0), "\n";
echo Core\Math::sin(-1.0), "\n";
Output
0
1
0.8414709848079
-0.8414709848079

Degrees and rounding

<?nvs
// Shows how to use an angle in degrees, and why you round the result.
// `Core\Math::toRadians` converts 30 degrees to radians. This prints 0.5.
echo Core\Math::sin(Core\Math::toRadians(30.0)), "\n";
// The sine of 180 degrees is zero, but the result is a tiny number.
// This prints 1.2246467991474E-16.
echo Core\Math::sin(Core\Math::toRadians(180.0)), "\n";
// Rounding to 10 digits after the point gives zero. This prints 0.
echo Core\Math::round(Core\Math::sin(Core\Math::toRadians(180.0)), { precision: 10 }), "\n";
Output
0.5
1.2246467991474E-16
0

How high a ramp rises

<?nvs
// Shows how a building tool finds how high a ramp rises.
class Ramp
{
// The height is the length of the ramp times the sine of its angle.
public static function rise(float $meters, float $angleDegrees): float
{
float $height = $meters * Core\Math::sin(Core\Math::toRadians($angleDegrees));
return Core\Math::round($height, { precision: 2 });
}
}
// A flat ramp does not rise. This prints 0.
echo Ramp::rise(12.0, 0.0), "\n";
// A ramp of 12 meters at an angle of 5 degrees. This prints 1.05.
echo Ramp::rise(12.0, 5.0), "\n";
// A ramp of 3 meters at 30 degrees. This prints 1.5.
echo Ramp::rise(3.0, 30.0), "\n";
Output
0
1.05
1.5