geometry · MIT License
Overview
Solve any side or angle of a triangle with the law of cosines and validate triangle inputs.
- Tested on a TI calculator
- Downloaded 45 times
- Version 1.0.0
- 7413 bytes
- Uses math
Tried it? Did this program work on your calculator?
Before you run it
- TI-84 Plus CE Python · tested on OS 5.8.3, Python App 5.8.3.0048
- TI Connect CE .py conversion to a Python AppVar in RAM; owner physically verified the supplied program.
- Supplied values describe one nondegenerate triangle.
- Sides are positive; angles are strictly between 0 and 180 degrees.
- Invalid triangle side sets are identified.
Try the DuckieDai calculator screen
This browser preview calculates side c from sides a and b and included angle C in degrees. The TI program also solves the other sides and all angles.
TI-84 Plus CE Python · screen preview
Learn the math
How the law of cosines works
The idea
The law of cosines is the Pythagorean theorem with a correction for triangles that have no right angle. It connects the three sides to one angle.
Use it when you know two sides and the angle between them, or all three sides.
The formula
c^2 = a^2 + b^2 - 2*a*b*cos(C)
a, b, c- side lengths
A, B, C- the angles opposite those sides, in degrees
Using the program
- 1 to 3 find a side from the other two sides and the angle between them.
- 4 to 6 find an angle from all three sides.
- The program checks that your numbers can form a triangle.
Worked example
Two sides of a triangle are 5 and 7 with a 60 degree angle between them. Choose 3 and enter 5, 7, and 60.
c^2 = 25 + 49 - 2 x 5 x 7 x cos(60) = 39
The program shows c = 6.24 units (it prints 6.244997998398397).
Tips and common mistakes
- Angle C is opposite side c. Matching letters is where most errors happen.
- Angles are in degrees.
- With a 90 degree angle the cosine term vanishes and you are back to a^2 + b^2 = c^2.
Where this comes up
It belongs to the non-right-triangle unit of trigonometry and precalculus, alongside the law of sines. Physics uses it to add two vectors that are not perpendicular.
Exact reviewed bytes
Source code
"""DuckieDai Law of Cosines for TI-84 Plus CE Python."""from math import sqrt, cos, acos, radians, degreesdef duckiedai_intro(program_name, wait=True):"""Show the reusable DuckieDai program opening."""title = program_name[:16]empty = 16 - len(title)left = empty // 2right = empty - leftprint("+----------------------+")print("| " + " " * left + title + " " * right + " |")print("| |")print("| __ |")print("| ___(o )> quack! |")print("| \\ <_. ) |")print("| `---' |")print("| by DuckieDai |")print("+----------------------+")print("Loading...")if wait:input("Press enter to start ")print("\n" * 6)def positive_number(prompt):"""Require a positive number."""while True:try:value = float(input(prompt))if value > 0 and value < 1e100:return valueexcept ValueError:passprint("Enter a number above 0.")def angle_number(prompt):"""Require an angle between 0 and 180 degrees."""while True:try:value = float(input(prompt))if value > 0 and value < 180:return valueexcept ValueError:passprint("Angle must be")print("between 0 and 180.")def valid_triangle(a, b, c):"""Check triangle inequality."""return (a + b > c anda + c > b andb + c > a)def safe_acos(value):"""Protect against tiny rounding errors."""if value > 1:value = 1if value < -1:value = -1return acos(value)def show_answer(name, value, unit):print()print("ANSWER")print(name + " = " + str(value))print(unit)def main():duckiedai_intro("LAW OF COSINES")print("LAW OF COSINES")print()print("c^2 = a^2 + b^2")print(" -2ab*cos(C)")print()print("What do you need?")print()print("1. Side a")print("2. Side b")print("3. Side c")print("4. Angle A")print("5. Angle B")print("6. Angle C")print()choice = input("Choose 1-6: ")print()# ==========================================# 1. SOLVE SIDE a## a^2 = b^2 + c^2 - 2bc*cos(A)# ==========================================if choice == "1":b = positive_number("Side b: ")c = positive_number("Side c: ")A = angle_number("Angle A (deg): ")value = (b ** 2 +c ** 2 -2 * b * c * cos(radians(A)))if value <= 0:print("No valid triangle.")else:a = sqrt(value)print()print("Using:")print("a^2=b^2+c^2")print(" -2bc*cos(A)")show_answer("a",a,"units")# ==========================================# 2. SOLVE SIDE b## b^2 = a^2 + c^2 - 2ac*cos(B)# ==========================================elif choice == "2":a = positive_number("Side a: ")c = positive_number("Side c: ")B = angle_number("Angle B (deg): ")value = (a ** 2 +c ** 2 -2 * a * c * cos(radians(B)))if value <= 0:print("No valid triangle.")else:b = sqrt(value)print()print("Using:")print("b^2=a^2+c^2")print(" -2ac*cos(B)")show_answer("b",b,"units")# ==========================================# 3. SOLVE SIDE c## c^2 = a^2 + b^2 - 2ab*cos(C)# ==========================================elif choice == "3":a = positive_number("Side a: ")b = positive_number("Side b: ")C = angle_number("Angle C (deg): ")value = (a ** 2 +b ** 2 -2 * a * b * cos(radians(C)))if value <= 0:print("No valid triangle.")else:c = sqrt(value)print()print("Using:")print("c^2=a^2+b^2")print(" -2ab*cos(C)")show_answer("c",c,"units")# ==========================================# ANGLES REQUIRE ALL 3 SIDES# ==========================================elif choice == "4" or \choice == "5" or \choice == "6":a = positive_number("Side a: ")b = positive_number("Side b: ")c = positive_number("Side c: ")if not valid_triangle(a, b, c):print()print("INVALID TRIANGLE")print()print("These sides cannot")print("form a triangle.")# ======================================# 4. SOLVE ANGLE A## cos(A) =# (b^2+c^2-a^2)/(2bc)# ======================================elif choice == "4":value = (b ** 2 +c ** 2 -a ** 2) / (2 * b * c)A = degrees(safe_acos(value))print()print("Using:")print("cos(A)=")print("(b^2+c^2-a^2)")print("/(2bc)")show_answer("A",A,"degrees")# ======================================# 5. SOLVE ANGLE B# ======================================elif choice == "5":value = (a ** 2 +c ** 2 -b ** 2) / (2 * a * c)B = degrees(safe_acos(value))print()print("Using:")print("cos(B)=")print("(a^2+c^2-b^2)")print("/(2ac)")show_answer("B",B,"degrees")# ======================================# 6. SOLVE ANGLE C# ======================================else:value = (a ** 2 +b ** 2 -c ** 2) / (2 * a * b)C = degrees(safe_acos(value))print()print("Using:")print("cos(C)=")print("(a^2+b^2-c^2)")print("/(2ab)")show_answer("C",C,"degrees")else:print("Invalid choice.")print()print("DuckieDai says: done!")# TI-Python imports the selected AppVar rather than# setting __name__ to main.main()
- Filename
COSLAW.py- Size
- 7413 bytes
- SHA-256
7dfce30271f47a7e54da6d412b332da2c91fe0a3695c6a6482257b3aedb045a6
Usage
Inputs
- Requested side or angle
- Two sides and included angle, or all three positive side lengths
Outputs
- Requested side or angle in degrees
Units: Use one consistent length unit; angles are degrees.
Assumptions and limitations
Assumptions
- Supplied values describe one nondegenerate triangle.
Constraints
- Sides are positive; angles are strictly between 0 and 180 degrees.
Known failures
- Invalid triangle side sets are identified.
- Inconsistent length units produce an incorrect result.
Compatibility and review
- Review status
- Tested on a TI calculator
- Tested on
- TI-84 Plus CE Python; OS 5.8.3; Python App 5.8.3.0048; exact source owner-tested through TI Connect CE
- What happened
- Owner confirmed the supplied law of cosines program runs on the physical calculator.
- Dependencies
- math
- Suggested calculator name
COSLAW— you can give it another valid, unique name when you transfer it- Desktop test cases
- Not available for this interactive-only source
This exact source auto-launches an interactive calculator session, so compatibility evidence comes from the recorded physical-device review rather than an importable desktop fixture.
Transfer and launch
Download the .py file, send it to your calculator with TI Connect CE as COSLAW or another name, then open the Python App and try the worked example on this page first.
Full installation and troubleshooting guide. Check your teacher's or exam's rules before using any program.
Version history
- 1.0.0 — current version, published . Download this reviewed version
Related learning
Did a DuckieDai program help with your homework? Every program here is free. If you would like to say thanks, you can leave a tip on Cash App ($duckiedai214) or read what tips pay for.
Independent compatibility resource: Hidden Word Scanner is independent and is not affiliated with or endorsed by Texas Instruments. TI product names describe compatibility only.
Action-count privacy: Successful downloads, preview starts, and preview completions increment same-origin aggregate counters, and so does a Yes or No answer about whether the program worked. Your browser notes that you answered; that note is never sent to the site. No entered values, source text, IP address, account, cookie, device identifier, or fingerprint is stored with these counts.