[LON-CAPA-cvs] cvs: loncom /homework default_homework.lcpm
raeburn
raeburn at source.lon-capa.org
Tue Jun 24 13:02:34 EDT 2014
raeburn Tue Jun 24 17:02:34 2014 EDT
Modified files:
/loncom/homework default_homework.lcpm
Log:
- Bug 6577
- More generalized solution than implemented previously in rev. 1.163.
- e.g., computed answer: 1900, can be formatted 3s (1.90E+3).
same for computed answer: 1.900E+3, can be formatted 3s (1.90E+3).
Index: loncom/homework/default_homework.lcpm
diff -u loncom/homework/default_homework.lcpm:1.164 loncom/homework/default_homework.lcpm:1.165
--- loncom/homework/default_homework.lcpm:1.164 Mon Jun 16 16:52:50 2014
+++ loncom/homework/default_homework.lcpm Tue Jun 24 17:02:33 2014
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
#
-# $Id: default_homework.lcpm,v 1.164 2014/06/16 16:52:50 bisitz Exp $
+# $Id: default_homework.lcpm,v 1.165 2014/06/24 17:02:33 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -1033,17 +1033,6 @@
$number = abs($number);
# needed to correct for a number greater than 1 (or
my $power = ($number < 1) ? 0 : 1;
- if ($power && $number =~ /^\d+$/) {
- my $nonzeros = $number;
- $nonzeros =~ s/0+$//;
- if (length($number) - length($nonzeros) > 1) {
- # convert to exponential form
- my $n = $sig-1;
- my $numSig = sprintf('%.' . $n . 'E', $number);
- # return number with sign
- return $sign.$numSig;
- }
- }
# could round up. Take the integer part of log10.
my $x10 = int(log($number)/log(10));
# find number with values left of decimal pt = # of sign figs.
@@ -1054,16 +1043,28 @@
my ($zeros) = ($xint =~ /(0+)$/);
# return number to original magnitude
my $numSig = $xint*10**($x10-$sig+$power);
- # insert trailing zero's if have decimal point
- $numSig =~ s/^(\d+)\.(\d+)(\e?(.*)?)$/$1\.$2$zeros$3/;
- # put a decimal pt for number ending with 0 and length = # of sig fig
- $numSig.='.' if (length($numSig) == $sig && $numSig =~ /0$/);
- if (length($numSig) < $sig) {
- $numSig.='.'.substr($zeros,0,($sig-length($numSig)));
+ if ($numSig =~ /^(\d+)\.(\d+)/) {
+ # insert trailing zero's if have decimal point
+ $numSig =~ s/^(\d+)\.(\d+)(\e?(.*)?)$/$1\.$2$zeros$3/;
+ } else {
+ if ($numSig =~ /0$/) {
+ # add decimal pt for number ending with 0 and length == # of sig figs
+ if (length($numSig) == $sig) {
+ $numSig.='.';
+ } elsif (length($numSig) > $sig) {
+ # exponential form for number ending with 0 and length > # of sig figs
+ my $fmtsig = $sig-1;
+ if ($fmtsig) {
+ $numSig = sprintf('%.'.$fmtsig.'e',$numSig);
+ }
+ }
+ }
+ if (length($numSig) < $sig) {
+ $numSig.='.'.substr($zeros,0,($sig-length($numSig)));
+ }
}
# return number with sign
return $sign.$numSig;
-
}
sub map {
@@ -1218,7 +1219,7 @@
$output =~ s/</</g;
$output =~ s/>/>/g;
$output =~ s/"/"/g;
- };
+ }
return $output;
}
More information about the LON-CAPA-cvs
mailing list