Page 1 of 1

Workaround for fround(x,y) for PIC?

Posted: Sun May 12, 2019 9:22 am
by jadiflow
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

Re: Workaround for fround(x,y) for PIC?

Posted: Sun May 12, 2019 12:47 pm
by mnf
How about something like:
fround.JPG
fround.JPG (7.15 KiB) Viewed 7275 times
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

Re: Workaround for fround(x,y) for PIC?

Posted: Mon May 13, 2019 9:39 am
by jadiflow
Thanks Martin, I'll try that. Certainly looks smarter than my method ;-)

Though I am new to the .mul etc format.

Jan

Re: Workaround for fround(x,y) for PIC?

Posted: Mon May 13, 2019 10:01 am
by jadiflow
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

Re: Workaround for fround(x,y) for PIC?

Posted: Mon May 13, 2019 10:21 am
by jadiflow
This works, as noted, though feels awkward.

Jan

Re: Workaround for fround(x,y) for PIC?

Posted: Mon May 13, 2019 11:06 am
by medelec35
Hi,
Have you read this post on a different forum?

Re: Workaround for fround(x,y) for PIC?

Posted: Mon May 13, 2019 11:13 am
by jadiflow
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

Re: Workaround for fround(x,y) for PIC?

Posted: Mon May 13, 2019 11:17 am
by jadiflow
Hi Medelec, thanks for chiming in. I hadn't seen that post, but as noted above have found an extremely simple solution!

Jan