Skip to content

Commit

Permalink
Properly add parentheses to Fraction::Value objects based on precedence.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvc committed Dec 9, 2020
1 parent a0da682 commit 9f2ca68
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions macros/contextFraction.pl
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,32 @@ sub reduce {
return $self->SUPER::reduce;
}

#
# Add parentheses if they were there originally, or are needed by precedence
#
sub string {
my $self = shift;
my $string = $self->SUPER::string($self, @_);
return $string unless $self->{value}->classMatch('Fraction');
my $precedence = shift;
my $frac = $self->context->operators->get('/')->{precedence};
$string = '(' . $string . ')' if $self->{hadParens} || (defined $precedence && $precedence > $frac);
return $string;
}

#
# Add parentheses if they are needed by precedence
#
sub TeX {
my $self = shift;
my $string = $self->SUPER::TeX($self, @_);
return $string unless $self->{value}->classMatch('Fraction');
my $precedence = shift;
my $frac = $self->context->operators->get('/')->{precedence};
$string = '\left(' . $string . '\right)' if defined $precedence && $precedence > $frac;
return $string;
}

###########################################################################

package context::Fraction::Real;
Expand Down

1 comment on commit 9f2ca68

@Alex-Jordan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 2.16, I am seeing Fraction Formula objects display excess parentheses now, and I wonder if it is a side effect of this commit. An example is:

DOCUMENT();

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "PGML.pl",
  "contextFraction.pl",
);

Context("Fraction");
$pos = Formula("1/2");
$neg = Formula("-$pos");

BEGIN_PGML
    [` [$pos] `]  shows up as (1/2)
    [` [$neg] `]  shows up as -((1/2))
END_PGML

ENDDOCUMENT();

Does it make sense that this commit would have such a side effect? If it's not from here, I'll post in the forum about the issue.

Please sign in to comment.