aboutsummaryrefslogtreecommitdiffstats
path: root/src/hal/components/abs.comp
blob: 8b32b4db1ff39713a210c0f1003d441c883ba526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//   This is a component for LinuxCNC HAL
//   Copyright 2006 John Kasunich <jmkasunich at sourceforge dot net>
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of version 2 of the GNU General
//   Public License as published by the Free Software Foundation.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
component abs "Compute the absolute value and sign of the input signal";

pin in float in "Analog input value" ;
pin out float out "Analog output value, always positive";
pin out bit sign "Sign of input, false for positive, true for negative" ;
pin out bit is_positive "TRUE if input is positive, FALSE if input is 0 or negative";
pin out bit is_negative "TRUE if input is negative, FALSE if input is 0 or positive";

function _;
license "GPL";
author "John Kasunich";
;;
FUNCTION(_) {
    double tmp = in;
    if ( tmp >= 0.0 ) {
	sign = 0;
	out = tmp;
    } else {
	sign = 1;
	out = -tmp;
    }

    if (tmp > 0.000001) is_positive = 1;
    else is_positive = 0;

    if (tmp < -0.000001) is_negative = 1;
    else is_negative = 0;
}
bues.ch cgit interface