MS3 1.2.3 source code released

Testing and development of Megasquirt 3

Moderators: jsmcortina, muythaibxr

elaw
Super MS/Extra'er
Posts: 2926
Joined: Fri Oct 16, 2009 6:20 am
Location: Wilmington, MA

Re: MS3 1.2.3 source code released

Post by elaw »

(Edit: huge post deleted because I figured out my problem... multiple brain farts while editing the .ini file. :? )
Eric Law
1990 Audi 80 quattro with AAN turbo engine: happily running on MS3+MS3X
2012 Audi A4 quattro, desperately in need of tweaking

Be alert! America needs more lerts.
elaw
Super MS/Extra'er
Posts: 2926
Joined: Fri Oct 16, 2009 6:20 am
Location: Wilmington, MA

Re: MS3 1.2.3 source code released

Post by elaw »

Can I ask one more dumb question? This really relates to the C compiler used and not the MS code itself but I'm not quite sure where else to get the answer!

Suppose I write this code:

Code: Select all

int a, b, c, d, y;
a = b = c = d = 10000;
y = (a + b + c + d) / 4;
If the compiler uses 32-bit math, the above will return (in y) the desired result of 10000. If it uses 16-bit math, the (a + b + c + d) calculation will overflow and the result will be a negative number which of course would be bad.

So does anyone know which method the compiler will use? If it's the latter, would changing the calculation to "y = (long)(a + b + c + d) / 4" fix the problem?
Eric Law
1990 Audi 80 quattro with AAN turbo engine: happily running on MS3+MS3X
2012 Audi A4 quattro, desperately in need of tweaking

Be alert! America needs more lerts.
subwoofer
Super MS/Extra'er
Posts: 884
Joined: Sat Apr 30, 2011 12:34 pm
Location: Sandefjord, Norway

Re: MS3 1.2.3 source code released

Post by subwoofer »

y = ((long)a+b+c+d)/4 should work, your code is not guaranteed to work since the conversion to long is optionally after the summation.
Joachim
1974 Jensen-Healey
1990 VW Caravelle Syncro - running MS3+X
2014 Ford Fiesta EcoBoost
jsmcortina
Site Admin
Posts: 39615
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: MS3 1.2.3 source code released

Post by jsmcortina »

subwoofer wrote:y = ((long)a+b+c+d)/4 should work, your code is not guaranteed to work since the conversion to long is optionally after the summation.
Yes, the INPUT to an equation must be cast to a large enough variable so that the output will fit. While apparently illogical, this is correct C.

Casting the inputs to a larger type is particularly important for multiplication.

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
elaw
Super MS/Extra'er
Posts: 2926
Joined: Fri Oct 16, 2009 6:20 am
Location: Wilmington, MA

Re: MS3 1.2.3 source code released

Post by elaw »

Great, thanks!
Eric Law
1990 Audi 80 quattro with AAN turbo engine: happily running on MS3+MS3X
2012 Audi A4 quattro, desperately in need of tweaking

Be alert! America needs more lerts.
Post Reply