Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

windows - SET Command - Floating point numbers?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...