Workaround for fround(x,y) for PIC?

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

Moderator: Benj

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

Workaround for fround(x,y) for PIC?

Postby jadiflow » Sun May 12, 2019 9:22 am

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: 493
Joined: Wed May 31, 2017 11:57 am
Has thanked: 52 times
Been thanked: 277 times
Contact:

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

Postby mnf » Sun May 12, 2019 12:47 pm

How about something like:
fround.JPG
fround.JPG (7.15 KiB) Viewed 495 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
These users thanked the author mnf for the post:
jadiflow (Mon May 13, 2019 9:39 am)
Rating: 5.56%
 

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

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

Postby jadiflow » Mon May 13, 2019 9:39 am

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: 249
Joined: Thu Apr 17, 2008 9:59 am
Has thanked: 16 times
Been thanked: 16 times
Contact:

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

Postby jadiflow » Mon May 13, 2019 10:01 am

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
fround.PNG (3.28 KiB) Viewed 479 times

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

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

Postby jadiflow » Mon May 13, 2019 10:21 am

This works, as noted, though feels awkward.

Jan
Attachments
round2.PNG
round2.PNG (11.24 KiB) Viewed 476 times

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8323
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2380 times
Been thanked: 3391 times
Contact:

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

Postby medelec35 » Mon May 13, 2019 11:06 am

Hi,
Have you read this post on a different forum?
These users thanked the author medelec35 for the post:
jadiflow (Mon May 13, 2019 11:15 am)
Rating: 5.56%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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

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

Postby jadiflow » Mon May 13, 2019 11:13 am

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
round3.PNG (2.89 KiB) Viewed 473 times

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

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

Postby jadiflow » Mon May 13, 2019 11:17 am

Hi Medelec, thanks for chiming in. I hadn't seen that post, but as noted above have found an extremely simple solution!

Jan
These users thanked the author jadiflow for the post:
medelec35 (Mon May 13, 2019 3:00 pm)
Rating: 5.56%