Replies: 12 comments
-
That's correct. Fraction.js supports this notation, but the expression parser of math.js can't since it conflicts with implicit multiplication. There is one way to create a Fraction with inifitely repeating decimals, by passing a string to the
|
Beta Was this translation helpful? Give feedback.
-
Could we make this possible with the quoted notation?
|
Beta Was this translation helpful? Give feedback.
-
Thanks for your suggestion Clément. I think that is technically possible, but I doubt if it would help since this is not a common notation (as far as I know). Thinking about this more, we could consider changing the parser such that it does accept Anyone interested in working this out in a PR? |
Beta Was this translation helpful? Give feedback.
-
this only works if the parenthesis is placed directly after the point For example |
Beta Was this translation helpful? Give feedback.
-
The common notation is to add a line over the repeated part, I think that's what the quoted notation try to simulate. |
Beta Was this translation helpful? Give feedback.
-
argh, you're right, I hadn't thought about that 🤔 |
Beta Was this translation helpful? Give feedback.
-
We can perhaps consider that if we have something of the form It would be curious to want to do 12.34 × (56) with parenthesis and implicit multiplication. |
Beta Was this translation helpful? Give feedback.
-
That is a good point. So the parsed number should only match digits inside the parenthesis, like |
Beta Was this translation helpful? Give feedback.
-
This examples should be parsed as decimal repetition This examples should be parsed as implicit multiplication Not sure for this example: |
Beta Was this translation helpful? Give feedback.
-
Good, clear summary 👌 . We can discuss about When implementing this, we should recon the different numeric types that we have: |
Beta Was this translation helpful? Give feedback.
-
I agree, we can't put an implicit multiplication between two numbers, it is like considering that |
Beta Was this translation helpful? Give feedback.
-
workaround: const mathFrac = math.create({ number: "Fraction" })
function parseRepeatingDecimals(expression) {
return expression.replaceAll(/\d*\.\d*(\(\d+\)|'\d+')/g, rd => `(${mathFrac.fraction(rd).toFraction()})`)
} usage example: const expression = "1.(3) * 2"
const parsedRD = parseRepeatingDecimals(expression) // (4/3) * 2
math.evaluate(expression).toString() // ⇒ 6
math.evaluate(parsedRD).toString() // ⇒ 2.6666666666666665 |
Beta Was this translation helpful? Give feedback.
-
math.eval('66.(3)');
returns198
Beta Was this translation helpful? Give feedback.
All reactions