Quick Function Question

Hello all,

I was looking for a %@Round function but surprising (to me) it doesn't seem TCC has one. Ok, I'll write my own. Looking to round to the nearest integer.

I created a user defined function in my functions file:
Code:
Round = %+ ^
    iff %@Left[1,%@Decimal[%1]] lt 5 then  %+ ^
        %@Int[%1]  %+ ^
    elseiff %@Left[1,%@Decimal[%1]] ge 5 then   %+ ^
        %@Int[%@Inc[%1]]  %+ ^
    endiff

The logic seems to work fine. However, I get an odd echo command. The first error in the following is expected. But I'm not sure why the 2nd produces the odd echo error.
Code:
TCC  22.00.30 x64   Windows 10 [Version 10.0.16299]
TCC Build 30   Windows 10 Build 16299

D:\Users\Michael>%@Round[4.5]
Unknown Command: 5

D:\Users\Michael>echo %@Round[4.5]
ECHO is OFF
Unknown Command: 5

D:\Users\Michael>

I'm sure it's something silly, but it's got me scratching my head.

I appreciate any thoughts and I hope everyone is having a great holiday season.

Michael

BTW, it would be really great if you didn't need to write a user defined function in one line. I love the library ability to use {}. It saves all of that %+ ^ stuff to keep it all on the same line.
 
May 20, 2008
11,424
99
Syracuse, NY, USA
UDF should evaluate to some text. In yours,
Code:
Round = %+ ^
iff %@Left[1,%@Decimal[%1]] lt 5 then  %+ ^
%@Int[%1]  %+ ^
elseiff %@Left[1,%@Decimal[%1]] ge 5 then   %+ ^
%@Int[%@Inc[%1]]  %+ ^
endiff

the two lines "%@Int[%1] %+ ^" are attempting to execute something (the command "5" in your example).

Try something like this (untested).

Code:
function round `%@if[%@Left[1,%@Decimal[%1]] lt 5,%@Int[%1],%@Int[%@Inc[%1]]]`
 
Hello Vince. Thanks for the comments. Your function is certainly more concise and I'll use it.

I understand having an expression evaluate to '5' would generate an error. But I'm still confused why I can't echo %@Round[4.5] and just have 5 displayed. What's causing the echo is off? The '5' is the parameter that should be echoed, so I'm not sure why that's displayed.

Much appreciated.

Michael
 
May 20, 2008
11,424
99
Syracuse, NY, USA
That's just the normal way of telling @EVAL how many decimal places you want. More examples:
Code:
v:\> echo %@eval[pi=7]
3.1415927

v:\> echo %@eval[pi=0]
3

In my function
Code:
function round `%@if[%@Left[1,%@Decimal[%1]] lt 5,%@Int[%1],%@Int[%@Inc[%1]]]`
the whole thing ***IS*** a string (and so it can be ECHO'd).

In the case of yours, it's the function itself.
Code:
Round = %+ ^
iff %@Left[1,%@Decimal[%1]] lt 5 then  %+ ^
%@Int[%1]  %+ ^
elseiff %@Left[1,%@Decimal[%1]] ge 5 then   %+ ^
%@Int[%@Inc[%1]]  %+ ^
endiff

It doesn't evaluate to anything; the whole thing must represent a string.

And when you say
Code:
IFF contition then
something

"something" is a command and TCC will try to execute it. In your example, it was trying to execute "5". I don't think you should do that in a user defined function.
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
I have no idea. I wrote that from my phone so it’s unchecked.

The ceiling and floor functions essentially do the same thing except one does it with positive numbers and the other with negative numbers. One does “not greater than” and the other “not less than”.

@ceiling[1.5] = 2
@floor[-1.5] = -2
 
May 20, 2008
11,424
99
Syracuse, NY, USA
I asked because it does this, which is not the usual (?) notion of rounding.
Code:
v:\> Function round=`%@if[%1 LT 0,%@floor[%1],%@ceiling[%1]]`

v:\> echo %@round[1.25]
2

I think you can patch it up like this.
Code:
Function round=`%@if[%1 LT 0,%@ceiling[%1-.5],%@floor[%1+.5]]`
 
Similar threads
Thread starter Title Forum Replies Date
Joe Caverly Quick Search in eWriter Help Support 1
M Quick tip: Close tabs in TCMD Support 0
vefatica History recall turns off Quick-Edit Support 4
vefatica Quick help nit Support 0
S How to? Mark threads for quick retrieval Support 3
C Documentation GOSUB quick help Support 6
P Quick help for aliases Support 3
vefatica Documentation Function parameters Support 0
Jay Sage Documentation Function @DRIVE Missing in Help Lists Support 0
Joe Caverly How to? Get name of current LIBRARY function? Support 4
x13 WeekNum function? Support 3
Joe Caverly No blank line in TEXT...ENDTEXT in a LIBRARY function Support 8
cgunhouse Ctrl-C from within a library function Support 6
C email[string] function Support 10
R FUNCTION to count NUMBER of files matching a pattern and specific length/not length Support 4
Dick Johnson Using the @instr function Support 5
vefatica '}' in a library function? Support 3
D Can the argument to RETURN be a function expansion? Support 3
J Trouble with @full[] function. Support 3
vefatica Quoting @Function parameters? Support 0
Joe Caverly Debugging a LIBRARY function Support 10
Steve Pitts Documentation @CAPI function summary Support 0
x13 %x$ in function Support 3
thorntonpg @eval function, interesting results any explanation? Support 3
x13 Function to get interface user-defined name? Support 10
vefatica Fixed Help @FUNCTION, missing paragraph Support 8
Mordachai Variable followed by function constantly failing... Support 5
nickles Call a complex function Support 13
M Fixed Cannot use the "Browse..." function in "Find files/text" dialogbox Support 2
R Function #IDOW returns only 2 characters Support 10
T scrput `` in alias or function Support 2
S International characters - TYPE vs %@LINE function Support 5
JohnQSmith WAD @REPEAT function Support 15
vefatica WAD Nested variable expansion in PDIR's @function[*]? Support 13
R In addition to ALIAS /Z and FUNCTION /Z, HISTORY /Z? Support 1
A pdir function usage Support 3
nikbackm FUNCTION with variable number of arguments Support 17
S %@REGQUERY function Support 1
Dan Glynhampton Documentation v15 help: Error on @TRIMALL function page Support 0
M Why doesn't this @Field function work? Support 0
fpefpe How to? user function Support 1
M Why is this function getting these results in these circumstances? Support 6
A WAD function return values with parentheses prevent evaluation of additional functions Support 31
M Just an out-of-curiosity question the List command vs. the @Lines function... Support 13
M How to? Just verification for what I think I know regarding "Function"... Support 4
Frank How to? need help with "function" Support 10
Joe Caverly How to? Function Key With INPUT Support 5
M How to? "Function" and "%[xxx]" Support 7
M Kind of a out-of-curiosity question re. the "Function" command... Support 5
M Simple question about "Function" command... Support 4

Similar threads