Skip to content

Commit 6d414b0

Browse files
committed
Add the ability to parse a sig fig number in the form 1.00 x 10^3 for example.
Removed the sigfig2.t macro. Updated the latex subroutine to not throw warnings when tests are run.
1 parent 09a7702 commit 6d414b0

3 files changed

Lines changed: 50 additions & 46 deletions

File tree

macros/contexts/contextSignificantFigures.pl

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,13 @@ sub expFor {
220220

221221
sub TeX {
222222
my $tex = shift->string;
223-
$tex =~ s/E(?:(-)|\+)0*([1-9]\d?)/\\times 10^{$1$2}/;
223+
# Capture the + or - in $1, then the digits in $2
224+
$tex =~ s/E([-+])0*(\d+)/'\\times 10^{' . ($1 eq '-' ? '-' : '') . $2 . '}' /e;
224225
return "{$tex}";
225226
}
226227

227228
# Format the number in $value in either 'E' (exponential form) or 'f' decimal form using
228-
# $n signficant figures.
229+
# $n significant figures.
229230

230231
# Example: format('E', '123.456', 6) returns 1.23456E+02
231232
# format('f', 1.23E-01, 3) returns '0.123'.
@@ -243,7 +244,7 @@ sub format {
243244
return sprintf("%.${n}${f}" . ($n == 0 && $f eq 'f' ? '.' : ''), $value);
244245
}
245246

246-
# Redefine addition. The leftmost signifcant place in the result is needed to get the
247+
# Redefine addition. The leftmost significant place in the result is needed to get the
247248
# correct value.
248249

249250
sub add {
@@ -253,7 +254,7 @@ sub add {
253254
return $self->new($value, sigfigs => main::max(0, $exp + $self->expFor($value, $exp)));
254255
}
255256

256-
# Redefine subtraction. The leftmost signifcant place in the result is needed to get the
257+
# Redefine subtraction. The leftmost significant place in the result is needed to get the
257258
# correct value.
258259

259260
sub sub {
@@ -333,20 +334,18 @@ sub ROUND {
333334

334335
package context::SignificantFigures::BOP::parse;
335336
our @ISA = ("Parser::BOP");
336-
use Data::Dumper;
337+
337338
sub _check {
338-
my $self = @_;
339+
my ($self) = @_;
339340
my ($lop, $rop) = ($self->{lop}, $self->{rop});
340-
print Dumper 'in _check';
341-
print Dumper [$self, ref $lop, ref $rop];
342-
343-
344341
}
345342

346343
sub _eval {
347-
my ($self, $a, $b) = @_;
348-
print Dumper [ref $self, $a->value, ref $b->value];
349-
return $self->package('SignificantFigures')->new("$_[0]E$_[1]");
344+
my ($self) = @_;
345+
my $exp = $_[2]->string;
346+
$exp = "+$exp" unless $exp < 0;
347+
$exp =~ s/\.$//;
348+
return context::SignificantFigures::Real->new("$_[1]E$exp");
350349
}
351350

352351
package context::SignificantFigures;
@@ -367,12 +366,13 @@ sub new {
367366
$context->{value}{Real} = 'context::SignificantFigures::Real';
368367
$context->functions->disable('All');
369368
$context->constants->clear();
370-
$context->{precedence}{SignifcantFigures} = $context->{precedence}{special};
369+
$context->{precedence}{SignificantFigures} = $context->{precedence}{special};
371370
$context->flags->set(limits => [ -1000, 1000, 1 ]);
372371

373-
$context->operators->add(
374-
'x 10^' => { class => 'context::SignificantFigures::BOP::parse'}
375-
);
372+
$context->operators->add('x 10^' => { class => 'context::SignificantFigures::BOP::parse' });
373+
$context->operators->add('x10^' => { class => 'context::SignificantFigures::BOP::parse' });
374+
$context->operators->add('* 10^' => { class => 'context::SignificantFigures::BOP::parse' });
375+
$context->operators->add('*10^' => { class => 'context::SignificantFigures::BOP::parse' });
376376

377377
return $context;
378378
}
@@ -408,5 +408,4 @@ sub perl {
408408
return $self->context->Package('Real') . '->new(' . $value->value . ',' . $value->N . ')';
409409
}
410410

411-
412411
1;

t/contexts/sigfig2.t

Lines changed: 0 additions & 21 deletions
This file was deleted.

t/contexts/significant_figures.t

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ subtest 'Create numbers with significant digits using Real' => sub {
163163

164164
my $a16 = Real('0.');
165165
is $a16->format('E'), '0E+00', 'Check the format of 0.';
166-
is $a16->sigfigs, 1, '0. has 1 signficant figure';
166+
is $a16->sigfigs, 1, '0. has 1 significant figure';
167167
is $a16->E, 0, 'The exponential of +00 is 0';
168168
is $a16->string, '0.', 'The string version is 0.';
169169
is $a16->TeX, '{0.}', 'The TeX version is {0.}';
@@ -384,11 +384,37 @@ subtest 'Division' => sub {
384384
is $a2->E, -1, '1.0000/4.00 = 0.2500 = 2.50 * 10^(-1) (check exp)';
385385

386386
};
387-
388-
# subtest 'Significant Figures for integers' => sub {
389-
# # my $a1 = Compute('1.0 * 10^2');
390-
# my $a1 = Compute('1.0E+02');
391-
# is $a1->format('E'), '1.0E+02', '1.0 * 10^2 internally is 1.0E+02';
392-
# };
387+
use Data::Dumper;
388+
subtest 'Significant Figures for integers' => sub {
389+
my $a1 = Compute('1.0 x 10^2');
390+
is $a1->format('E'), '1.0E+02', '1.0 x 10^2 internally is 1.0E+02';
391+
is $a1->sigfigs, 2, '1.0 x 10^2 has 2 significant figures.';
392+
is $a1->E, 2, '1.0 x 10^2 = 1.0 * 10^2 (check exp)';
393+
394+
my $a2 = Compute('1.0x10^2');
395+
is $a2->format('E'), '1.0E+02', '1.0 x 10^2 internally is 1.0E+02';
396+
is $a2->sigfigs, 2, '1.0 x 10^2 has 2 significant figures.';
397+
is $a2->E, 2, '1.0 x 10^2 = 1.0 * 10^2 (check exp)';
398+
399+
my $a3 = Compute('1.234 x 10^8');
400+
is $a3->format('E'), '1.234E+08', '1.234 x 10^8 internally is 1.234E+08';
401+
is $a3->sigfigs, 4, '1.234 x 10^8 has 4 significant figures.';
402+
is $a3->E, 8, '1.234 x 10^8 = 1.234 * 10^8 (check exp)';
403+
404+
my $a4 = Compute('-1.23 x 10^-4');
405+
is $a4->format('E'), '-1.23E-04', '-1.23 x 10^-4 internally is -1.23E-04';
406+
is $a4->sigfigs, 3, '-1.23 x 10^-4 has 3 significant figures.';
407+
is $a4->E, -4, '-1.23 x 10^-4 = -1.23 * 10^-4 (check exp)';
408+
409+
my $a5 = Compute('1.23 * 10^3');
410+
is $a5->format('E'), '1.23E+03', '1.23 * 10^3 internally is 1.23E+03';
411+
is $a5->sigfigs, 3, '1.23 * 10^3 has 3 significant figures.';
412+
is $a5->E, 3, '1.23 * 10^3 = 1.23 * 10^3 (check exp)';
413+
414+
my $a6 = Compute('-3.28*10^5');
415+
is $a6->format('E'), '-3.28E+05', '-3.28 * 10^5 internally is -3.28E+05';
416+
is $a6->sigfigs, 3, '-3.28 * 10^5 has 3 significant figures.';
417+
is $a6->E, 5, '-3.28 * 10^5 = -3.28 * 10^5 (check exp)';
418+
};
393419

394420
done_testing();

0 commit comments

Comments
 (0)