Is there an easy way to round a floting point number up/dn

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
Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times

Is there an easy way to round a floting point number up/dn

Post by Werner »

Hi Guys, I have a variable that is a float point number and I would like to convert it to the closest int? Is there an easy way to do this? I am working with Pic 16F877A. I did find a little info on the forum but could not get it figured out. Thanks for the help!

Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

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

Re: Is there an easy way to round a floting point number up/

Post by medelec35 »

Hi Werner,
Not sure if this will help, But rounding a float and leaving as a float is fairly awkward. It was briefly covered here:
http://www.matrixmultimedia.com/mmforum ... 89&p=17676

Perhaps an alternative you may what to consider is just convert a float to an integer, then the integer will be the float rounded to 0 decimal places.
If you click on the properties of a calculation box (or just double click on the calculation box ) You will see a functions... button.
select that then you can use the Fload2Int function. That will convert a float variable to an int variable.
float to int.png
(61.01 KiB) Downloaded 3017 times

Martin
Martin

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

Re: Is there an easy way to round a floting point number up/

Post by medelec35 »

If what I posted does not help, then if you give an example of the original float value and what you want the value to be, then I see if I can post a flowchart example.
Please remember a float takes 32bits.
So if you can get away with an integer then do so.
If you have want to display 12.34, you can have 1234 as an integer, then have a routine to convert it.
I have got examples of this.
See here for example:
http://www.matrixmultimedia.com/mmforum ... 326#p20326

Martin
Martin

Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times

Re: Is there an easy way to round a floting point number up/

Post by Werner »

Hi Martin, Thanks! Is Float_Var listed as a Float point (Type)? I tried it and it simulates and compiles however does not seem to return an int?
[img]
Flag.jpg
Flag.jpg (22.63 KiB) Viewed 5730 times
[/img]


Thanks for the help Buddy have a great day!

Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

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

Re: Is there an easy way to round a floting point number up/

Post by medelec35 »

Hi Werner,
I have posted an example flowchart.

The reason why yours is not working is probably because you have not used fmul() function to multiply the float by 0.7.
If you just use float_Var * 0.7 then float result in simulator will be 9.936800, but it will be 0 in real life.

Martin
Attachments
float 2 int.fcf
(5 KiB) Downloaded 267 times
Martin

Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times

Re: Is there an easy way to round a floting point number up/

Post by Werner »

Hi Martin, Thanks! I think I see where my problem is? LengthTotal is an Int, made up from a adding a few variables together. It does not look like I can do what I want and that is to take my float variable TotalLengthFlot = LengthTotal a none float?
[img]
FloatPic.jpg
(202.99 KiB) Downloaded 1282 times
[/img]

Thanks Buddy!

Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

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

Re: Is there an easy way to round a floting point number up/

Post by medelec35 »

Would you like to post your flowchart (pm if u want to keep it private) then I will see if I can get it working the way you want it to.

Martin
Martin

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

Re: Is there an easy way to round a floting point number up/

Post by Spanish_dude »

Hi,

There's an int2float function. Try that with your first calculation box (even thought I don't think it will change anything).
What is the value of TotalLength4 ? Is it a point number ?

I'd double check the variable types, just in case you oversaw a wrong type.

Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times

Re: Is there an easy way to round a floting point number up/

Post by Werner »

Thanks Spanish, Yep that did it! You and Martin had both found it. You Guys are great!! :D

Have a great day!

Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

Post Reply