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.5.0"
version = "2.5.1"
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 = "5.0.0", features = ["serde", "influxdb"] }
factorion-lib = { path = "../factorion-lib", version = "5.0.1", 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.6.0"
version = "5.6.1"
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 = "5.0.0", features = ["serde", "influxdb"]}
factorion-lib = {path = "../factorion-lib", version = "5.0.1", 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 = "5.0.0"
version = "5.0.1"
edition = "2024"
description = "A library used to create bots to recognize and calculate factorials and related concepts"
license = "MIT"
Expand Down
2 changes: 2 additions & 0 deletions factorion-lib/src/calculation_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::format::{
use crate::impl_all_bitwise;
use crate::impl_bitwise;
use factorion_math::length;
use factorion_math::rug::Complete;
use factorion_math::rug::integer::IntegerExt64;
#[cfg(any(feature = "serde", test))]
use serde::{Deserialize, Serialize};
use std::ops::BitAnd;
Expand Down
47 changes: 33 additions & 14 deletions factorion-lib/src/calculation_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ impl CalculationJob {
let prec = consts.float_precision;
let calc_num = match num {
CalculationResult::Approximate(base, exponent) => {
let res = base.as_float() * Float::with_val(prec, 10).pow(&exponent);
if Float::is_finite(&(res.clone() * math::APPROX_FACT_SAFE_UPPER_BOUND_FACTOR)) {
res.to_integer().unwrap()
if exponent <= consts.integer_construction_limit {
let x: Float = base.as_float() * Float::with_val(prec, 10).pow(&exponent);
x.to_integer().unwrap()
} else {
return Some(if base.as_float() < &0.0 {
CalculationResult::ComplexInfinity
Expand All @@ -143,7 +143,11 @@ impl CalculationJob {
(Float::from(base), exponent),
-level as u32,
);
CalculationResult::Approximate(termial.0.into(), termial.1)
if termial.0 == 1 {
CalculationResult::ApproximateDigits(false, termial.1)
} else {
CalculationResult::Approximate(termial.0.into(), termial.1)
}
} else {
let mut exponent = exponent;
exponent.add_from(math::length(&exponent, prec));
Expand All @@ -152,17 +156,32 @@ impl CalculationJob {
}
}
CalculationResult::ApproximateDigits(was_neg, digits) => {
return Some(if digits.is_negative() {
CalculationResult::Float(Float::new(prec).into())
} else if was_neg {
CalculationResult::ComplexInfinity
} else if level < 0 {
CalculationResult::ApproximateDigits(false, (digits - 1) * 2 + 1)
if digits <= consts.integer_construction_limit {
let x: Float = Float::with_val(prec, 10).pow(digits.clone() - 1);
x.to_integer().unwrap()
} else {
let mut digits = digits;
digits.add_from(math::length(&digits, prec));
CalculationResult::ApproximateDigitsTower(false, false, 1.into(), digits)
});
return Some(if digits.is_negative() {
CalculationResult::Float(Float::new(prec).into())
} else if was_neg {
CalculationResult::ComplexInfinity
} else if level < 0 {
let mut one = Float::with_val(consts.float_precision, 1);
if was_neg {
one *= -1;
}
let termial =
math::approximate_approx_termial((one, digits), -level as u32);
if termial.0 == 1 {
CalculationResult::ApproximateDigits(false, termial.1)
} else {
CalculationResult::Approximate(termial.0.into(), termial.1)
}
} else {
let mut digits = digits;
digits.add_from(math::length(&digits, prec));
CalculationResult::ApproximateDigitsTower(false, false, 1.into(), digits)
});
}
}
CalculationResult::ApproximateDigitsTower(was_neg, neg, depth, exponent) => {
return Some(if neg {
Expand Down
49 changes: 37 additions & 12 deletions factorion-lib/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module holds the underlying formatting functions used in [`calculation_result`]
use crate::{Consts, locale};
use factorion_math::rug::{Float, Integer, ops::Pow};
use factorion_math::rug::{Complete, Float, Integer, integer::IntegerExt64, ops::Pow};
use std::{borrow::Cow, fmt::Write};

const SINGLES: [&str; 10] = [
Expand Down Expand Up @@ -293,11 +293,16 @@ pub fn truncate(number: &Integer, consts: &Consts) -> (String, bool) {
.to_integer_round(crate::rug::float::Round::Down)
.unwrap()
.0;
let truncated_number: Integer = &number
/ (Float::with_val(prec, 10)
.pow((length.clone() - consts.number_decimals_scientific - 1u8).max(Integer::ZERO))
.to_integer()
.unwrap());
let ten_exp = Integer::u64_pow_u64(
10,
(length.clone() - consts.number_decimals_scientific - 1u8)
.max(Integer::ZERO)
.to_u64()
.unwrap(),
)
.complete();
let rough = !number.is_divisible(&ten_exp);
let truncated_number: Integer = &number / ten_exp;
let mut truncated_number = truncated_number.to_string();
if truncated_number.len() > consts.number_decimals_scientific {
round(&mut truncated_number);
Expand All @@ -319,7 +324,11 @@ pub fn truncate(number: &Integer, consts: &Consts) -> (String, bool) {
truncated_number.insert(0, '-');
}
if length > consts.number_decimals_scientific + 1 {
(format!("{truncated_number} × 10^{length}"), true)
if truncated_number == "1" {
(format!("10^{length}"), rough)
} else {
(format!("{truncated_number} × 10^{length}"), rough)
}
} else {
(orig_number.to_string(), false)
}
Expand Down Expand Up @@ -530,23 +539,39 @@ mod tests {
&consts
)
.0,
"1 × 10^300"
"10^300"
);
assert_eq!(
truncate(
&Integer::from_str(&format!("1{}", "0".repeat(300))).unwrap(),
&consts
)
.1,
false
);
assert_eq!(
truncate(
&Integer::from_str(&format!("2{}", "0".repeat(300))).unwrap(),
&consts
)
.0,
"2 × 10^300"
);
assert_eq!(
truncate(
&-Integer::from_str(&format!("1{}", "0".repeat(300))).unwrap(),
&-Integer::from_str(&format!("2{}", "0".repeat(300))).unwrap(),
&consts
)
.0,
"-1 × 10^300"
"-2 × 10^300"
);
assert_eq!(
truncate(
&Integer::from_str(&format!("1{}", "0".repeat(2000000))).unwrap(),
&Integer::from_str(&format!("2{}", "0".repeat(2000000))).unwrap(),
&consts
)
.0,
"1 × 10^2000000"
"2 × 10^2000000"
);
}

Expand Down
39 changes: 31 additions & 8 deletions factorion-lib/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
pub mod recommended {
use factorion_math::rug::Integer;

pub static INTEGER_CONSTRUCTION_LIMIT: fn() -> Integer = || 10_000_000u128.into();
pub static INTEGER_CONSTRUCTION_LIMIT: fn() -> Integer = || 200_000_000u128.into();
}

const POI_STARTS: &[char] = &[
Expand Down Expand Up @@ -579,12 +579,19 @@ fn parse_num(
*text = &text[3..];
let part = part.replace(SEPARATORS, "");
let n = part.parse::<Integer>().ok()?;
return Some(Number::ApproximateDigitsTower(
false,
false,
n - 1,
1.into(),
));
match n.to_usize() {
Some(0) => return Some(1.into()),
Some(1) => return Some(10.into()),
Some(2) => return Some(Number::Exact(10_000_000_000u64.into())),
_ => {
return Some(Number::ApproximateDigitsTower(
false,
false,
n - 1,
1.into(),
));
}
}
} else {
// Skip ^ only (because ret None)
*text = orig_text;
Expand Down Expand Up @@ -666,6 +673,11 @@ fn parse_num(
}

if depth == 1 {
if top < consts.integer_construction_limit {
return Some(Number::Exact(
Integer::u64_pow_u64(10, top.to_u64().unwrap()).complete(),
));
}
return Some(Number::ApproximateDigits(false, top));
} else {
return Some(Number::ApproximateDigitsTower(
Expand Down Expand Up @@ -1846,6 +1858,17 @@ mod test {
5.into()
))
);
let num = parse_num(
&mut "10^50000000000",
false,
false,
&consts,
&NumFormat::V1(&locale::v1::NumFormat { decimal: '.' }),
);
assert_eq!(
num,
Some(Number::ApproximateDigits(false, 50000000000u64.into()))
);
let num = parse_num(
&mut "10^5!",
false,
Expand All @@ -1861,7 +1884,7 @@ mod test {
&consts,
&NumFormat::V1(&locale::v1::NumFormat { decimal: '.' }),
);
assert_eq!(num, Some(Number::ApproximateDigits(false, 5.into())));
assert_eq!(num, Some(Number::Exact(100000.into())));
let num = parse_num(
&mut "10^5",
false,
Expand Down
Loading