Support fractions using superscripts and subscripts
peterhoucll opened this issue · 19 comments
+1
Can you describe more what you’re asking for? This project is primarily for turning text in to math. There is already support for the Unicode-defines fractions (½
, ⅓
, ⅔
, ¼
, ¾
, ⅕
, ⅖
, ⅗
, ⅘
, ⅙
, ⅚
, ⅛
, ⅜
, ⅝
, and ⅞
), but I’m not sure how you’d want to write a fraction in a way that’s not division.
If you’re asking about more general features such as reducing fractions and keeping them as rational numbers instead of performing the division and turning them in to Doubles
, we can chat about that too. It’s something I’ve thought about, but I’m reluctant to turn this project in to a full symbolic evaluation engine a la Mathematica.
I hope to support the calculation of any fractions, thank you.
¹²⁄₂ + ⁴²⁄₂ = 27
¹⁴⁄₅ + ⁵⁄₂₁
4 ⁵⁄₆ + 3 ²⁵⁄₈
How do propose representing these fractions in a string of text?
Oh, are those superscript and subscript characters?
Yes, can you support it?
Perhaps... I already recognize superscript characters as exponents, such as 5²
. I'd have to think about how to account for this sort of possibility.
@kerusan is this what you have in mind as well?
The separate superscript is still expressed as an exponents.
When the superscript + ⁄ + subscript is expressed as a fraction, is this ok?
Yes, checking for the /
+ subscripts would be the way to go
Some of the trickiness would be to correctly interpret 5¹/₂
as 5.5
and not 5 * ½
Can this solution be used?
#142
... I completely forgot I'd implemented that already. Yes, that was basically how I was thinking of doing it (using implicit addition instead of implicit multiplication)
So, are you going to support fractions calculation?
Yeah, this will probably be ok. I'll warn you up-front however that there will be no way to get ³/₂
as an answer. You'll get 1.5
, because everything gets converted to Doubles
.
Is it possible to add a switch for the calculation result? If it is on, it is a fraction; if it is off, it is a Double, which is off by default.
Not at this time. DDMathParser only supports calculations as Doubles. See #99.
Supporting a different kind of evaluation number would require a very very very large and invasive change to the entire codebase. It's something I'd like to do eventually, but that sort of change is years down the road. This sort of "recognize fractions" bit is doable in days.
How is this solution?
https://stackoverflow.com/questions/35895154/decimal-to-fraction-conversion-in-swift
For using in your own code, that seems reasonable at first glance.
The problem with "using rational arithmetic throughout all calculations" as the answer suggests is that there is an entire domain of numbers that are not rational. As soon as you toss something like π in your string, you have to start dealing with mixing rational and irrational numbers as part of parsing and evaluation. This means that you're no longer doing evaluation of mathematical values, but are instead doing symbolic manipulations of them. Basically, you start dealing with numbers as a type that have a rational component, an irrational component, and an imaginary component, and then things get really weird as you try to define what it means to manipulate them.
As much as I'd like DDMathParser to be a "complete" solution for parsing code and handling this sort of stuff, I'm just one guy doing this in my free time. 😁
Ok, I am converting the calculation results myself, huh.
thank you very much.