WeBWorK Problems

scientific notation in Sage is parsed incorrectly by Webwork

scientific notation in Sage is parsed incorrectly by Webwork

by Boxuan Zhong -
Number of replies: 1

For the below example code : p_minZeta will be computed to be -1 + 0.707*I
Then Text2 = p_minZeta+1 should equal 0.707*I;

However, p_minZeta actually equals i*0.408248*sqrt(3)  + 2.22045*exp(1) -16 in the example code instead of 0.707*I.  

My understanding is : due to calculated error, "0" becomes 2.22045*e^(-16) in Sage but webwork mistakenly converts it to be 2.22045*exp(1) -16, which is -9.96. Is that correct?How can we solve this problem?

##############################
#  Initialization

DOCUMENT(); 

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"parserFunction.pl",
"answerHints.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

###########################
#  Setup

Context("Complex");
Context()->constants->add( I=>{value => i} );

$z1=-2;
$z2=-3;
$p1 = 0;
$p2 = -1;
$pi = pi;

$SageCode1=<<END;
####The code below is to generate p_minZeta = -1 + 0.707*I
p1 = $p1;
p2 = $p2;
z1 = $z1;
z2 = $z2;
var('x')
f= (x-p1)*(x-p2)*(x-0.5*(z1+z2))-(x-z1)*(x-z2)*(x-0.5*(p1+p2))
Root = f.roots()
xc = 0.5*(Root[0][0]+Root[1][0])
R = 0.5*(Root[1][0] - Root[0][0])
d = sqrt(xc*xc -R*R)
a = atan2(R,d)
I = ComplexDoubleElement(0,1)
p_minZeta = -cos(a)*d + sin(a)*d*I;
####The code above is to generate p_minZeta = -1 + 0.707*I

#p_minZeta = -1 + 0.707*I;# if I do this line, the answer will be correct. 
Text1 = p_minZeta
Text2 = p_minZeta+1
Text3 = abs(p_minZeta+1)


WEBWORK['Text1']=Text1 
WEBWORK['Text2']=Text2 
WEBWORK['Text3']=Text3 

END

$result = AskSage($SageCode1,{
       accepted_tos=>true, 
       debug=>0,  
});


Context()->{format}{number} = "%.2f"; 
#################################
$Text1= Compute($result->{webwork}->{Text1});
$Text2= Compute($result->{webwork}->{Text2});
$Text3= Compute($result->{webwork}->{Text3});
$Text4 = (1+0*i)*$Text2; # in order to change formula to numbers
$Text5 = (1+0*i)*$Text3; # in order to change formula to numbers
TEXT(pretty_print($result->{webwork}));
#############################
#  Main text

Context()->texStrings;
BEGIN_TEXT
$BR
 \( Text1 = \)  \{ ans_rule(10)\} ,  \(Text2= \) \{ ans_rule(10)\} ,  \(Text3= \) \{ ans_rule(10)\},  \(Text4= \) \{ ans_rule(10)\} ,  \(Text5= \) \{ ans_rule(10)\}
$PAR


END_TEXT
Context()->normalStrings;


##############################
#  Answer evaluation

$showPartialCorrectAnswers = 1;

ANS($Text1->cmp());
ANS($Text2->cmp());
ANS($Text3->cmp());
ANS($Text4->cmp());
ANS($Text5->cmp());

ENDDOCUMENT();

Attachment _G3_N_U_DC_E_UP_4ARN3.png
In reply to Boxuan Zhong

Re: scientific notation in Sage is parsed incorrectly by Webwork

by Michael Gage -
The basic mismatch is that Sage/python evaluates  e-5 as 10^(-5) while as 
for WeBWorK e is the base of natural logarithms. In WeBWorK   E-5 is equivalent to  10^(-5).

The easiest solution is:

$result = AskSage($SageCode1,{
       accepted_tos=>true, 
       debug=>0,  
});

$result->{webwork}->{Text2}=~s/e/E/g;
$result->{webwork}->{Text3}=~s/e/E/g;

The last two lines replace all of the e's in the string with E.  This simple replacement might not work for all types of answers but it will work with this one since the answers from Sage look like:

0.40824829046386296*I*sqrt(3) + 2.22044604925031e-16

You can also use

ANS($Text1->cmp());
ANS($Text2->cmp());
ANS($Text3->cmp());
ANS(Compute($Text2)->cmp());
ANS(Compute($Text3)->cmp());

To force the last two answers to print simply as 0.71i instead of 0.41*I*sqrt(3.00)+0.
This looks at the string created by $Text2 and then makes a new MathObject from that.
Your method of multiplying by 1 works just as well. The important part is that you
changed the context of the format so that only 2 digits are printed. This only changes the formatting of printed version of the correct answer -- so the answer with the square root will be accepted as will the answer 0.70711i but the answer 0.71i does not have enough precision to be accepted.  I'd change the format to print 5 figures of accuracy.