Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix log0s and colwidth #35

Merged
merged 1 commit into from Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,14 @@
0.0.12 (2021-09-09)
==================
* [BUG #33](https://github.com/alexhallam/tv/issues/33) Elipses used when NA should replace on unquoted string missingness #33
This problem was caused by all of the columns being width 1. When width is 1 the length of the string "NA" is 2. Since 2 was greater
than 1 NA was converted to elipses. To fix this problem I added a min width of 2 and while I was at it I includeed a new option `lower-column-width`
* [BUG #32](https://github.com/alexhallam/tv/issues/32) Column with integer 1 and 0 returns NaN for 0.
This bug was caused by logging 0s. I added a condition on the sigfig decision tree to fix.
* **Feature 1** `lower-column-width`: `The lower (minimum) width of columns. Must be 2 or larger. Default 2. `
* **Feature 2** `upper-column-width`: `The upper (maxiumum) width of columns. Default 20.`
* **Feature 2** `debug-mode`: `Print object details to make it easier for the maintainer to find and resolve bugs.` This is to save me time in the futre :smile:

0.0.10 (2021-08-05)
==================
* [BUG #29](https://github.com/alexhallam/tv/issues/29) Turns out the column count was correct. `tv` was not printing the last column
Expand Down
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions Cargo.toml
Expand Up @@ -8,22 +8,22 @@ license = "Unlicense/MIT"
name = "tidy-viewer"
readme = "README.md"
repository = "https://github.com/alexhallam/tv"
version = "0.0.10"
version = "0.0.11"

[package.metadata.deb]
maintainer = "Alex Hallam <alexhallam6.28@gmail.com>"
license-file = ["UNLICENSE", "0"]
assets = [
["target/release/tidy-viewer", "usr/bin/", "755"],
["README.md", "usr/share/doc/cargo-deb/README", "644"],
]
depends = "$auto"
extended-description = """\
Tidy Viewer (tv) is a csv pretty printer that uses \
column styling to maximize viewer enjoyment.
"""
depends = "$auto"
section = "utility"
license-file = ["UNLICENSE", "0"]
maintainer = "Alex Hallam <alexhallam6.28@gmail.com>"
priority = "optional"
assets = [
["target/release/tidy-viewer", "usr/bin/", "755"],
["README.md", "usr/share/doc/cargo-deb/README", "644"],
]
section = "utility"

[dependencies]
console = "0.14.1"
Expand Down
4 changes: 4 additions & 0 deletions data/issue_32.csv
@@ -0,0 +1,4 @@
b, c
one,1
zero,0.0
zero,0
4 changes: 4 additions & 0 deletions data/issue_33.csv
@@ -0,0 +1,4 @@
a,b,c,e,f,g
a,t,1,a,t,3
b,u,2,b,u,2
c,v,3,,,
2 changes: 1 addition & 1 deletion data/sigs.csv
Expand Up @@ -6,4 +6,4 @@ test_123,test_100,test_n123,test_n100
1.2345,0.01,-1.2345,-0.01
0.12345,0.001,-0.12345,-0.001
0.012345,0.0001,-0.012345,-0.0001

0,0,0,0.0
258 changes: 132 additions & 126 deletions src/datatype/sigfig.rs
Expand Up @@ -125,144 +125,149 @@ fn is_decimal(x: f64) -> bool {
l > 1.0
}
pub fn get_final_string(x: f64, lhs: f64, rhs: f64, neg: bool, sigfig: i64) -> String {
if lhs == 0.0 {
//n = ((floor(log10(abs(x))) + 1 - sigfig)
//r =(10^n) * round(x / (10^n))
let n = x.abs().log10().floor() + 1.0 - sigfig as f64;
let r: f64 = 10f64.powf(n) * ((x / 10f64.powf(n)).round());
let tmp_string = r.to_string();
if tmp_string.len() > 13 {
// 13 is arbitraty. There may be a more general solution here!
// Problem: debug val: 0.0001 => final_string: "0.00009999999999999999"
let w = (x.abs().log10().floor()).abs() as usize;
let fstring = format!("{:.w$}", r, w = w);
fstring
} else {
tmp_string
}
if lhs.abs() + rhs.abs() == 0.0 {
let r = "0".to_string();
r
} else {
if lhs.log10() + 1.0 >= sigfig as f64 {
if rhs > 0.0 {
if neg == true {
//concatonate:
//(-)
//(lhs)
//(point)
//(-123.45 -> -123.)
let total = lhs + rhs;
let total_string = total.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take = vec[0].len() + 1; // lhs + point
let pos_string = (total_string[..len_to_take]).to_string();
let neg_string = "-".to_string();
let r = [neg_string, pos_string].join("");
r
} else {
//concatonate:
//(lhs)
//(point)
//(123.45 -> 123.)
//(123.45 -> 123.)
let total = lhs + rhs;
let total_string = total.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take = vec[0].len() + 1; // lhs + point
let r = total_string[..len_to_take].to_string();
r
}
if lhs == 0.0 {
//n = ((floor(log10(abs(x))) + 1 - sigfig)
//r =(10^n) * round(x / (10^n))
let n = x.abs().log10().floor() + 1.0 - sigfig as f64;
let r: f64 = 10f64.powf(n) * ((x / 10f64.powf(n)).round());
let tmp_string = r.to_string();
if tmp_string.len() > 13 {
// 13 is arbitraty. There may be a more general solution here!
// Problem: debug val: 0.0001 => final_string: "0.00009999999999999999"
let w = (x.abs().log10().floor()).abs() as usize;
let fstring = format!("{:.w$}", r, w = w);
fstring
} else {
if neg == true {
//concatonate:
//(-)
//(lhs)
//(-1234.0 -> -1234)
let total = lhs + rhs;
let total_string = total.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take = vec[0].len(); // lhs
let pos_string = (total_string[..len_to_take]).to_string();
let neg_string = "-".to_string();
let r = [neg_string, pos_string].join("");
r
} else {
//concatonate:
//(lhs)
//(1234.0 -> 1234)
//(100.0 -> 100)
//let total = lhs + rhs;
//let total_string = total.to_string();
let total_string = x.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take = vec[0].len(); // lhs
let r = total_string[..len_to_take].to_string();
r
}
tmp_string
}
} else {
if rhs == 0.0 {
//concatonate:
//(lhs)
//(point)
//+ sigfig - log10(lhs) from rhs
let total_string = x.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take_lhs = vec[0].len(); // point -> +1 to sigfig
let r = total_string[..len_to_take_lhs].to_string();
r
if lhs.log10() + 1.0 >= sigfig as f64 {
if rhs > 0.0 {
if neg == true {
//concatonate:
//(-)
//(lhs)
//(point)
//(-123.45 -> -123.)
let total = lhs + rhs;
let total_string = total.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take = vec[0].len() + 1; // lhs + point
let pos_string = (total_string[..len_to_take]).to_string();
let neg_string = "-".to_string();
let r = [neg_string, pos_string].join("");
r
} else {
//concatonate:
//(lhs)
//(point)
//(123.45 -> 123.)
//(123.45 -> 123.)
let total = lhs + rhs;
let total_string = total.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take = vec[0].len() + 1; // lhs + point
let r = total_string[..len_to_take].to_string();
r
}
} else {
if neg == true {
//concatonate:
//(-)
//(lhs)
//(-1234.0 -> -1234)
let total = lhs + rhs;
let total_string = total.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take = vec[0].len(); // lhs
let pos_string = (total_string[..len_to_take]).to_string();
let neg_string = "-".to_string();
let r = [neg_string, pos_string].join("");
r
} else {
//concatonate:
//(lhs)
//(1234.0 -> 1234)
//(100.0 -> 100)
//let total = lhs + rhs;
//let total_string = total.to_string();
let total_string = x.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take = vec[0].len(); // lhs
let r = total_string[..len_to_take].to_string();
r
}
}
} else {
if neg == true {
if rhs == 0.0 {
//concatonate:
//(-)
//(lhs)
//(point)
//+ sigfig - log10(lhs) from rhs
//(12.345 -> 12.3)
//(1.2345 -> 1.23)
// need a rhs arguments here
//let total = lhs + rhs;
//let total_string = total.to_string();
let total_string = x.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take_lhs = vec[0].len(); // point -> +1 to sigfig
let len_to_take_rhs = ((sigfig + 1) as usize) - len_to_take_lhs;
let len_to_take = len_to_take_lhs + len_to_take_rhs + 1; // +1 for the space the neg sign takes
let r = total_string[..len_to_take].to_string();
let r = total_string[..len_to_take_lhs].to_string();
r
} else {
//concatonate:
//(lhs)
//(point)
//+ sigfig - log10(lhs) from rhs
//(12.345 -> 12.3)
//(1.2345 -> 1.23)
// need a rhs arguments here
//let total = lhs + rhs;
//let total_string = total.to_string();
let total_string = x.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take_lhs = vec[0].len(); // point -> +1 to sigfig
let len_to_take_rhs = ((sigfig + 1) as usize) - len_to_take_lhs;
let len_to_take = len_to_take_lhs + len_to_take_rhs;
if len_to_take >= total_string.len() {
let r = total_string.to_string();
r
} else {
if neg == true {
//concatonate:
//(-)
//(lhs)
//(point)
//+ sigfig - log10(lhs) from rhs
//(12.345 -> 12.3)
//(1.2345 -> 1.23)
// need a rhs arguments here
//let total = lhs + rhs;
//let total_string = total.to_string();
let total_string = x.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take_lhs = vec[0].len(); // point -> +1 to sigfig
let len_to_take_rhs = ((sigfig + 1) as usize) - len_to_take_lhs;
let len_to_take = len_to_take_lhs + len_to_take_rhs + 1; // +1 for the space the neg sign takes
let r = total_string[..len_to_take].to_string();
r
} else {
//concatonate:
//(lhs)
//(point)
//+ sigfig - log10(lhs) from rhs
//(12.345 -> 12.3)
//(1.2345 -> 1.23)
// need a rhs arguments here
//let total = lhs + rhs;
//let total_string = total.to_string();
let total_string = x.to_string();
let total_clone = total_string.clone();
let split = total_clone.split(".");
let vec: Vec<&str> = split.collect();
let len_to_take_lhs = vec[0].len(); // point -> +1 to sigfig
let len_to_take_rhs = ((sigfig + 1) as usize) - len_to_take_lhs;
let len_to_take = len_to_take_lhs + len_to_take_rhs;
if len_to_take >= total_string.len() {
let r = total_string.to_string();
r
} else {
let r = total_string[..len_to_take].to_string();
r
}
}
}
}
Expand Down Expand Up @@ -360,20 +365,21 @@ fn sigfig_index_to(final_string: String, sigfig: i64) -> Option<usize> {

#[test]
fn test_f12345() {
let f12345 = vec![12345.0, 1234.50, 123.45, 12.345, 1.2345, 0.12345];
let test_sigfig = vec![3, 3, 3, 3, 3, 3];
let test_neg = vec![false, false, false, false, false, false];
let test_lhs = vec![12345.0, 1234.0, 123.0, 12.0, 1.0, 0.0];
let f12345 = vec![12345.0, 1234.50, 123.45, 12.345, 1.2345, 0.12345, 0.0];
let test_sigfig = vec![3, 3, 3, 3, 3, 3, 3];
let test_neg = vec![false, false, false, false, false, false, false];
let test_lhs = vec![12345.0, 1234.0, 123.0, 12.0, 1.0, 0.0, 0.0];
let test_rhs = vec![
0.0,
0.5,
0.45000000000000284,
0.34500000000000064,
0.23449999999999993,
0.12345,
0.0,
];
let test_dec = vec![false, true, true, true, true, true];
let test_final_string = vec!["12345", "1234.", "123.", "12.3", "1.23", "0.123"];
let test_dec = vec![false, true, true, true, true, true, false];
let test_final_string = vec!["12345", "1234.", "123.", "12.3", "1.23", "0.123", "0"];

for i in 0..f12345.len() {
let value = f12345[i];
Expand Down