中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-29 08:04
47,816 topics / 349,916 posts / today 0 new / 48,265 members
DOS批处理 & 脚本技术(批处理室) » [Help] Get the absolute value through four arithmetic operations in the command line
Printable Version  4,491 / 15
Floor1 everest79 Posted 2007-01-17 12:58
金牌会员 Posts 1,127 Credits 2,564
Probably as follows

set /p a=input
set /a b=algorithm
%b% is the absolute value of the input number

Without using character replacement or IF judgment
Floor2 namejm Posted 2007-01-17 13:27
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The simplest way is to use a string replacement statement. The demonstration code is as follows:

——————————————————
It turns out that character replacement is not wanted, and the use of if statements is not allowed. It's really too BT. I don't know what special needs you have for such a requirement. Here's another one:


[ Last edited by namejm on 2007-1-17 at 12:33 AM ]
Floor3 everest79 Posted 2007-01-17 13:42
金牌会员 Posts 1,127 Credits 2,564
Hehe, the moderator you cheated, I want to get the absolute value through calculation, I've thought about it all afternoon
Floor4 redtek Posted 2007-01-17 22:23
金牌会员 Posts 1,147 Credits 2,902
Method 1: Principle of Two Negatives Make a Positive



There is an error when calculating numbers with decimals ~ : )
There is an error when calculating positive numbers : )



Method 2: (Negative number) - (Negative number) * 2 = (Absolute value)



Does not support floating-point operations.
Only supports finding the absolute value of a negative number (errors occur when entering a positive number)
Floor5 scriptor Posted 2007-01-17 23:30
银牌会员 Posts 555 Credits 1,187
I got one:

@echo off
set /p n="Input any negative number: "
set /a m=~n+1
echo The absolute of %n% is: %m%
pause >nul

It's too easy by using it.

No decimal fraction, but zero is permitted.

[ Last edited by scriptor on 2007-1-17 at 11:16 AM ]
Floor6 qzwqzw Posted 2007-01-17 23:37
银牌会员 Posts 636 Credits 2,343
A crude example

Each of the upper and lower limits of the value of a is reduced by half

set /a b=(a*2+1)%%2*a
Floor7 scriptor Posted 2007-01-17 23:47
银牌会员 Posts 555 Credits 1,187
Please delete it. It was written wrong.

[ Last edited by scriptor on 2007-1-17 at 11:14 AM ]
Floor8 scriptor Posted 2007-01-17 23:51
银牌会员 Posts 555 Credits 1,187
Please let the moderator delete it.

[ Last edited by scriptor on 2007-1-17 at 11:12 AM ]
Floor9 qzwqzw Posted 2007-01-17 23:59
银牌会员 Posts 636 Credits 2,343
Brother scriptor

Here are two suggestions for you

1. Use more Chinese
2. Do more testing
Floor10 scriptor Posted 2007-01-18 00:09
银牌会员 Posts 555 Credits 1,187
Originally posted by qzwqzw at 2007-1-17 10:59:
Brother Scriptor

Here are two suggestions for you

1. Use more Chinese
2. Do more testing


You mean the code on floor 8?
Sorry, I made a mistake, that one can only calculate odd numbers

Thank you
I was in a hurry and sent it over
Floor11 qzwqzw Posted 2007-01-18 00:15
银牌会员 Posts 636 Credits 2,343
Have you tested the ~n+1 code in #4 with positive integers?

In addition, keeping the layout concise is our common obligation
Floor12 scriptor Posted 2007-01-18 00:19
银牌会员 Posts 555 Credits 1,187
I have stated there:
@echo off
set /p n="Input any negative number: "
is to input a negative number.

Thanks. Remembered
I found that I made many mistakes these days.
Really embarrassed to post again
Floor13 everest79 Posted 2007-01-18 01:13
金牌会员 Posts 1,127 Credits 2,564
Originally posted by qzwqzw at 2007-1-17 10:37 AM:
A crude example

Shrink the upper and lower limits of a by half each

set /a b=(a*2+1)%%2*a


Thank you qzwqzw, it seems I overcomplicated the problem
Floor14 namejm Posted 2007-01-18 10:26
荣誉版主 Posts 1,737 Credits 5,226 From 成都
qzwqzw's idea is good, PF.
Floor15 tghksj Posted 2007-01-18 11:38
社区乞丐 Posts 90 Credits -49
Gently ask:

Teacher qzwqzw

Can we use the binary assignment symbol of SET for conversion??

[ Last edited by tghksj on 2007-1-17 at 10:50 PM ]
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023