Workaround for fround(x,y) for PIC?

A forums to allow bugs and problems with Flowcode v7 to be reported and resolved.

Moderator: Benj

Post Reply
jadiflow
Flowcode v5 User
Posts: 273
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 19 times
Been thanked: 16 times
Contact:

Workaround for fround(x,y) for PIC?

Post 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

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

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

Post by mnf »

How about something like:
fround.JPG
fround.JPG (7.15 KiB) Viewed 7118 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

jadiflow
Flowcode v5 User
Posts: 273
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 19 times
Been thanked: 16 times
Contact:

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

Post by jadiflow »

Thanks Martin, I'll try that. Certainly looks smarter than my method ;-)

Though I am new to the .mul etc format.

Jan

jadiflow
Flowcode v5 User
Posts: 273
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 19 times
Been thanked: 16 times
Contact:

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

Post 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
Attachments
fround.PNG
(3.28 KiB) Downloaded 2082 times

jadiflow
Flowcode v5 User
Posts: 273
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 19 times
Been thanked: 16 times
Contact:

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

Post by jadiflow »

This works, as noted, though feels awkward.

Jan
Attachments
round2.PNG
(11.24 KiB) Downloaded 2079 times

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: Workaround for fround(x,y) for PIC?

Post by medelec35 »

Hi,
Have you read this post on a different forum?
Martin

jadiflow
Flowcode v5 User
Posts: 273
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 19 times
Been thanked: 16 times
Contact:

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

Post 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
Attachments
round3.PNG
(2.89 KiB) Downloaded 2076 times

jadiflow
Flowcode v5 User
Posts: 273
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 19 times
Been thanked: 16 times
Contact:

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

Post 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

Post Reply