Skip to content

Instantly share code, notes, and snippets.

@ejangi
Created June 28, 2024 23:47
Show Gist options
  • Save ejangi/6cc6885e77d25cbbde13055b3788b78f to your computer and use it in GitHub Desktop.
Save ejangi/6cc6885e77d25cbbde13055b3788b78f to your computer and use it in GitHub Desktop.
// If I have an integer between 4 and 5 called avg and I have another integer between 5000 and 100000 called count how could I logarithmically reduce the avg integer as the count gets closer to its maximum value? Can you write me a function to do this in rust?
fn logarithmic_reduce(avg: f64, count: u32, max_count: u32) -> f64 {
// Ensure count does not exceed max_count to prevent negative logarithm values
let clamped_count = count.min(max_count);
// Scale factor to ensure the reduction effect
let scale = (clamped_count as f64) / (max_count as f64);
// Logarithmic reduction effect
let reduction = avg * (1.0 - (scale.ln() / (1.0f64.ln())));
// Clamp the value to be between 4 and avg
reduction.max(4.0)
}
fn main() {
let avg = 5.0;
let count = 10000;
let max_count = 100000;
let reduced_avg = logarithmic_reduce(avg, count, max_count);
println!("Reduced avg: {}", reduced_avg);
}
/*
Explanation:
1. Clamp the count: Ensure that count does not exceed max_count to avoid negative logarithmic values.
2. Calculate the scale: This is the ratio of count to max_count, which gives a value between 0 and 1.
3. Logarithmic reduction: Use the natural logarithm of the scale factor to create a reduction effect. The scale.ln() / (1.0f64.ln()) part normalizes the logarithm to a 0-1 range.
4. Clamp the result: Ensure that the reduced value does not go below 4.
This function will reduce avg logarithmically as count approaches max_count. You can adjust the parameters and the clamping values as needed to fit your specific requirements.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment