{
  "id": "law-of-cosines",
  "title": "Law of Cosines",
  "lang": "en",
  "source_lang": "en",
  "summary": "The law of cosines relates the three side lengths of a triangle to the cosine of one of its angles, generalizing the Pythagorean theorem.",
  "category": "mathematics",
  "subcategory": "geometry",
  "type": "concept",
  "aliases": [
    "Cosine rule",
    "Cosine law",
    "Al-Kashi's theorem"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/law-of-cosines/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/law-of-cosines/",
      "json": "https://aiwiki.page/zh/law-of-cosines.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "The law of cosines is a theorem in Euclidean geometry relating the lengths of a triangle’s sides to the cosine of one of its angles. If a triangle has side lengths \\(a\\), \\(b\\), and \\(c\\), opposite angles \\(A\\), \\(B\\), and \\(C\\), respectively, then\n\n\\[c^2=a^2+b^2-2ab\\cos C.\\]\n\nHere \\(C\\) is the angle between the sides of lengths \\(a\\) and \\(b\\). The theorem applies to acute, right, and obtuse triangles and generalizes the Pythagorean theorem. It is a basic tool in trigonometry for finding an unknown side from two sides and their included angle, or an unknown angle from three sides."
    },
    {
      "heading": "Statement and interpretation",
      "text": "For a nondegenerate triangle, the side lengths are positive and each interior angle lies strictly between \\(0\\) and \\(\\pi\\) radians. The three equivalent forms of the law are\n\n\\[\\begin{aligned} a^2&=b^2+c^2-2bc\\cos A,\\\\ b^2&=c^2+a^2-2ca\\cos B,\\\\ c^2&=a^2+b^2-2ab\\cos C. \\end{aligned}\\]\n\nEach formula uses two sides and the angle between those sides to determine the opposite side. Substituting a nonincluded angle generally gives an incorrect result.\n\nThe term \\(-2ab\\cos C\\) describes the departure from the right-angle case:\n\n- If \\(C<90^\\circ\\), then \\(\\cos C>0\\), so \\(c^2<a^2+b^2\\).\n- If \\(C=90^\\circ\\), then \\(\\cos C=0\\), so \\(c^2=a^2+b^2\\).\n- If \\(C>90^\\circ\\), then \\(\\cos C<0\\), so \\(c^2>a^2+b^2\\).\n\nThus the theorem gives both the Pythagorean theorem and its converse for a right triangle.\n\nThe limiting cases also have geometric meaning. When \\(C=0\\), the formula gives \\(c=|a-b|\\); when \\(C=\\pi\\), it gives \\(c=a+b\\). These are degenerate configurations in which the vertices lie on a straight line."
    },
    {
      "heading": "Proofs",
      "text": "Coordinate proof\n\nChoose a Cartesian coordinate system with the vertex of angle \\(C\\) at the origin. Place the other two vertices at\n\n\\[P=(b,0), \\qquad Q=(a\\cos C,a\\sin C).\\]\n\nThe side opposite \\(C\\) is the segment \\(PQ\\). By the formula for Euclidean distance,\n\n\\[\\begin{aligned} c^2 &=(a\\cos C-b)^2+(a\\sin C)^2\\\\ &=a^2\\cos^2 C-2ab\\cos C+b^2+a^2\\sin^2 C\\\\ &=a^2+b^2-2ab\\cos C, \\end{aligned}\\]\n\nusing the identity \\(\\sin^2 C+\\cos^2 C=1\\) for the trigonometric functions.\n\nThis proof handles acute and obtuse angles without separate cases because the coordinate \\(a\\cos C\\) can be positive, zero, or negative.\n\nVector proof\n\nLet \\(\\mathbf u\\) and \\(\\mathbf v\\) be vectors from the vertex of angle \\(C\\) to the other two vertices, with lengths \\(a\\) and \\(b\\). The opposite side has length \\(\\|\\mathbf u-\\mathbf v\\|\\). Expanding the dot product gives\n\n\\[\\begin{aligned} c^2 &=(\\mathbf u-\\mathbf v)\\cdot(\\mathbf u-\\mathbf v)\\\\ &=\\|\\mathbf u\\|^2+\\|\\mathbf v\\|^2-2\\mathbf u\\cdot\\mathbf v\\\\ &=a^2+b^2-2ab\\cos C. \\end{aligned}\\]\n\nThe same argument works in any real inner product space, with the angle between nonzero vectors defined by\n\n\\[\\cos C=\\frac{\\langle\\mathbf u,\\mathbf v\\rangle} {\\|\\mathbf u\\|\\|\\mathbf v\\|}.\\]\n\nConsequently, the identity is not restricted to a two-dimensional coordinate system, although any three noncollinear points in Euclidean space lie in a plane."
    },
    {
      "heading": "Solving triangles",
      "text": "Finding a side\n\nWhen two side lengths and their included angle are known, the opposite side is\n\n\\[c=\\sqrt{a^2+b^2-2ab\\cos C}.\\]\n\nFor example, if \\(a=5\\), \\(b=7\\), and \\(C=60^\\circ\\), then\n\n\\[c^2=25+49-70\\left(\\frac12\\right)=39,\\]\n\nso\n\n\\[c=\\sqrt{39}\\approx6.245.\\]\n\nTwo positive sides and an included angle strictly between \\(0\\) and \\(\\pi\\) determine a triangle uniquely up to congruence, allowing reflection.\n\nFinding an angle\n\nRearranging the law gives\n\n\\[\\cos C=\\frac{a^2+b^2-c^2}{2ab}.\\]\n\nHence\n\n\\[C=\\arccos\\left(\\frac{a^2+b^2-c^2}{2ab}\\right),\\]\n\nwhere \\(\\arccos\\) is the inverse cosine with values in \\([0,\\pi]\\).\n\nFor a triangle with side lengths \\(5\\), \\(6\\), and \\(7\\), the angle opposite the side of length \\(7\\) satisfies\n\n\\[\\cos C=\\frac{25+36-49}{2\\cdot5\\cdot6}=\\frac15,\\]\n\ngiving \\(C\\approx78.46^\\circ\\).\n\nThree positive lengths form a nondegenerate triangle precisely when they satisfy the strict triangle inequalities:\n\n\\[a<b+c,\\qquad b<c+a,\\qquad c<a+b.\\]\n\nThese conditions ensure that each computed cosine lies strictly between \\(-1\\) and \\(1\\). Arbitrary positive lengths need not describe a triangle."
    },
    {
      "heading": "Consequences and applications",
      "text": "Classifying a triangle from its sides\n\nLet \\(c\\) be the longest side. Since \\(C\\) is then the largest angle, the law gives a complete classification:\n\n\\[\\begin{array}{ll} c^2<a^2+b^2 &: \\text{acute triangle},\\\\ c^2=a^2+b^2 &: \\text{right triangle},\\\\ c^2>a^2+b^2 &: \\text{obtuse triangle}. \\end{array}\\]\n\nFor an equilateral triangle, setting \\(a=b=c\\) gives \\(\\cos C=1/2\\), so each angle is \\(60^\\circ\\).\n\nDistances and vector magnitudes\n\nThe theorem calculates the distance between two points when their distances from a common observation point and the angle between their directions are known. This is useful in surveying, navigation, and geometric reconstruction.\n\nIn polar coordinates, points with coordinates \\((r_1,\\theta_1)\\) and \\((r_2,\\theta_2)\\) have separation\n\n\\[d^2=r_1^2+r_2^2-2r_1r_2\\cos(\\theta_1-\\theta_2).\\]\n\nA related vector identity,\n\n\\[\\|\\mathbf u+\\mathbf v\\|^2 =\\|\\mathbf u\\|^2+\\|\\mathbf v\\|^2 +2\\|\\mathbf u\\|\\|\\mathbf v\\|\\cos\\theta,\\]\n\ndetermines the magnitude of a resultant when two vectors and the angle between them are known. It is widely used for forces, velocities, and displacements in classical mechanics.\n\nRecovering inner products from distances\n\nRearranging the vector form yields\n\n\\[\\langle\\mathbf u,\\mathbf v\\rangle =\\frac{\\|\\mathbf u\\|^2+\\|\\mathbf v\\|^2-\\|\\mathbf u-\\mathbf v\\|^2}{2}.\\]\n\nThus lengths and pairwise distances determine inner products. This identity underlies methods that reconstruct geometric configurations from distance data.\n\nFor unit vectors, it becomes\n\n\\[\\|\\mathbf u-\\mathbf v\\|^2=2-2\\cos\\theta.\\]\n\nConsequently, Euclidean distance and cosine similarity are directly related when vectors have been normalized."
    },
    {
      "heading": "Numerical considerations",
      "text": "A mathematically correct formula can lose accuracy when evaluated using floating-point arithmetic. In particular, if \\(a\\) and \\(b\\) are nearly equal and \\(C\\) is small, the expression\n\n\\[a^2+b^2-2ab\\cos C\\]\n\nsubtracts nearly equal quantities. This can cause substantial cancellation.\n\nAn equivalent expression is\n\n\\[c^2=(a-b)^2+4ab\\sin^2\\left(\\frac C2\\right).\\]\n\nIt avoids that particular subtraction and is often more suitable for small angles, although overflow, underflow, and the accuracy of the input data still matter.\n\nWhen recovering an angle, rounding may produce a cosine value slightly outside \\([-1,1]\\). Restricting such a value to that interval can handle small rounding errors, but should not conceal side lengths that genuinely violate the triangle inequalities.\n\nAngle recovery is also sensitive near \\(0\\) and \\(\\pi\\), because\n\n\\[\\frac{d}{dx}\\arccos x=-\\frac{1}{\\sqrt{1-x^2}}.\\]\n\nThe magnitude of this derivative becomes large near \\(x=\\pm1\\). This sensitivity is an intrinsic conditioning issue, distinct from the numerical stability of a particular implementation."
    },
    {
      "heading": "Historical development",
      "text": "Geometric equivalents of the law appear in Book II of Euclid’s *Elements*, compiled around 300 BCE. Propositions 12 and 13 treat obtuse and acute triangles through relations among squares, rectangles, and projections. They do not use modern trigonometric notation.\n\nThe development of trigonometric calculation later made the relation a practical tool for solving triangles. The fifteenth-century mathematician Jamshīd al-Kāshī gave a formulation equivalent to the modern rule. The name Al-Kashi’s theorem remains in use in some mathematical traditions, especially French-language education."
    },
    {
      "heading": "Analogues in curved geometry",
      "text": "The Euclidean formula does not apply unchanged to triangles whose sides are curved-surface shortest paths. In non-Euclidean geometry, corresponding cosine laws incorporate curvature.\n\nFor a spherical triangle on a sphere of radius \\(R\\), with sides formed by shorter great-circle arcs of lengths \\(a\\), \\(b\\), and \\(c\\),\n\n\\[\\cos\\left(\\frac cR\\right) = \\cos\\left(\\frac aR\\right)\\cos\\left(\\frac bR\\right) + \\sin\\left(\\frac aR\\right)\\sin\\left(\\frac bR\\right)\\cos C.\\]\n\nFor a triangle in the hyperbolic plane of constant curvature \\(-1/R^2\\),\n\n\\[\\cosh\\left(\\frac cR\\right) = \\cosh\\left(\\frac aR\\right)\\cosh\\left(\\frac bR\\right) - \\sinh\\left(\\frac aR\\right)\\sinh\\left(\\frac bR\\right)\\cos C.\\]\n\nIn both formulas, \\(C\\) is the included angle between the sides of lengths \\(a\\) and \\(b\\). Expanding the functions for side lengths small compared with \\(R\\) recovers the Euclidean law of cosines at leading order."
    }
  ],
  "links": [
    "euclidean-geometry",
    "pythagorean-theorem",
    "trigonometry",
    "right-triangle",
    "cartesian-coordinate-system",
    "euclidean-distance",
    "trigonometric-functions",
    "dot-product",
    "inner-product",
    "triangle-inequality",
    "polar-coordinates",
    "classical-mechanics",
    "cosine-similarity",
    "floating-point-arithmetic",
    "derivative",
    "numerical-stability",
    "euclids-elements",
    "non-euclidean-geometry"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}