As far as I see, your formulae are correct. But for large or acute triangle you're possibly outside the precision range of var variables. Better use float for trigonometry.
If a formula does not give the desired result, split it in sub-steps and check in which of the steps the result becomes wrong. You must do this yourself - find the reason and fix it. This does not belong to the bug forum. Blaming the programming language for wrong results of your script won't help.