Jump to content

Archived

This topic is now archived and is closed to further replies.

kylew

**SOLVED** Float Rounding Error

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Thanks a bunch, I would never have thought of that, but an easy way to eliminate roundoff error. It certainly did the trick.

Share this post


Link to post
Share on other sites

×

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.