Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int.toString().interpret() returns a num of type double not int #6

Closed
DaniyalDolare opened this issue Nov 12, 2021 · 4 comments
Closed

Comments

@DaniyalDolare
Copy link

The output of "1".interpret() should be 1 but it come as 1.0
Similary for "1+1".interpret(), the output is 2.0 but it should be 2

@Throvn
Copy link

Throvn commented Nov 20, 2021

You can parse the output to a string and 'regex it away' :)
At least thats what I did haha.

// get ints without comma
final result = "1 + 1".interpret();
print(result.toString().replaceAll(RegExp(r'\.0+\b'), ""));

// get ints in LaTeX equation without commas
final equationAsTex = "1 + 1".interpret()
          .toString()
          .toSingleVariableFunction()
          .tex;
print(equationAsTex.replaceAll(RegExp(r'\.0+\b'), ""));

btw. this .replaceAll(RegExp(r'\.0+\b'), "") works also with the LaTeX string.

@ram6ler
Copy link
Owner

ram6ler commented Nov 21, 2021

The output of "1".interpret() should be 1 but it come as 1.0 Similary for "1+1".interpret(), the output is 2.0 but it should be 2

Thanks for your interest in this library!

Actually, '1'.interpret() originally would have returned an int and '1.0'.interpret() would have returned a double, but then I deliberately changed it in response to a request so that numbers would always be treated as doubles to avoid issues with integer overflow cases. In general, I think this is the behavior users are expecting.

If you want, however, you can change this behavior by changing line 62 of interpreter.dart from

final x = double.tryParse(expression);

to

final x = num.tryParse(expression);

Hope that helps!

@DaniyalDolare
Copy link
Author

DaniyalDolare commented Nov 21, 2021

Instead of users doing this, you can add this logic to check if the answer of interpret() is equal to interpret ().toInt(), then return answer.toInt() else answer.
So for the case of 1, it would check like
var a = "1".interpret(); // 1.0
var b = "1".interpret().toInt(); // 1
if(a==b) // 1.0 == 1 is true
{
answer = b; // 1
}else{
answer=a; // 1.0
}

So for 1.2, the above logic will return 1.2 not 1
Hope you understand @ram6ler .

@ram6ler
Copy link
Owner

ram6ler commented Nov 23, 2021

Thanks for the idea. There's no need to do the check, though: num.tryParse already uses context to return an int or a double if that is the desired output.

In any case, it looks as though it's pretty easy to force a result to be an int when that is what is desired, but in most use cases, it either doesn't matter whether the output is an int or a double, or the desired output is a double.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants