[LON-CAPA-cvs] cvs: loncom /homework default_homework.lcpm /html/adm/help/tex Numerical_Response_Format.tex all_functions_table.tex

albertel lon-capa-cvs@mail.lon-capa.org
Thu, 31 Mar 2005 14:43:14 -0000


albertel		Thu Mar 31 09:43:14 2005 EDT

  Modified files:              
    /loncom/homework	default_homework.lcpm 
    /loncom/html/adm/help/tex	Numerical_Response_Format.tex 
                             	all_functions_table.tex 
  Log:
  - BUG#4028 (implemented by Hon-Kie) add new format mode 3s, and document it
  
  
Index: loncom/homework/default_homework.lcpm
diff -u loncom/homework/default_homework.lcpm:1.96 loncom/homework/default_homework.lcpm:1.97
--- loncom/homework/default_homework.lcpm:1.96	Tue Mar 29 12:57:51 2005
+++ loncom/homework/default_homework.lcpm	Thu Mar 31 09:43:13 2005
@@ -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.96 2005/03/29 17:57:51 albertel Exp $
+# $Id: default_homework.lcpm,v 1.97 2005/03/31 14:43:13 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -518,10 +518,14 @@
     #if ($options =~ /\$/) { $dollamode=1; }
     #if ($options =~ /,/)  { $commamode=1; }
     if ($options =~ /\./) { $alwaysperiod=1; }
-    $fmt=~s/e/E/g;
-    my $result=sprintf('%.'.$fmt,$value);
-    if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
-    $result=~s/(E[+-]*)0/$1/;
+    if ($fmt=~/s$/i) {
+	$result=&format_significant_figures($value,$fmt);
+    } else {
+	$fmt=~s/e/E/g;
+	my $result=sprintf('%.'.$fmt,$value);
+	if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
+	$result=~s/(E[+-]*)0/$1/;
+    }
     #if ($dollarmode) {$result=&dollarformat($result);}
     #if ($commamode) {$result=&commaformat($result);}
     return $result;
@@ -585,7 +589,11 @@
     if ($options =~ /\$/) { $dollarmode=1; }
     if ($options =~ /,/)  { $commamode=1; }
     if ($options =~ /\./) { $alwaysperiod=1; }
-    if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
+    if ($fmt=~/s$/i) {
+	$value=&format_significant_figures($value,$fmt);
+    } elsif ($fmt) {
+	$value=sprintf('%.'.$fmt,$value);
+    }
     if ($alwaysperiod && $fmt eq '0f') {
 	if ($target eq 'tex') {
 	    $value .='\\ensuremath{.}';
@@ -647,6 +655,36 @@
     return $number; 
 }
 
+# format of form ns or nS where n is an integer
+sub format_significant_figures {
+    my ($number,$format) = @_; 
+    return '0' if ($number == 0);
+    # extract number of significant figures needed
+    my ($sig) = ($format =~ /(\d+)s/i);
+    # arbitrary choice - suggestions ?? or throw error message?
+    $sig = 3 if ($sig eq '');
+    # save the minus sign
+    my $sign = ($number < 0) ? '-' : '';
+    $number = abs($number);
+    # needed to correct for a number greater than 1 (or
+    my $power = ($number < 1) ? 0 : 1;
+    # 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.
+    my $xsig = $number*10**($sig-$x10-$power);
+    # get just digits left of decimal pt - also rounds off correctly
+    my $xint  = sprintf('%.0f',$xsig);
+    # save any trailing zero's
+    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/;
+    # return number with sign
+    return $sign.$numSig;
+
+}
+
 sub map {
     my ($phrase,$dest,$source)=@_;
     my @oldseed=&random_get_seed();
Index: loncom/html/adm/help/tex/Numerical_Response_Format.tex
diff -u loncom/html/adm/help/tex/Numerical_Response_Format.tex:1.2 loncom/html/adm/help/tex/Numerical_Response_Format.tex:1.3
--- loncom/html/adm/help/tex/Numerical_Response_Format.tex:1.2	Thu Jul 18 11:52:27 2002
+++ loncom/html/adm/help/tex/Numerical_Response_Format.tex	Thu Mar 31 09:43:14 2005
@@ -5,5 +5,6 @@
 that, you can use the \textbf{Format} field. Format strings like {}``2E''
 (without the quotes) will display three significant digits in scientific
 notation. Format strings like {}``2f'' will display two digits after the
-decimal point.
+decimal point. Format strings like {}``2s'' will round a number to 
+2 significant digits.
 
Index: loncom/html/adm/help/tex/all_functions_table.tex
diff -u loncom/html/adm/help/tex/all_functions_table.tex:1.4 loncom/html/adm/help/tex/all_functions_table.tex:1.5
--- loncom/html/adm/help/tex/all_functions_table.tex:1.4	Mon Oct 18 14:44:12 2004
+++ loncom/html/adm/help/tex/all_functions_table.tex	Thu Mar 31 09:43:14 2005
@@ -34,9 +34,9 @@
  \hline 
 asinh(x), acosh(x), atanh(x) &\&asinh(\$x), \&acosh(\$x), \&atanh(\$x) &Inverse hyperbolic functions. \$x can be a pure number &  \\
  \hline 
-/DIS(\$x,''nn'') &\&format(\$x,'nn') &Display or format \$x as nn where nn is nF or nE and n is an integer. & The difference is obvious. \\
+/DIS(\$x,''nn'') &\&format(\$x,'nn') &Display or format \$x as nn where nn is nF or nE or nS and n is an integer. & The difference is obvious. \\
  \hline 
-Not in CAPA &\&prettyprint(\$x,'nn','optional target') &Display or format \$x as nn where nn is nF or nE and n is an integer. Also supports the first character being a \$, it then will format the result with a a call to \&dollarformat() described below. If the first character is a , it will format it wiht commas grouping the thousands. In E mode it will attempt to generate a pretty x10\^{}3 rather than a E3 following the number, the 'optional target' argument is optional but can be used to force \&prettyprint to generate either 'tex' output, or 'web' output, most people do not need to specify this argument and can leave it blank. &  \\
+Not in CAPA &\&prettyprint(\$x,'nn','optional target') &Display or format \$x as nn where nn is nF or nE or nS and n is an integer. Also supports the first character being a \$, it then will format the result with a a call to \&dollarformat() described below. If the first character is a , it will format it with commas grouping the thousands. In S mode it will fromat the number to the specified number of significant figures and display it in F mode. In E mode it will attempt to generate a pretty x10\^{}3 rather than a E3 following the number, the 'optional target' argument is optional but can be used to force \&prettyprint to generate either 'tex' output, or 'web' output, most people do not need to specify this argument and can leave it blank. &  \\
  \hline 
 Not in CAPA &\&dollarformat(\$x,'optional target') &Reformats \$x to have a \$ (or $\backslash$\$ if in tex mode) and to have , grouping thousands. The 'optional target' argument is optional but can be used to force \&prettyprint to generate either 'tex' output, or 'web' output, most people do not need to specify this argument and can leave it blank. &  \\
  \hline