mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

math.eval doesn't recognise numbers with infinitely repeating decimals i.e. `0.(3)`

Open husayt opened this issue 9 years ago • 11 comments

math.eval('66.(3)'); returns 198

husayt avatar Apr 16 '16 02:04 husayt

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 fraction factory function:

math.fraction('66.(3)')
math.eval('fraction("66.(3)")')

josdejong avatar Apr 16 '16 07:04 josdejong

[...] since it conflicts with implicit multiplication

Could we make this possible with the quoted notation?

Math.evaluate("5.'3'") // = 16/3
Math.evaluate("5.'3' ^ 3") // = 4096/27

yukulele avatar Jun 28 '22 19:06 yukulele

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 math.eval('66.(3)') as a repeating decimal notation. It is a bit odd to interpret this as an implicit multiplication (the current behavior), since the dot in 66. is meaningless and someone would just enter math.eval('66(3)') instead when wanting to do an implicit multiplication. We would have to think though if it doesn't conflict with other things, but I think it could work.

Anyone interested in working this out in a PR?

josdejong avatar Jun 29 '22 08:06 josdejong

this only works if the parenthesis is placed directly after the point

For example 66.2(3) is 66.2 × 3 or 66.23333... ?

yukulele avatar Jun 29 '22 09:06 yukulele

but I doubt if it would help since this is not a common notation

The common notation is to add a line over the repeated part, I think that's what the quoted notation try to simulate.

https://en.wikipedia.org/wiki/Repeating_decimal#Notation

yukulele avatar Jun 29 '22 09:06 yukulele

this only works if the parenthesis is placed directly after the point

For example 66.2(3) is 66.2 × 3 or 66.23333... ?

argh, you're right, I hadn't thought about that 🤔

josdejong avatar Jun 29 '22 09:06 josdejong

We can perhaps consider that if we have something of the form /\d*\.\d*\(\d+\)/ (like 12.34(65)) then it is a decimal repetition.

It would be curious to want to do 12.34 × (56) with parenthesis and implicit multiplication.

yukulele avatar Jun 29 '22 10:06 yukulele

That is a good point. So the parsed number should only match digits inside the parenthesis, like 12.34(65), and not something like 12.34(2 + x). I think that can work. It is another special rule around implicit multiplication, but it makes sense I htink.

josdejong avatar Jul 01 '22 11:07 josdejong

This examples should be parsed as decimal repetition 12.34(7) => 12.347777… 12.(7) => 12.7777… .12(7) => 0.127777… .(7) => 0.7777…

This examples should be parsed as implicit multiplication 1234(7) => 1234 × 7 12.34(7.6) => 12.34 × 7.6

Not sure for this example: 1.2(3)4 => ??? 1.2(3)pi => ???

yukulele avatar Jul 01 '22 16:07 yukulele

Good, clear summary 👌 .

We can discuss about 1.2(3)4 and 1.2(3)pi: either throw an error or parse the last part as an implicit multiplication. My initial feeling is: the first example 1.2(3)4 simply looks confusing, so maybe best to not allow it and throw an error. The second one doesn't look very odd to me, we could allow that and parse it as 12.3444444... × pi. What do you think?

When implementing this, we should recon the different numeric types that we have: number, BigNumber, and Fraction (which does already have support for this notation in it's constructor function).

josdejong avatar Jul 04 '22 08:07 josdejong

What do you think?

I agree, we can't put an implicit multiplication between two numbers, it is like considering that 34 would be equal to 3 × 4. On the other hand 3pi is unambiguously equal to 3 × pi

yukulele avatar Jul 04 '22 10:07 yukulele