aboutsummaryrefslogtreecommitdiffstats
path: root/src/hal/components/div2.comp
blob: 487107f5a3a1fd134d4c0c6a74f191a6ac0b508f (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//   This is a component for LinuxCNC HAL
//   Copyright 2021 Noel Rodes
//
//   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.
//
//   Do not use a zero Divisor.
//   Dividing by zero creates a bad, indefinite result.  NOT ALLOWED
//   This is simple math.
//

component div2 "Quotient of two floating point inputs";
pin in  float in0 "the Dividend";
pin in  float in1 "the Divisor";
pin out float out "the Quotient   out = in0 / in1";
param rw float deadband "The \\fBout\\fR will be zero if \\fBin\\fR is between -\\fBdeadband\\fR and +\\fBdeadband\\fR" ;

description """
A very simple comp to divide a floating point number
by another floating point number, to get a floating point result.
Remember, not to use a zero divisor. 
A zero divisor creates an indefinte result.
This is simple mathematics. """;

license "GPL"; // indicates GPL v2 or later
author "Noel Rodes";
see_also "mult2(9), invert(9) ";
function _;

;;

#include <rtapi_math.h>

static bool error_msg = 0;

FUNCTION(_) {
    double dividend = in0;
    double divisor = in1;
    if ( deadband < 1e-12 ) deadband = 1e-12;
    if ( divisor > -deadband && divisor < 0 ) {
        out = ( dividend / -deadband );
        if (!error_msg)
            rtapi_print_msg(RTAPI_MSG_ERR,
                            "\n \n DIV2 DIVISOR IS TOO CLOSE TO -ZERO,\n THEREFORE -DEADBAND IS USED.\n INCREASE THE DIVISOR, in1.\n \n \n");
        error_msg = 1;
    } else if (divisor >= 0 && divisor < deadband) {
        out = ( dividend / deadband );
        if (!error_msg)
            rtapi_print_msg(RTAPI_MSG_ERR,
                            "\n \n DIV2 DIVISOR IS TOO CLOSE TO +ZERO,\n THEREFORE +DEADBAND IS USED.\n INCREASE THE DIVISOR, in1.\n \n \n");
        error_msg = 1;
    } else {
        out = ( dividend / divisor );
        error_msg = 0;
    }
}

bues.ch cgit interface