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
Is there an easy way to round a floting point number up/dn
Moderator: Benj
-
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/
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.
Martin
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.
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/
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
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
Re: Is there an easy way to round a floting point number up/
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][/img]
Thanks for the help Buddy have a great day!
Werner
[img][/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"
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/
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
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
Re: Is there an easy way to round a floting point number up/
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][/img]
Thanks Buddy!
Werner
[img][/img]
Thanks Buddy!
Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
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/
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
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/
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.
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.
Re: Is there an easy way to round a floting point number up/
Thanks Spanish, Yep that did it! You and Martin had both found it. You Guys are great!!
Have a great day!
Werner
Have a great day!
Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"