Velocity Reviews > Perl > Writing Math Equations in PERL for HTML

Writing Math Equations in PERL for HTML

cylurian@gmail.com
Guest
Posts: n/a

 02-02-2007
Hello everyone. I create a lot of math questions with PERL and
display them in html. One item that kills me is using negative signs,
especially for displaying on html. Question is what is the best
efficient way to account for positive and negative (addition and
subtraction) signs when displaying in html? I write many conditional
statements so I can account for the sign (operations). For example,
lets say I have a quadratic: ax^2 + bc + c = 0

And I want a, b and c to be random from -9 to -1 and 1 to 9. When I
print the problem, I have to all the cases for displaying in html.

If ((\$a >= 1)&&(\$b >= 1)&&(\$c >= 1) {

print "\$a x<sup>2</sup> + \$b x + \$c = 0";

}elsif ((\$a >= 1)&&(\$b <= 1)&&(\$c >= 1)) {

print "\$a x<sup>2</sup> \$b x + \$c = 0";

}..... keeps going until I cover all possible combinations with negative
and positive signs. Any suggestions on being more efficient?

Brian McCauley
Guest
Posts: n/a

 02-02-2007
On Feb 2, 10:15 pm, (E-Mail Removed) wrote:
> Hello everyone. I create a lot of math questions with PERL and
> display them in html. One item that kills me is using negative signs,
> especially for displaying on html. Question is what is the best
> efficient way to account for positive and negative (addition and
> subtraction) signs when displaying in html? I write many conditional
> statements so I can account for the sign (operations). For example,
> lets say I have a quadratic: ax^2 + bc + c = 0
>
> And I want a, b and c to be random from -9 to -1 and 1 to 9. When I
> print the problem, I have to all the cases for displaying in html.
>
> If ((\$a >= 1)&&(\$b >= 1)&&(\$c >= 1) {
>
> print "\$a x<sup>2</sup> + \$b x + \$c = 0";
>
> }elsif ((\$a >= 1)&&(\$b <= 1)&&(\$c >= 1)) {
>
> print "\$a x<sup>2</sup> \$b x + \$c = 0";
>
> }..... keeps going until I cover all possible combinations with negative
>
> and positive signs. Any suggestions on being more efficient?

printf "%dx<sup>2</sup>%+dx%+d=0",\$a,\$b,\$c;

cylurian@gmail.com
Guest
Posts: n/a

 02-02-2007
Wow, thanks. What can I do if \$a = -1 and I want nothing to show, is
there a faster way then doing a separte condition for \$a like

if (\$a == -1) {
printf "x<sup>2</sup>%+dx%+d=0",\$a,\$b,\$c;
}else {
printf "%dx<sup>2</sup>%+dx%+d=0",\$a,\$b,\$c;
}

On Feb 2, 2:27 pm, "Brian McCauley" <(E-Mail Removed)> wrote:
> On Feb 2, 10:15 pm, (E-Mail Removed) wrote:
>
>
>
> > Hello everyone. I create a lot of math questions with PERL and
> > display them in html. One item that kills me is using negative signs,
> > especially for displaying on html. Question is what is the best
> > efficient way to account for positive and negative (addition and
> > subtraction) signs when displaying in html? I write many conditional
> > statements so I can account for the sign (operations). For example,
> > lets say I have a quadratic: ax^2 + bc + c = 0

>
> > And I want a, b and c to be random from -9 to -1 and 1 to 9. When I
> > print the problem, I have to all the cases for displaying in html.

>
> > If ((\$a >= 1)&&(\$b >= 1)&&(\$c >= 1) {

>
> > print "\$a x<sup>2</sup> + \$b x + \$c = 0";

>
> > }elsif ((\$a >= 1)&&(\$b <= 1)&&(\$c >= 1)) {

>
> > print "\$a x<sup>2</sup> \$b x + \$c = 0";

>
> > }..... keeps going until I cover all possible combinations with negative

>
> > and positive signs. Any suggestions on being more efficient?

>
> printf "%dx<sup>2</sup>%+dx%+d=0",\$a,\$b,\$c;

cylurian@gmail.com
Guest
Posts: n/a

 02-02-2007
Wow, thanks. Is there a way for me to show only a negative for 1
instead of -1. I know of two ways, but is there a better way.

if (\$a == -1 {

\$a = "-";
}

or

if (\$a == -1) {

printf "-x<sup>2</sup>%+dx%+d=0", \$b,\$c;

}else {

printf "%dx<sup>2</sup>%+dx%+d=0",\$a,\$b,\$c;

}

On Feb 2, 2:27 pm, "Brian McCauley" <(E-Mail Removed)> wrote:
> On Feb 2, 10:15 pm, (E-Mail Removed) wrote:
>
>
>
> > Hello everyone. I create a lot of math questions with PERL and
> > display them in html. One item that kills me is using negative signs,
> > especially for displaying on html. Question is what is the best
> > efficient way to account for positive and negative (addition and
> > subtraction) signs when displaying in html? I write many conditional
> > statements so I can account for the sign (operations). For example,
> > lets say I have a quadratic: ax^2 + bc + c = 0

>
> > And I want a, b and c to be random from -9 to -1 and 1 to 9. When I
> > print the problem, I have to all the cases for displaying in html.

>
> > If ((\$a >= 1)&&(\$b >= 1)&&(\$c >= 1) {

>
> > print "\$a x<sup>2</sup> + \$b x + \$c = 0";

>
> > }elsif ((\$a >= 1)&&(\$b <= 1)&&(\$c >= 1)) {

>
> > print "\$a x<sup>2</sup> \$b x + \$c = 0";

>
> > }..... keeps going until I cover all possible combinations with negative

>
> > and positive signs. Any suggestions on being more efficient?

>
> printf "%dx<sup>2</sup>%+dx%+d=0",\$a,\$b,\$c;

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post VK Javascript 15 05-02-2010 03:43 PM Jeremy Watts Java 17 05-27-2008 10:56 PM stacey Java 6 02-09-2007 08:32 PM AciD_X C++ 4 04-01-2004 07:29 PM chirs Java 18 03-02-2004 06:00 PM