Git Product home page Git Product logo

rotary-add's Introduction

mirror crates.io docs.rs

RotaryAdd: Cyclical arithmetic with unsigned integers

This crate provides 3 traits, RotaryAdd, CycleAdd and SeriesAdd, with simple methods to apply cyclical or modular addition and subtraction with three unsigned integer types, u8, u16 and u32. Unlike the default + and - operators, additions and subtractions never overflow.

RotaryAdd

This trait has addition and subtraction methods that act on the whole range of an unsigned integer, e.g. from 0 to 255 for u8 or from 0 to 65535 for u16. This example with an 8-bit unsigned integer illustrates the concept with rotary_add and rotary_sub:

Addition:

let first_number: u8 = 255;
let second_number: u8 = 6;

let result = first_number.rotary_add(&second_number);
// yields 5. first_number + second_number would panic as the value would overflow

Subtraction

let first_number: u8 = 3;
let second_number: u8 = 6;
let result = first_number.rotary_sub(&second_number);
// yields 253. first_number - second_number would panic as the value would overflow

CycleAdd

This trait provides addition and subtraction methods that let you define a custom cyclic base.

These examples show the concept:

Addition:

let first_number: u8 = 22;
let second_number: u8 = 6;

let result = first_number.cycle_add(&second_number, 24);
// yields 4. We have to use u8 as a primitive data type with a higher maximum value

Subtraction

let first_number: u8 = 37;
let second_number: u8 = 78;
let result = first_number.cycle_sub(&second_number, 100);
// yields 59. first_number - second_number would panic as the value would overflow

Serial addition, subtraction and modulus, starting from 1

Many common series start from one. The months of a year, days of a month and weekdays are commonly expressed with the numerals 1 to the maximum. Alas this does not work with modular arithmetic where additions or subtractions overflow. If 1 means Monday and 7 means Sunday, we'd expect 2 (Tue) - 3 to equal 7 (Sun) and 6 (Sat) + 1 to equal 7 (Sun). Instead we need to subtract one from the input number, that may be no lower than 1, mod it by the maximum number and then add 1 to the result. The series_add(), series_sub() and series_mod() methods avoid the need for these conversions when dealing with 1-based series.

let sample_month_1: u8 = 11;
let limit = 12; // months of the year
let result = sample_month_1.series_add(1, limit);
// yields 12. 
let sample_month_2: u8 = 3;
let result = sample_month_2.series_sub(3, limit);
// yields 12

let sample_month_value: u8 = 24; // 24th month
let result = sample_month_value.series_mod(limit);
// yields 12, but would yield 0 with 0-indexed modulus

Unlike the related Ring360 crate, this library only extends core unsigned integer types for use with cryptography, metrology and as a building block for other crates, e.g. converting characters first to u32 values and then shifting their values in one direction in the encoding stage and reversing the process in the decoding stage.

Dev notes

This is an alpha release, but is otherwise feature-complete.

0.1.2

  • Added series_add(), series_sub(), series_mod() methods.

rotary-add's People

Contributors

neilg63 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.