WeBWorK Main Forum

Avoid zero denominator

Avoid zero denominator

by Adam Z -
Number of replies: 9

Hello all: 

these are answers of the one of the probelm i just created. all are random variables:

$ans3 = ($c/$a)-(($f*$a*$b-$c*$b*$d)/($e*$a*$a-$b*$d*$a));

$ans4 = ($f*$a-$c*$d)/($e*$a-$b*$d);

$a is defined already to be non zero random 

My question is how to avoid having zero in the denominator for these :   $e*$a*$a-$b*$d*$a  , and  $e*$a-$b*$d canot be zero?

Best


 


In reply to Adam Z

Re: Avoid zero denominator

by Andras Balogh -
You can regenerate the random variables in a do-while loop. Something like
do{ $e=random...; $a=random...; ... }while( $e*$a*$a-$b*$d*$a == 0);
In reply to Adam Z

Re: Avoid zero denominator

by Alex Jordan -

For example, if you had:

$a = non_zero_random(-9,9,1);
$b = non_zero_random(-9,9,1);
$d = non_zero_random(-9,9,1);
$e = non_zero_random(-9,9,1);

$ans3 = ($c/$a)-(($f*$a*$b-$c*$b*$d)/($e*$a*$a-$b*$d*$a));
$ans4 = ($f*$a-$c*$d)/($e*$a-$b*$d);


You could change it to

do {
$a = non_zero_random(-9,9,1);

$b = non_zero_random(-9,9,1);
$d = non_zero_random(-9,9,1);
$e = non_zero_random(-9,9,1);
} until ($e*$a - $b*$d != 0);

$ans3 = ($c/$a)-(($f*$a*$b-$c*$b*$d)/($e*$a*$a-$b*$d*$a));
$ans4 = ($f*$a-$c*$d)/($e*$a-$b*$d);


That will re-loop the variable initializations until neither denominator is 0.

Or an alternative is to define $a in a way that guarantees these denominators to be nonzero. It seems that if $a is nonzero, not ±1, and also relatively prime to $b*$d, that would prevent the denominators from being 0. So the line that makes $a could be:

do {$a = list_random(-9,..-2,2..9)} until (gcd($a,$b*$d) == 1);


With more information about how/why you are making $a, $b, $d, and $e, you could possibly define them all deterministically to avoid division by 0. (As opposed to these approaches above which are probabilistic and introduce some inefficiency because of running a loop.)



In reply to Alex Jordan

Re: Avoid zero denominator

by Adam Z -
Thank you very much. This is so clear. I will fix it and come back since I still have another question :)
In reply to Adam Z

Re: Avoid zero denominator

by Adam Z -
What I am trying to do here is create a problem of solving system of linear equations of this format
$ax+$by=$c
$dx+$ey=$f

I am still learning this language. I am also stuck with the no solution or infinity many solution cases, I am trying to use IF else but generates errors.
In reply to Adam Z

Re: Avoid zero denominator

by Alex Jordan -

In case it is helpful, let me recommend against coding a problem where for some random versions there is one solution, and for some random versions there are no solutions, and for some random versions there are infinitely many.

Once upon a time that is how I coded WeBWorK questions. Just randomize the parameters that are displayed to the student, and code the rest of it to handle whatever happened next. But after time I came to decide this was a mistake. It is a different pedagogical experience for the student who encounters this system with one solution than it is for the student who encounters a version with no solutions (or infinitely many).

So if it were me, I'd code three problem files. And rest knowing that I had exposed each student to each of the three situations in an exercise set.



In reply to Alex Jordan

Re: Avoid zero denominator

by Andras Balogh -
I agree that it is better to cover all special cases instead of randomly choosing one.
Regarding learning the language l usually use existing library problems as examples.
In reply to Alex Jordan

Re: Avoid zero denominator

by Adam Z -

I agree, I thought about it and decided to have three different questions. Thanks for sharing your thoughts.