Division by zero in Papyrus

Post » Sat Nov 17, 2012 2:06 pm

Dividing by zero in Papyrus seems to always return the value 1. I currently have a script where some trigonometry is used, and sometimes one of the variables is zero which messes up the results. I do not want the result to be 1 in that case, but rather just have no effect at all, i.e. 36 / 0 returns 36.

What I'm curious about is if there is any other way to handle this except for checking if variables are 0 before calculation takes place and if a var is 0 doing a different set of calculations.

Is this a common problem in programming and how is it usually handled?
User avatar
Jonathan Windmon
 
Posts: 3410
Joined: Wed Oct 10, 2007 12:23 pm

Post » Sat Nov 17, 2012 6:15 pm

It's probably a fail-safe to prevent the script from crashing. Division by zero is an illegal arithmetical function.
User avatar
jessica robson
 
Posts: 3436
Joined: Mon Oct 09, 2006 11:54 am

Post » Sat Nov 17, 2012 3:16 pm

Other languages often have exception handling mechanisms which can trap divisions by zero, but still, I think, the normal way of handling the issue is to check the variable for zero first. :)
User avatar
Shae Munro
 
Posts: 3443
Joined: Fri Feb 23, 2007 11:32 am

Post » Sat Nov 17, 2012 4:42 pm

It's probably a fail-safe to prevent the script from crashing. Division by zero is an illegal arithmetical function.
And an illegal function of the universe.

If Beth hadn't put this in the code, we wouldn't be here right now.

/bad jokes

*leaves thread*
User avatar
.X chantelle .x Smith
 
Posts: 3399
Joined: Thu Jun 15, 2006 6:25 pm

Post » Sun Nov 18, 2012 12:12 am

I'd prefer to get log messages about attempts to divide by zero rather than having it return 1 with no notice.
User avatar
Tracy Byworth
 
Posts: 3403
Joined: Sun Jul 02, 2006 10:09 pm

Post » Sat Nov 17, 2012 1:05 pm

This is WAAAAAAAAAYYYYY better than in Oblivion:


If actor.isincombat > 0 && actor.isincombat < 0
If 1 == 3
x = 1/0
endif
endif


EVEN THAT would STILL CTD in Oblivion! I had to go thru hundreds of my scripts from 6 years of mods to check /0 errors once I discovered that in Oblivion.

If x > 0
z = y/x
endif

I had to change to:

z = y/(x+0.001)

To stop the CTD in Oblivion. Not a great way to deal with it but there was no way I was going to rework hundreds of my scripts at that point.


Dividing by zero in Papyrus seems to always return the value 1. I currently have a script where some trigonometry is used, and sometimes one of the variables is zero which messes up the results. I do not want the result to be 1 in that case, but rather just have no effect at all, i.e. 36 / 0 returns 36.

What I'm curious about is if there is any other way to handle this except for checking if variables are 0 before calculation takes place and if a var is 0 doing a different set of calculations.

Is this a common problem in programming and how is it usually handled?
User avatar
Nauty
 
Posts: 3410
Joined: Wed Jan 24, 2007 6:58 pm

Post » Sat Nov 17, 2012 11:32 pm

I'd prefer to get log messages about attempts to divide by zero rather than having it return 1 with no notice.

You'll get a "Cannot divide by zero" error in the log with full stack trace and the division will be ignored. The "1" in the above case may be whatever is left over in the temp variable the compiler made after the divide opcode was skipped. In short, divide by 0 is undefined and will spit out an appropriate error.
User avatar
Ellie English
 
Posts: 3457
Joined: Tue Jul 11, 2006 4:47 pm

Post » Sat Nov 17, 2012 1:43 pm

Ah, good to know, thanks.
User avatar
Guy Pearce
 
Posts: 3499
Joined: Sun May 20, 2007 3:08 pm


Return to V - Skyrim