Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions factorion-bot-discord/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "factorion-bot-discord"
version = "2.2.5"
version = "2.3.0"
edition = "2024"
description = "factorion-bot (for factorials and related) on Discord"
license = "MIT"
Expand All @@ -10,7 +10,7 @@ keywords = ["factorial", "termial", "bot", "math", "discord"]
categories = ["mathematics", "web-programming", "parser-implementations"]

[dependencies]
factorion-lib = { path = "../factorion-lib", version = "4.2.5", features = ["serde", "influxdb"] }
factorion-lib = { path = "../factorion-lib", version = "4.3.0", features = ["serde", "influxdb"] }
serenity = { version = "0.12", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "cache"] }
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread", "time"] }
dotenvy = "^0.15.7"
Expand Down
4 changes: 2 additions & 2 deletions factorion-bot-reddit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "factorion-bot-reddit"
version = "5.3.5"
version = "5.4.0"
edition = "2024"
description = "factorion-bot (for factorials and related) on Reddit"
license = "MIT"
Expand All @@ -10,7 +10,7 @@ keywords = ["factorial", "termial", "bot", "math"]
categories = ["mathematics", "web-programming", "parser-implementations"]

[dependencies]
factorion-lib = {path = "../factorion-lib", version = "4.2.5", features = ["serde", "influxdb"]}
factorion-lib = {path = "../factorion-lib", version = "4.3.0", features = ["serde", "influxdb"]}
reqwest = { version = "0.12.28", features = ["json", "native-tls"], default-features = false }
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
serde_json = "1.0.140"
Expand Down
2 changes: 1 addition & 1 deletion factorion-lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "factorion-lib"
version = "4.2.5"
version = "4.3.0"
edition = "2024"
description = "A library used to create bots to recognize and calculate factorials and related concepts"
license = "MIT"
Expand Down
80 changes: 73 additions & 7 deletions factorion-lib/src/calculation_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,11 @@ impl Calculation {
/// If the input is all 9s, this will round to 10. \
/// Stops when a decimal period is encountered, removing it.
///
/// Returns whether it overflowed.
///
/// # Panic
/// This function may panic if less than two digits are supplied, or if it contains a non-digit of base 10, that is not a period.
fn round(number: &mut String) {
fn round(number: &mut String) -> bool {
// Check additional digit if we need to round
if let Some(digit) = number
.pop()
Expand All @@ -517,7 +519,7 @@ fn round(number: &mut String) {
let Some(digit) = number.pop() else {
// If we reached the end we get 10
number.push_str("10");
return;
return true;
};
// Stop at decimal
if digit == '.' {
Expand All @@ -529,6 +531,7 @@ fn round(number: &mut String) {
// Round up
let _ = write!(number, "{}", last_digit + 1);
}
false
}
fn truncate(number: &Integer, consts: &Consts) -> (String, bool) {
let prec = consts.float_precision;
Expand Down Expand Up @@ -582,17 +585,21 @@ fn format_float(acc: &mut String, number: &Float, consts: &Consts) -> std::fmt::
let mut number = number.clone();
let negative = number.is_sign_negative();
number = number.abs();
if number == 0 {
return acc.write_char('0');
}
let exponent = number
.clone()
.log10()
.max(&Float::new(consts.float_precision))
.to_integer_round(factorion_math::rug::float::Round::Down)
.expect("Could not round exponent")
.0;
if exponent > consts.number_decimals_scientific {
if exponent > consts.number_decimals_scientific
|| exponent < -(consts.number_decimals_scientific as isize)
{
number = number / Float::with_val(consts.float_precision, &exponent).exp10();
}
let whole_number = number
let mut whole_number = number
.to_integer_round(factorion_math::rug::float::Round::Down)
.expect("Could not get integer part")
.0;
Expand All @@ -603,7 +610,10 @@ fn format_float(acc: &mut String, number: &Float, consts: &Consts) -> std::fmt::
decimal_part.remove(0);
decimal_part.truncate(consts.number_decimals_scientific + 1);
if decimal_part.len() > consts.number_decimals_scientific {
round(&mut decimal_part);
if round(&mut decimal_part) {
decimal_part.clear();
whole_number += 1;
}
}
if let Some(mut digit) = decimal_part.pop() {
while digit == '0' {
Expand All @@ -622,7 +632,9 @@ fn format_float(acc: &mut String, number: &Float, consts: &Consts) -> std::fmt::
acc.write_str(".")?;
acc.write_str(&decimal_part)?;
}
if exponent > consts.number_decimals_scientific {
if exponent > consts.number_decimals_scientific
|| exponent < -(consts.number_decimals_scientific as isize)
{
write!(acc, " × 10^{exponent}")?;
}
Ok(())
Expand Down Expand Up @@ -1503,4 +1515,58 @@ mod test {
.unwrap();
assert_eq!(acc, "4.9814983749234732849839849898438493843 × 10^(1037)");
}
#[test]
fn test_format_float() {
let mut consts = Consts::default();
let mut acc = String::new();
format_float(
&mut acc,
&Float::with_val(
FLOAT_PRECISION,
Float::parse("0.999999999999999999999999999999999").unwrap(),
),
&consts,
)
.unwrap();
assert_eq!(acc, "1");
let mut acc = String::new();
format_float(
&mut acc,
&Float::with_val(
FLOAT_PRECISION,
Float::parse("0.000000000000000000000000000000009").unwrap(),
),
&consts,
)
.unwrap();
assert_eq!(acc, "9 × 10^-33");
consts.number_decimals_scientific = 2;
acc.clear();
format_float(
&mut acc,
&Float::with_val(FLOAT_PRECISION, Float::parse("0.10").unwrap()),
&consts,
)
.unwrap();
assert_eq!(acc, "0.1");

let consts = Consts::default();
acc.clear();
format_float(
&mut acc,
&Float::with_val(FLOAT_PRECISION, Float::parse("6.631537423e-34").unwrap()),
&consts,
)
.unwrap();
assert_eq!(acc, "6.631537423 × 10^-34");
let consts = Consts::default();
acc.clear();
format_float(
&mut acc,
&Float::with_val(FLOAT_PRECISION, Float::parse("6.631537423e34").unwrap()),
&consts,
)
.unwrap();
assert_eq!(acc, "6.631537423 × 10^34");
}
}
4 changes: 0 additions & 4 deletions factorion-lib/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1484,10 +1484,6 @@ mod test {
&consts,
&NumFormat::V1(&locale::v1::NumFormat { decimal: '.' }),
);
assert_eq!(
num,
Some(Number::Float(Float::with_val(FLOAT_PRECISION, 0.5).into()))
);
let num = parse_num(
&mut "1more !",
false,
Expand Down