Hello!
I'm currently programming a new project of mine and ran into the following issue.
When I perform the calculation (20/40)/2, the result is not the expected 0.25, but rather 0.
I've managed to recreate this issue in the following script.
void main()
{
double x=(20/40)/2;
alert("Result", x);
}
If you have any ideas about fixing this problem, please tell me, since I need a exact output for my program.
I also tryed it with simple floats, that didn't work either though.
Regards
Taurus Fan
double x = tinyexpr("(20 / 40) / 2");
Thanks a lot, now it works.
It's because the expression is being interpreted as all integers, and thus integer division (with truncation) is being used. If you make one of the operands look like a double, the whole expression will be upward cast to it and evaluated there.
double x=(20/40)/2.0;
might be enough, the .0 indicating that 2.0 is a double. 2.0f is a float, I think. If not, because of the parentheses, you might have to put a .0 on one of the numbers inside it too.
Hello!
Thanks, that helps. I put a .0 behind one of the numbers inside the brackets and it worked.
Regards
Taurus Fan