Method and source check
Last checked:
Automated source and implementation review, including worked examples and documented numerical limits. This review does not provide professional advice or specialist approval.
Method
For a right triangle, square and add the legs to find the hypotenuse, or subtract the square of the known leg from the hypotenuse squared to find the missing leg. Reject nonpositive lengths and a hypotenuse no larger than the known leg. Use Math.hypot for the hypotenuse and a factored difference for a leg to avoid unnecessary square overflow.
c² = a² + b²
a = √(c² − b²)
Variables and units
- a,b
- positive perpendicular leg lengths
- c
- positive hypotenuse length
Worked example
With legs 3 and 4, c = √(9+16) = 5. With c=13 and b=5, a=12.
Limitations
Displayed side lengths and worked calculation are rounded to six decimal places. When scaling for Math.round would overflow, the finite representable value is retained.
- Applies only to right triangles.
- Irrational side lengths are shown approximately.
- Unsupported derived numerical ranges are rejected with a localized error on a visible form field; the calculations use floating-point arithmetic.
- 3,4,5 and 5,12,13 are exact right-triangle examples.
- The hypotenuse must be longer than either leg.
- A 3e154 by 4e154 right-angle displacement yields a finite 5e154 length.