<?xml version="1.0" encoding="UTF-8"?>
<quiz>
  <question type="stack">
    <name>
      <text>HELM 30.1 E2 Loss of precision</text>
    </name>
    <questiontext format="markdown">
      <text>**30.1 E2 Loss of precision**

Suppose we wish to calculate

\\\[f(x)=\sqrt{x+1}-\sqrt{x}\\\]

for relatively large values of \\\(x\\\). 

The following value was computed with  double precision (16 mantissa digits) and displayed with 14 d.p.:

{@table([x,f(x)],[v, dispdp(fref,14)])@}

Compute \\\(f(x)\\\) using {#n#} significant digits (round the intermediate result to this precision). 

\\\(f(x)\approx(\rm{{@n@}\,s.f.})\\\) [[input:S_fn]] [[validation:S_fn]] [[feedback:fn]]

How many digits of the result are correct?
[[input:S_sf]] [[validation:S_sf]] [[feedback:sf]]</text>
    </questiontext>
    <generalfeedback format="markdown">
      <text/>
    </generalfeedback>
    <defaultgrade>1</defaultgrade>
    <penalty>0.1</penalty>
    <hidden>0</hidden>
    <idnumber/>
    <stackversion>
      <text>2024012900</text>
    </stackversion>
    <questionvariables>
      <text><![CDATA[v: rand_with_step(1,9,1)*10^rand([2,3]);
n: rand_with_step(4,6,1);
fref: float(significantfigures(sqrt(v+1)-sqrt(v),14));
fn: decimalplaces(significantfigures(sqrt(v+1),n)-significantfigures(sqrt(v),n),10);

asf(a,b):= block(
    if b = 0.0 or b=0 then return(0),
    OMa: floor(float(log(abs(a))/log(10))),
    OMb: floor(float(log(abs(b))/log(10))),
    if OMa < 0 then 
           OMa: -smismatch(string(abs(a)),"0.00000000000000000000000000")+2 else
           OMa: slength(split(string(abs(a)),".")[1])-1,
    if OMb < 0 then 
           OMb: -smismatch(string(abs(b)),"0.00000000000000000000000000")+2 else
           OMb: slength(split(string(abs(b)),".")[1])-1,
    if OMa # OMb then return(0),
    sna:string(float(abs(a)/10^(OMa+1))),
    snb:string(float(abs(b)/10^(OMb+1))),
    ad: smismatch(sna,snb)-3,
    if round(a/10^(OMa+1)*10^(ad))#round(b/10^(OMb+1)*10^(ad)) then return(ad-1),
    if round(a/10^(OMa+1)*10^(ad+1))=round(b/10^(OMb+1)*10^(ad+1)) and ad<(slength(snb)-2) 
    then ad: ad+1,
ad); 

sf: asf(fref,fn);]]></text>
    </questionvariables>
    <specificfeedback format="markdown">
      <text/>
    </specificfeedback>
    <questionnote format="moodle_auto_format">
      <text>{@fref@}, {@fn@},  {@sf@}</text>
    </questionnote>
    <questiondescription format="markdown">
      <text/>
    </questiondescription>
    <questionsimplify>1</questionsimplify>
    <assumepositive>0</assumepositive>
    <assumereal>0</assumereal>
    <prtcorrect format="markdown">
      <text>Richtige Antwort, gut gemacht!</text>
    </prtcorrect>
    <prtpartiallycorrect format="markdown">
      <text>Ihre Antwort ist teilweise korrekt.</text>
    </prtpartiallycorrect>
    <prtincorrect format="markdown">
      <text>Falsche Antwort.</text>
    </prtincorrect>
    <decimals>.</decimals>
    <scientificnotation>*10</scientificnotation>
    <multiplicationsign>dot</multiplicationsign>
    <sqrtsign>1</sqrtsign>
    <complexno>i</complexno>
    <inversetrig>cos-1</inversetrig>
    <logicsymbol>lang</logicsymbol>
    <matrixparens>[</matrixparens>
    <isbroken>0</isbroken>
    <variantsselectionseed/>
    <input>
      <name>S_fn</name>
      <type>numerical</type>
      <tans>fn</tans>
      <boxsize>15</boxsize>
      <strictsyntax>1</strictsyntax>
      <insertstars>0</insertstars>
      <syntaxhint>Number</syntaxhint>
      <syntaxattribute>1</syntaxattribute>
      <forbidwords/>
      <allowwords/>
      <forbidfloat>0</forbidfloat>
      <requirelowestterms>0</requirelowestterms>
      <checkanswertype>0</checkanswertype>
      <mustverify>1</mustverify>
      <showvalidation>3</showvalidation>
      <options/>
    </input>
    <input>
      <name>S_sf</name>
      <type>numerical</type>
      <tans>sf</tans>
      <boxsize>6</boxsize>
      <strictsyntax>1</strictsyntax>
      <insertstars>0</insertstars>
      <syntaxhint>Number</syntaxhint>
      <syntaxattribute>1</syntaxattribute>
      <forbidwords/>
      <allowwords/>
      <forbidfloat>1</forbidfloat>
      <requirelowestterms>0</requirelowestterms>
      <checkanswertype>0</checkanswertype>
      <mustverify>1</mustverify>
      <showvalidation>3</showvalidation>
      <options>intnum</options>
    </input>
    <prt>
      <name>fn</name>
      <value>1.0000000</value>
      <autosimplify>1</autosimplify>
      <feedbackstyle>2</feedbackstyle>
      <feedbackvariables>
        <text/>
      </feedbackvariables>
      <node>
        <name>0</name>
        <description/>
        <answertest>NumAbsolute</answertest>
        <sans>S_fn</sans>
        <tans>fn</tans>
        <testoptions>1e-9</testoptions>
        <quiet>0</quiet>
        <truescoremode>=</truescoremode>
        <truescore>1</truescore>
        <truepenalty/>
        <truenextnode>-1</truenextnode>
        <trueanswernote>vn-1-T</trueanswernote>
        <truefeedback format="html">
          <text/>
        </truefeedback>
        <falsescoremode>=</falsescoremode>
        <falsescore>0</falsescore>
        <falsepenalty/>
        <falsenextnode>-1</falsenextnode>
        <falseanswernote>vn-1-F</falseanswernote>
        <falsefeedback format="html">
          <text/>
        </falsefeedback>
      </node>
    </prt>
    <prt>
      <name>sf</name>
      <value>1.0000000</value>
      <autosimplify>1</autosimplify>
      <feedbackstyle>2</feedbackstyle>
      <feedbackvariables>
        <text/>
      </feedbackvariables>
      <node>
        <name>0</name>
        <description/>
        <answertest>NumAbsolute</answertest>
        <sans>S_sf</sans>
        <tans>sf</tans>
        <testoptions>0.1</testoptions>
        <quiet>1</quiet>
        <truescoremode>=</truescoremode>
        <truescore>1</truescore>
        <truepenalty/>
        <truenextnode>-1</truenextnode>
        <trueanswernote>sf-1-T</trueanswernote>
        <truefeedback format="html">
          <text/>
        </truefeedback>
        <falsescoremode>=</falsescoremode>
        <falsescore>0</falsescore>
        <falsepenalty/>
        <falsenextnode>-1</falsenextnode>
        <falseanswernote>sf-1-F</falseanswernote>
        <falsefeedback format="html">
          <text><![CDATA[Your value for \(f(x)\) does't have {@S_sf@} correct significant digits.<br>For example, 2 s.f. would mean that your solution has at least 2 s.f. and they match the digits of the reference if rounded to 2 s.f.]]></text>
        </falsefeedback>
      </node>
    </prt>
    <deployedseed>844300686</deployedseed>
    <deployedseed>1258067187</deployedseed>
    <deployedseed>1544534811</deployedseed>
    <deployedseed>1514694284</deployedseed>
    <deployedseed>1022375444</deployedseed>
    <deployedseed>1401936240</deployedseed>
    <deployedseed>1550313043</deployedseed>
    <deployedseed>1699951669</deployedseed>
    <deployedseed>1895463734</deployedseed>
    <deployedseed>1529970030</deployedseed>
    <deployedseed>1051364321</deployedseed>
    <deployedseed>642335731</deployedseed>
    <deployedseed>574638545</deployedseed>
    <deployedseed>115482924</deployedseed>
    <deployedseed>2115795020</deployedseed>
    <deployedseed>1349932477</deployedseed>
    <deployedseed>2034272177</deployedseed>
    <deployedseed>1292811772</deployedseed>
    <qtest>
      <testcase>1</testcase>
      <description>Test case assuming the teacher's input gets full marks.</description>
      <testinput>
        <name>S_fn</name>
        <value>fn</value>
      </testinput>
      <testinput>
        <name>S_sf</name>
        <value>sf</value>
      </testinput>
      <expected>
        <name>fn</name>
        <expectedscore>1.0000000</expectedscore>
        <expectedpenalty>0.0000000</expectedpenalty>
        <expectedanswernote>vn-1-T</expectedanswernote>
      </expected>
      <expected>
        <name>sf</name>
        <expectedscore>1.0000000</expectedscore>
        <expectedpenalty>0.0000000</expectedpenalty>
        <expectedanswernote>sf-1-T</expectedanswernote>
      </expected>
    </qtest>
    <qtest>
      <testcase>2</testcase>
      <description>Test case assuming the teacher's input gets full marks.</description>
      <testinput>
        <name>S_fn</name>
        <value>fn</value>
      </testinput>
      <testinput>
        <name>S_sf</name>
        <value>sf</value>
      </testinput>
      <expected>
        <name>fn</name>
        <expectedscore>1.0000000</expectedscore>
        <expectedpenalty>0.0000000</expectedpenalty>
        <expectedanswernote>vn-1-T</expectedanswernote>
      </expected>
      <expected>
        <name>sf</name>
        <expectedscore>1.0000000</expectedscore>
        <expectedpenalty>0.0000000</expectedpenalty>
        <expectedanswernote>sf-1-T</expectedanswernote>
      </expected>
    </qtest>
  </question>
</quiz>
