Pesky negative in front of a fraction

Is there a way to parse a fraction in latex form with the negative in front of the fraction?
For now, I just need to check if an inputted answer is reduced. But if the answer is a negative fraction it does not work. I cannot figure out how to check if the fraction is reduced. Here is a simple slide that works, UNLESS there is a negative in front of the fraction.

Parsing a negative fraction is a little counterintuitive, but this will parse the numerator and denominator. (You will have to conditionally pull the negative back in for the simplified answer.)

frac= p.fraction(p.integer,p.integer).parse(sa)
negFrac= p.negative(p.fraction(p.integer,p.integer)).parse(sa).negated
fracSign= when this.numericValue>=0 `` otherwise `-`

Top= when this.numericValue>=0 frac.numerator.latex otherwise negFrac.numerator.latex
Bot= when this.numericValue>=0 frac.denominator.latex otherwise negFrac.denominator.latex

UPDATE: Here’s a working version.

I want it to accept both answers, whether the negative is with the numerator, or out in front. This does not seem to work with the negative with the numerator.
Also, it is part of a lager project that checks a solution to any equation, so there will also be another check involved. If I understand your code correctly, I will not need the part where it puts the negative back into the latex. Is that accurate?

And as usual, thanks for your help!

how about just an isDefined for the negfraction? If there is a negative out front, the negFrac will be defined, etc…

If I understand correctly and you want to accept both positive and negative, then really just parsing the numerator and denominator and checking equivalence should be sufficient. If you want exact, then check parsed N and D are equivalent, and that the actual equivalence or the sign by whatever method of choice (i.e. numericValue should be the same regardless of the sign’s placement).

Interestingly, it can’t parse with my current conditionals when the negative is within the fraction, so as you said using the isDefined may be the way.

I think it worked…again, this is part of a larger project. I did not know about the “negated” parse. That was the key! Thanks again.

Something along these lines:

top = when not(isDefined(negFrac)) etc… otherwise negFrac…etc

1 Like

So…here is the final project. It’s kind of a hobby for me to try to make templates and random problem makers. This is a “template” that you can put pretty much any linear equation into the top cell of a table hidden from the user. Variables other than x go in the second cell. (default is x) Anyways, I only wanted to allow integers or reduced fractions. (no decimals). The negative sign in front of fractions threw me-until you helped- Thank you!!! The other nuisance was the occasional crazy decimal that caused rounding issues. (fixed-i hope)
It is also set up so the student can use the table to show work (up to 10 steps) or do work on the side and just put the answer in the top box.
Lots of copy paste for the table code. I find using google docs to “find/replace” makes that a breeze.

It’s not perfect — it will accept -3/-5 or - -3/-5 etc…

Anyways…here it is if you want to play around with it and if you find issues, let me know, (or anyone else who comes accross this) I am sure you have plenty of free time! I also would not be surprised if there were other versions of this type of slide out there, but this is the one I enjoyed making. Again, thanks for your help. You have been awesome to me on this journey. If you go back a few years, you might find some pretty basic questions that you helped me with along the way, that looking back on I feel a bit silly how basic they were.

1 Like

Thanks for the recognition! Been a little busier the past year, but I’ve always enjoyed sharing my learning here.

Not looking at it directly right now, but you can probably catch those double negatives by getting more sourdough (lol specific) in your frac pattern by using p.integer.satifies(`x>=0`).