From c3d3373b6a3ba62a3a15b7f675884c4266e305e3 Mon Sep 17 00:00:00 2001 From: k9er Date: Thu, 5 Dec 2024 01:01:20 -0800 Subject: [PATCH 1/2] Prevent VM_rint outputting negative zero As the name suggests, rounding to an int should output a value that can actually be stored in an int, which negative zero can't. --- prvm_cmds.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/prvm_cmds.c b/prvm_cmds.c index e540e8517..e56a0f813 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -1511,10 +1511,13 @@ void VM_rint(prvm_prog_t *prog) VM_SAFEPARMCOUNT(1,VM_rint); f = PRVM_G_FLOAT(OFS_PARM0); - if (f > 0) + if (f >= 0) PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5); else - PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5); + { + f = ceil(f - 0.5); + PRVM_G_FLOAT(OFS_RETURN) = f == -0.0 ? 0.0 : f; + } } /* From 5f4488eda116cf4f08d72825ae152636b5f47dce Mon Sep 17 00:00:00 2001 From: k9er Date: Sat, 21 Dec 2024 08:47:38 -0800 Subject: [PATCH 2/2] Improve optimization of VM_rint Changes suggested by @terencehill --- prvm_cmds.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/prvm_cmds.c b/prvm_cmds.c index e56a0f813..25ad3d51f 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -1511,13 +1511,12 @@ void VM_rint(prvm_prog_t *prog) VM_SAFEPARMCOUNT(1,VM_rint); f = PRVM_G_FLOAT(OFS_PARM0); - if (f >= 0) + if (f >= 0.5) // >= 0 works too, but would be slightly less optimized PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5); + else if (f > -0.5) + PRVM_G_FLOAT(OFS_RETURN) = 0; // prevents negative 0 else - { - f = ceil(f - 0.5); - PRVM_G_FLOAT(OFS_RETURN) = f == -0.0 ? 0.0 : f; - } + PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5); } /*