WeBWorK Problems

Exact values of sin and cos

Re: Exact values of sin and cos

by Alex Jordan -
Number of replies: 0
I'm not sure if this is any more slick than the hash you describe, but:

Assuming that you are only using those special multiples of pi for angles, then all of these trig values squared are rational numbers. You could take advantage of the Fraction context and do something like the following untested code:

$theta = (list_random(0, pi/6, pi/4, pi/3, pi/2) - pi)*random(-1,1,2)
#get one of the nice angles

$x = sin($theta);
Context("Fraction");

($xSqNum, $xSqDen) = Fraction(int(4*$x**2),4)->value;
#multiplication by 4 is just to make everything integer for a sec

$xNum = (sqrt($xSqNum) == int(sqrt($xSqNum)) ? sqrt($xSqNum) : "sqrt($xSqNum)";
#if numerator is 1 or 4, get 1 or 2; otherwise, get "sqrt(3)"
$xDen = sqrt($xSqDen); #will be integer 1 or 2

$x = ($x < 0) ? Formula("-(x/$xSqDen)")->reduce : Formula("x/$xSqDen")->reduce;
# to get rid of "/1" if needed
Context()->flags->set(reduceConstantFunctions=>0);
$x = $x->substitute(x => Formula($xNum));

should leave $x as a Formula object among what you would want: "1", "0", "sqrt(2)/2", "-sqrt(3)/2",...