Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non-canonical form does not consistently parse fractions (Rational vs. Divide)

See original GitHub issue

Either I don’t understand how this is supposed to work, or there is a bug here. Canonical form is explained in the documentation. The example shown is:

// ➔ ["Rational", -3, 5]

ce.parse("\\frac{3}{-5}", {canonical: false})
// ➔ ["Divide", 3, -5]

However, it doesn’t really seem like setting canonical to false really does anything. Rather, it seems that fractions with positive numerators and denominators are parsed as Rational while putting anything else in the numerator and denominator (including simply adding a -) will result in a Divide.

Example in this CodePen: mathLiveIssueDemo4

Sod the question is two-fold:

  1. Does setting {canonical:false} do anything in this context? I can’t seem to get it to work.
  2. Aren’t fractions supposed to be interpreted as Divide rather than Rational in the non-canonical parsing mode?

Thanks @arnog!

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

MatousAccommented, Nov 16, 2022

Thanks. Yeah, I think I understand that better now. I’m kinda new to issues (all of mine so far have been interactions with you) so thanks for your patience.

I tried your suggestion and that worked. Thank you so much!
arnogcommented, Nov 16, 2022

The CodePen could be more useful if it demonstrated what the problem you are talking about is. It’s a fairly generic code that doesn’t demonstrate specifically the problem (I still have to know what I’m supposed to type in the field in order to see what the problem is), and it includes a lot of things that are not necessary to demonstrate the issue (some CSS, some UI, some event handling).

Since you ask, here’s what would have made it easier:

When executing:

console.log(ce.parse('\\frac{3}{4}', { canonical: false }).toJSON());

I get ["Rational", 3, 4]. I was expecting ["Divide", 3, 4] instead since canonical is false.

This is code that I can copy and paste to verify I get the same result, and I can clearly see what you are getting vs what you are expecting.

That said, to answer you question, if you want to avoid Rational in the json output, you can use this:

ce.jsonSerializationOptions = { exclude: ['Rational'] };
console.log(ce.parse('\\frac{3}{4}', { canonical: false }).toJSON());
// -> ["Divide", 3, 4]

The exclude JSON serialization option can be used to indicate identifiers that should be avoided and replaced by an equivalent form when possible. In this case, Rational will be replaced with a Divide.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Canonical Form - CortexJS
It is not always “the simplest” way to represent an expression. ... this is // interpreted as a regular fraction ("Divide"), not a...
Read more >
fractions — Rational numbers — Python 3.11.1 documentation
The fractions module provides support for rational number arithmetic. A Fraction instance can be constructed from a pair of integers, from another rational...
Read more >
Implementation of rational number arithmetic for .NET ... - GitHub
Reducing fractions, canonical form​​ To reduce a fraction, you can use the CanonicalForm property. That returns a rational number that's irreducible, and where ......
Read more >
The Real Numbers: Not All Decimals Are Fractions - Frontiers
A rational number is any number that we can write as a fraction a b of two integers (whole numbers or their negatives),...
Read more >
c# - Convert a text fraction to a decimal - Stack Overflow
Try splitting it on the space and slash, something like: double FractionToDouble(string fraction) { double result; if(double.TryParse(fraction, out result)) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found