-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
2,475 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"files":{"Cargo.lock":"a390402b06571120e254b24e073d9455338ed1d187ea62efd17de518b790b2f8","Cargo.toml":"6d78ade7d94598b21d949de3df0cb556788c59afa8095106a05b961d69ea8a14","LICENSE":"0844ba5b279f92f2b44896ba99c6365e567ba845e1c3f4da6ad3cef1dfa4b268","README.md":"7d21117ff40f499fab630d874ca7dd1ebefc16edf8257e7c7212fa948d45b174","examples/iteration.rs":"a4ab0b86fc71fefba9e84fc118cf547696b776dc9a8121abdeaf10bffa2e28be","examples/simple.rs":"620d495a175055fa4b69d9f9fc75e99a84657dd07c4c1d9fd3a2e3f241f3201f","src/extras.rs":"68080b1ba4c0679c8ea02e66b9a45df0e310a05e2bc40701d987c42e02955ef0","src/lib.rs":"d9962ea4b3648f5936c2e032476c39cc7600b4010aea3cba18a33362309b1a92","src/num.rs":"ea77d5a51d2a3beb9cfb2a9ba78f8f87a30dbe31ef22d0ccca8282800717abba","src/ops.rs":"a833f25103b6613e8cdea1a767667c0b0a44585f5a340cc977ce64a4996558e8"},"package":"31fc9d4c236e72ada45bbd2b743c301f45cb4da4c350b8a73abe2e1e65046d80"} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
# | ||
# When uploading crates to the registry Cargo will automatically | ||
# "normalize" Cargo.toml files for maximal compatibility | ||
# with all versions of Cargo and also rewrite `path` dependencies | ||
# to registry (e.g., crates.io) dependencies. | ||
# | ||
# If you are reading this file be aware that the original Cargo.toml | ||
# will likely look very different (and much more reasonable). | ||
# See Cargo.toml.orig for the original contents. | ||
|
||
[package] | ||
edition = "2018" | ||
name = "rational" | ||
version = "1.3.0" | ||
authors = ["Isak Jägberg <[email protected]>"] | ||
description = "Minimalistic library for rational numbers" | ||
homepage = "https://github.com/ijagberg/rational" | ||
readme = "README.md" | ||
keywords = [ | ||
"rational", | ||
"ratio", | ||
"fraction", | ||
"fractions", | ||
] | ||
categories = [ | ||
"mathematics", | ||
"science", | ||
] | ||
license = "MIT" | ||
repository = "https://github.com/ijagberg/rational" | ||
|
||
[dependencies.num-traits] | ||
version = "0.2.16" | ||
optional = true | ||
|
||
[dev-dependencies.rand] | ||
version = "0.8.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Isak Jägberg | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
A minimal library for representing rational numbers (ratios of integers). | ||
|
||
## Construction | ||
|
||
```rust | ||
// Rationals are automatically reduced when created: | ||
let one_half = Rational::new(1, 2); | ||
let two_quarters = Rational::new(2, 4); | ||
assert_eq!(one_half, two_quarters); | ||
|
||
// `From` is implemented for integers and integer tuples: | ||
assert_eq!(Rational::from(1), Rational::new(1, 1)); | ||
assert_eq!(Rational::from((1, 2)), Rational::new(1, 2)); | ||
|
||
// The `new` method takes a numerator and denominator that implement `Into<Rational>`: | ||
let one_half_over_one_quarter = Rational::new((1, 2), (1, 4)); | ||
assert_eq!(one_half_over_one_quarter, Rational::new(2, 1)); | ||
``` | ||
|
||
## Mathematical operations | ||
|
||
```rust | ||
// Operations and comparisons are implemented for Rationals and integers: | ||
let one_ninth = Rational::new(1, 9); | ||
assert_eq!(one_ninth + Rational::new(5, 4), Rational::new(49, 36)); | ||
assert_eq!(one_ninth - 4, Rational::new(-35, 9)); | ||
assert_eq!(one_ninth / Rational::new(21, 6), Rational::new(2, 63)); | ||
assert!(one_ninth < Rational::new(1, 8)); | ||
assert!(one_ninth < 1); | ||
``` | ||
|
||
## Other properties | ||
|
||
```rust | ||
// Inverse: | ||
let eight_thirds = Rational::new(8, 3); | ||
let inverse = eight_thirds.inverse(); | ||
assert_eq!(inverse, Rational::new(3, 8)); | ||
|
||
// Mixed fractions: | ||
let (whole, fractional) = eight_thirds.mixed_fraction(); | ||
assert_eq!(whole, 2); | ||
assert_eq!(fractional, Rational::new(2, 3)); | ||
``` | ||
|
||
## Features | ||
|
||
- Enable the `num-traits` feature to gain access to implementations of many of the traits defined in the `num-traits` crate for `Rationals` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use rational::Rational; | ||
|
||
/// example taken from [this YouTube video](https://www.youtube.com/watch?v=N92w4e-hrA4) | ||
fn main() { | ||
assert_sequence( | ||
Rational::new(0, 1), | ||
2, | ||
Rational::one(), | ||
&[ | ||
Rational::integer(0), | ||
Rational::integer(1), | ||
Rational::integer(2), | ||
Rational::integer(5), | ||
Rational::integer(26), | ||
], | ||
); | ||
|
||
assert_sequence( | ||
Rational::integer(0), | ||
2, | ||
Rational::new(1, 4), | ||
&[ | ||
Rational::new(0, 1), | ||
Rational::new(1, 4), | ||
Rational::new(5, 16), | ||
Rational::new(89, 256), | ||
], | ||
); | ||
|
||
assert_sequence( | ||
Rational::new(1, 2), | ||
2, | ||
Rational::new(1, 4), | ||
&[Rational::new(1, 2), Rational::new(1, 2)], | ||
); | ||
|
||
assert_sequence( | ||
Rational::new(-7, 4), | ||
2, | ||
Rational::new(-29, 16), | ||
&[ | ||
Rational::new(-7, 4), | ||
Rational::new(5, 4), | ||
Rational::new(-1, 4), | ||
Rational::new(-7, 4), | ||
], | ||
); | ||
} | ||
|
||
fn assert_sequence(start: Rational, exp: u32, c: Rational, expected: &[Rational]) { | ||
let mut current = start; | ||
let mut actual = Vec::new(); | ||
for _ in 0..expected.len() { | ||
actual.push(current); | ||
current = next(current, exp, c); | ||
} | ||
assert_eq!(actual, expected); | ||
} | ||
|
||
fn next(z: Rational, exp: u32, c: Rational) -> Rational { | ||
z.pow(exp as i32) + c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use rational::Rational; | ||
|
||
fn main() { | ||
let one_half = Rational::new(1, 2); | ||
|
||
println!("{}", one_half); // prints "1/2" | ||
|
||
println!("{:0b}", -6); | ||
println!("{}", (-4_i128 | 2_i128).trailing_zeros()); | ||
} |
Oops, something went wrong.