Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do I use the SET command in windows for floating point arithmatic. /A stands for arithmetic and %VAR% prints the data of VAR not the name.

For example when I do:

SET /A VAR="2.5+3.1"
ECHO %VAR%
pause

I get the error: 'Missing Operator'. The output (5.6) should be converted to floating point too

I'm still busy learning the basic syntax.

Regards, Snipe

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

The arithmetic operations of SET /A command are performed over 32-bits integer numbers only; however, you may easily simulate fixed point operations with SET /A if you choose a number of decimal digits and preserve they throughout the operations. For example:

REM Select two decimal digits for all operations
SET /A VAR=250+310
ECHO RESULT: %VAR:~0,-2%.%VAR:~-2%

This method allows you to directly perform addition or subtraction of two "Fixed Point" numbers. Of course, if you want to input numbers in the usual way (with decimal point), you must eliminate the decimal point and left zeros before perform the operations.

You may also directly multiply or divide a FP (fixed point) number by an integer and the result is correct. To multiply or divide two FP numbers we may use an auxiliary variable called ONE with the correct number of decimal places. For example:

rem Set "ONE" variable with two decimal places:
SET ONE=100
rem To multiply two FP numbers, divide the result by ONE:
SET /A MUL=A*B/ONE
rem To divide two FP numbers, multiply the first by ONE:
SET /A DIV=A*ONE/B

Further details at this post.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...