From 8bc960b30c3cd3222e03eebbafe85d02339cc84a Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Thu, 26 Oct 2023 11:03:43 -0500 Subject: [PATCH 1/2] Alternate to #946 and probably better (slightly) approach. Investigating things closer what I see is that the `Real` call added in the problem in question, that context happens to be a variant of the `Fraction-NoDecimals` context. The `Real` call that was added in #935 is not creating a fraction in that context when it should. This alternate approach only returns the result of the `Real` call in the case that an ambiguous value is detected. Otherwise it continues on the the rest of the compute method the way that it did before #935 was added. --- macros/core/Parser.pl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/macros/core/Parser.pl b/macros/core/Parser.pl index 12932d8e18..672805aa0b 100644 --- a/macros/core/Parser.pl +++ b/macros/core/Parser.pl @@ -97,11 +97,12 @@ sub Compute { my $formula = Formula($string); if (Value::matchNumber($string)) { my $real = Real($string); - warn "Compute() called with ambiguous value: $string\n" - . '-- use Real() for scientific notation' - . " ($real) or use Formula() for $formula \n" - unless ($real == $formula); - return $real; + unless ($real == $formula) { + warn "Compute() called with ambiguous value: $string\n" + . '-- use Real() for scientific notation' + . " ($real) or use Formula() for $formula \n"; + return $real; + } } $formula = $formula->{tree}->Compute if $formula->{tree}{canCompute}; my $context = $formula->context; From 4344e322d2446b435af14abd46ce262f6566045f Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Fri, 27 Oct 2023 08:36:57 -0500 Subject: [PATCH 2/2] Switch to directly checking for a scientific number format as suggested by @dpvc. The warning is left for now. Do we really want that? As noted by @dpvc this will still not detect things like ```perl $a = .000012; Compute("${a}x + $b"); ``` so authors still need to be aware of perl number formats. --- macros/core/Parser.pl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/macros/core/Parser.pl b/macros/core/Parser.pl index 672805aa0b..0703d5d355 100644 --- a/macros/core/Parser.pl +++ b/macros/core/Parser.pl @@ -93,17 +93,16 @@ =head2 Compute # ^uses Formula # ^uses Value::contextSet sub Compute { - my $string = shift; - my $formula = Formula($string); - if (Value::matchNumber($string)) { - my $real = Real($string); - unless ($real == $formula) { - warn "Compute() called with ambiguous value: $string\n" - . '-- use Real() for scientific notation' - . " ($real) or use Formula() for $formula \n"; - return $real; - } + my $string = shift; + if ($string =~ m/^\s*-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[-+]\d+)\s*$/ + && Value::matchNumber($string) + && ($string ^ $string) eq '0') + { + warn "Compute() called with ambiguous value: $string\n" + . "-- use Real() for scientific notation or Formula() for Euler's number e\n"; + $string = uc($string); } + my $formula = Formula($string); $formula = $formula->{tree}->Compute if $formula->{tree}{canCompute}; my $context = $formula->context; my $flags = Value::contextSet($context, reduceConstants => 0, reduceConstantFunctions => 0, showExtraParens => 0);