logo

Javascript Math

Overview: Learn how to use the javascript math library: sqrt, sin, arcsin, cos, arccos, tan, arc tan, radians to degrees, degrees to radians, exponential powers, round, absolute numbers

function getSqrt(aNumber)
{	
	var val;
	val=Math.sqrt(aNumber);
	return(val);
}
function getSin(opp,hyp)
{
	var val;
	
	if (hyp==0)
	{
		val=0;
	}
	else
	{
		val=eval(opp/hyp);
	}
	return(val);
}
function getArcSin(val)
{
	var val;
	val=eval(Math.asin(val));
	return(val);
}
function getSinAngle(opp,hyp)
{
	var val;
	
	if (hyp==0)
	{
		val=0;
	}
	else
	{
		val=eval(convertToDegrees(getArcSin(opp/hyp)));
	}
	return(val);
}
function getCos(adj,hyp)
{
	var val;
	
	if (hyp==0)
	{
		val=0;
	}
	else
	{
		val=eval(adj/hyp);
	}
	return(val);
}
function getArcCos(val)
{
	var val;
	val=eval(Math.acos(val));
	return(val);
}
function getCosAngle(adj,hyp)
{
	var val;
	
	if (hyp==0)
	{
		val=90;
	}
	else
	{
		val=eval(convertToDegrees(getArcCos(adj/hyp)) );
	}
	return(val);
}
function getTan(opp,adj)
{
	var val;
	
	if (adj==0)
	{
		val=0;
	}
	else
	{
		val=eval(opp/adj);
	}
	return(val);
}
function getArcTan(val)
{
	var val;
	
	val=eval(Math.atan(val));

	return(val);
}
function getTanAngle(opp,adj)
{
	var val;
	
	if (adj==0)
	{
		val=0;
	}
	else
	{
		val=eval(convertToDegrees(getArcTan(opp/adj)) );
	}
	return(val);
}
function convertToDegrees(val)
{
	val=val*(180/Math.PI);
	return(val);
}
function lawOfCosine(a,b,c)
{
	/*c2=a2+b2-2ab Cos C*/
	val=convertToDegrees(getArcCos(eval((-1*Math.pow(c,2) + Math.pow(a,2) + Math.pow(b,2))/(2*a*b) )));
	return(val);
}
function getPow(val,exponent)
{
	val=Math.pow(val,exponent);
	return(val);
}
function getRound(val)
{
	val=Math.round(val);
	return(val);
}
function getAbs(val)
{
	val=Math.abs(val);
	return(val);
}
s