Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-36025

Error with dataset value "-0" in calculated question type

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.3.2, 2.4
    • Fix Version/s: 2.2.7, 2.3.4, 2.4.1
    • Component/s: Quiz
    • Labels:
    • Testing Instructions:
      Hide

      1. Create a calculated question with a correct answer like

      {a}

      -

      {b}

      .

      2. Set the corrected answer to be calculated with a set number of significant figures.

      3. Create a dataset involving the number -0 for b. (In IEEE floating point, -0 is different from 0.)

      4. Preview the question. Make sure there is not an error message about a formula error.

      Show
      1. Create a calculated question with a correct answer like {a} - {b} . 2. Set the corrected answer to be calculated with a set number of significant figures. 3. Create a dataset involving the number -0 for b. (In IEEE floating point, -0 is different from 0.) 4. Preview the question. Make sure there is not an error message about a formula error.
    • Affected Branches:
      MOODLE_23_STABLE, MOODLE_24_STABLE
    • Fixed Branches:
      MOODLE_22_STABLE, MOODLE_23_STABLE, MOODLE_24_STABLE
    • Pull from Repository:
    • Pull Master Branch:
      MDL-36025-negative-zero

      Description

      The processing of "-0" as a dataset value is incorrect. I'm not sure the circumstances that create "-0", but moodle must be creating it as we have lots on our site. It's also possible to enter "-0" manually as a dataset value.

      The bug is in evaluating a formula with the "-0". In qtype_calculate - >substitute_variables(), it won't use parentheses around "-0" since "-0" >= 0. For example. -

      {A}

      becomes in --0. This results in a PHP error being printed.

      For format "Significant figures", this actually results in an infinite loop since there is no check that $answer is numeric before executing:

      qtype_calculated_calculate_answer():

              $p10 = 0;
              while ($answer < 1) {
                  --$p10;
                  $answer *= 10;
              }

      Suggested fixes:

      1. In qtype_calculate->substitute_variables(), use parens for $val <= 0
      2. In qtype_calculate->substitute_variables_and_eval(), use a different variable than $str in the eval(). Otherwise, if the eval() fails, the function will return the original formula. This triggers the infinite loop above.
      3. Check for and reject "--" (and "++") in qtype_calculated_find_formula_errors().

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  14/Jan/13