Skip to content

Commit

Permalink
Make division by 0 return NAN when output is a floating point type, n…
Browse files Browse the repository at this point in the history
…ot 0.

For now the division by zero warning is kept.

Fixed #240.
  • Loading branch information
divVerent committed Jan 6, 2025
1 parent 99e5a50 commit 95b1362
Showing 1 changed file with 11 additions and 29 deletions.
40 changes: 11 additions & 29 deletions prvm_execprogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,12 @@ prvm_eval_t *src;
OPC->vector[2] = tempfloat * OPA->vector[2];
DISPATCH_OPCODE();
HANDLE_OPCODE(OP_DIV_F):
if( OPB->_float != 0.0f )
{
OPC->_float = OPA->_float / OPB->_float;
}
else
if( OPB->_float == 0.0f )
{
PRE_ERROR();
VM_Warning(prog, "Attempted division of %f by zero\n", OPA->_float);
OPC->_float = 0.0f;
}
OPC->_float = OPA->_float / OPB->_float;
DISPATCH_OPCODE();
HANDLE_OPCODE(OP_BITAND_F):
OPC->_float = (prvm_int_t)OPA->_float & (prvm_int_t)OPB->_float;
Expand Down Expand Up @@ -884,21 +880,15 @@ prvm_eval_t *src;
OPC->vector[2] = (prvm_vec_t) OPB->_int * OPA->vector[2];
DISPATCH_OPCODE();
HANDLE_OPCODE(OP_DIV_VF):
if( OPB->_float != 0.0f )
{
tempfloat = OPB->_float;
OPC->vector[0] = OPA->vector[0] / tempfloat;
OPC->vector[1] = OPA->vector[1] / tempfloat;
OPC->vector[2] = OPA->vector[2] / tempfloat;
}
else
if( OPB->_float == 0.0f )
{
PRE_ERROR();
VM_Warning(prog, "Attempted division of '%f %f %f' by zero\n", OPA->vector[0], OPA->vector[1], OPA->vector[2]);
OPC->vector[0] = 0.0f;
OPC->vector[1] = 0.0f;
OPC->vector[2] = 0.0f;
}
tempfloat = OPB->_float;
OPC->vector[0] = OPA->vector[0] / tempfloat;
OPC->vector[1] = OPA->vector[1] / tempfloat;
OPC->vector[2] = OPA->vector[2] / tempfloat;
DISPATCH_OPCODE();
HANDLE_OPCODE(OP_DIV_I):
// NOTE: This also catches the second kind of division that can trap, namely, -2147483648 / -1,
Expand All @@ -915,28 +905,20 @@ prvm_eval_t *src;
}
DISPATCH_OPCODE();
HANDLE_OPCODE(OP_DIV_IF):
if( OPB->_float != 0.0f )
{
OPC->_float = ((prvm_vec_t) OPA->_int) / OPB->_float;
}
else
if( OPB->_float == 0.0f )
{
PRE_ERROR();
VM_Warning(prog, "Attempted division of %"PRVM_PRIi" by zero\n", OPA->_int);
OPC->_float = 0;
}
OPC->_float = ((prvm_vec_t) OPA->_int) / OPB->_float;
DISPATCH_OPCODE();
HANDLE_OPCODE(OP_DIV_FI):
if( OPB->_int != 0 )
{
OPC->_float = OPA->_float / (prvm_vec_t) OPB->_int;
}
else
if( OPB->_int == 0 )
{
PRE_ERROR();
VM_Warning(prog, "Attempted division of %f by zero\n", OPA->_float);
OPC->_float = 0;
}
OPC->_float = OPA->_float / (prvm_vec_t) OPB->_int;
DISPATCH_OPCODE();
HANDLE_OPCODE(OP_CONV_ITOF):
OPC->_float = OPA->_int;
Expand Down

0 comments on commit 95b1362

Please sign in to comment.