Hello,
Due to my limited programming experience, the following questions are probably naive, so please forgive me...
Also they could be considered generic Basic language questions, but since I think they are possibly compiler dependent, I'm posting them in this section, hoping not to be OT
So, consider these two simple cases:
1)
dim x as byte
dim y as word
Assigning
x=y
as long as y value is certain to be < 255, is to be considered safe or else can lead to variables area corruption nonetheless?
2)
dim x as byte
dim y as byte
the statement
if (x*y > 1000) then ....
where evidently the result of the test is larger than a byte, is performed correctly or not? That is, the test is made automatically through a larger implicit register or else this is another recipe for register overflow?
Thanks in advance to those willing to help clarify my doubts!
Doubts on type casting
-
- Posts: 158
- Joined: 24 May 2008 15:55
- Location: Germany
Re: Doubts on type casting
I am using mikrobasic a long time and I can asure you, that it handles these cases very professional.
It automatically converts the types when necessary. But you can do it manually with the byte() and word() or float() or ... type qulifier.
It automatically converts the types when necessary. But you can do it manually with the byte() and word() or float() or ... type qulifier.
Re: Doubts on type casting
I agree with Thomas.Pahl that mikroBasic will not cause any bad behaviour from register overflow in your examples.
I know very little about mikroBasic, but I have written a simple test program to find out actual behaviour. The if statement behaviour surprises me, but seems to behave as documented, rather than as I think it should work.
Here is my test code. Be sure to read comments for hints on use.
I know very little about mikroBasic, but I have written a simple test program to find out actual behaviour. The if statement behaviour surprises me, but seems to behave as documented, rather than as I think it should work.
Here is my test code. Be sure to read comments for hints on use.
Code: Select all
' simple test program for forum - use software debugger to step through while watching varx, vary varz in watch window
' read "Conditional expressions" section of help function for explanation of byte multipplication in if statement
program Forum
' Declarations section
dim varw as byte
dim varx as byte
dim vary as word
dim varz as word ' make sure nothing overflows into this variable
main:
varz = 0 ' initialise
vary = 100 ' less than 256 - so no overflow
varx = vary ' copy a word to a byte - expect 100
vary = 1000 ' more than 256 - so expect truncation to 8 bits
varx = vary ' copy a word to a byte - should truncate to 232 - make sure no overflow to varz
varw = 50
varx = 100 ' multiply byte by byte you would think would result in 5000, but seems to truncate to byte
if (varw * varx) > 1000 then
varz = 1234 ' use debug single step to check that we get here - also check varz value
' my observation is that we do not get here - which surprises me
end if
while TRUE ' loop forever
wend
end.
Start every day with a smile...... (get it over with)
-
- Posts: 158
- Joined: 24 May 2008 15:55
- Location: Germany
Re: Doubts on type casting
The if clause will not become true. read the chapter about implicit conversion.
Re: Doubts on type casting
Nice reference Thanks....Thomas.Pahl@t-online.de wrote: ↑28 Oct 2021 15:33The if clause will not become true. read the chapter about implicit conversion.
The documentation does explain the behaviour.
Not easy for beginners like me, but at least the behaviour is documented.
Appreciate the clarification from an experienced user.
Start every day with a smile...... (get it over with)
-
- Posts: 158
- Joined: 24 May 2008 15:55
- Location: Germany
Re: Doubts on type casting
I appreciate your educational explanations. Perhaps better then simple reading.
Re: Doubts on type casting
Thanks so much for your time and the clear explanations!