]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
mod() builtin: match gmqcc and fteqw
authorbones_was_here <bones_was_here@xonotic.au>
Fri, 21 Jul 2023 08:34:28 +0000 (18:34 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Fri, 21 Jul 2023 08:38:51 +0000 (18:38 +1000)
Also catch div0, he escaped again

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
prvm_cmds.c
prvm_execprogram.h

index fc39fa33067c6ea2440d4f982835f2969dfcd062..6535ea0ad04074b19b830c3a29a302998471d819 100644 (file)
@@ -3064,13 +3064,21 @@ float   mod(float val, float m)
 */
 void VM_modulo(prvm_prog_t *prog)
 {
-       prvm_int_t val, m;
+       vec_t val, m;
+
        VM_SAFEPARMCOUNT(2, VM_modulo);
 
-       val = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM0);
-       m       = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM1);
+       val = PRVM_G_FLOAT(OFS_PARM0);
+       m   = PRVM_G_FLOAT(OFS_PARM1);
 
-       PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) (val % m);
+       // matches how gmqcc implements % when mod() builtin isn't defined, and FTEQW mod()
+       if (m)
+               PRVM_G_FLOAT(OFS_RETURN) = val - m * (prvm_int_t)(val / m);
+       else
+       {
+               VM_Warning(prog, "Attempted modulo of %f by zero\n", val);
+               PRVM_G_FLOAT(OFS_RETURN) = 0;
+       }
 }
 
 static void VM_Search_Init(prvm_prog_t *prog)
index d9331426cf6e11ee873bf8ab3f34af3f19d6270e..99a8990688682f4b87b44ffd5cf3a74b90fc3365 100644 (file)
@@ -377,11 +377,8 @@ int i;
                                }
                                else
                                {
-                                       if (developer.integer)
-                                       {
-                                               PRE_ERROR();
-                                               VM_Warning(prog, "Attempted division by zero in %s\n", prog->name );
-                                       }
+                                       PRE_ERROR();
+                                       VM_Warning(prog, "Attempted division of %f by zero\n", OPA->_float);
                                        OPC->_float = 0.0f;
                                }
                                DISPATCH_OPCODE();