Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
2d6f03c28c |
|||
|
5cdfbfea5e |
|||
|
acc49edd31 |
|||
|
da82532361 |
|||
|
046a3e5b20 |
4 changed files with 63 additions and 70 deletions
17
flake.lock
generated
17
flake.lock
generated
|
|
@ -20,18 +20,15 @@
|
||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1780749050,
|
"lastModified": 1791260994,
|
||||||
"narHash": "sha256-3av0pIjlOWQ6rDbNOmpUSvbNnJkGORQKKjb4LtCZsIY=",
|
"narHash": "sha256-MFBjw66d4h06MvpW6lIl7RwarLteD0sk859CncGwb1Q=",
|
||||||
"owner": "NixOS",
|
"rev": "151fa4e8ddfdd8dd25d945ad94ed54a13de9f6e4",
|
||||||
"repo": "nixpkgs",
|
"type": "tarball",
|
||||||
"rev": "a799d3e3886da994fa307f817a6bc705ae538eeb",
|
"url": "https://releases.nixos.org/nixos/unstable/nixos-26.11pre1086391.151fa4e8ddfd/nixexprs.tar.zst?lastModified=1791260994&rev=151fa4e8ddfdd8dd25d945ad94ed54a13de9f6e4"
|
||||||
"type": "github"
|
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "NixOS",
|
"type": "tarball",
|
||||||
"ref": "nixos-unstable",
|
"url": "https://channels.nixos.org/nixos-unstable/nixexprs.tar.zst"
|
||||||
"repo": "nixpkgs",
|
|
||||||
"type": "github"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"root": {
|
"root": {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
nixpkgs.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.zst";
|
||||||
|
|
||||||
flake-utils.url = "github:numtide/flake-utils";
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,19 +2,20 @@ use rug::Float;
|
||||||
|
|
||||||
use crate::nodes::{self, Error};
|
use crate::nodes::{self, Error};
|
||||||
|
|
||||||
pub fn pretty_eval(mut input: String, max_digits: usize) -> Result<String, Error> {
|
pub fn pretty_eval(input: &str, max_digits: usize) -> Result<String, Error> {
|
||||||
// no need to remove the characters here,
|
let (prefix, deliminator, input) = input
|
||||||
// as we ignore the them in all other logic already
|
.rsplit_once('=')
|
||||||
let should_add_back_alignment = input.contains('&');
|
.map(|(p, i)| (p, "= ", i))
|
||||||
let should_add_back_backslash = input.contains('\\');
|
.unwrap_or_else(|| {
|
||||||
let leading_spaces = input.chars().take_while(|c| *c == ' ').count();
|
input
|
||||||
|
.rsplit_once('&')
|
||||||
|
.map(|(p, i)| (p, "& ", i))
|
||||||
|
.unwrap_or(("", "", input))
|
||||||
|
});
|
||||||
|
|
||||||
let should_add_back_equals = input
|
// no need to remove the character here,
|
||||||
.find('=')
|
// as we ignore it in all other logic already
|
||||||
.inspect(|i| {
|
let should_add_back_backslash = input.contains('\\');
|
||||||
input.remove(*i);
|
|
||||||
})
|
|
||||||
.is_some();
|
|
||||||
|
|
||||||
let result = nodes::eval(&input)?;
|
let result = nodes::eval(&input)?;
|
||||||
let display_digits =
|
let display_digits =
|
||||||
|
|
@ -32,38 +33,11 @@ pub fn pretty_eval(mut input: String, max_digits: usize) -> Result<String, Error
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(format!(
|
Ok(format!(
|
||||||
"{}{}{result:.display_digits$}{}",
|
"{prefix}{deliminator}{result:.display_digits$}{}",
|
||||||
" ".repeat(leading_spaces),
|
|
||||||
if should_add_back_equals && should_add_back_alignment {
|
|
||||||
"& = "
|
|
||||||
} else if should_add_back_equals {
|
|
||||||
"= "
|
|
||||||
} else if should_add_back_alignment {
|
|
||||||
"& "
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
if should_add_back_backslash { " \\" } else { "" }
|
if should_add_back_backslash { " \\" } else { "" }
|
||||||
)
|
)
|
||||||
.replace('e', " * 10^"))
|
.replace('e', " * 10^"))
|
||||||
}
|
}
|
||||||
#[test]
|
|
||||||
fn test_character_readdition() {
|
|
||||||
assert_eq!(pretty_eval(" = 11".into(), 10).unwrap(), " = 11");
|
|
||||||
assert_eq!(pretty_eval(" &= 11".into(), 10).unwrap(), " & = 11");
|
|
||||||
assert_eq!(pretty_eval(" & 11".into(), 10).unwrap(), " & 11");
|
|
||||||
assert_eq!(pretty_eval(" & 11 \\".into(), 10).unwrap(), " & 11 \\");
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn test_basic_up_to_999() {
|
|
||||||
assert_eq!(pretty_eval("1".into(), 10).unwrap(), "1");
|
|
||||||
assert_eq!(pretty_eval("10".into(), 10).unwrap(), "10");
|
|
||||||
assert_eq!(pretty_eval("100".into(), 10).unwrap(), "100");
|
|
||||||
assert_eq!(pretty_eval("999".into(), 10).unwrap(), "999");
|
|
||||||
assert_eq!(pretty_eval("1000".into(), 10).unwrap(), "1 * 10^3");
|
|
||||||
|
|
||||||
assert_eq!(pretty_eval("11.111111".into(), 10).unwrap(), "11.111111");
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_significant_digits(n: &Float) -> usize {
|
pub fn get_significant_digits(n: &Float) -> usize {
|
||||||
let raw_string = n.to_string().replace('.', "");
|
let raw_string = n.to_string().replace('.', "");
|
||||||
|
|
@ -99,12 +73,33 @@ pub fn get_significant_digits(n: &Float) -> usize {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
fn eval(s: &str) -> Float {
|
fn eval(s: &str) -> Float {
|
||||||
crate::nodes::eval(s).unwrap()
|
crate::nodes::eval(s).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_significant_digits() {
|
fn character_readdition() {
|
||||||
|
assert_eq!(pretty_eval(" = 11", 10).unwrap(), " = 11");
|
||||||
|
assert_eq!(pretty_eval(" &= 11", 10).unwrap(), " &= 11");
|
||||||
|
assert_eq!(pretty_eval(" & 11", 10).unwrap(), " & 11");
|
||||||
|
assert_eq!(pretty_eval(" & 11 \\", 10).unwrap(), " & 11 \\");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_output_up_to_999() {
|
||||||
|
assert_eq!(pretty_eval("1", 10).unwrap(), "1");
|
||||||
|
assert_eq!(pretty_eval("10", 10).unwrap(), "10");
|
||||||
|
assert_eq!(pretty_eval("100", 10).unwrap(), "100");
|
||||||
|
assert_eq!(pretty_eval("999", 10).unwrap(), "999");
|
||||||
|
assert_eq!(pretty_eval("1000", 10).unwrap(), "1 * 10^3");
|
||||||
|
|
||||||
|
assert_eq!(pretty_eval("11.111111", 10).unwrap(), "11.111111");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_get_significant_digits() {
|
||||||
let many_zeros = eval("14.00643 + 1.00784 * 4 + 35.446 + 15.9994 * 4");
|
let many_zeros = eval("14.00643 + 1.00784 * 4 + 35.446 + 15.9994 * 4");
|
||||||
assert_eq!(many_zeros.to_f64(), 117.48139);
|
assert_eq!(many_zeros.to_f64(), 117.48139);
|
||||||
let many_nines = eval("12.0096 + 15.9994 * 2");
|
let many_nines = eval("12.0096 + 15.9994 * 2");
|
||||||
|
|
@ -119,3 +114,4 @@ fn test_get_significant_digits() {
|
||||||
assert_eq!(get_significant_digits(&zero_nine_zero), 6);
|
assert_eq!(get_significant_digits(&zero_nine_zero), 6);
|
||||||
assert_eq!(get_significant_digits(&test_4), 7);
|
assert_eq!(get_significant_digits(&test_4), 7);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ fn main() -> ExitCode {
|
||||||
buf
|
buf
|
||||||
});
|
});
|
||||||
|
|
||||||
match lib::formatting::pretty_eval(input, 10) {
|
match lib::formatting::pretty_eval(&input, 10) {
|
||||||
Ok(s) => println!("{s}"),
|
Ok(s) => println!("{s}"),
|
||||||
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue