A lightweight, header-only C++23 library for working with complex numbers using modern C++ features.
ComplexNumber is a personal C++ project focused on template programming, operator overloading, type safety, and clean library design.
- Header-only: Easy to integrate into existing C++ projects.
- C++23: Uses modern C++ language features.
- Template-based: Supports floating-point types such as
float,double, andlong double. - Operator overloading: Supports addition, subtraction, multiplication, and division of complex numbers.
- Compound assignment: Supports
+=,-=,*=, and/=. - Type constraints: Uses the
std::floating_pointconcept to restrict supported types. - Comparison operators: Supports equality (
==) and inequality (!=). - Accessors and setters: Provides access to and modification of real and imaginary components.
- Stream operators: Supports input (
>>) and output (<<). - Complex conjugate: Provides a
conj()member function. - CMake integration: Supports configuration and builds through CMake presets.
- Testing: Includes a test source file and CTest integration.
- Compiler support: Configured for Clang and GCC toolchains.
Note: Division by zero and extreme floating-point values are not explicitly handled. Stream output formatting for negative imaginary components can also be improved.
ComplexNumber/
├── include/
│ └── complex.hpp
├── examples/
│ └── main.cpp
├── tests/
│ └── complex_test.cpp
├── .gitignore
├── CMakeLists.txt
├── CMakePresets.json
├── LICENSE
└── README.md
- A C++23-compatible compiler:
- Clang
- GCC
- CMake 3.28 or newer
- Ninja build system
git clone https://github.com/CodeWithMSami/ComplexNumber.git
cd ComplexNumberConfigure using the Clang Debug preset:
cmake --preset clang-debugAlternatively, use the GCC Release preset:
cmake --preset gcc-releaseFor Clang:
cmake --build --preset clang-debugFor GCC:
cmake --build --preset gcc-releasectest --preset clang-debugThe test source covers constructors, accessors, setters, arithmetic operators, compound assignments, comparisons, stream operations, and complex conjugation. Division tests currently cover ordinary, nonzero divisors.
Include the library header in your C++ project:
#include <complex.hpp>The Complex<T> class provides constructors, accessors, setters, arithmetic and compound assignment operators, equality comparisons, stream input/output, and complex conjugation.
The header is intended to be exposed through the CMake target ComplexNumber, allowing consuming targets to inherit its include directory and C++23 requirement.
- Implement the templated
Complex<T>class. - Add constructors and accessors.
- Implement arithmetic operators.
- Implement compound assignment operators.
- Add equality comparisons.
- Add stream input and output support.
- Implement complex conjugation.
- Add an example program and test source.
The project aims to demonstrate:
- Modern C++23 programming.
- Generic programming with templates and concepts.
- Operator overloading and const correctness.
- Reusable header-only library design.
- CMake-based build management.
- Testing and documentation practices.
Suggestions, bug reports, and improvements are welcome. Please open an issue to discuss substantial changes before submitting a pull request.
This project is licensed under the MIT License. See LICENSE for details.
CodeWithMSami
GitHub: @CodeWithMSami
Built as a modern C++ showcase project.