From 78203fc219f8c21459303c4e4ff4e03ea85a20c7 Mon Sep 17 00:00:00 2001 From: QuanTech0 <1337h4x0rname@gmail.com> Date: Wed, 1 Jan 2020 23:03:35 -0500 Subject: [PATCH] Division by Zero fix (#689) --- src/simulation/elements/DEUT.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/simulation/elements/DEUT.cpp b/src/simulation/elements/DEUT.cpp index ff613cd02..1e2926a84 100644 --- a/src/simulation/elements/DEUT.cpp +++ b/src/simulation/elements/DEUT.cpp @@ -1,4 +1,5 @@ #include "simulation/ElementCommon.h" +#include "common/tpt-minmax.h" //#TPT-Directive ElementClass Element_DEUT PT_DEUT 95 Element_DEUT::Element_DEUT() { @@ -52,8 +53,10 @@ int Element_DEUT::update(UPDATE_FUNC_ARGS) { int r, rx, ry, trade, np; float gravtot = fabs(sim->gravy[(y/CELL)*(XRES/CELL)+(x/CELL)])+fabs(sim->gravx[(y/CELL)*(XRES/CELL)+(x/CELL)]); - int maxlife = ((10000/(parts[i].temp + 1))-1); - if (RNG::Ref().chance(10000 % static_cast(parts[i].temp + 1), static_cast(parts[i].temp + 1))) + // Prevent division by 0 + float temp = std::max(1.0f, (parts[i].temp + 1)); + int maxlife = ((10000/(temp + 1))-1); + if (RNG::Ref().chance(10000 % static_cast(temp + 1), static_cast(temp + 1))) maxlife++; // Compress when Newtonian gravity is applied // multiplier=1 when gravtot=0, multiplier -> 5 as gravtot -> inf