Old bug in macros.h

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
User avatar
ASM
Engineer
Engineer
Posts: 5
Joined: 29 Dec 2004 17:21
Location: Moscow, Russia

Old bug in macros.h

Post by ASM »

There is a bug in CHANCE16 macros which causes industry growth miscalculation with smooth economy. Its in SVN since revision 1.
Patch for 0.4.7 attached, should apply to trunk too.
Attachments
macros-0.4.7.patch
(750 Bytes) Downloaded 214 times
Ryalla
Engineer
Engineer
Posts: 19
Joined: 15 Apr 2006 06:13

Re: Old bug in macros.h

Post by Ryalla »

ASM wrote:There is a bug in CHANCE16 macros which causes industry growth miscalculation with smooth economy. Its in SVN since revision 1.
Patch for 0.4.7 attached, should apply to trunk too.
If it's been in macros.h since revision 1, how do you know it's an error? :P
User avatar
ASM
Engineer
Engineer
Posts: 5
Joined: 29 Dec 2004 17:21
Location: Moscow, Russia

Re: Old bug in macros.h

Post by ASM »

Ryalla wrote:If it's been in macros.h since revision 1, how do you know it's an error? :P
Because it makes smooth economy unusable :(
With it we have 20/1024 chance of production decrease and same chance of production increase, so industries doesn't grow at all.
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Re: Old bug in macros.h

Post by SirkoZ »

ASM wrote:
Ryalla wrote:If it's been in macros.h since revision 1, how do you know it's an error? :P
Because it makes smooth economy unusable :(
With it we have 20/1024 chance of production decrease and same chance of production increase, so industries doesn't grow at all.
Indeed it is - smooth economy without my improved patch (didn't touch the macros though) is totally unusable - industries mostly decrease if they are being used and increase if they are not used... :-S

At any rate - I'll test your patch...
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

Wow - it's already been included into trunk...

It works - quite nicely - now 2 more bugs with the current smooth_economy - IT_FARM_2 = tropical maize_farm doesn't plant any fields and once industry production decreases to 32/40 units per month it does not increase anymore, which is annoying - here is a quick fix for those 2 bugs.
Attachments
smooth_econ_quick_fix.patch
diff_-_r5095
(905 Bytes) Downloaded 193 times
henrik
Engineer
Engineer
Posts: 19
Joined: 05 Mar 2005 12:53

Post by henrik »

And this is why macros are evil and should rarely be used. Inline functions are so much better for this kind of thing!
MeusH
Tycoon
Tycoon
Posts: 4349
Joined: 25 Oct 2004 15:39
Location: Mississauga

Post by MeusH »

Can anyone explain me the patch by ASM? How do a and (a) differ?
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

henrik wrote:And this is why macros are evil and should rarely be used. Inline functions are so much better for this kind of thing!
Oh cry me a river - it's fixed now, ain't it?
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

MeusH wrote:Can anyone explain me the patch by ASM? How do a and (a) differ?
It takes into account all of the a, especially if a is something like
x + ((20 * pct_transported) >> 8 ). ;-)
hertogjan
Director
Director
Posts: 560
Joined: 03 Jan 2006 20:45
Location: Netherlands

Post by hertogjan »

MeusH wrote:Can anyone explain me the patch by ASM? How do a and (a) differ?
It's about what happens at the precompiling stage. Suppose you have the code

Code: Select all

#define SQUARE(a) a*a
which you want to use to square a number.
Now if you put in something like

Code: Select all

SQUARE(2+3);
it is changed to 2+3*2+3 (it just replaces a by 2+3), which equals 11, and not 25 which you were after. To prevent this from happening, you'll have to define

Code: Select all

#define SQUARE(a) (a)*(a)
Now

Code: Select all

SQUARE(2+3);
will be changed to (2+3)*(2+3), yielding the correct result 25.

[edit]Whoops. Don't forget to check when copy-pasting next time :mrgreen:[/edit]
Last edited by hertogjan on 20 Jun 2006 19:03, edited 1 time in total.
User avatar
glx
OpenTTD Developer
OpenTTD Developer
Posts: 623
Joined: 02 Dec 2005 15:43
Location: Drancy(93) - France
Contact:

Post by glx »

Code: Select all

#define SQUARE(a) (a)*(a)
return the correct value :)
User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post by Darkvater »

glx wrote:

Code: Select all

#define SQUARE(a) (a)*(a)
return the correct value :)
Let's turn it into this, shall we? ;)

Code: Select all

#define SQUARE(a) ((a)*(a))
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."
MeusH
Tycoon
Tycoon
Posts: 4349
Joined: 25 Oct 2004 15:39
Location: Mississauga

Post by MeusH »

Thank you so much for answers :)
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 9 guests