How can I do an Absolute Function Abs

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

How can I do an Absolute Function Abs

Post 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);

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

Re: How can I do an Absolute Function Abs

Post 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.

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times

Re: How can I do an Absolute Function Abs

Post by kersing »

How about creating a macro that checks if the input < 0, if so return -input else return input
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Docara
Posts: 315
Joined: Sun Jun 23, 2013 1:29 pm
Has thanked: 28 times
Been thanked: 61 times

Re: How can I do an Absolute Function Abs

Post 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
Last edited by Docara on Sat Jul 01, 2017 2:02 am, edited 1 time in total.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

Re: How can I do an Absolute Function Abs

Post 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 253 times

Post Reply