|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『楼 主』:
[讨论]脚本+算法=四舍五入,精确除法运算(申精)
使用 LLM 解释/回答一下
突然在帖子里看到了一个关于四舍五入的请教问题!
心里还在笑笑,CMD又不支持四舍五入运算。
但就在转过去看其他的帖子的时候,却灵机一动,想出了如下算法。。
现在把自己的算法写在下面。希望和高手们一起进步。
也希望高手们可以精简自己的代码。
——————————————————————————————————
思想:
取 除法的 余数 ,若余数减去 被除数的二分之一能大于 0 就证明这个数为5入运算。否则为4舍运算。
加入了对余数的判断,因为余数只能是2n或 2N+1 .若为2n+1,则该数/2后满足4舍5入的条件。若不是,则继续下面的判断
——————————————————————————————————
@echo off & SetLocal EnableDelayedExpansion
set /a 除数=25
set /a 被除数=10
set /a 余数=%除数% %% %被除数%
set /a 参考值=%被除数%/2
set /a 参考值1=%被除数%-%参考值%*2
set /a 进位=%余数%-(%参考值%+%参考值1%)
echo %参考值%,参考值1,%进位%
if %进位% LSS 0 (
echo %除数% ÷ %被除数% 的结果为 4舍运算!
set /a 结果=%除数%/%被除数%
) else (
echo %除数% ÷ %被除数% 的结果为 5入运算!
set /a 结果=%除数%/%被除数%+1
)
echo 答案为:%结果%
pause
--------------------------------------------------------------------------------------------
在这里我只是抛了一块砖。。却引了一块玉出来。。
大家看看bjsh的代码,一次比一次完善。。
现在基本可以应付任意位数的除法运算,且保证精度了。
大家多给bjsh加分吧:)
Last edited by flyinspace on 2007-4-1 at 08:33 PM ]
Suddenly saw a question about rounding in the post!
Thought to myself, CMD doesn't support rounding operations.
But then when I turned to look at other posts, I had an idea and came up with the following algorithm..
Now I'm writing my algorithm below. Hope to progress together with the experts.
Also hope experts can streamline their code.
——————————————————————————————————
Idea:
Take the remainder of division. If the remainder minus half of the dividend is greater than 0, it means this number is rounded up. Otherwise, it's rounded down.
Added a judgment on the remainder, because the remainder can only be 2n or 2N+1. If it's 2n+1, then dividing this number by 2 meets the conditions for rounding. If not, continue with the following judgment
——————————————————————————————————
@echo off & SetLocal EnableDelayedExpansion
set /a divisor=25
set /a dividend=10
set /a remainder=%divisor% %% %dividend%
set /a reference_value=%dividend%/2
set /a reference_value1=%dividend%-%reference_value%*2
set /a carry=%remainder%-(%reference_value%+%reference_value1%)
echo %reference_value%,%reference_value1%,%carry%
if %carry% LSS 0 (
echo The result of %divisor% ÷ %dividend% is rounding down!
set /a result=%divisor%/%dividend%
) else (
echo The result of %divisor% ÷ %dividend% is rounding up!
set /a result=%divisor%/%dividend%+1
)
echo The answer is:%result%
pause
--------------------------------------------------------------------------------------------
Here I just threw out a brick.. but attracted a piece of jade out..
Everyone take a look at bjsh's code, it's getting more and more perfect each time.
Now it can basically handle division operations of any number of digits and ensure precision.
Everyone give bjsh more points :)
Last edited by flyinspace on 2007-4-1 at 08:33 PM ]
|

知,不觉多。不知,乃求知 |
|
2007-4-1 23:51 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
思路不错,可惜还是不能突破“批处理不能做四舍五入”这个难关。
用27/5试试。
The idea is good, but unfortunately it still can't break through the difficulty that "batch processing can't do rounding".
Try 27/5.
|
|
2007-4-2 00:06 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
谢谢lxmxn的指出
现在问题已经解决。。
另:拷贝文件时进度条显示进度问题已经解决!
复制文件时,可以看到拷贝的进度了。
只是会产生临时文件。
在硬盘速度过快,拷贝文件过大的情况下有1~3秒的显示差异。
现在考虑修改这个显示问题中。
Last edited by flyinspace on 2007-4-1 at 02:04 PM ]
Thanks for lxmxn's pointing out.
Now the problem has been solved..
Another thing: the problem of progress bar display during file copying has been solved!
When copying files, you can see the progress of copying.
Only temporary files are generated.
There is a 1-3 second display difference when the hard drive is too fast and the file being copied is too large.
Now considering modifying this display issue.
Last edited by flyinspace on 2007-4-1 at 02:04 PM ]
|

知,不觉多。不知,乃求知 |
|
2007-4-2 00:26 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
@echo off & SetLocal EnableDelayedExpansion
set /p 除数=
set /p 被除数=
set /a 余数=%除数% %% %被除数%
set /a 参考值=%被除数%/2
if %余数% LSS %参考值% (
echo %除数% ÷ %被除数% 的结果为 4舍运算!
set /a 结果=%除数%/%被除数%
) else (
echo %除数% ÷ %被除数% 的结果为 5入运算!
set /a 结果=%除数%/%被除数%+1
)
echo 答案为:%结果%
pause
直接判断余数和被除数的1/2不行?为什么要-然后和0比大小?
@echo off & SetLocal EnableDelayedExpansion
set /p divisor=
set /p dividend=
set /a remainder=%divisor% %% %dividend%
set /a reference_value=%dividend%/2
if %remainder% LSS %reference_value% (
echo The result of %divisor% ÷ %dividend% is rounding down!
set /a result=%divisor%/%dividend%
) else (
echo The result of %divisor% ÷ %dividend% is rounding up!
set /a result=%divisor%/%dividend%+1
)
echo The answer is:%result%
pause
Directly judge whether the remainder is less than 1/2 of the dividend? Why subtract and then compare with 0?
|
|
2007-4-2 00:49 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
呵呵,当然行啊。。。。。但现在你复制的这个代码会有问题。
在特殊情况下会出现判断错误的情况。。
回4楼。有道理,是我多做了一下判断。。
准备写个批处理和c程序做除数1-100000 被除数 1-100000的测试。
看代码会不会出现误判的情况。。
Hehe, of course it can... But the code you copied now has problems. There will be cases of incorrect judgment under special circumstances...
Reply to floor 4. It makes sense. I made one more judgment.
I'm going to write a batch script and a C program to test divisors from 1 to 100,000 and dividends from 1 to 100,000.
To see if the code will have incorrect judgment.
|

知,不觉多。不知,乃求知 |
|
2007-4-2 01:02 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
这是我写的;
精确到你指定的小数点位;四舍五入的;大家帮忙测试下
代码可以再精简的;有时间的话把精简后的发上来;
大家也可以帮忙精简下代码
- @echo off &SetLocal EnableDelayedExpansion
- set /p x=请输入被除数:
- set /p y=请输入除数:
- set /p n=请输入欲保留的小数的位数:
- set /a count=0
- set /a bjsh=10
- :loop
- if %count% geq %n% goto start
- set /a bjsh*=10
- set /a count+=1
- goto loop
- :start
- set /a rest=%x%*%bjsh%/%y%-%x%*%bjsh%/10/%y%*10
- if "%n%"=="0" (set /a last=%x%/%y%) else (
- set /a last=%x%*%bjsh%/10/%y%-%x%*%bjsh%/100/%y%*10
- )
- if %rest% geq 5 set /a last+=1
- if "%n%"=="0" echo 结果为%last% & pause & goto exit
- set /a result=%x%/%y%
- set result=%result%.
- set /a end=%n%-1
- set /a bjsh=10
- for /l %%i in (1,1,%end%) do (
- set /a add=%x%*!bjsh!/%y%-%x%*!bjsh!/10/%y%*10
- set result=!result!!add!
- set /a bjsh*=10
- )
- set result=%result%%last%
- echo 结果为%result% & pause
- :exit
BJSH发表于: 2007-04-01 13:22
Last edited by bjsh on 2007-4-1 at 01:32 PM ]
This is what I wrote;
Precise to the decimal point you specified; rounded; everyone help test it
The code can be further streamlined; if you have time, post the streamlined one;
Everyone can also help streamline the code
- @echo off &SetLocal EnableDelayedExpansion
- set /p x=Please enter the dividend:
- set /p y=Please enter the divisor:
- set /p n=Please enter the number of decimal places to retain:
- set /a count=0
- set /a bjsh=10
- :loop
- if %count% geq %n% goto start
- set /a bjsh*=10
- set /a count+=1
- goto loop
- :start
- set /a rest=%x%*%bjsh%/%y%-%x%*%bjsh%/10/%y%*10
- if "%n%"=="0" (set /a last=%x%/%y%) else (
- set /a last=%x%*%bjsh%/10/%y%-%x%*%bjsh%/100/%y%*10
- )
- if %rest% geq 5 set /a last+=1
- if "%n%"=="0" echo The result is %last% & pause & goto exit
- set /a result=%x%/%y%
- set result=%result%.
- set /a end=%n%-1
- set /a bjsh=10
- for /l %%i in (1,1,%end%) do (
- set /a add=%x%*!bjsh!/%y%-%x%*!bjsh!/10/%y%*10
- set result=!result!!add!
- set /a bjsh*=10
- )
- set result=%result%%last%
- echo The result is %result% & pause
- :exit
BJSH posted on: 2007-04-01 13:22
Last edited by bjsh on 2007-4-1 at 01:32 PM ]
|
|
2007-4-2 02:27 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
呵,算法只是提供一种思路而已。。
。不过bjsh真的把这个发挥得不错:)
至于测试嘛。。我想就算了:)
精度不高。不如不测试。
而且既然已经准备写到实用的级别。。
就应该对错误进行判断了。
要不然,这个就没有意义。
Hehē, the algorithm is just to provide a way of thinking.
. But bjsh really played this well : )
As for the test... I think forget it : )
The precision is not high. It's better not to test.
And since it's already ready to be written to a practical level...
Errors should be judged.
Otherwise, this is meaningless.
|

知,不觉多。不知,乃求知 |
|
2007-4-2 02:44 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
是的
只能精确到小数点后八位;
再高就会出现随机数字了;
搞到百位以上就会出现莫名其妙的-和数字的组合了;
到这里应该是和系统有关了
Yes
Only accurate to eight decimal places;
If it's higher, random numbers will appear;
When it's above a hundred, there will be strange combinations of - and numbers;
Here it should be related to the system
|
|
2007-4-2 02:48 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
我写的代码的算法倒是和flyinspace的不同
flyinspace兄的算法是
取 除法的 余数 ,若余数减去 被除数的二分之一能大于 0 就证明这个数为5入运算
我的是:利用把实际结果10^n放大取到欲精确的位的后一位;然后进行判断
{/color]
The algorithm of the code I wrote is different from that of flyinspace.
Brother flyinspace's algorithm is
Take the remainder of the division. If the remainder minus half of the dividend can be greater than 0, it proves that this number is rounded up by 5
Mine is: Use to magnify the actual result by 10^n to get the digit after the desired precision digit; then make a judgment
{/color]
|
|
2007-4-2 02:52 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
嗯。那么第一点。需要对输入的位数进行判断!尽量不出现随机数字。。
第二,屏蔽到后面的随机数字。。
第三,出错处理机制。
我想这一点应该不难吧。
Well. Then the first point. Need to judge the number of input digits! Try not to have random numbers.
Second, block the following random numbers.
Third, error handling mechanism.
I think this shouldn't be difficult, right?
|

知,不觉多。不知,乃求知 |
|
2007-4-2 02:53 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
嗯;
那倒是挺简单的
判断%n% geq 8 提示 精确度不能高于8位; 后面也用不到屏蔽了
至于出错机制;
也只有在前面判断
x y n 是否为数字就可以了;
这个东西我就不加进去了;留给想用的人加吧;反正也就几行代码的问题
Well;
That is quite simple.
Judge that %n% is greater than or equal to 8 and prompt that the precision cannot be higher than 8 digits; it won't be used later and can be shielded.
As for the error mechanism;
It is only judged in the front.
Just check whether x, y, and n are numbers.
I won't add this thing; leave it for those who want to use it to add. Anyway, it's just a few lines of code.
|
|
2007-4-2 02:57 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
呵呵,试一下
100000 / 7
嗯。。你也考虑一下dos进度条实用性的问题吧。。
Last edited by flyinspace on 2007-4-1 at 02:05 PM ]
Hehe, let's try
100000 / 7
Hmm. You should also consider the practicality of the DOS progress bar issue, etc.
Last edited by flyinspace on 2007-4-1 at 02:05 PM ]
|

知,不觉多。不知,乃求知 |
|
2007-4-2 03:01 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
用100000/7
只能精确到5位;而且因为第6位不准所以第五位也是不准的;
是不是可以想出一种办法可以是精确度再提高啊
Dividing 100000 by 7 can only be accurate to 5 decimal places; and because the 6th decimal place is inaccurate, the 5th decimal place is also inaccurate; Is there a way to improve the accuracy?
|
|
2007-4-2 03:09 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
用 18/7算了一下;
可以精确到第8位但是第八位也是不准的;
精确到7位是准的;
100000/7 和 18/7 的区别在于位数;
所以10^n放大确实会影响精确度的;
隐约感觉 改进算法会提高精确度;虽然系统本身就存在精确度限制;
但是应该可以发挥到极致
Calculated with 18/7;
Can be accurate to the 8th digit but the 8th digit is also inaccurate;
Accurate to 7 digits is accurate;
The difference between 100000/7 and 18/7 lies in the number of digits;
So amplifying by 10^n does affect the precision;
Vaguely feel that improving the algorithm will increase precision; although the system itself has precision limitations;
But it should be able to be maximized
|
|
2007-4-2 03:14 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
嗯。。看一下你自己的算法。你就明白啦:)
把结果只截取你自己估算的精度出来。
而不只是对输入进行判断。输入只是为了在一定程度上帮助用户不走弯路而已。
核心的还是你自己的脚本代码问题。
你觉得呢??bjsh 兄。
Um.. take a look at your own algorithm. You'll understand it :)
Only intercept the precision you estimate by yourself from the result.
And not just to judge the input. The input is only to help users not go astray to a certain extent.
The core is still the problem of your own script code.
What do you think?? Brother bjsh
|

知,不觉多。不知,乃求知 |
|
2007-4-2 03:28 |
|