Hi,
As it is known that the math function fround(x,y) doesn't work for the PIC family, I was wondering if anyone has a recommended workaround for it?
What I do is convert the float to string, then look at a specific character to see if it is above 5 or not, and if so, add the round to the original number.
But it somehow feels awkward this way, there must be a smarter and more elegant method!
Jan Didden
Linear Audio
Workaround for fround(x,y) for PIC?
Moderator: Benj
-
- Valued Contributor
- Posts: 1208
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
Re: Workaround for fround(x,y) for PIC?
How about something like:
Which will work for float values of limited range (up to a 32 bit integer value when shifted left by the required number of decimal places - which may or may not be helpful?)
Note that this also highlights the fact that floats aren't necessarily exact - try 1.234 to 1 or 2 digits!
Martin
Where x is a float argument, y number of digits reqd after the point and temp is an unsigned 32 bit int.Which will work for float values of limited range (up to a 32 bit integer value when shifted left by the required number of decimal places - which may or may not be helpful?)
Note that this also highlights the fact that floats aren't necessarily exact - try 1.234 to 1 or 2 digits!
Martin
-
- Flowcode v5 User
- Posts: 273
- Joined: Thu Apr 17, 2008 9:59 am
- Has thanked: 19 times
- Been thanked: 16 times
Re: Workaround for fround(x,y) for PIC?
Martin, I tried the attached, which should be equivalent to your example, but it does not give what I expected. With an initial value of 1.006 I expect a rounding to 1.01 but I am getting 1.00 . What am I missing?
Jan
Jan
- Attachments
-
- fround.PNG
- (3.28 KiB) Downloaded 2082 times
-
- Flowcode v5 User
- Posts: 273
- Joined: Thu Apr 17, 2008 9:59 am
- Has thanked: 19 times
- Been thanked: 16 times
Re: Workaround for fround(x,y) for PIC?
This works, as noted, though feels awkward.
Jan
Jan
- Attachments
-
- round2.PNG
- (11.24 KiB) Downloaded 2079 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
-
- Flowcode v5 User
- Posts: 273
- Joined: Thu Apr 17, 2008 9:59 am
- Has thanked: 19 times
- Been thanked: 16 times
Re: Workaround for fround(x,y) for PIC?
Actually, it is much simpler ...
Just add 0.005 and display only 2 decimals. If the 3rd decimal is > 5, it automagically rolls over to 1.01, if not it remains 1.00 .
Jan
Just add 0.005 and display only 2 decimals. If the 3rd decimal is > 5, it automagically rolls over to 1.01, if not it remains 1.00 .
Jan
- Attachments
-
- round3.PNG
- (2.89 KiB) Downloaded 2076 times