Mathematical formulae

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

Moderators: Benj, Mods

Post Reply
echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Mathematical formulae

Post by echase »

Pls remind me how formula are supposed to be entering into Flowcode for PICs.

Say I want to read an analogue 0- 5V voltage as an 8 bit input. i.e. it returns 0-255.

To display the voltage on an LCD in millivolts I enter the formula

V= (reading x 5000)/256 and set V to a 16bit variable, so it can be up to 32,767.

But reading x 5000 may be well over 32,767. So do I have to rewrite the formula as:-

V= ((reading x 250)/256)*20

Another error would be dividing by too small a number so that it gives a very inaccurate answer

e.g. (198/256)*5000 = 3867

but (198/4/4/4/4)*5000 = 0 if you round down to a whole integer every time you divide by 4.

Also:-

(255/16)*5000/16 = 4687 if you round down to a whole integer every time you divide

But so does:-

(240/16)*5000/16 = 4687 so the answer is inaccurate

Does Boost C sort out these formula into ones that do not ever overflow 32,767 or give inaccurate results? Or do I have to do it by reshuffling the formula?

Does Boost C do 5/4 rounding if it’s set to 16bit variables and is setting it to 16bit the same thing as it using floating point calculations? Or do I have to force it some other way to use floating point?


Is there a tutorial on this?
Last edited by echase on Fri Feb 20, 2009 4:01 pm, edited 1 time in total.

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: Mathematical formulae

Post by Benj »

Hello

No an Int 16-bit variable will overflow without giving any warning you have to be careful of this.

I would do the following

First read the analogue into your 8-bit Flowcode variable. eg var_adc.

Then using a C code block enter the following

Code: Select all

long calc;
calc = FCV_VAR_ADC * 5000;
calc = calc / 256;
FCV_INT_VAR = calc;
The above code uses a 32-bit variable to do the calculation and then stores the returned value back into the Flowcode 16-bit variable.

There is a way to get floating point numbers into Flowcode PIC but the way its done in BoostC is far from easy.

Here is the link to the topic on Floating point support.
http://www.matrixmultimedia.com/support ... .php?t=505

Flowcode V4 for PIC will have native floating point support similar to AVR and ARM versions.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Re: Mathematical formulae

Post by echase »

That’s great and not too difficult to enter even for people like me who understand little of C.

If I converted the formula (VAR_ADC /16)*5000/16 into:-

long calc;
calc = FCV_VAR_ADC/16;
calc = calc *5000;
calc = calc /16;
FCV_INT_VAR = calc;

would it still return the incorrect 4687 for VAR_ADC=240?

So whilst this C route enables me to use much bigger numbers, do I still have to be careful to avoid returning small answers in the intermediate steps of the calc that will become very inaccurate by being rounded down? Does it do 5/4 rounding?

Also will the C route work with negative numbers like 16bit variables do?

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Re: Mathematical formulae

Post by echase »

Just to be completely clear in my mind, if I enter

(VAR_ADC /256)*5000 as a direct Flowcode statement or

long calc;
calc = calc / 256;
calc = FCV_VAR_ADC * 5000;
FCV_INT_VAR = calc;

as C

The first bit of the calc returns a number less than 1 which is rounded down to 0 or up to 1, if 5/4 rounding is applied. Is Boost C clever enough to work out that there is a problem and shuffle the formula to get it right? Or does it simply step through the calc exactly as written, whichever of the 2 types of input I have used? If the latter is true then presumably the rules to use for the order of a formula are:-

Do + and * first to avoid too small numbers, with - and / last.
Use the largest sized variable needed to stop it overflowing at the biggest part of the calc.
If the above returns too large a number, rethink it more carefully.

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: Mathematical formulae

Post by Benj »

Hello

Yes you would still get the whole number 4687 at the end of the calculation. You could use the floating point library if you wish for exact calculations however these are much more heavy going on the PIC and require the use of the floating point maths functions. Another problem with the floating point maths is that it is hard to pull out working values eg you would have to create a routine to take the whole part and the real part of the number and save them both into flowcode variables for printing out etc.

The floating point example can be found here. Read the comment at the top of the file for help.
http://www.matrixmultimedia.com/Downloa ... .php?id=46

Also helpfull
http://www.matrixmultimedia.com/support ... .php?t=505

The normal maths operations as shown in my and your example will always use integer maths and as such will always round down to the nearest whole number on every calculation. So yes you still have to be a bit careful about loss of accuracy. Negative numbers will work fine when using C code long variables and Flowcode INT variables. Flowcode byte variables cannot go negative.

Post Reply