Floating point

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
mario1
Posts: 8
Joined: Fri Apr 08, 2011 3:55 am
Has thanked: 18 times
Contact:

Floating point

Post by mario1 »

Hi
Is there any way of reducing the no. of digits after the decimal point when its displayed in a Lcd in Flowcode..The displayed value being a result of a floating point calculation.
For example can we display 1.23 instead of 1.23456.
Thanks
Mario.

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Floating point

Post by JohnCrow »

Hi
When you print the result of a float calculation on the LCD you print it as a string.
If you need a fixed length, you can set the lenght of the string in the variables setting.

floatstring[5] would print a sting of 5 characters (including the decimal point)

Not sure if you can set a variable lenght though, never tried that.

floatstring[lenght] where length is calculated in the flowchart.
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Floating point

Post by medelec35 »

Hi Mario,
Try this attachment.
If float=1.234567, then 1.23 should be displayed on LCD.
I'm not saying this is best or simplest method, but it's the the way I would do it.
It is very easy to expand the range (currently float has to be less than 1000). and amount of decimal places.
If you want a higher range and get stuck then let me know expected float range please.
Can you let me know if this way works for you please.
Note: Untested on hardware.

Martin
Attachments
Rounding floats.fcf
(7.5 KiB) Downloaded 694 times
Martin

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: Floating point

Post by Benj »

Hello,

I'm not sure if Medelec's example will work on the hardware as BoostC cannot do floating point operations or comparisons without using the floating point function library.

John's ides is good but if you have a varying number of integer digits then this will not always give you 2 decimal points. There is also the potential for variable corruption as you are basically trying to write to a string that isn't long enough. EDIT - I have looked into this possibility and the function was written so it actively stops any corruption so this is confirmed as not an issue.

The way I would probably do it is to change the internals.h file located in the Flowcode FCD folder.

Find the FCI_FLOAT_TO_STRING function and add this line of code to the top of the function.

Precision = 2;

You should end up with something like this.

Code: Select all

char FCI_FLOAT_TO_STRING(float Number, char Precision, char* String, char MSZ_String)
{
	int whole;
	char str_length;
	char idx;
	char stringidx = 0;
	float real, temp;
	char old;
	char temp_string[10];
    Precision = 2;                    //manually added 05/05/11
This way you are doing less processing rather then doing all the processing and then more processing to strip off the extra numbers.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Floating point

Post by medelec35 »

Thanks Ben,
Did not realise that.

Martin
Martin

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: Floating point

Post by Benj »

No worries, caught me out a few times too :(

Its annoying at best :) But other then Floats BoostC is a great compiler. I'll maybe have a word with the authors and see if we can do anything about the poor float support.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Floating point

Post by medelec35 »

@Ben.
Two things.
Benj wrote:I'm not sure if Medelec's example will work on the hardware as BoostC cannot do floating point operations or comparisons without using the floating point function library.
1) I tried on real hardware using 18F4455, and to my surprise it did actually work :o

2)
Benj wrote: But other then Floats BoostC is a great compiler. I'll maybe have a word with the authors and see if we can do anything about the poor float support.
Talking of BoostC are they able to optimise code like Hitec compiler?
For instance I was programming 16F616 with boost C and I had about 1byte of rom free. Ouch! I thought, that is tight.
I then tried the 30 day trial version of hitec compiler with the same flowchart.
To my amazement I had only used 50% rom, I have put loads more calculations,decisions and RS232 outputs that give diagnostic information.
This optimization will enable great product like Flowcode to be even better as there will be more room available on the chip.

Martin
Attachments
Rounding floats.fcf
Same Flowchart as above but configured and tested with 18F4455
(8 KiB) Downloaded 631 times
Martin

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: Floating point

Post by Benj »

Hi Medelec,
I tried on real hardware using 18F4455, and to my surprise it did actually work :o
Wow ok that's great. I wonder why that works when the float operations require the functions. Thanks for letting me know.
To my amazement I had only used 50% rom, I have put loads more calculations,decisions and RS232 outputs that give diagnostic information.
To be fair Hi Tech is probably the better compiler but it does have its issues. Mainly the price to buy a license, $1195 for a license that does not switch off the optimization and this only works for either 16F or 18F devices so essentially you have to buy it twice :(

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Floating point

Post by medelec35 »

medelec35 wrote:
Talking of BoostC are they able to optimise code like Hitec compiler?
Thanks for reply Ben.
I was asking if the makers of Boostc could optimise code, similar to Hitec compiler.

Was not suggesting to use Hitec because of how expensive it is.

Martin
Martin

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: Floating point

Post by Benj »

Hello,

I will also suggest this to the guys at Sourceboost and see if there is anything that can be done to make the code more efficient.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Floating point

Post by Spanish_dude »

Benj wrote: Mainly the price to buy a license, $1195 for a license that does not switch off the optimization and this only works for either 16F or 18F devices so essentially you have to buy it twice :(
:shock: omg :shock:

I didn't know it was that expensive

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Floating point

Post by medelec35 »

Ben,
Your idea of :

Code: Select all

Precision = 2;   
Would be a better way to go.
No only does it simplify the flowchart, It would use up less room on target device.

Re Boost C V Hitec.
Boost C appears to have less bugs and is more compatible on different window versions.

E.g when I was testing Hitec compiler in demo mode ,although code length was significantly reduced,
Using same flowchart:
Hex file generated by 64bit win7 (did not work correctly,no RS232 chars on hyperterminal) was a lot different to hex file generated by 32bit win7 (100% working)
This is not the case with BoostC.

As for the cost: Ouch! I can see why you don't go down the full Hitec route. I can't imaging any Flowcodes being sold that is compatible for Both 16F and 18F devices ...hmm cost about £1500...hmm wheres my credit card :lol:
Martin

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: Floating point

Post by Benj »

Hello all,

Just had a thought and if anyone needs to have multiple precisions on the floating point string output then this should work.

Change the line in the internals file to this.

Precision = FCV_PRECISION;

and then in your Flowcode program make a byte variable named precision. Load this variable with your required number of decimal points eg 2 or 6 and then run the function as normal.

The only downside to this mod is that the internals file will not compile correctly without the precision variable present in your program.

Therefore I recommend you keep two versions of the internals file so you can switch between them if necessary.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Floating point

Post by medelec35 »

I have had an idea base on
Benj wrote: Change the line in the internals file to this.

Precision = FCV_PRECISION;

and then in your Flowcode program make a byte variable named precision. Load this variable with your required number of decimal points eg 2 or 6 and then run the function as normal.

The only downside to this mod is that the internals file will not compile correctly without the precision variable present in your program.
But i'm not sure on the correct format.

Is it possible to do something like:

Code: Select all

MX_UINT8 FCI_FLOAT_TO_STRING(MX_FLOAT Number, MX_UINT8 Precision, MX_STRING String, MX_UINT8 MSZ_String)
{
	MX_SINT16 whole;
	MX_UINT8 str_length;
	MX_UINT8 idx;
	MX_UINT8 stringidx = 0;
	MX_FLOAT real, temp;
	MX_UINT8 old;
	MX_UINT8 temp_string[10];
	#ifdef FCV_PRECISION; 
	Precision = FCV_PRECISION; 
	#endif
?

But in the correct format since the above is incorrect?
I don't the correct way of checking to see if a variable named Precision has been defined.
Reason I would like to do it this was is so two PIC_CAL_String.c are not required.
Also if Precision variable within Flowcode was not created, then no errors would be produced and Precision would be the default setting.
When Precision was created within flowchart and set to 2, using just :

Code: Select all

Precision = FCV_PRECISION; 
with Flowocde V5 failed to compile with:

C:\Program Files (x86)\Flowcode\v5\FCD\..\CAL\PIC\PIC_CAL_String.c(353:14): error: unknown identifier 'FCV_PRECISION'
C:\Program Files (x86)\Flowcode\v5\FCD\..\CAL\PIC\PIC_CAL_String.c(353:14): error: invalid operand 'FCV_PRECISION'
C:\Program Files (x86)\Flowcode\v5\FCD\..\CAL\PIC\PIC_CAL_String.c(353:12): error: failed to generate expression

This is not high on my list of importance, but would be a nice thing to do.

Martin
Martin

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: Floating point

Post by JonnyW »

Hi Martin.

Change
#ifdef FCV_PRECISION;
To
#ifdef FCV_PRECISION

For the correct syntax. But am I right that FCV_PRECISION is a variable? You can not use the pre-processor on variables - this is a text replacement only, before the variables are defined.

Either you can '#define USE_PRECISION' in supplementary code or some flag to use the FCV_VARIABLE:
#ifdef USE_PRECISION
Precision = FCV_PRECISION
#endif
Or use C code without preprocessor:
if (FCV_PRECISION != 0) Precision = FCV_PRECISION;

In v5 the code can be told to generate functions however you want, so this is not necessary.

Cheers,

Jonny

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Floating point

Post by medelec35 »

Thanks for your reply Jonny.

My intention is to either add a variable in Flowchart called Precision (via normal variable manager and not C if poss) and assign with the number of places to round a string to that has been converted from a float.
E.g Float_Var=1.234567
Precision=3
After FloatToString
String_Var = "1.234"

Or not have the variable in Flowchart called Precision at all so:
Float_Var=1.234567
After FloatToString
String_Var = "1.234"

So I was going to place in PIC_CAL_String.c a little routine to assign Precision variable that would either retrieve value from Variable in Flowchart called Precision (only if it has indeed been added)
Or just leave as is, but only if no variable in Flowchart called Precision has been added

So it's the routine to place in PIC_CAL_String.c I'm after.

From what your saying there is no automatic method of getting PIC_CAL_String.c to reassign Precision variable from the variable assigned in Flowcode.
I Do understand what your saying because preprocessor is carried out before FC variables are assigned.

I did try and place:
#ifdef USE_PRECISION
Precision = FCV_PRECISION
#endif
In supplementary code but there was no rounding, so may have misread your reply.

Maybe I should of done something to PIC_CAL_String.c as well?
Tried adding:
#ifdef FCV_PRECISION
Precision = FCV_PRECISION;
#endif
As well as code supplementary code
No errors, but not rounding either. :(

Attached is flowchart Im trying the rounding on.


I appreciate your reply Jonny :)

Martin
Attachments
Rounding floats.fcf
(11.5 KiB) Downloaded 447 times
Martin

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: Floating point

Post by JonnyW »

Hi Martin. Sorry, I wasn't clear, heads in the clouds today!

1) In supplementary code add the line:

Code: Select all

#define FLOAT_PRECISION FCV_PRECISION
2) In the CAL string stuff at the start of the FCI_FLOAT_TO_STRING() function add:

Code: Select all

#ifdef FLOAT_PRECISION
  Precision = FLOAT_PRECISION;
#endif
3) Create a Flowcode global variable called 'Precision' and assign it the value 3 or at the start of Main() (or in its initialiser in v5). This will be used in the CAL stuff as FCV_PRECISION.

This should work as you want it to. Note, this isn't exactly what I was trying to say before - I have changed this a little so if you do not define FLOAT_PRECISION then the code will work as it previously did, and if you define this as a constant you do not need the variable.

Cheers,

Jonny

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Floating point

Post by medelec35 »

No problem Jonny,
Thank you for replying.

I tried what you suggested but unfortunately still could not get it to work.
I looks like

Code: Select all

#ifdef FLOAT_PRECISION
 Precision = FLOAT_PRECISION;
#endif
Is not being accessed.
To confrim this I altered code slightly to:

Code: Select all

MX_UINT8 FCI_FLOAT_TO_STRING(MX_FLOAT Number, MX_UINT8 Precision, MX_STRING String, MX_UINT8 MSZ_String)
{
	#ifdef FLOAT_PRECISION
   Precision = FLOAT_PRECISION;
    Precision = 2;
	#endif
	
	MX_SINT16 whole;
	MX_UINT8 str_length;
	MX_UINT8 idx;
	MX_UINT8 stringidx = 0;
	MX_FLOAT real, temp;
	MX_UINT8 old;
	MX_UINT8 temp_string[10];
	
Precision was not assigned to 3
Even though added
#define FLOAT_PRECISION FCV_PRECISION
to supplementary code etc:
Rounding Float1.png
(70.45 KiB) Downloaded 14451 times
Martin
Martin

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Floating point

Post by hyperion007 »

Sorry to bumb this old thread but I can't seem to get any further with my questions.

The math function: fround(x,y) will not compile in FC6

I need to round off a floating number to two decimal places. I know that I can change the precision as suggested above but I need the default precision for all the calculations leading up to where I need to round the number. And if I just change the precision, will that not just discard the digits below the two decimal places?

If the number originally is say 123.45678 I need it to round to 123.46 and not 123.45
And I would like to avoid changing the precision back and forth every cycle.

Any ideas?

Post Reply