MB 4.0 beta : ShortToStr ? !

Beta Testing discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

MB 4.0 beta : ShortToStr ? !

#1 Post by Kalain » 06 Aug 2010 11:03

Hi,

This code works correctly under MB 3.80 but not anymore under MB 4.0 beta.

Code: Select all

Dim elev as short
Dim Elev_n, Elev_z, Elev_pos as word
Dim Vbatt_long as Longint
 
Elev = -10
ShortToStr(Elev,Text_4)
Glcd_Write_Text(Text_4,90,2,0)
Elev_z = 1310
Elev_n = 2270
Vbatt_long = Elev_z - (Elev_n - Elev_z) * Elev div 180
Elev_pos = word(Vbatt_long)
Under MB 3.80 :
Vbatt_long = 1363. (Which is ok)

Under MB4.0 beta :
VBatt_long = -23859566

Now, if you remove ShortToStr(Elev,Text_4) this works correctly under MB 4.0 beta. :shock:
Alain

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

Re: MB 4.0 beta : ShortToStr ? !

#2 Post by Kalain » 06 Aug 2010 12:05

ShortToStr is not involved in this behaviour.

I solved my problem with this code rather than to write it in only one line.

Code: Select all

Vbatt_long = (Elev_n - Elev_z) * Elev
Vbatt_long = VBatt_long div 180
Vbatt_long = Elev_z - VBatt_long
Elev_pos = word(Vbatt_long)
But a potential problem still remain in MB 4.0 beta (?)
Alain

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: MB 4.0 beta : ShortToStr ? !

#3 Post by janni » 07 Aug 2010 13:17

There was always a problem with mixing signed and unsigned variables in expressions (such practices were even forbidden in old mB). Note, that in version 3.8, where left-side of assignment decided the type with which partial expression evaluation was done, there'd be calculation error in statement identical to yours but with left-side of type Longword.

Version 4.0 is the first where an attempt at solving this issue was made (BTW, there's a chapter on expression evaluation in Help, now) - apparently it's not perfect yet, but the general rule about expression evaluation holds (expression evaluation is performed at the level of highest-type variable, or one step higher, if necessary - in your expression highest type is Word and evaluation is done on Longword level). Ideally, presence of any signed operand should force Longint as evaluation level, but at the moment it works only with highest type of variable.
In other words, your statement will work properly with explicit typecasting

Code: Select all

Vbatt_long = Elev_z - Longint(Elev_n - Elev_z) * Elev div 180
or

Code: Select all

Vbatt_long = Elev_z - Integer(Elev_n - Elev_z) * Elev div 180
or if you simply declare Elev as Integer.

Note, that with new evaluation rules introduced in v 4.0, the following will work properly

Code: Select all

Elev_pos = Elev_z - Integer(Elev_n - Elev_z) * Elev div 180
i.e. right side will be evaluated using highest necessary type and only then implicitely typecast to left-side variable type. It's definitely an advantage over previous versions, though I regard as most advantageus the fact that one may now better optimise code size (though it may require explicit typecasting).

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

Re: MB 4.0 beta : ShortToStr ? !

#4 Post by anikolic » 09 Aug 2010 10:14

Janni, thank you so much for taking time to explain new expression evaluation rules, which are now closest to Basic standard as possible.
We are working on better promoting our products and changes, and we hope that users will have much more insight on what features and improvements have been introduced into each version of our compilers.

As for your report, Kalain, I have actually managed to reproduce the problem with expression evaluation when following two lines of code are commented out, or uncommented back again:

Code: Select all

ShortToStr(Elev,Text_4)
Glcd_Write_Text(Text_4,90,2,0)
I have reported this to our developers, and I will inform you of their findings. Thank you for reporting this, and sorry for the inconvenience.

Best regards,
Aleksandar
Web Department Manager

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: MB 4.0 beta : ShortToStr ? !

#5 Post by janni » 09 Aug 2010 11:47

aleksandar.nikolic wrote:I have actually managed to reproduce the problem with expression evaluation when following two lines of code are commented out, or uncommented back again:...
That is caused by difference between runtime and compile-time calculations. When ShortToStr(Elev,Text_4) is commented or removed, all calculations are done at compile-time. When this call is present, compiler assumes that Elev might have changed, and most calculations are left to be done at runtime.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: MB 4.0 beta : ShortToStr ? !

#6 Post by Dany » 09 Aug 2010 12:48

janni wrote:
aleksandar.nikolic wrote:I have actually managed to reproduce the problem with expression evaluation when following two lines of code are commented out, or uncommented back again:...
That is caused by difference between runtime and compile-time calculations. When ShortToStr(Elev,Text_4) is commented or removed, all calculations are done at compile-time. When this call is present, compiler assumes that Elev might have changed, and most calculations are left to be done at runtime.
Expression evaluation should always be the same disregarding the moment of evaluation (compile time or run time). See also: http://www.mikroe.com/forum/viewtopic.php?f=86&t=25035. So, the evaluation result should also be independant of optimisations done by the compiler.
This is still a big issue (mP and mB). :shock:
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

Re: MB 4.0 beta : ShortToStr ? !

#7 Post by anikolic » 10 Aug 2010 10:57

Janni, I am impressed with your expertise, and Dany, you are also right.

I have been informed that the bug was located and resolved, and will affect final release.

Kalan, Janni, Dany, thank you all for contributing to solving this matter.

Best regards,
Aleksandar
Web Department Manager

Post Reply

Return to “mikroBasic PRO for PIC Beta Testing”