Hidden WordScanner
Private scans · No account required

DuckieDai Law of Cosines

Solve any side or angle of a triangle with the law of cosines and validate triangle inputs.

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

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.

JavaScript is required for the optional browser simulation. Source viewing and downloading remain available without it.

Simulation boundary: This preview uses a reviewed browser adapter. It does not execute the downloaded Python and does not prove TI-Python or calculator compatibility.

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

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

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

Download .py

View source below, copy it when JavaScript is available, or download the exact .py bytes.

  1. """DuckieDai Law of Cosines for TI-84 Plus CE Python."""
  2. from math import sqrt, cos, acos, radians, degrees
  3. def duckiedai_intro(program_name, wait=True):
  4. """Show the reusable DuckieDai program opening."""
  5. title = program_name[:16]
  6. empty = 16 - len(title)
  7. left = empty // 2
  8. right = empty - left
  9. print("+----------------------+")
  10. print("| " + " " * left + title + " " * right + " |")
  11. print("| |")
  12. print("| __ |")
  13. print("| ___(o )> quack! |")
  14. print("| \\ <_. ) |")
  15. print("| `---' |")
  16. print("| by DuckieDai |")
  17. print("+----------------------+")
  18. print("Loading...")
  19. if wait:
  20. input("Press enter to start ")
  21. print("\n" * 6)
  22. def positive_number(prompt):
  23. """Require a positive number."""
  24. while True:
  25. try:
  26. value = float(input(prompt))
  27. if value > 0 and value < 1e100:
  28. return value
  29. except ValueError:
  30. pass
  31. print("Enter a number above 0.")
  32. def angle_number(prompt):
  33. """Require an angle between 0 and 180 degrees."""
  34. while True:
  35. try:
  36. value = float(input(prompt))
  37. if value > 0 and value < 180:
  38. return value
  39. except ValueError:
  40. pass
  41. print("Angle must be")
  42. print("between 0 and 180.")
  43. def valid_triangle(a, b, c):
  44. """Check triangle inequality."""
  45. return (
  46. a + b > c and
  47. a + c > b and
  48. b + c > a
  49. )
  50. def safe_acos(value):
  51. """Protect against tiny rounding errors."""
  52. if value > 1:
  53. value = 1
  54. if value < -1:
  55. value = -1
  56. return acos(value)
  57. def show_answer(name, value, unit):
  58. print()
  59. print("ANSWER")
  60. print(name + " = " + str(value))
  61. print(unit)
  62. def main():
  63. duckiedai_intro("LAW OF COSINES")
  64. print("LAW OF COSINES")
  65. print()
  66. print("c^2 = a^2 + b^2")
  67. print(" -2ab*cos(C)")
  68. print()
  69. print("What do you need?")
  70. print()
  71. print("1. Side a")
  72. print("2. Side b")
  73. print("3. Side c")
  74. print("4. Angle A")
  75. print("5. Angle B")
  76. print("6. Angle C")
  77. print()
  78. choice = input("Choose 1-6: ")
  79. print()
  80. # ==========================================
  81. # 1. SOLVE SIDE a
  82. #
  83. # a^2 = b^2 + c^2 - 2bc*cos(A)
  84. # ==========================================
  85. if choice == "1":
  86. b = positive_number("Side b: ")
  87. c = positive_number("Side c: ")
  88. A = angle_number("Angle A (deg): ")
  89. value = (
  90. b ** 2 +
  91. c ** 2 -
  92. 2 * b * c * cos(radians(A))
  93. )
  94. if value <= 0:
  95. print("No valid triangle.")
  96. else:
  97. a = sqrt(value)
  98. print()
  99. print("Using:")
  100. print("a^2=b^2+c^2")
  101. print(" -2bc*cos(A)")
  102. show_answer(
  103. "a",
  104. a,
  105. "units"
  106. )
  107. # ==========================================
  108. # 2. SOLVE SIDE b
  109. #
  110. # b^2 = a^2 + c^2 - 2ac*cos(B)
  111. # ==========================================
  112. elif choice == "2":
  113. a = positive_number("Side a: ")
  114. c = positive_number("Side c: ")
  115. B = angle_number("Angle B (deg): ")
  116. value = (
  117. a ** 2 +
  118. c ** 2 -
  119. 2 * a * c * cos(radians(B))
  120. )
  121. if value <= 0:
  122. print("No valid triangle.")
  123. else:
  124. b = sqrt(value)
  125. print()
  126. print("Using:")
  127. print("b^2=a^2+c^2")
  128. print(" -2ac*cos(B)")
  129. show_answer(
  130. "b",
  131. b,
  132. "units"
  133. )
  134. # ==========================================
  135. # 3. SOLVE SIDE c
  136. #
  137. # c^2 = a^2 + b^2 - 2ab*cos(C)
  138. # ==========================================
  139. elif choice == "3":
  140. a = positive_number("Side a: ")
  141. b = positive_number("Side b: ")
  142. C = angle_number("Angle C (deg): ")
  143. value = (
  144. a ** 2 +
  145. b ** 2 -
  146. 2 * a * b * cos(radians(C))
  147. )
  148. if value <= 0:
  149. print("No valid triangle.")
  150. else:
  151. c = sqrt(value)
  152. print()
  153. print("Using:")
  154. print("c^2=a^2+b^2")
  155. print(" -2ab*cos(C)")
  156. show_answer(
  157. "c",
  158. c,
  159. "units"
  160. )
  161. # ==========================================
  162. # ANGLES REQUIRE ALL 3 SIDES
  163. # ==========================================
  164. elif choice == "4" or \
  165. choice == "5" or \
  166. choice == "6":
  167. a = positive_number("Side a: ")
  168. b = positive_number("Side b: ")
  169. c = positive_number("Side c: ")
  170. if not valid_triangle(a, b, c):
  171. print()
  172. print("INVALID TRIANGLE")
  173. print()
  174. print("These sides cannot")
  175. print("form a triangle.")
  176. # ======================================
  177. # 4. SOLVE ANGLE A
  178. #
  179. # cos(A) =
  180. # (b^2+c^2-a^2)/(2bc)
  181. # ======================================
  182. elif choice == "4":
  183. value = (
  184. b ** 2 +
  185. c ** 2 -
  186. a ** 2
  187. ) / (2 * b * c)
  188. A = degrees(
  189. safe_acos(value)
  190. )
  191. print()
  192. print("Using:")
  193. print("cos(A)=")
  194. print("(b^2+c^2-a^2)")
  195. print("/(2bc)")
  196. show_answer(
  197. "A",
  198. A,
  199. "degrees"
  200. )
  201. # ======================================
  202. # 5. SOLVE ANGLE B
  203. # ======================================
  204. elif choice == "5":
  205. value = (
  206. a ** 2 +
  207. c ** 2 -
  208. b ** 2
  209. ) / (2 * a * c)
  210. B = degrees(
  211. safe_acos(value)
  212. )
  213. print()
  214. print("Using:")
  215. print("cos(B)=")
  216. print("(a^2+c^2-b^2)")
  217. print("/(2ac)")
  218. show_answer(
  219. "B",
  220. B,
  221. "degrees"
  222. )
  223. # ======================================
  224. # 6. SOLVE ANGLE C
  225. # ======================================
  226. else:
  227. value = (
  228. a ** 2 +
  229. b ** 2 -
  230. c ** 2
  231. ) / (2 * a * b)
  232. C = degrees(
  233. safe_acos(value)
  234. )
  235. print()
  236. print("Using:")
  237. print("cos(C)=")
  238. print("(a^2+b^2-c^2)")
  239. print("/(2ab)")
  240. show_answer(
  241. "C",
  242. C,
  243. "degrees"
  244. )
  245. else:
  246. print("Invalid choice.")
  247. print()
  248. print("DuckieDai says: done!")
  249. # TI-Python imports the selected AppVar rather than
  250. # setting __name__ to main.
  251. 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

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.