From 7f33aa0cd42ee76b3a7ada51ec71ff6d0b19955a Mon Sep 17 00:00:00 2001 From: pichetp Date: Mon, 10 Mar 2008 21:58:20 +0000 Subject: [PATCH] MDL-12503 Loguniform merging from head --- question/type/calculated/questiontype.php | 54 +---------------------- 1 file changed, 2 insertions(+), 52 deletions(-) diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index 24690e6a782..f7cc0d25c7b 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -669,62 +669,12 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype } if ($regs[1] == 'uniform') { $nbr = $regs[2] + ($regs[3]-$regs[2])*mt_rand()/mt_getrandmax(); - return round($nbr, $regs[4]); + return sprintf("%.".$regs[4]."f",$nbr); } else if ($regs[1] == 'loguniform') { $log0 = log(abs($regs[2])); // It would have worked the other way to $nbr = exp($log0 + (log(abs($regs[3])) - $log0)*mt_rand()/mt_getrandmax()); - - // Reformat according to the precision $regs[4]: - - // Determine the format 0.[1-9][0-9]* for the nbr... - $p10 = 0; - while ($nbr < 1) { - --$p10; - $nbr *= 10; - } - while ($nbr >= 1) { - ++$p10; - $nbr /= 10; - } - // ... and have the nbr rounded off to the correct length - $nbr = round($nbr, $regs[4]); - - // Have the nbr written on a suitable format, - // Either scientific or plain numeric - if (-2 > $p10 || 4 < $p10) { - // Use scientific format: - $eX = 'e'.--$p10; - $nbr *= 10; - if (1 == $regs[4]) { - $nbr = $nbr.$eX; - } else { - // Attach additional zeros at the end of $nbr, - $nbr .= (1==strlen($nbr) ? '.' : '') - . '00000000000000000000000000000000000000000x'; - $nbr = substr($nbr, 0, $regs[4] +1).$eX; - } - } else { - // Stick to plain numeric format - $nbr *= "1e$p10"; - if (0.1 <= $nbr / "1e$regs[4]") { - $nbr = $nbr; - } else { - // Could be an idea to add some zeros here - $nbr .= (ereg('^[0-9]*$', $nbr) ? '.' : '') - . '00000000000000000000000000000000000000000x'; - $oklen = $regs[4] + ($p10 < 1 ? 2-$p10 : 1); - $nbr = substr($nbr, 0, $oklen); - } - } - - // The larger of the values decide the sign in case the - // have equal different signs (which they really must not have) - if ($regs[2] + $regs[3] > 0) { - return $nbr; - } else { - return -$nbr; - } + return sprintf("%.".$regs[4]."f",$nbr); } else { error("The distribution $regs[1] caused problems");