Different variable types in calculation?

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Different variable types in calculation?

Post by Creative25 »

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

User avatar
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?

Post by Benj »

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.

Creative25
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?

Post by Creative25 »

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

User avatar
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?

Post by JonnyW »

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

Creative25
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?

Post by Creative25 »

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

User avatar
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?

Post by JonnyW »

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

Post Reply