|
Post by felixp7 on Oct 28, 2019 9:49:27 GMT 1
Sorry to bother you again, but this one's just baffling. Trying to compile the following code (with BaCon 3.7.2 and GCC 7.3.0 ):
GLOBAL dx = COS(0) TYPE float GLOBAL dy = -SIN(0) TYPE float
PRINT dx, " ", dy
IF ABS(dx) < 0.01 THEN dx = 0 IF ABS(dy) < 0.01 THEN dy = 0
PRINT dx, " ", dy yields an error that makes no sense to me: Even more strange, if I change the assignment to "dy = 0.0" instead, the error message changes to "cannot convert to a pointer type".
Obviously the other line also acts up. What am I doing wrong? Can't seem to spot a related entry in the BaCon changelog, so it's probably yet another misunderstanding on my part.
|
|
|
Post by vovchik on Oct 28, 2019 15:46:06 GMT 1
Dear felixp7,
Thanks for all your very interesting and nicely crafted code. In order for others to try to replicate your errors and engage in some debugging, I would recommend using the latest Bacon version. It is easy to install and Joe (bigbass) has posted a script to grab the latest BaCon from the fossil repository painlessly. Some of those problems you may be encountering may have been fixed in subsequent releases and in beta versions (but maybe not!). The latest betas have all been all OK in my experience, and sometimes contain important fixes and enhancements/features. As a rule, although it is pedantic, I always type cast all variables (and, usually, values returned from functions), just to be on the safe side. I know it is more wordy, but it may prevent the type of error you are experiencing. There are also a number of reserved variable names if they actually are reserved as such in c.
With kind regards, vovchik
|
|
|
Post by felixp7 on Oct 28, 2019 16:33:20 GMT 1
All right, I installed BaCon 3.9.2 from the .deb package. That should be recent enough. I'm getting the exact same behavior. And I don't understand what to cast in this case, a numeric constant? I actually tried this: `dy = (float) 0.0`, and it didn't seem to change anything. And it's not from the IF expressions, those work.
|
|
|
Post by bigbass on Oct 28, 2019 17:02:55 GMT 1
If you use only gcc (the standard way) vovchik's way of getting the latest fossil always works well basic-converter.proboards.com/post/11014I use that script too a very small adjustment should work Joe
DECLARE dx TYPE float '--- dx is the result of dx = COS(0)
DECLARE dy TYPE float dy = -SIN(0)
PRINT dx, " ", dy
IF ABS(dx) < 0.01 THEN dx = 0 IF ABS(dy) < 0.01 THEN dy = 0
PRINT dx, " ", dy
|
|
|
Post by felixp7 on Oct 28, 2019 17:15:44 GMT 1
So it does, thank you very much! However now I'm very confused, because declaring and initializing floats in the same statement works perfectly well for other variables in the same code. Is there a rule I need to observe? Also, please don't ask me to rebuild every new beta from scratch on my ancient PC. Is the latest stable version not recent enough?
|
|
|
Post by bigbass on Oct 28, 2019 17:51:36 GMT 1
Hello felixp7
the recent version of bacon is enough for this code snippet
I am a hobby programmer ( it's some type of a therapy for me)
Peter is the author and a programmer and would have the rules well versed
but what seems to work for me Is DECLARE then assign some value or string
maybe for simple things you could do both on one line but with this example you want the result of a function call
Joe
|
|
|
Post by Pjot on Oct 28, 2019 18:38:39 GMT 1
Hi felixp7, You're not bothering at all, please keep notifying us about the issues you encounter. It will help improving BaCon which is beneficial to us all Regarding your issue, this typically is compiler behavior. In C, variables can be initialized during declaration, but only with static defined values. For example: DECLARE a = 0.0, b = 1.0 TYPE float
This way, the C-compiler knows how to generate a valid binary during compile time. However, your declaration tries to initialize a variable with a function, and we only know the outcome of a function during runtime. In other words, the C compiler can not evaluate the function when it is compiling the code. Therefore, when the C compiler sees a function name, it will automatically think that you are defining the variable as a function pointer, like a void*. This is what you can see in your error. The distinction between conversion time, compile time and runtime is very important when programming in BaCon. It has caused a lot of confusion to many users before, including myself. You can find some notes in the manual, and please feel free to ask if you have more questions about this. Best regards Peter
|
|
|
Post by felixp7 on Oct 28, 2019 19:06:45 GMT 1
Thank you very much, it makes perfect sense now! I really should learn more about C again if I'm going to keep using BaCon. (I've done a little C++ recently, but clearly not enough.) Just for fun, here's the block of declarations that caused the confusion:
LOCAL x = ship[0].sector_x TYPE float LOCAL y = ship[0].sector_y TYPE float LOCAL c = (course - 1) / 8 * 2 * PI TYPE float LOCAL dx, dy TYPE float dx = COS(c) dy = -SIN(c) The initializers for x, y and c were accepted and compiled on first try. (If they turn out not to work, I'll know exactly why.) Only dx and dy caused trouble. Guess that's just how things are.
(In the code above, course is a subroutine argument, and ship is a global array of records.)
|
|
|
Post by Pjot on Oct 28, 2019 19:23:19 GMT 1
Right, so my wording may not have been precise enough: in C, variables can be initialized during declaration, but only with constant initializer elements. Only then the C compiler will know how to compile the code. A function clearly is not a constant element and therefore compilation fails. BR, Peter
|
|