Quick Function Question

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
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.
 
#3
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]]]`
 
#4
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
 
#7
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,232
11
Solon, OH, USA
#16
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
 
#17
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]]`