From 09a7702730caf179501e1393ef35393ca18279ed Mon Sep 17 00:00:00 2001 From: Peter Staab Date: Sat, 14 Jun 2025 09:35:59 -0400 Subject: [PATCH] Initial work on parsing a number in a x 10^b notation --- macros/contexts/contextSignificantFigures.pl | 23 ++++++++++++++++++++ t/contexts/sigfig2.t | 21 ++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 t/contexts/sigfig2.t diff --git a/macros/contexts/contextSignificantFigures.pl b/macros/contexts/contextSignificantFigures.pl index 8e5287f18b..66f7e66712 100644 --- a/macros/contexts/contextSignificantFigures.pl +++ b/macros/contexts/contextSignificantFigures.pl @@ -331,6 +331,24 @@ sub ROUND { return sprintf("%.0E", $x + $d) - $d; } +package context::SignificantFigures::BOP::parse; +our @ISA = ("Parser::BOP"); +use Data::Dumper; +sub _check { + my $self = @_; + my ($lop, $rop) = ($self->{lop}, $self->{rop}); +print Dumper 'in _check'; +print Dumper [$self, ref $lop, ref $rop]; + + +} + +sub _eval { + my ($self, $a, $b) = @_; + print Dumper [ref $self, $a->value, ref $b->value]; + return $self->package('SignificantFigures')->new("$_[0]E$_[1]"); +} + package context::SignificantFigures; sub Init { @@ -352,6 +370,10 @@ sub new { $context->{precedence}{SignifcantFigures} = $context->{precedence}{special}; $context->flags->set(limits => [ -1000, 1000, 1 ]); + $context->operators->add( + 'x 10^' => { class => 'context::SignificantFigures::BOP::parse'} + ); + return $context; } @@ -386,4 +408,5 @@ sub perl { return $self->context->Package('Real') . '->new(' . $value->value . ',' . $value->N . ')'; } + 1; diff --git a/t/contexts/sigfig2.t b/t/contexts/sigfig2.t new file mode 100644 index 0000000000..5f8f59a35d --- /dev/null +++ b/t/contexts/sigfig2.t @@ -0,0 +1,21 @@ +use Test2::V0 '!E', { E => 'EXISTS' }; + +die "PG_ROOT not found in environment.\n" unless $ENV{PG_ROOT}; +do "$ENV{PG_ROOT}/t/build_PG_envir.pl"; + +use lib "$ENV{PG_ROOT}/lib"; + +loadMacros('contextSignificantFigures.pl'); + +use Value; +require Parser::Legacy; +import Parser::Legacy; + +use Data::Dumper; + +Context('SignificantFigures'); + +subtest 'Entering sigfig in sci notation' => sub { + my $a1 = Compute('1.0 x 10^2'); + is $a1->format('E'), '1.0E+02', 'Ensure that the internal storage of 1.0 * 10^2 is correct.'; +};