Page 1 of 1

How can I do an Absolute Function Abs

Posted: Fri Jun 30, 2017 3:14 pm
by Jay Dee
Hi Guys,
An Abs() function will always return a positive value, thus stripping the -ve sign if its present.

Is there a cute way of doing this in FC?

can it be done in a C component?
FCV_Var1 = abs(FCV_Var2);

Re: How can I do an Absolute Function Abs

Posted: Fri Jun 30, 2017 3:33 pm
by Jay Dee
Ok.. :roll:
Yep a line of C seem to do the trick. :D
Since C does not simulate, tested OK on hardware. MIAC dsPIC, which is the test brick of choice this week!!
MIAC_dsPIC_Abs_Test.fcfx
abs test on MIAC
(21.89 KiB) Downloaded 360 times
if anyone knows a reason this may not be the best route ( or if its native to FC), let me know. :-) J.

Re: How can I do an Absolute Function Abs

Posted: Fri Jun 30, 2017 3:37 pm
by kersing
How about creating a macro that checks if the input < 0, if so return -input else return input

Re: How can I do an Absolute Function Abs

Posted: Fri Jun 30, 2017 3:46 pm
by Docara
HI Jay,

Other than specific info from Ben the way I could you not consider the number type and where and how the sign is stored eg the last bit is set (asm). Store the value carry out the ABS command and reintroduce possibly. alternatively if you know would the sign should be after calculation multiple the answer by -1

Matt

Re: How can I do an Absolute Function Abs

Posted: Fri Jun 30, 2017 4:00 pm
by Jay Dee
Thanks Kersing & Matt;
Yes, I was doing a check for <0 and then inverting previously, it just seemed a bit clunky.. but then again in FC, so is un-simulateable C :lol:
For me at least checking for the signed bit, is a bit more work than I really wanted but its a darn good alternative to consider for other applications.
Thanks guys. J.
EDIT
On reflection and having cleaned up other garbage in my development FC, your method is neater! Thanks.
MIAC_dsPIC_Abs_Test2.fcfx
abs method 2
(22.09 KiB) Downloaded 254 times