Skip to content

Commit 858d782

Browse files
committed
Add the ability to parse a sig fig number in the form 1.00 x 10^3 for example.
1 parent 09a7702 commit 858d782

2 files changed

Lines changed: 48 additions & 24 deletions

File tree

macros/contexts/contextSignificantFigures.pl

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ sub TeX {
225225
}
226226

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

230230
# Example: format('E', '123.456', 6) returns 1.23456E+02
231231
# format('f', 1.23E-01, 3) returns '0.123'.
@@ -243,7 +243,7 @@ sub format {
243243
return sprintf("%.${n}${f}" . ($n == 0 && $f eq 'f' ? '.' : ''), $value);
244244
}
245245

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

249249
sub add {
@@ -253,7 +253,7 @@ sub add {
253253
return $self->new($value, sigfigs => main::max(0, $exp + $self->expFor($value, $exp)));
254254
}
255255

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

259259
sub sub {
@@ -333,20 +333,18 @@ sub ROUND {
333333

334334
package context::SignificantFigures::BOP::parse;
335335
our @ISA = ("Parser::BOP");
336-
use Data::Dumper;
336+
337337
sub _check {
338-
my $self = @_;
338+
my ($self) = @_;
339339
my ($lop, $rop) = ($self->{lop}, $self->{rop});
340-
print Dumper 'in _check';
341-
print Dumper [$self, ref $lop, ref $rop];
342-
343-
344340
}
345341

346342
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]");
343+
my ($self) = @_;
344+
my $exp = $_[2]->string;
345+
$exp = "+$exp" unless $exp < 0;
346+
$exp =~ s/\.$//;
347+
return context::SignificantFigures::Real->new("$_[1]E$exp");
350348
}
351349

352350
package context::SignificantFigures;
@@ -367,12 +365,13 @@ sub new {
367365
$context->{value}{Real} = 'context::SignificantFigures::Real';
368366
$context->functions->disable('All');
369367
$context->constants->clear();
370-
$context->{precedence}{SignifcantFigures} = $context->{precedence}{special};
368+
$context->{precedence}{SignificantFigures} = $context->{precedence}{special};
371369
$context->flags->set(limits => [ -1000, 1000, 1 ]);
372370

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

377376
return $context;
378377
}
@@ -408,5 +407,4 @@ sub perl {
408407
return $self->context->Package('Real') . '->new(' . $value->value . ',' . $value->N . ')';
409408
}
410409

411-
412410
1;

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)