Hi I have a question about calculations.
Is it possible to use different variable Types in the same calculation?
C=A/B
Say the result "C" is bigger than 255 therefore it needs a Int variable.
"A" is a long number therefore it needs a Long variable and "B" is always less than 255 therefore it only needs a byte variable.
Is it Ok to use different variable types? Or is it better to use the same type of variable through the whole calculation?
Best Regards:
Uli
Different variable types in calculation?
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
- Contact:
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Different variable types in calculation?
Hi Uli,
The different variable types should all work fine together.
Int = Long / Byte
The only time there will be a problem is if the result is too large to fit inside the variable type specified. There can be casting issues when using longs on a 8-bit device but give it a go and see how you get on.
The different variable types should all work fine together.
Int = Long / Byte
The only time there will be a problem is if the result is too large to fit inside the variable type specified. There can be casting issues when using longs on a 8-bit device but give it a go and see how you get on.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
- Contact:
Re: Different variable types in calculation?
Hi Benj
Thanks for the Info.
What I have noticed is that some calculations gave a return of 0 instead of the right result.
"A=(b*c)/d"
After I broke the calculation down into smaller ones
"A=b*c
A=a/d"
it works fine.
Best Regards:
Uli
Thanks for the Info.
What I have noticed is that some calculations gave a return of 0 instead of the right result.
"A=(b*c)/d"
After I broke the calculation down into smaller ones
"A=b*c
A=a/d"
it works fine.
Best Regards:
Uli
- JonnyW
- Posts: 1230
- Joined: Fri Oct 29, 2010 9:13 am
- Location: Matrix Multimedia Ltd
- Has thanked: 63 times
- Been thanked: 290 times
- Contact:
Re: Different variable types in calculation?
Hello. What version of Flowcode is this, and are you talking simulation or download?
For example in simulation Flowcode 6 will always use 32-bit for its intermediate calculations. The compact A=(b*c)/d should always work OK in this case.
Can you post a flowchart that shows your problem?
Cheers,
Jonny
For example in simulation Flowcode 6 will always use 32-bit for its intermediate calculations. The compact A=(b*c)/d should always work OK in this case.
Can you post a flowchart that shows your problem?
Cheers,
Jonny
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
- Contact:
Re: Different variable types in calculation?
Hi I am Talking about Flowcode5
Using a program in real Hardware.
A Pic 16F1939
My code is working now. I just want to learn More about variables.
Best Regards.
Uli
Using a program in real Hardware.
A Pic 16F1939
My code is working now. I just want to learn More about variables.
Best Regards.
Uli
- JonnyW
- Posts: 1230
- Joined: Fri Oct 29, 2010 9:13 am
- Location: Matrix Multimedia Ltd
- Has thanked: 63 times
- Been thanked: 290 times
- Contact:
Re: Different variable types in calculation?
Hi. OK, no probs.
Flowcode 5 will behave in simulation the same as v6, which is to keep as like to C as it can.
What a C compiler should do is perform any intermediate calculations as 'int', meaning multiplying two bytes will result in an integer. The optimisation may be 'clever' though (read 'wrong') and first look at what you are assigning it to. Many bugs in compilers come down to over-zealous optimisations.
I would be interested in the values that would cause A=(b * c) / d to be zero but not the broken up version, and have a look at the assembly code produced.
Jonny
Flowcode 5 will behave in simulation the same as v6, which is to keep as like to C as it can.
What a C compiler should do is perform any intermediate calculations as 'int', meaning multiplying two bytes will result in an integer. The optimisation may be 'clever' though (read 'wrong') and first look at what you are assigning it to. Many bugs in compilers come down to over-zealous optimisations.
I would be interested in the values that would cause A=(b * c) / d to be zero but not the broken up version, and have a look at the assembly code produced.
Jonny