Skip to content

only use parentheses (not brackets) for Formula strings #1277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Parser/Context/Default.pm
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ $flags = {
reduceConstants => 1, # 1 = automatically combine constants
reduceConstantFunctions => 1, # 1 = compute function values of constants
showExtraParens => 1, # 1 = add useful parens, 2 = make things painfully unambiguous
stringifyNoBrackets => 0, # 1 = only use parentheses not brackets when stringifying
formatStudentAnswer => 'evaluated', # or 'parsed' or 'reduced'
allowMissingOperands => 0, # 1 is used by Typeset context
allowMissingFunctionInputs => 0, # 1 is used by Typeset context
Expand Down
4 changes: 2 additions & 2 deletions lib/Parser/Item.pm
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ sub canBeInUnion {0}
sub isSetOfReals { (shift)->type =~ m/^(Interval|Union|Set)$/ }

#
# Add parens to an expression (alternating the type of paren)
# Add parens to an expression (alternating the type of paren unless stringifyNoBrackets is set)
#
sub addParens {
my $self = shift;
my $string = shift;
if ($string =~ m/^[^\[]*\(/) { return '[' . $string . ']' }
return '[' . $string . ']' if $string =~ m/^[^\[]*\(/ && !$self->context->flag('stringifyNoBrackets');
return '(' . $string . ')';
}

Expand Down
5 changes: 2 additions & 3 deletions lib/Plots/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ sub function_string {
my $format = $formula->context->{format}{number};
$formula->context->flags->set(showExtraParens => 2);
$formula->context->{format}{number} = "%f#";
my $func = $formula->string;
# Get no bracket string for $formula
my $func = $formula . "";
$func =~ s/\s//g;
$formula->context->flags->set(showExtraParens => $extraParens);
$formula->context->{format}{number} = $format;
Expand Down Expand Up @@ -368,8 +369,6 @@ sub function_string {
}
}

$out =~ s/\[/(/g;
$out =~ s/\]/)/g;
return $out;
}

Expand Down
9 changes: 8 additions & 1 deletion lib/Value/Formula.pm
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ sub _dot {
$l->SUPER::_dot($r, $flag);
}

sub pdot { '(' . (shift->stringify) . ')' }
sub pdot {
my $self = shift;
my $nobrackets = $self->context->flag('stringifyNoBrackets');
$self->context->flags->set(stringifyNoBrackets => 1);
my $string = '(' . ($self->stringify) . ')';
$self->context->flags->set(stringifyNoBrackets => $nobrackets);
return $string;
}

#
# Call the Parser::Function call function
Expand Down