Computing.Net > Forums > Programming > math in batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

math in batch

Reply to Message Icon

Name: hatlad
Date: August 26, 2008 at 20:14:37 Pacific
OS: Windows XP home
CPU/Ram: 512mb
Product: Dell Custom
Comment:

i want to make a batch file that can do math like multiplication/division and adding/subtracting. Anyone?

Cheese, the more you eat it, the less you care about how mouldy it is!



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: August 26, 2008 at 21:51:19 Pacific
Reply:

The command you want is "set /a". goto the command line and type set /? or google set /a math batch command. Or go here and check out the set command, speciffically "set /a calculations" about midway down the page that the link for the set command takes you to.

There is a lot of info good about this around so there is no point in me repeating it.


0

Response Number 2
Name: Valerie (by Garibaldi)
Date: August 27, 2008 at 00:47:05 Pacific
Reply:

Also note that Set /A cannot handle floating point numbers and any number starting with 0 (zero) is treated as Octal so 08 & 09 will be invalid..


0

Response Number 3
Name: hatlad
Date: August 27, 2008 at 17:06:11 Pacific
Reply:

octal?

Cheese, the more you eat it, the less you care about how mouldy it is!


0

Response Number 4
Name: Razor2.3
Date: August 27, 2008 at 17:23:32 Pacific
Reply:
0

Response Number 5
Name: hatlad
Date: August 27, 2008 at 19:38:51 Pacific
Reply:

ill skip the next round of octaling and ask what an octal and a hexidecimal are. plz put it in the layman's terms for a noob like me.

Cheese, the more you eat it, the less you care about how mouldy it is!


0

Related Posts

See More



Response Number 6
Name: hatlad
Date: August 27, 2008 at 19:59:11 Pacific
Reply:

i wanted to make a simple cipher making batch it says the syntax of the math is wrong can u help?
here it is:

@echo off
:start
cls
set choice=
set /p choice=What is the letter?
if not '%choice%'=='' set choice=%choice:~0,2%
if '%choice%'=='1' goto z
if '%choice%'=='2' goto y
if '%choice%'=='3' goto x
if '%choice%'=='4' goto w
if '%choice%'=='5' goto v
if '%choice%'=='6' goto u
if '%choice%'=='7' goto t
if '%choice%'=='8' goto s
if '%choice%'=='9' goto r
if '%choice%'=='10' goto q
if '%choice%'=='11' goto p
if '%choice%'=='12' goto o
if '%choice%'=='13' goto n
if '%choice%'=='14' goto m
if '%choice%'=='15' goto l
if '%choice%'=='16' goto k
if '%choice%'=='17' goto j
if '%choice%'=='18' goto i
if '%choice%'=='19' goto h
if '%choice%'=='20' goto g
if '%choice%'=='21' goto f
if '%choice%'=='22' goto e
if '%choice%'=='23' goto d
if '%choice%'=='24' goto c
if '%choice%'=='25' goto b
if '%choice%'=='26' goto a
ECHO "%choice%" is not valid
goto start
:a
cls
set choice=
set /p choice=What was the previous letter?
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto A
if '%choice%'=='2' goto A
if '%choice%'=='3' goto A
if '%choice%'=='4' goto A
if '%choice%'=='5' goto A
if '%choice%'=='6' goto A
if '%choice%'=='7' goto A
if '%choice%'=='8' goto A
if '%choice%'=='9' goto A
if '%choice%'=='10' goto A
if '%choice%'=='11' goto A
if '%choice%'=='12' goto A
if '%choice%'=='13' goto A
if '%choice%'=='14' goto A
if '%choice%'=='15' goto A
if '%choice%'=='16' goto A
if '%choice%'=='17' goto A
if '%choice%'=='18' goto A
if '%choice%'=='19' goto A
if '%choice%'=='20' goto A
if '%choice%'=='21' goto A
if '%choice%'=='22' goto A
if '%choice%'=='23' goto A
if '%choice%'=='24' goto A
if '%choice%'=='25' goto A
if '%choice%'=='26' goto A
if '%choice%'=='0' goto A
ECHO "%choice%" is not valid
goto a
:A
SET /P (1*4.5)+%choice%
pause

Cheese, the more you eat it, the less you care about how mouldy it is!


0

Response Number 7
Name: Valerie (by Garibaldi)
Date: August 27, 2008 at 21:20:33 Pacific
Reply:

What are you trying to do with this "SET /P (1*4.5)+%choice%" ??

The Set /p format is SET /P Variable=(PromptString)

If you are trying to multiply 1 X 4.5 then add %choice% you must use Set /A but then I already posted that Set /A cannot handle floating point math, i.e. it will only perform arithmetic calculations on integers. So SET /A Variable=(1*4.5)+%Choice% will fail with the error message "Unbalanced Parenthesis" and SET /A Variable=1*4.5+%choice% will fail with the error message "Missing Operand"


0

Response Number 8
Name: Razor2.3
Date: August 27, 2008 at 22:22:44 Pacific
Reply:

hatlad: what an octal and a hexidecimal are
Something wrong with the link I posted?


0

Response Number 9
Name: Mechanix2Go
Date: August 28, 2008 at 03:58:40 Pacific
Reply:

Am I the only one who can't find the labels that all those GOTOs need?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 10
Name: Valerie (by Garibaldi)
Date: August 28, 2008 at 16:09:47 Pacific
Reply:

M2G - no, you're not, they jest ain't there. The only response I get to work when asked "What is the letter?" is 26, funny kinda letter but then programmers and would-be programmers are funny people!!

Valerie.


0

Response Number 11
Name: hatlad
Date: August 28, 2008 at 17:19:53 Pacific
Reply:

ok so i didnt check the link and i turned each leter into a number and i didn't bother to do the rest when it woulndt work. im sorry. What do i need to do to get the math problem correct and display the answer?

Cheese, the more you eat it, the less you care about how mouldy it is!


0

Response Number 12
Name: Mechanix2Go
Date: August 28, 2008 at 23:05:23 Pacific
Reply:

I don't at all see what the script logic is. You ask for letter input then test for a number.

"goto a
:A"

Doesn't mean much because it's going to the next line regardless.

You don't have any 'math' in there. If this:

SET /P (1*4.5)+%choice%

is intended to do 'math' you need SET /A [not SET /P]. And, as stated earlier, no floating point.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 13
Name: hatlad
Date: August 29, 2008 at 10:47:43 Pacific
Reply:

the point of the goto A is that if they dont type in a number that is valid, then it asks them to type in one that is. I changed it to /P but now, as Valeria rightly said, I need to balance the parenthesis of add an operator. What do theese mean? I dont know much about this stuf or else i wouldn't be asking you. i just want it do find and display the answer. What is a floating number? I tried to look it up but coulndt make sense of it. Pls help me! Your telling me what im doing wrong but not what i should do!

Cheese, the more you eat it, the less you care about how mouldy it is!


0

Response Number 14
Name: Valerie (by Garibaldi)
Date: August 29, 2008 at 21:11:51 Pacific
Reply:

"Your telling me what im doing wrong but not what i should do!"

Right, I'll tell you what you should do.... Before you use a command, display it on the Command Prompt screen by entering the command followed by /? e.g. set/? You can then study the command and its parameters and ensure the command can produce the result you want to achieve.

"I changed it to /P but now, as Valeria rightly said, I need to balance the parenthesis of add an operator."

No - SET /P will NOT perform math in any form, you must use SET /A and the parameters must be consistent with what is permitted. Your set line could be:

Set /a variablename=(1*4)+%choice%

Because Set /A cannot perform floating point math you cannot use 4.5, in other words Set /A can only perform math on whole numbers aka integers.


0

Response Number 15
Name: Mechanix2Go
Date: August 30, 2008 at 01:04:14 Pacific
Reply:

Get a book [remember those?] on high school math.

Your attempt to write a 'cipher' without being conversant in math is a little like the guy trying to write a whiz-bang high end game when he can't do 'hello world' in c++


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 16
Name: hatlad
Date: August 30, 2008 at 10:18:35 Pacific
Reply:

soory 4 wasting ur time but when i said /p i ment /a

Cheese, the more you eat it, the less you care about how mouldy it is!


0

Response Number 17
Name: Valerie (by Garibaldi)
Date: August 30, 2008 at 16:38:38 Pacific
Reply:

Hatlad - you're not wasting our time, we give our time voluntarily in the hope that we can help... However, we won't create your coding for you, that's your part, we will attempt to get you to correct any error.

I suggest you start with one command, say SET, read the help file and generate small scripts to test it. Here's one to start with:

:: Code start.........................
:: Batch script to display the result of multiplication and addition.
@echo off & setlocal
cls

set choice=2
set output=(3*47)+%choice
echo.
echo Result is %output% ... The result should be 143
echo.
endlocal
:: Code ends...........................

The code will fail to produce the correct result, go ahead & amend it to get the correct result.

If there is anything in the code you don't understand research it on the internet & come back to us only if you cannot find a reference or do not understand the explanation(s).

Another thing you can do is grab someone else's coding and go thru' it line by line so that you understand what each line does. Timo Salmi has made 160+ scripts available in the file 1CMDFAQ.TXT which can be downloaded as part of TSCMD.ZIP from http://www.usenet.com/newsgroups/co...

Some of the examples are simple four or five liners others are more complex.

Good luck


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: math in batch

Accurate Math in Batch www.computing.net/answers/programming/accurate-math-in-batch/19373.html

Numbers and basic math in BATCH www.computing.net/answers/programming/numbers-and-basic-math-in-batch/20285.html

Math in batch files www.computing.net/answers/programming/math-in-batch-files/14219.html