Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pymath/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from pymath.arithmetic import add, subtract, multiply, divide

__all__ = ["add", "subtract", "multiply", "divide"]
27 changes: 27 additions & 0 deletions pymath/arithmetic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# mathlib/arithmetic.py


def add(a: float, b: float) -> float:
"""Return the sum of a and b."""
return a + b


def subtract(a: float, b: float) -> float:
"""Return a minus b."""
return a - b


def multiply(a: float, b: float) -> float:
"""Return a multiplied by b."""
return a * b


def divide(a: float, b: float) -> float:
"""Return a divided by b.

Raises:
ValueError: if b is zero.
"""
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build-system]
requires = ["setuptools>=68"]
build-backend = "etuptools.build_meta"
build-backend = "setuptools.build_meta"

[project]
name = "mathlib"
Expand Down
42 changes: 42 additions & 0 deletions test/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# tests/test_arithmetic.py
import pytest
from pymath import add, subtract, multiply, divide


class TestAdd:
def test_positive_numbers(self):
assert add(2, 3) == 5

def test_negative_numbers(self):
assert add(-1, -2) == -3

def test_floats(self):
assert add(0.1, 0.2) == pytest.approx(0.3) # ← important!


class TestSubtract:
def test_basic(self):
assert subtract(10, 4) == 6

def test_result_is_negative(self):
assert subtract(2, 5) == -3


class TestMultiply:
def test_basic(self):
assert multiply(3, 4) == 12

def test_by_zero(self):
assert multiply(99, 0) == 0


class TestDivide:
def test_basic(self):
assert divide(10, 2) == 5.0

def test_returns_float(self):
assert isinstance(divide(7, 2), float)

def test_divide_by_zero_raises(self):
with pytest.raises(ValueError, match="Cannot divide by zero"):
divide(5, 0)
Loading