WeBWorK Problems

gcd values from variables create zero division errors?

gcd values from variables create zero division errors?

by tim Payer -
Number of replies: 3
Hello  folks,

Can you please explain why a   $gc = gcd($a,$b) command would create a zero division error?

The values of $a = -64, and $b = 19683 have a gcd of 1.

If I use their numeric equivalents of : $gc = gcd(-64, 19683) the gcd is still one and the code block will run fine, but when the $a and $b are used the $gc value creates a zero division error when placed in the denominator.

Please help!  Is this something that should be obvious?

Thanks, tim

In reply to tim Payer

Re: gcd values from variables create zero division errors?

by Alex Jordan -
Could you post the full block of code that makes this happen? Either pasted if it is a small snippet easily inserted into a pg file, or attached as a pg file?


In reply to tim Payer

Re: gcd values from variables create zero division errors?

by Alex Jordan -
Tim sent me code. In a nutshell, the issue is that gcd(0,0) is 0, so you need to be aware of that when coding.

The reason gcd(0,0)=0 is for one thing, what else would it be? 1 divides them both; 23 divides them both, etc.

But also, here is a discussion of why: http://math.stackexchange.com/questions/495119/what-is-gcd0-0
In reply to tim Payer

Re: gcd values from variables create zero division errors?

by Michael Gage -
Just a quick check.  Does the same thing happen if you use the variables
$c = -64 and $d=19683 and calculuate gc($c,$d)?

$a and $b have special meanings in perl for historical reasons.  I think we've prevented unexpected behavior in PG code when using these variables but just in case try using $c and $d instead and see if the behavior changes.

-- Mike