Code Monkey home page Code Monkey logo

digits's Introduction

digits

Build Status crates.io version Documentation

Digits is a custom character base numeric sequencer. This crate is designed for infinite character progressions. It will contain additive methods such as add and mul.

This is an extension on top of base_custom.

The largest unsigned digit type in Rust is u64. Consider this an upgrade to u∞ The limits that this can calculate to are unknown and may only be limited to your systems RAM should you try to reach infinity ;-).

This package lets you invent your own numeric systems and perform basic math on them including:

  • addition
  • multiplication
  • multiply by powers of
  • and simple +1/-1 steps with succ and pred_till_zero
  • as of version 0.3 Digits preserves zero padding for addition methods

You may consider this a highly advanced score card flipper (character sequences) with basic math methods added to help progress through sequences as you would like.

Installation

Add the following to your Cargo.toml file

[dependencies]
digits = "^1.0"

To include it for usage add

extern crate digits;
use digits::prelude::*;

to your file.

Usage

There are several ways to create a new instance of Digits, but before any of that you need to define you own numeric base from the base_custom package.

// Define your own numeric base to use using a set of any characters
// We'll use the string representations for base 10 so you can see this
// work with something familiar.

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());

// Once you have a custom numeric base defined you can create instances of Digits in many ways.

let hundred = Digits::new(base10.clone(), "100".to_string());

// If you don't want to have to pass the base value in each time you create a new number
// you can propagate a new one out with the `propagate` method.

let two = hundred.propagate("2".to_string()); // re-uses internal base10 mappings

// Now we have two instances of Digits created: one for 100 and one for 2

// The mathematical methods mutate the Digits instance they're called from
// so you need to either use `let mut` or call `clone` to use them.

hundred.clone().add(two).to_s() // outputs: "102"
hundred.clone().mul(two).to_s() // outputs: "200"
hundred.clone().pow(two).to_s() // outputs: "10000"

// There are several ways to create and check one or zero.

let one = Digits::new_one(&base10); // A Digits instance with the value of 1
one.is_one() // true
one.is_zero() // false

let zero = Digits::new_zero(&base10); // A Digits instance with the value of 0
zero.is_one() // false
zero.is_zero() // true

// And you can create a one or zero off of an existing Digits instance with `one` or `zero`
hundred.one() // A Digits instance with the value of 1
hundred.zero() // A Digits instance with the value of 0

// Count down or up with `pred_till_zero` and `succ`
let mut ten = Digits::new(base10.clone(), "10".to_string());
assert_eq!(ten.pred_till_zero().to_s(), "09");
assert_eq!(ten.pred_till_zero().to_s(), "08");
assert_eq!(ten.pred_till_zero().to_s(), "07");

let mut nine = Digits::new(base10.clone(), "9".to_string());
assert_eq!(nine.succ().to_s(), "10");
assert_eq!(nine.succ().to_s(), "11");
assert_eq!(nine.succ().to_s(), "12");

And this is just with normal 0 thourgh 9 values. Imagine if you invent your own numeric bases and character sets. It can be used for quite a lot!

Goals / Roadmap

  1. The first goal of this library is to be thread safe and function well for sequencing characters.

  2. The secondary goal, which may improve with time, is performance.

  3. The third goal is to have fun re-inventing mathematics and experiments.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

digits's People

Contributors

danielpclark avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

digits's Issues

Zero fill edge case for first step in StepMap

A number with many zeros should have a minimal first step. Detect that and flip appropriate ones.

Since this will only happen once, and on the first time, we need to avoid conditional checks continuing. To support this I propose adding additional types for the states of Digits produced.

UnSteppedDigits & SteppedDigits seem like a good type.

May actually need a completely different situational StepMap for the UnSteppedDigits.

This will be a breaking change for next_non_adjacent to accept either a UnSteppedDigits or a SteppedDigits as input instead of a Digits instance.

Potential optimization for numeric base conversion

Hexadecimal can convert directly into binary into 4 bits per character. Since hex is a number in the compounded power of binary this rule may be possible to apply to all multiples of similar powers.

# Example given:
16 = 2**4

Need early return alternative method to max_adjacent with upper limit

The max_adjacent method consumes the entire Digits by traversing to the end before returning a value and allowing us to check if the max is too big. Since we know what's too big before hand we should create a method that will return immediately once the maximum threshold has been crossed and return a Boolean value to check with. This should be refactored into the non-adjacent stepper method.

Refactor add and mul to work with different numeric bases (if reasonable)

add and mul may be able to benefit from performing mathematics between different numeric bases. If there is no performance cost this could be cool. Otherwise leave as is.

If this can be accomplished reasonably then we may remove the panic! check.

It's important to note that conversion can already be accomplished with the from method but this is an added cost at the moment.

Consider Negative Radices

Reading this post: http://datagenetics.com/blog/december22015/index.html I find negative numeric bases quite interesting. I'd like to consider implementing this for the experience and the fun of it.

We can apply the same negative radix strategy to describe numbers in base -10 (called 'negadecimal')

17478-10
= 1×(-10)4 + 7×(-10)3 + 4×(-10)2 + 7×(-10)1 + 8×(-10)0
= 1×(10000) - 7×(1000) + 4×(100) - 7×(10) + 8×(1)
= 10000 − 7000 + 400 − 70 + 8
= 333810

Just like negabinary, numbers encoded in negadecimal do not need explicit sign indicators; this is encapsulated into the number system, and all can be treated exactly the same way.

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.