Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): add more tests & update README desc #12

Merged
merged 6 commits into from
Feb 26, 2024
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
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
.vscode/
build/
.idea

build/
.build_debug/*
.build_release/*
distribute/*
*.testbin
*.bin
cmake_build
.cmake_build
cmake-build-*
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ numbers
</h1>

[![CMake CI Matrix](https://github.com/guuzaa/numbers/actions/workflows/cmake.yml/badge.svg?branch=main)](https://github.com/guuzaa/numbers/actions/workflows/cmake.yml)
![Static Badge](https://img.shields.io/badge/Language-C++17-red)
![Static Badge](https://img.shields.io/badge/License-MIT-pink)
![Static Badge](https://img.shields.io/badge/OS-Linux-blue)
![Static Badge](https://img.shields.io/badge/OS-macOS-blue)

**Note: This project is in the early stages of development. The codebase is subject to significant changes and reorganization. Expect breaking changes as we refine the architecture, fix bugs, and implement new features.**

`numbers` is a library for C++17 and later versions that handles integer overflow similar to Rust. It simplifies integer computations and offers control over how to handle overflow situations.

Expand All @@ -12,29 +18,29 @@ numbers

- **Like Primitive Types**

- **Signed Integers**: Int8 Int16 Int32 Int64
- **Signed Integers**: int8 int16 int32 int64 int128

- **Unsigned Integers**: Uint8 Uint16 Uint32 Uint64
- **Unsigned Integers**: uint8 uint16 uint32 uint64

## How to use
## Usage

### operator +
```c++
Int8 a = 100_i8;
numbers::int8 a = 100;
std::cout << a << '\n';
try {
a = a + a;
std::cout << a << '\n';
} catch (std::runtime_error err) {
} catch (std::runtime_error &err) {
std::cout << "Catch error: " << err.what() << '\n';
}
```

### checked sub
```c++
Int8 a = Int8::MIN;
numbers::int8 a = numbers::int8::MIN;
std::cout << a << '\n';
std::optional<Int8> ret = a.checked_sub(1);
std::optional<numbers::int8> ret = a.checked_sub(1);
if (ret) {
std::cout << ret.value() << '\n';
} else {
Expand All @@ -44,8 +50,8 @@ if (ret) {

### overflowing div
```c++
Int16 a = 40_i16;
Int16 b = 2_i16;
numbers::int16 a = 40;
numbers::int16 b = 2;
auto [ret, overflowing] = a.overflowing_div(b);
std::cout <<"a= " << a << ", b= " << b << '\n';
if (!overflowing) {
Expand All @@ -57,9 +63,17 @@ if (!overflowing) {

### saturating mul
```c++
Int64 a = 40_i64;
Int64 b = Int64::MAX;
numbers::int64 a = 40;
numbers::int64 b = numbers::int64::MAX;
std::cout << "a= " << a << ", b= " << b << '\n';
Int64 ret = a.saturating_mul(b);
numbers::int64 ret = a.saturating_mul(b);
std::cout << ret << '\n';
```
```

## Contribute

We welcome contributions, but please be aware that the project's design and conventions are still evolving. If you'd like to contribute, it's a good idea to discuss your plans with the project maintainers before starting work.

For the latest updates and discussions, please see our [issues](./issues) and [pull requests](./pulls).

Stay tuned for more updates, and thank you for your interest in contributing to our project!
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(NumbersExample LANGUAGES CXX)

add_executable(${PROJECT_NAME} EXCLUDE_FROM_ALL main.cc)

target_link_libraries(${PROJECT_NAME} PRIVATE numbers_obj)

add_custom_target(example
COMMAND ${PROJECT_NAME}
DEPENDS ${PROJECT_NAME}
Expand Down
41 changes: 26 additions & 15 deletions examples/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

void checked_sub_example() {
std::cout << "==== checked_sub_example ==== \n";
Int8 a = Int8::MIN;
numbers::int8 a = numbers::int8::MIN;
std::cout << a << '\n';
std::optional<Int8> ret = a.checked_sub(1);
std::optional<numbers::int8> ret = a.checked_sub(1);
if (ret) {
std::cout << ret.value() << '\n';
} else {
Expand All @@ -17,8 +17,8 @@ void checked_sub_example() {

void overflowing_div_example() {
std::cout << "==== overflowing_div_example ==== \n";
Int16 a = 40_i16;
Int16 b = 2_i16;
numbers::int16 a = 40;
numbers::int16 b = 2;
auto [ret, overflowing] = a.overflowing_div(b);
std::cout << "a= " << a << ", b= " << b << '\n';
if (!overflowing) {
Expand All @@ -30,46 +30,57 @@ void overflowing_div_example() {

void saturating_mul_example() {
std::cout << "==== saturating_mul_example ==== \n";
Int64 a = 40_i64;
Int64 b = Int64::MAX;
numbers::int64 a = 40;
numbers::int64 b = numbers::int64::MAX;
std::cout << "a= " << a << ", b= " << b << '\n';
Int64 ret = a.saturating_mul(b);
numbers::int64 ret = a.saturating_mul(b);
std::cout << ret << '\n';
}

void int128_example() {
std::cout << "==== int128_example ==== \n";
numbers::int128 a = 40;
numbers::int128 max = numbers::int128::MAX;
numbers::int128 min = numbers::int128::MIN;
std::cout << "a= " << a << ", max= " << max << ", min= " << min << '\n';
numbers::int128 ret = max - a;
std::cout << "max - a = " << ret << '\n';
}

int main(int argc, char const *argv[]) {
auto a = 100_i8;
auto a = 100;
std::cout << a << '\n';
try {
a = a + a;
std::cout << a << '\n';
} catch (std::runtime_error err) {
} catch (std::runtime_error &err) {
std::cout << "Catch error: " << err.what() << '\n';
}

auto b = 127_i8;
Int8 c = 0;
numbers::int8 b = 127;
numbers::int8 c = 0;
try {
Int8 ret = c - b;
numbers::int8 ret = c - b;
std::cout << ret << '\n';
c = -10;
ret = c - b;
std::cout << ret << '\n';
} catch (std::runtime_error err) {
} catch (std::runtime_error &err) {
std::cout << "Catch error: " << err.what() << '\n';
}

auto d = static_cast<Int16>(b);
auto d = static_cast<numbers::int16>(b);
try {
d = d + d;
std::cout << d << '\n';
} catch (std::runtime_error err) {
} catch (std::runtime_error &err) {
std::cout << "Catch error: " << err.what() << '\n';
}

checked_sub_example();
overflowing_div_example();
saturating_mul_example();
int128_example();

return 0;
}
Loading