mathnet / mathnet-symbolics

Math.NET Symbolics
http://symbolics.mathdotnet.com
MIT License
341 stars 66 forks source link

Suggestion: Expression x+2/3 should not be treated as x+floor(2/3) #70

Closed sadqiang closed 5 years ago

sadqiang commented 5 years ago

For symbolic computation, treating 2/3 as 0 is not so elegant. Should we write 2.0/3 or 2/3.0 or 2.0/3.0 to avoid "flooring" in symbolic computation?

var x = Variable("x");
Func<double, double> f = (x+2/3).Compile("x");// it will be interpreted  as  f = x + floor(2/3) = x
double c = 1.0 / 3;
 f(c).ToString(); // produces 0.333333

Any suggestions are always welcome! Thank you!

FoggyFinder commented 5 years ago

Any suggestions are always welcome!

Just use F# and you won't have such problem

cdrnet commented 5 years ago

Unfortunately to my understanding there is nothing we can do about this in this form, since the compiler handles this before we can touch it (i.e. it figures out both are integers and thus performs an integer division, consistent with the C# language spec). Even in F# you have to remember to use the Q literal in this case, e.g. x+2Q/3 or x+2/3Q.

Of course you could use our own infix parser, i.e. Parse("x+2/3"), but I guess that's not what you're looking for.