Dividing an integrer by 0 = crash

Started by CB.., Mon 08/03/2004 11:23:54

Previous topic - Next topic

CB..

i know the soloution is to avoid dividing by 0 but im using ints and constantly changing global variables to try to create a damage sytem for my ye old "subsim"

heres the basic code

int damage1, damage2, result;
damage1 = 1000;
damage2 = dcdepth;
result = 1000/dcdepth;
Display("%d", result);



///dcdepth is GetGlobalInt (7) which is altered by the submarine changing depth/////

this is with the stable 2.61 release
any thoughts? shud i be trying to set it up to stop global int 7 from reaching 0 or is it a bug

Robert Eric

Why do you have damage2 equal dcdepth, then type dcdepth.  Why not just type damage2 for all mention of dcdepth after that point, or remove the line "damage2 = dcdepth;"?
Ã, Ã, 

CB..

#2
 ;D im a newbie what can i say!!

the dcdepth is used elswhere to control an animated depth guage..so i thought i'd try to re-use the same int (as it has the same function...telling the game how deep the submarine is)
re-using the dcdepth name tage for the int allows me to easily keep track of what int is doing what (my brain is only 64mb) it all works but if dcdepth hits 0 (ie the sub is surfaced) then the game crashes...it does show an inform CJ message so i thought id post the question

Scorpiorus

#3
Quote...it all works but if dcdepth hits 0 (ie the sub is surfaced) then the game crashes...it does show an inform CJ message so i thought id post the question
Yep, it would display error message like

Error: run_text_script1: error -6 (Error (line 30): Integer divide by zero) running function 'room_a'


because division by zero is an illegal operation (i.e. AGS won't process it but display the appropriate error message). You need to check it before dividing, like:

if (dcdepth != 0) // if there is no potential division by zero
result = 1000/dcdepth; // perform operation
else {

// skip that part or do something if it's zero

}

~Cheers

CB..

 ;D many thanks again , that solves it

Pumaman

It's not possible for AGS to handle this, since there is mathematically no valid value to set the result of a division by zero to (it's infinity).

Therefore, you need to check yourself as Scorpiorus suggests, and avoid dividing by zero.

Gregjazz

Yes! CJ agrees with me on the divide by zero issue! As long as you are dealing with only positive numbers, any number divided by zero equals infinity. Anyways, I don't want to cause an argument. :)

CB, you could probably just put a catch that will ignore the division if the denominator is zero.

RickJ

The problem is that's not always the desired action.  That solution is likely to cause more problems than it fixes and those problems would be harder to find.        

CB..

ok many thanks guys that makes sense, will do// maths has never been my strong point i have to admit

Gilbert

Heh better stay undefined for convention divisions in the division by zero outcome, as under normal definitions, the sets of the divisors exclude zero.

Ok enough math craps...

SMF spam blocked by CleanTalk