Jump to content

**SOLVED** Float Rounding Error


kylew

Recommended Posts

I'm authoring a site that tests students on basic math. It tests everything from simple addition to decimals to fractions. I have a MySQL db that stores all information. I'm working on a couple of the specific tests right now, and am having serious trouble with roundoff error. For multiplying decimals, I've had to round to ten decimal places to remove extraneous bits that are messing up the grading of the quizzes. I'm working on division right now, and every once in a while, I'll get a division question that doesn't accept the correct answer as being correct. I have the data type for the questions in the db as FLOAT, and randomly generate decimals to store in the database. When the student proceeds to click "Grade Sheet", the code runs though the questions, calculates the correct answers, compares to the inputted answers, and scores the sheet. Somewhere in that comparison enlies the issue.

[code]        
        $additive1[$count]=$fields[$count2];
        $additive2[$count]=$fields[$count2+1];
        if($values[$count]==($additive2[$count]/$additive1[$count]))
        {
            $correct[$count]=true;
            $numcorrect++;
        }
        else
        {
            $correct[$count]=false;
        }
[/code]

This code checks to see if the entered value in $values[$count] (retrieved from $_POST[]) is equal to the correct answer. I've outputted the values and visually checked that the value the script is calculating and the value that is inputted match. There is some error in roundoff, because I rounded the decimal multiplication test and everything works well now, but there has to be some other error.

The error might be where I generate random numbers with decimals. This is the fuction I use:

[code]
function random_0_1($min,$max)
{
    $rand=mt_rand($min,$max);
    $decimal=((float)mt_rand()/(float)mt_getrandmax());
    while($decimal<.05 || $decimal>.95)
    {
        $decimal=((float)mt_rand()/(float)mt_getrandmax());
    }
    $output=$rand+$decimal;
    return $output;
}
[/code]

Is there any particular way that I should initialize arrays used to store question values and answers to make them float friendly? I've been coding PHP for a while, but I'm still not very good at it. If you need any further information (which is entirely likely), let me know. I have not included a ton of the script, first because it is messy and poorly written, and second because this particluar script is over 750 lines long.

Thank you in advance.
Link to comment
Share on other sites

Because of the rounding error with fp calculations you need to change the way you test if the result is right.

Your calculation may result in, say, 42.6999999999 and the student's answer is 42.70

$calc = 42.6999999999;
$ans = 42.70

This checking code

[code]if ($calc == $ans)
    echo "Right";
else
    echo "Wrong";[/code]

gives --> Wrong

whereas this checking code

[code]if (abs($calc - $ans) < 0.001)
    echo "Right";
else
    echo "Wrong";[/code]

gives --> Right
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.