round() and trunc()
BradC
Posts: 2,601
While trying to figure out why bstc generated code different to the Propeller Tool when using complex compile time float expressions, I noticed that round() and trunc() are handled inconsistently by the Parallax compiler :
The oddity here is if the expression begins with trunc() or round() the Parallax compiler treats the entire expression as a constant and reduces it in the compiler. If not then the expression is parsed and compiled normally.
I can't find this behaviour documented in the manual and it appears inconsistent and broken. Can someone else verify this behaviour?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
Con A = 1.234 B = 2.345 PUB Fred | X X := Trunc(A+b)/Trunc(A-B) <-- is valid X := X + Trunc(A+B) <-- is valid X := Trunc(A+B) + X <-- is not
The oddity here is if the expression begins with trunc() or round() the Parallax compiler treats the entire expression as a constant and reduces it in the compiler. If not then the expression is parsed and compiled normally.
I can't find this behaviour documented in the manual and it appears inconsistent and broken. Can someone else verify this behaviour?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
Comments
it is my understanding that people shouldn't be using the + or any other math operator on floating point numbers, unless it is all constants or literals, so the compiler can pre-calculate it.
i mean, it's insightful, but I don't really expect it to work.
Trunc() and Round() turn the remainder of the expression into a constant expression.
If you have something like
X := Y + Z / Trunc(A+B) + 2 + U
It will fail on the "U" as anything after the Trunc() must be a constant.
On the flip side, the compiler calculates the Trunc() and any expressions after it into a single constant value completely ignoring the overall precedence rules.
That above expression is passed to the interpreter as Y + Z / (Trunc(A+B) + 2 + U) (or it would be if it compiled. Substitute the U for a constant to see it),
whereas it *should* parse as Y + (Z / Trunc(A+B)) + 2 + U if it followed the precedence rules.
Parallax, is this a bug in the compiler?
This is the output from the Propeller Tool.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
Trunc() and Round() both generate integers.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.