![]() |
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-10 17:51 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » [Discussion] Script + Algorithm = Rounding, Precise Division Operation (Seeking Essence) |
| Printable Version 18,485 / 32 |
| Floor1 flyinspace | Posted 2007-04-01 23:51 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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 ] |
|
| Floor2 lxmxn | Posted 2007-04-02 00:06 |
| 版主 Posts 4,938 Credits 11,386 | |
|
The idea is good, but unfortunately it still can't break through the difficulty that "batch processing can't do rounding".
Try 27/5. |
|
| Floor3 flyinspace | Posted 2007-04-02 00:26 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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 ] |
|
| Floor4 slore | Posted 2007-04-02 00:49 |
| 铂金会员 Posts 2,478 Credits 5,212 | |
|
@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? |
|
| Floor5 flyinspace | Posted 2007-04-02 01:02 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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. |
|
| Floor6 bjsh | Posted 2007-04-02 02:27 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
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
[ Last edited by bjsh on 2007-4-1 at 01:32 PM ] |
|
| Floor7 flyinspace | Posted 2007-04-02 02:44 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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. |
|
| Floor8 bjsh | Posted 2007-04-02 02:48 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
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 |
|
| Floor9 bjsh | Posted 2007-04-02 02:52 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
The algorithm of the code I wrote is different from that of flyinspace.
Brother flyinspace's algorithm is
|
|
| Floor10 flyinspace | Posted 2007-04-02 02:53 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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? |
|
| Floor11 bjsh | Posted 2007-04-02 02:57 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
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. |
|
| Floor12 flyinspace | Posted 2007-04-02 03:01 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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 ] |
|
| Floor13 bjsh | Posted 2007-04-02 03:09 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
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?
|
|
| Floor14 bjsh | Posted 2007-04-02 03:14 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
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 |
|
| Floor15 flyinspace | Posted 2007-04-02 03:28 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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 |
|
| 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |