Math. Batch (missing operand, or missing operator) - math

set /a var=2
(
set /a %var%=var * 2
set /a %var%=%var% * 2
set /a %var%*= 2
)
Why does it say its wrong?
I got messages like Missing operand and Missing operator.
Well, operator is there, though I don't know what's operand.
Yes I searched for answer in here, but non of all solves... St least ones that I saw.

You're misusing the % syntax. % means expand the contents of this variable, which is not what you want to do. You want to store the result of the calculation back into the variable. Just remove the % on the left side of the assignment operator.
Try this instead:
set /a var=2
#echo %var%
set /a var=%var% * 2
set /a var=%var% * 2
set /a var*= 2
#echo %var%

Thats all :)
set /a var=2
set /a var*=2
..value of %var% is 4.
see operand

I believe, that %var% is syntax for interpreter (shell) and it replaces %var% with value of that variable. And it should happen before any command is called.
Set works with environment variables, so it has to work with them and not their values.
Try set /a var = 2; set /a var = var * 2.
By the way, in "4 + 5" + is the operator and 4,5 are operands (arguments of operator).

Related

Unix Commands interpretation

what's the meaning of the following notation in Unix ?
:- or :=
what does the following means
if [ ${path:=0} -eq 1 ]
if [ ${path:-0} -eq 1 ]
The : is a parameter expansion, so if for instance $path is not set, it will fall back to 0 in your example code.
Using a hyphen :- will use the value, and using := will assign the value.
Additional info prompted by comment:
In your example, you are using a variable called $path, so using the := will go like this: if $path is not set, set $path to the value (0 in your case). Any time you use $path afterwards it would be set to your value, 0.
Using :- will simply just use the default value given (again, 0 in your case) and NOT assign it to $path. Any uses of $path afterwards still be not set.

TCL how to have a number return without scientific notation

The title says it all. It's not that I don't like this functionality or anything I just want to know if it's posable to have the number return without any scientific notation attached to it.
returns --> 1.25e-10
what I would like to see --> .000000000125
I looked at the man pages of expr but all it really says that it can handle such values not that it will convert them.
Also I know I can just write something to handle this problem, but I just want to know if there something already built in that I missed or a cool trick.
Thanks!
the result of an expr is internally represented as a float. When you turn it back to a string in an implicit way, you get a lossless* string conversion, which is most readable in scientific notation, so that's what's used. If you don't want that, don't let tcl do the conversion, but be wary that you give up the exact string representation:
% puts [expr {1. / 8 / 1000000}]
1.25e-7
% puts [format "%f" [expr {1. / 8 / 1000000}]]
0.000000
% puts [format "%.20f" [expr {1. / 8 / 1000000}]]
0.00000012500000000000
*lossless only as of Tcl8.5

TCL global stumping me

I have a script where I am doing what appears to be the exact same thing, but it works in that one and not this one. I'm hoping that a few more pairs of eyes will be able to help me find out the issue, I've already spent an hour and a half on it.
I have a proc that edits the global var, it's more complex that this but I'll use this for simplicity's sake:
proc myCustomProc { var } {
global __myGlobal
set __myGlobal [ expr $__myGlobal + 1 ]
}
I have defined a variable globally in my "main" proc:
proc FOO {} {
global __myGlobal
...
...
myCustomProc 5
puts $__myGlobal
Then I get can't read "__myGlobal": no such variable
I have the exact code with a different varname working in a different script, so I'm stumped. Obviously it's NOT identical, I just cannot find the issue.
Edit: both procs are in the same .tcl file
You can't read from a variable that is unset, and that's true whether that variable is global or not. Thus, in the code:
set __myGlobal [ expr $__myGlobal + 1 ]
It first reads the value from the global variable, then adds one to that value, then writes the result back to the global variable. (Actually, it is interpreting the contents of the variable as an expression fragment, which I'd lay good money on being something you don't want as it is slow and unsafe; put the whole expression in braces please.)
For adding one to an integer (and from Tcl 8.5 onwards) you should just use the incr command instead, as that interprets a non-existent value as if it was zero:
incr __myGlobal
But if you're doing something more complex (or working in 8.4 or before), you should instead put a check with info exists like this in front:
if {![info exists __myGlobal]} {
set __myGlobal "the default value"; # Or whatever default you want
}
You could also use a more complex expression like this:
set __myGlobal [expr {[info exists __myGlobal] ? $__myGlobal+1 : 1}]
But I usually try to avoid the ternary operator; it's often not that readable.
The short answer is, you are using $__myGlobal in the expr command before it has been set.

How do I properly perform modulus operations in Batch?

I'm trying to write a batch file that performs operations depending on the result of a modulus operation performed on a set variable. However, I can't seem to get it quite right.
To first of all test my syntax for the mathematical operation, I've been trying to get a simpler script to produce desired results.
:START
SETLOCAL
SET /P Input-Num="Input Number: "
SET /A Input-Num=%Input-Num% %% 2
ECHO %Input-Num%
ENDLOCAL
PAUSE
:END
If I input 5, the expected output is 1. However, instead I get a message saying Missing operator. and then it outputs 5.
What am I doing wrong here?
Using SET /P is your problem, as 5 is no longer treated as a numerical value. Your example as above works as expected

Does a "Power to" function in batch files exist? (Exponent)

Problem
Is there a way to put a variable "to the power" of a number or other variable a batch file? Does a function exist for this? An example of this would be in Python where you can use ** for "to the power of".
EDIT
You can do maths in a batch file... http://en.wikipedia.org/wiki/Batch_file
The power to function is not available in batch scripting, as you may have already figured out from the answers.
One option is to use a loop. You can do the looping the way #Kirk Broadhurst did it last time he had to do the batch scripting, or you can use another way that has become available since then or otherwise may have gone unnoticed by Kirk:
:: calculate x^n
SET x=3
SET n=5
SET result=1
FOR /L %%i IN (1,1,%n%) DO SET /A result*=x
ECHO %result%
Another option is to use the approach described in this answer.
You don't have many maths functions / operators to work with, and you don't have proper loops either so you need to simulate these.
The basic algorithm for x^n
result = 1
for (i = 0 ; i < n; i++)
{
result = result * x;
}
In a batch file you'd need to use goto statements rather than a real loop.
set result=1
set i=1
:multiply
set /a result=result*x
set /a i=i+1
if %i% lss %n% goto multiply
This won't work for non-integer or negative / zero exponents, but you can work that out.
#echo off
:start
echo Set the value of the Base
set x=
set /p x=
echo set the power the Base is raised to
set n=
set /p n=
set y=%x%
If %n%==0 goto sol1
If %n%==1 goto solx
set /a n=%n%-1
set p=0
:eloop
set p=%P%
set /a p=%P%+1
set /a y=%y%*%x%
If %P%==%n% goto sol
goto :eloop
:sol1
set y=1
echo %y%
pause
goto :start
:solx
set y=%x%
echo %Y%
pause
goto start
:sol
set y=%y%
echo %y%
pause
goto start
This code works for all positive integers you select the base then the power the base is raised to it then loops the expression multiple times by adding 1 to a variable p until it matches a variable n multiplying x by itself each time then giving the solution it also gives a solution of %x% for n=1 and 1 for n=0.
I really like the answer by #Andriy, but I'd add one thing to make it a reusable function.
#echo off
CALL :pow 2 3 :: 8
CALL :pow 3 3 :: 27
CALL :pow 5 5 :: 3125
CALL :pow 256 3 :: 16777216
set /p=End of Script, press any key to exit...
GOTO :EOF
:: ----- Call Functions -----
:pow
SET pow=1
FOR /L %%i IN (1,1,%2) DO SET /A pow*=%1
ECHO %pow%
GOTO :EOF
P.S. You can also put the "functions" in files (for example "pow.bat", usage would be just "pow n n") and call them that way, which can be handy (especially if you start using the path variable). I've always found creating reusable functions in Batch to be the coolest but least known "feature" of the scripting language. Additionally, you'd be able to use the variable %pow% in your script (or assign it to another variable) until overwritten by calling the function again.
One last point I'd like to make is that while this is a fun exercise, there is a precision limitation to Batch.. I've found that batch fails to compute properly numbers greater than 2**31 (32 bit limitation).
Best!

Resources