Skip to content

Commit

Permalink
feat(add): i128 overflowing operations and update type name (#13)
Browse files Browse the repository at this point in the history
* update README desc

* refactor Integer

* i128 support add & sub overflow operation

* try to support mul's overflow operation for i128

* support div, abs, neg overflowing operations for i128

* modify type name
  • Loading branch information
guuzaa authored Mar 2, 2024
1 parent 0f813ff commit 61ac0d5
Show file tree
Hide file tree
Showing 11 changed files with 1,245 additions and 568 deletions.
37 changes: 18 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@ 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)
![language c++17](https://img.shields.io/badge/Language-C++17-red)
[![license mit](https://img.shields.io/badge/License-MIT-pink)](https://github.com/guuzaa/numbers/blob/main/LICENSE.txt)
![linux](https://img.shields.io/badge/OS-Linux-blue)
![macOS](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.**
> [!IMPORTANT]
> 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.

## Features

- **Full Control**
- **Full Control** over handling integer overflow

- **Like Primitive Types**
- Same as **Plain Old Data** (WIP)

- **Signed Integers**: int8 int16 int32 int64 int128

- **Unsigned Integers**: uint8 uint16 uint32 uint64
- **Support Integers**: i8, i16, i32, i64, u8, u16, u32, u64, even i128 & u128 (not ready yet)

## Usage

### operator +
```c++
numbers::int8 a = 100;
numbers::i8 a = 100;
std::cout << a << '\n';
try {
a = a + a;
Expand All @@ -38,9 +37,9 @@ try {

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

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

### saturating mul
```c++
numbers::int64 a = 40;
numbers::int64 b = numbers::int64::MAX;
numbers::i64 a = 40;
numbers::i64 b = numbers::i64::MAX;
std::cout << "a= " << a << ", b= " << b << '\n';
numbers::int64 ret = a.saturating_mul(b);
numbers::i64 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).
For the latest updates and discussions, please see our [issues](https://github.com/guuzaa/numbers/issues) and [pull requests](https://github.com/guuzaa/numbers/pulls).

Stay tuned for more updates, and thank you for your interest in contributing to our project!
36 changes: 18 additions & 18 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";
numbers::int8 a = numbers::int8::MIN;
numbers::i8 a = numbers::i8::MIN;
std::cout << a << '\n';
std::optional<numbers::int8> ret = a.checked_sub(1);
std::optional<numbers::i8> 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";
numbers::int16 a = 40;
numbers::int16 b = 2;
numbers::i16 a = 40;
numbers::i16 b = 2;
auto [ret, overflowing] = a.overflowing_div(b);
std::cout << "a= " << a << ", b= " << b << '\n';
if (!overflowing) {
Expand All @@ -30,20 +30,20 @@ void overflowing_div_example() {

void saturating_mul_example() {
std::cout << "==== saturating_mul_example ==== \n";
numbers::int64 a = 40;
numbers::int64 b = numbers::int64::MAX;
numbers::i64 a = 40;
numbers::i64 b = numbers::i64::MAX;
std::cout << "a= " << a << ", b= " << b << '\n';
numbers::int64 ret = a.saturating_mul(b);
numbers::i64 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;
void i128_example() {
std::cout << "==== i128_example ==== \n";
numbers::i128 a = 40;
numbers::i128 max = numbers::i128::MAX;
numbers::i128 min = numbers::i128::MIN;
std::cout << "a= " << a << ", max= " << max << ", min= " << min << '\n';
numbers::int128 ret = max - a;
numbers::i128 ret = max - a;
std::cout << "max - a = " << ret << '\n';
}

Expand All @@ -57,10 +57,10 @@ int main(int argc, char const *argv[]) {
std::cout << "Catch error: " << err.what() << '\n';
}

numbers::int8 b = 127;
numbers::int8 c = 0;
numbers::i8 b = 127;
numbers::i8 c = 0;
try {
numbers::int8 ret = c - b;
numbers::i8 ret = c - b;
std::cout << ret << '\n';
c = -10;
ret = c - b;
Expand All @@ -69,7 +69,7 @@ int main(int argc, char const *argv[]) {
std::cout << "Catch error: " << err.what() << '\n';
}

auto d = static_cast<numbers::int16>(b);
auto d = static_cast<numbers::i16>(b);
try {
d = d + d;
std::cout << d << '\n';
Expand All @@ -80,7 +80,7 @@ int main(int argc, char const *argv[]) {
checked_sub_example();
overflowing_div_example();
saturating_mul_example();
int128_example();
i128_example();

return 0;
}
6 changes: 4 additions & 2 deletions src/include/int128.hh
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class int128 {
constexpr explicit operator long long() const;

constexpr explicit operator unsigned long long() const;

// TODO unimplemented!
explicit operator float() const;
explicit operator double() const;
explicit operator long double() const;
Expand Down Expand Up @@ -367,8 +369,8 @@ inline uint128 &uint128::operator=(int128 v) { return *this = uint128(v); }

// Arithmetic operators.

constexpr uint128 operator<<(uint128 lhs, int amout);
constexpr uint128 operator>>(uint128 lhs, int amout);
constexpr uint128 operator<<(uint128 lhs, int amount);
constexpr uint128 operator>>(uint128 lhs, int amount);
constexpr uint128 operator+(uint128 lhs, uint128 rhs);
constexpr uint128 operator-(uint128 lhs, uint128 rhs);
uint128 operator*(uint128 lhs, uint128 rhs);
Expand Down
Loading

0 comments on commit 61ac0d5

Please sign in to comment.