MATH problem

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
gharryh
Posts: 5
Joined: Sun Feb 11, 2007 2:09 pm
Location: Enschede, Netherlands
Contact:

MATH problem

Post by gharryh »

I want to perform this routine

distanceProd = distance * 36
timeProd = seconds * 10000
KPH = distanceProd / timeProd
sKPH = KPH * 32

where distance = 10000 (this is the maximum distance)
and seconds = 3600 (the minimum KPH of 0.01 at maximum distance)

This wouild mean a 32/24bit opperation. How can i do that in FlowCode

Regarsd

Harry

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:

Post by Benj »

Hello Harry

You may be able to do the calculations using int variables but if the numbers are getting too large then you can define short variables and use a C code Block.

Code: Select all

short distanceProd, timeProd, KPH, sKPH; 

distanceProd = FCV_DISTANCE * 36;
timeProd = FCV_SECONDS * 10000; 
KPH = distanceProd / timeProd; 
sKPH = KPH * 32; 
However this calculation routine would be very inefficient and would probably not work correctly due to instructions running into the thousands if not higher. A simpler approach would be to scale down the equasions and use psudo numbers to represent the actual numbers.

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

32-bit maths can be done within C using the "long" datatype, so this may work:

Code: Select all

long distanceProd, timeProd, KPH; 

distanceProd = FCV_DISTANCE * 36; 
timeProd = FCV_SECONDS * 10000; 
KPH = distanceProd / timeProd; 
FCV_SKPH = KPH * 32; 
(where the 3 FCV_ variables are defined within Flowcode as 16-bit "integers").

Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

Post by Mark »

gharryh

I would personally consider this as a math problem rather than a Flowcode capability issue.

You generate a number *36 and divide it by a number *1000. It makes more sense to combine these constants into a single divide by 28. This should then sit nicely within the +/- 32000 that Flowcode provides.

As a general point, 32,000 effectively allows for 2^15 accuracy, which is 2^ 3 or 8 times better resolution than a good A/D input. Hence, I would personally put 32 bit math as a low priority for Flowcode.

Hope this helps,

Mark

Mark


ps the 28 is an approximation and could be *10 then /277
Go with the Flow.

gharryh
Posts: 5
Joined: Sun Feb 11, 2007 2:09 pm
Location: Enschede, Netherlands
Contact:

Post by gharryh »

Hi Steve,
steve wrote:32-bit maths can be done within C using the "long" datatype, so this may work:

Code: Select all

long distanceProd, timeProd, KPH; 

distanceProd = FCV_DISTANCE * 36; 
timeProd = FCV_SECONDS * 10000; 
KPH = distanceProd / timeProd; 
FCV_SKPH = KPH * 32; 
(where the 3 FCV_ variables are defined within Flowcode as 16-bit "integers").
If i would use this inside flowcode how should i do that??

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Create 3 integer variables, "Distance", "Seconds" and "sKPH", within Flowcode.

Put the code into a C icon within your flowchart.

Before this icon, populate "Distance" and "Seconds" with appropriate values.

After the C icon, the value in "sKPH" should be correct.

NOTE: this code will not simulate, so you would need to download it to a PICmicro to see it working.

gharryh
Posts: 5
Joined: Sun Feb 11, 2007 2:09 pm
Location: Enschede, Netherlands
Contact:

Post by gharryh »

Can i attach my flowcode for a review??

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

steve wrote:1. Create 3 integer variables, "Distance", "Seconds" and "sKPH", within Flowcode.

2. Put the code into a C icon within your flowchart.

3. Before this icon, populate "Distance" and "Seconds" with appropriate values.

4. After the C icon, the value in "sKPH" should be correct.

NOTE: this code will not simulate, so you would need to download it to a PICmicro to see it working.
The code you need to place into a C icon in step (2) is the code previously mentioned, ie:

Code: Select all

long distanceProd, timeProd, KPH; 

distanceProd = FCV_DISTANCE * 36; 
timeProd = FCV_SECONDS * 10000; 
KPH = distanceProd / timeProd; 
FCV_SKPH = KPH * 32; 
But also pay attention to Mark's post. He is quite right; with a few adjustments to your maths routines, you will not need to delve into C and use 32-bit maths.

We will be allowing users to post their code for review on this site, hopefully within the next month or two (we are waiting for an update to the BB software).

If you were a registered user of Flowcode, I would suggest that you send the code to us and we would have a quick look. But unfortunately, you have not bought Flowcode and so we cannot provide that extra level of support.

Post Reply