MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / area_triangle

Function area_triangle

maths/area.py:287–312  ·  view source on GitHub ↗

Calculate the area of a triangle given the base and height. >>> area_triangle(10, 10) 50.0 >>> area_triangle(1.6, 2.6) 2.08 >>> area_triangle(0, 0) 0.0 >>> area_triangle(-1, -2) Traceback (most recent call last): ... ValueError: area_tria

(base: float, height: float)

Source from the content-addressed store, hash-verified

285
286
287def area_triangle(base: float, height: float) -> float:
288 """
289 Calculate the area of a triangle given the base and height.
290
291 >>> area_triangle(10, 10)
292 50.0
293 >>> area_triangle(1.6, 2.6)
294 2.08
295 >>> area_triangle(0, 0)
296 0.0
297 >>> area_triangle(-1, -2)
298 Traceback (most recent call last):
299 ...
300 ValueError: area_triangle() only accepts non-negative values
301 >>> area_triangle(1, -2)
302 Traceback (most recent call last):
303 ...
304 ValueError: area_triangle() only accepts non-negative values
305 >>> area_triangle(-1, 2)
306 Traceback (most recent call last):
307 ...
308 ValueError: area_triangle() only accepts non-negative values
309 """
310 if base < 0 or height < 0:
311 raise ValueError("area_triangle() only accepts non-negative values")
312 return (base * height) / 2
313
314
315def area_triangle_three_sides(side1: float, side2: float, side3: float) -> float:

Callers 1

area.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected