Define Acos trig function for consine law

Hey, I am trying to find the angles of a triangle with the lengths of the sides. I already tried using math.acos() to invert the cos in order to find the angle, however the acos always comes back as nan. any fixes or help would be apreciated.

local point = game.Workspace.triangle.point
local point0 = game.Workspace.triangle.point0
local point1 = game.Workspace.triangle.point1

local pointpos =point.Position
local pointpos0 =point0.Position
local pointpos1 =point1.Position
--Keep in mind Z=Y axis because roblox is 3-dimensional
local pointX = pointpos.X
local pointY = pointpos.Z

local point0X = pointpos0.X
local point0Y = pointpos0.Z

local point1X = pointpos1.X
local point1Y = pointpos1.Z

local length = 0
local length0 = 0
local length1= 0

local angle = 0
local angle0 = 0
local angle1 = 0

local function cosinelaw (a,b,c,angle)
print(c)
print(b)
print(a)
	print(math.acos(a^2+b^2-c^2/2*a*b))
	print(tostring(math.cos(2)))
if angle ==1 then 
	
elseif angle ==2 then
	
elseif angle == 3 then
	
	end
end

local function calcdistance(x1,y1,x2,y2,side)
	--print("length: "..math.sqrt((x1-x2)^2+(y1-y2)^2))
if side == 1 then
		length = math.sqrt((x1-x2)^2+(y1-y2)^2)
elseif side == 2 then
		length0 = math.sqrt((x1-x2)^2+(y1-y2)^2)
elseif side ==3 then 
		length1 = math.sqrt((x1-x2)^2+(y1-y2)^2)
 end
end

--point and point0
calcdistance(pointX,pointY,point0X,point0Y,1)
--point and point1
calcdistance(pointX,pointY,point1X,point1Y,2)
--point0 and point1
calcdistance(point0X,point0Y,point1X,point1Y,3)


wait(1)
print(length)
print(length0)
print(length1)
wait(4)
cosinelaw(tostring(length),tostring(length0),tostring(length1),1)
type or paste code here

Why are you converting the numbers to strings when passing it into this function?

cosinelaw(tostring(length),tostring(length0),tostring(length1),1)

debug i tried. i found out how to do it though

`-- References to the points in the workspace
local point = game.Workspace.triangle.point
local point0 = game.Workspace.triangle.point0
local point1 = game.Workspace.triangle.point1

– Positions of the points
local pointpos = point.Position
local pointpos0 = point0.Position
local pointpos1 = point1.Position

– Coordinates of the points (note Z is Y axis in Roblox)
local pointX = pointpos.X
local pointY = pointpos.Z

local point0X = pointpos0.X
local point0Y = pointpos0.Z

local point1X = pointpos1.X
local point1Y = pointpos1.Z

– Distances between points
local length = 0
local length0 = 0
local length1 = 0

– Function to calculate distance between two points
local function calcdistance(x1, y1, x2, y2, side)
local distance = math.sqrt((x1 - x2)^2 + (y1 - y2)^2)
if side == 1 then
length = distance
elseif side == 2 then
length0 = distance
elseif side == 3 then
length1 = distance
end
end

– Calculate distances
calcdistance(pointX, pointY, point0X, point0Y, 1)
calcdistance(pointX, pointY, point1X, point1Y, 2)
calcdistance(point0X, point0Y, point1X, point1Y, 3)

– Function to calculate angle using the law of cosines
local function cosinelaw(a, b, c)
local cos_theta = (a^2 + b^2 - c^2) / (2 * a * b)
if cos_theta >= -1 and cos_theta <= 1 then
return math.acos(cos_theta)
else
return nil – Return nil if cos_theta is out of valid range
end
end

– Calculate angles in radians
local angle = cosinelaw(length, length0, length1)
local angle0 = cosinelaw(length0, length1, length)
local angle1 = cosinelaw(length1, length, length0)

– Print distances
print("Length: " … length)
print("Length0: " … length0)
print("Length1: " … length1)

– Print angles in radians
print("Angle (radians): " … (angle or “Invalid”))
print("Angle0 (radians): " … (angle0 or “Invalid”))
print("Angle1 (radians): " … (angle1 or “Invalid”))

– Optional: Print angles in degrees
if angle then print("Angle (degrees): " … math.deg(angle)) end
if angle0 then print("Angle0 (degrees): " … math.deg(angle0)) end
if angle1 then print("Angle1 (degrees): " … math.deg(angle1)) end
`

I’m a bit confused though because (a^2 + b^2 - c^2) / (2 * a * b) should always be between -1 and 1. You shouldn’t have to explicitly check whether it’s between -1 and 1. I’m not sure why you’re getting values outside of that range unless you’re calculating the lengths of the sides incorrectly.

i just need a way to find the angles man

I understand lol. If it works, it works. All I’m just saying is something odd is going on if you’re getting values outside of [-1, 1].

yeah, thanks. ill keep you updated.