From ae23f6fdf4b06c4bda6be6c7d161079e353a6aae Mon Sep 17 00:00:00 2001 From: Corwin Date: Fri, 2 Jun 2023 01:25:52 +0100 Subject: [PATCH] round towards nearest --- examples/hyperspace-roll/src/level_generation.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/hyperspace-roll/src/level_generation.rs b/examples/hyperspace-roll/src/level_generation.rs index 3daa43d2..523332d6 100644 --- a/examples/hyperspace-roll/src/level_generation.rs +++ b/examples/hyperspace-roll/src/level_generation.rs @@ -45,8 +45,14 @@ fn roll_dice_scaled(number_of_dice: u32, bits_per_dice: u32, width: u32) -> i32 let dice_around_zero = dice - current_mean; - - dice_around_zero * width as i32 / current_width + fn divide_nearest(numerator: i32, denominator: i32) -> i32 { + if (numerator < 0) ^ (denominator < 0) { + (numerator - denominator / 2) / denominator + } else { + (numerator + denominator / 2) / denominator + } + } + divide_nearest(dice_around_zero * width as i32, current_width) } fn default_roll(width: u32) -> i32 {