1 //-------------------------------------------------------------------------
\r
3 // This file contains some DataFlex 3.2 Console Mode functions
\r
4 // to provide extended mathematical capabilities.
\r
6 // This file is to be included in df32func.mk
\r
8 // Copyright (c) 2006-2015, glyn@8kb.co.uk
\r
10 // df32func/math.inc
\r
11 //-------------------------------------------------------------------------
\r
13 //-------------------------------------------------------------------------
\r
15 //-------------------------------------------------------------------------
\r
17 // Round a number to n decimal places. Returns a string.
\r
18 // argv = number argv2 = decimal places
\r
19 function decround global number argv integer argv2 returns string
\r
20 local number l_nInVal l_nDcnVal l_nDcnDecVal l_nDcnAppPoints
\r
21 local integer l_iDcnPoints
\r
22 local string l_sReturn
\r
24 move argv to l_nInVal
\r
25 move argv2 to l_iDcnPoints
\r
27 move (number("0."+repeat(0,l_iDcnPoints)+"5")) to l_nDcnDecVal
\r
28 if (l_nInVal < 0) calc (l_nDcnDecVal*-1) to l_nDcnDecVal
\r
29 calc (l_nInVal+l_nDcnDecVal) to l_nDcnVal
\r
31 move (l_iDcnPoints-(length(l_nDcnVal)-pos(".", l_nDcnVal))) to l_nDcnAppPoints
\r
32 if (l_iDcnPoints >= 1) move (left(l_nDcnVal,((pos(".",l_nDcnVal))+l_iDcnPoints))) to l_sReturn
\r
33 else move (left(l_nDcnVal,((pos(".",l_nDcnVal))+l_iDcnPoints)-1)) to l_sReturn
\r
34 if (not (l_sReturn contains ".") and l_iDcnPoints <> 0) append l_sReturn "." (repeat("0",l_iDcnPoints))
\r
35 else if (l_nDcnAppPoints > 0) append l_sReturn (repeat("0",l_nDcnAppPoints))
\r
36 if (number(l_sReturn) = 0) move (replaces("-",l_sReturn,"")) to l_sReturn
\r
38 function_return l_sReturn
\r
42 // Performs a right binary shift on a variable.
\r
43 // Relies on the embedded binary arithmetic of ior and iand
\r
44 function rshift global integer argv integer shift_by returns integer
\r
45 function_return (integer(argv/(2^shift_by)) iand 255)
\r
48 // Performs a left binary shift on a variable.
\r
49 // Relies on the embedded binary arithmetic of ior and iand
\r
50 function lshift global integer argv integer shift_by returns integer
\r
51 function_return (integer(argv*(2^shift_by)) iand 255)
\r
54 // Ternary operator function like perls boolean ? "true var" : "false var"
\r
55 // Relies on TERNARY command (macro.inc) - beware all evaluations passed to
\r
56 // the function are actually run, so only usefull for strings really in argv2/3
\r
57 function ternary global string argv string argv2 string argv3 returns string
\r
58 local string l_sReturn
\r
61 if ((argv = "0") or (argv = "1")) begin
\r
62 ternary (integer(argv)) argv2 argv3 l_sReturn
\r
65 else if ("@t@f@false@true@" contains ("@"+lowercase(argv)+"@")) begin
\r
66 ternary argv argv2 argv3 l_sReturn
\r
69 custom_error ERROR_CODE_INVALID_BOOLEAN$ ERROR_MSG_INVALID_BOOLEAN ERROR_DETAIL_INVALID_BOOLEAN argv
\r
71 function_return l_sReturn
\r