China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-25 10:43
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] Script + Algorithm = Rounding, Precise Division Operation (Seeking Essence) DigestI View 17,245 Replies 32
Floor 16 Posted 2007-04-02 03:35 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Slightly simplified the code


  1. @echo off &SetLocal EnableDelayedExpansion
  2. set /p x=Please enter the dividend:
  3. set /p y=Please enter the divisor:
  4. set /p n=Please enter the number of decimal places to retain:
  5. set /a count=0
  6. set /a bjsh=10
  7. :loop
  8. if %count% geq %n% goto start
  9. set /a bjsh*=10
  10. set /a count+=1
  11. goto loop
  12. :start
  13. set /a rest=%x%*%bjsh%/%y%-%x%*%bjsh%/10/%y%*10
  14. if "%n%"=="0" (set /a last=%x%/%y%) else (
  15. set /a last=%x%*%bjsh%/10/%y%-%x%*%bjsh%/100/%y%*10
  16. )
  17. if %rest% geq 5 set /a last+=1
  18. if "%n%"=="0" echo The result is: %last% & pause & goto exit
  19. if "%n%"=="1" set /a t=%x%/%y% && echo The result is: !t!.%last% & pause & goto exit
  20. set /a result=%x%*%bjsh%/100/%y%
  21. set /a end=%n%-1
  22. call echo The result is: %%result:~0,-%end%%%.%%result:~-%end%%%%%last%% && pause
  23. :exit
BJSH posted on: 2007-04-01 14:43


Brother flyinspace's words I need to think carefully

[ Last edited by bjsh on 2007-4-1 at 02:53 PM ]
Floor 17 Posted 2007-04-02 03:49 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Let's change the algorithm..

1. Get the result of dividend / divisor.
2. Get the remainder of dividend / divisor.
3. The dividend minus the remainder is the actual integer divisor.
3. If the remainder is 0, then the number is divisible. Prompt for divisibility, and then output several 0s.
4. If not 0, then actual integer divisor / divisor is the value of the actual integer part (this value must be accurate).
5. Get the number of digits of the divisor. Suppose it is 2 digits. The output decimal point is 7 digits. (Neither value can be greater than 10)
6. Use a loop to multiply the remainder by 10 (10 to the 9th power).
7. The result of 6 divided by the divisor (then use the rounding algorithm) (give a prompt) to output the decimal part.
8. Output the result: %integer part%.%decimal part%

In this way, the precision is also satisfied. Your requirement is also met.

How do you think, bjsh?
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bjsh +2 2007-04-02 05:47
知,不觉多。不知,乃求知
Floor 18 Posted 2007-04-02 04:06 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Brother flyinspace indeed gave me a reminder;

The system can only accurately calculate the ninth digit exactly;

That is to say, the maximum accurate value is eight digits;

So my previous algorithm is absolutely no problem for smaller numbers in terms of accuracy;

For example, 18/7

But for large numbers like 100000/7, the accuracy is smaller than the former; it is also related to the system's maximum ability to calculate up to the ninth digit;

So in this regard, I adopted Brother flyinspace's suggestion;

First find the remainder; convert the large number into a small number; then use the remainder to continue dividing
Floor 19 Posted 2007-04-02 04:10 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Hehe, if you think it's good, give me more points : )

Am I quite cheeky
知,不觉多。不知,乃求知
Floor 20 Posted 2007-04-02 04:38 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
I need to correct my previous statement.

System

The digital precision limit is 32 bits.

So the ninth digit is not guaranteed to be calculated;

For example, set /a p=4000000000/7
C:\>set /a p=4000000000/7
-42138185

So the eighth digit is the one calculated precisely;

Therefore, the final precision should be 7 digits
Floor 21 Posted 2007-04-02 04:49 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
It's just a problem with a long int. The precision is right here.

set /a p=400000000 % 7
set /a a=400000000 - % p%
set /a integer_part=(400000000 - %a%) / 7
The above must be divisible. Why is it not accurate for 9 digits...
知,不觉多。不知,乃求知
Floor 22 Posted 2007-04-02 04:53 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
New code with precision 7



  1. @echo off &SetLocal EnableDelayedExpansion
  2. set /p x=Please enter the dividend:
  3. set /p y=Please enter the divisor:
  4. :n
  5. set /p n=Please enter the number of decimal places to retain:
  6. if %n% gtr 7 echo The maximum precision is 7 digits, please re-enter && goto n
  7. set /a count=0
  8. set /a bjsh=10
  9. set /a system_result=%x%/%y%
  10. set /a system_rest=%x%%%%y%
  11. set x=%system_rest%
  12. :loop
  13. if %count% geq %n% goto start
  14. set /a bjsh*=10
  15. set /a count+=1
  16. goto loop
  17. :start
  18. set /a rest=%x%*%bjsh%/%y%-%x%*%bjsh%/10/%y%*10
  19. if "%n%"=="0" (set /a last=%rest%) else (
  20. set /a last=%x%*%bjsh%/10/%y%-%x%*%bjsh%/100/%y%*10
  21. )
  22. if "%n%"=="0" if %rest% geq 5 set /a last=%system_result%+1 && echo Result is: !last! & pause & goto exit
  23. if %rest% geq 5 set /a last+=1
  24. if "%n%"=="1" echo Result is: %system_result%.%last% & pause & goto exit
  25. set /a result=%x%*%bjsh%/100/%y%
  26. set /a end=%n%-1
  27. call echo Result is: %%system_result%%.%%result:~-%end%%%%%last%% && pause
  28. :exit
Posted by BJSH on: 2007-04-01 15:44
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +2 2007-04-02 04:55
Floor 23 Posted 2007-04-02 04:55 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Brother flyinspace,What you said makes sense!Through cyclic remainder;It may be of arbitrary precision
Floor 24 Posted 2007-04-02 05:46 ·  IANA 局域网IP(Private-Use)
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Final code;
Precise to any digit



  1. @echo off
  2. set /p x=Please enter the dividend:
  3. set /p y=Please enter the divisor:
  4. set /p n=Please enter the number of decimal places to retain:
  5. set /a end=%n%-1
  6. set /a count=0
  7. set /a system_result=%x%/%y%
  8. set /a rest=%x%%%%y%
  9. set result=%system_result%.
  10. :loop
  11. set /a rest*=10
  12. set /a last=%rest%/%y%
  13. set /a count+=1
  14. if %count% geq %n% set /a rest=%rest%%%%y%*10/%y% && goto jump_loop
  15. set result=%result%%last%
  16. set /a rest=%rest%%%%y%
  17. goto loop
  18. :jump_loop
  19. if %n%==0 if %last% geq 5 set /a result+=1 && echo The result is: !result!&& goto exit
  20. if %rest% geq 5 set /a last+=1
  21. echo %result%%last% & pause
BJSH posted on: 2007-04-01 18:28


[ Last edited by bjsh on 2007-4-1 at 06:40 PM ]
Floor 25 Posted 2007-04-02 05:49 ·  IANA 局域网IP(Private-Use)
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
The idea of this code is attributed to flyinspace. I originally wanted to give him ten points; unfortunately, the forum restricts it to only two points..

May all those who have read it fulfill my wish for me
Floor 26 Posted 2007-04-02 07:45 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
The previous ones broke the lower limit;
The following one after modification, which cannot calculate higher than 10^9 times, broke both the upper and lower limits;
Can calculate any number of digits; and can be precise to any number of digits; (it seems that for something too large like 1 million digits, it will prompt that the output line is too long)


  1. @echo off & setlocal enabledelayedexpansion
  2. set /p x=Please enter the dividend:
  3. set /p y=Please enter the divisor:
  4. if %y% gtr 2000000000 echo The divisor is too large && goto exit
  5. set /p n=Please enter the number of decimal places to keep:
  6. if %x% gtr 2000000000 goto big
  7. :start
  8. set /a count=0
  9. set /a system_result=%x%/%y%
  10. set /a rest=%x%%%%y%
  11. set result=%system_result%.
  12. :loop
  13. set /a rest*=10
  14. set /a last=%rest%/%y%
  15. set /a count+=1
  16. if %count% geq %n% set /a rest=%rest%%%%y%*10/%y% && goto jump_loop
  17. set result=%result%%last%
  18. set /a rest=%rest%%%%y%
  19. goto loop
  20. :jump_loop
  21. if %n%==0 if %last% geq 5 set /a result+=1 && echo The result is: !result!&& goto exit
  22. if %rest% geq 5 set /a last+=1
  23. echo The result is: %result%%last% && goto exit
  24. :big
  25. set /a l=0
  26. set result=
  27. :big_loop
  28. set p=%x:~0,1%
  29. set /a m=%l%*10+%p%
  30. set /a s=%m%/%y%
  31. set result=%result%%s%
  32. if "%result%"=="0" set result=
  33. set /a l=%m%%%%y%
  34. set x=%x:~1%
  35. if not defined x goto big_end
  36. goto big_loop
  37. :big_end
  38. set result=%result%.
  39. set /a count=0
  40. set rest=%l%
  41. goto loop
  42. :exit
  43. pause
BJSH posted on: 2007-04-01 18:56


[ Last edited by bjsh on 2007-11-22 at 10:30 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
nanhui112 +2 2008-01-28 16:45
Floor 27 Posted 2007-04-02 08:01 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Unable to solve the problem where the divisor is greater than 10^9

[ Last edited by bjsh on 2007-4-1 at 08:53 PM ]
Floor 28 Posted 2007-04-02 09:26 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by bjsh at 2007-4-1 07:01 PM:
Unable to solve the problem where the dividend is greater than 10^9

In dividend / divisor = result.

The dividend can be greater than 10^9.
But the divisor cannot.

set dividend=9999999999999999999999999999999
Assume this 9 has 20 digits. Of course, the length can be arbitrary.

Use set new=%dividend:~0,9% to take out 9 digits.

Of course, at the application level, we will use a for loop to solve the problem of continuous values.

Here, only one situation is explained. You can handle other situations by yourself :)

First, judge whether new is greater than the divisor
Yes --》If it is greater, start doing the remainder operation...

Assume the divisor is 111111110.

Then, after obtaining the precise value according to the above method...

Obtain the number of digits of the remainder.

In this example, it is one digit... and the value is 9.

Then, use set to fill in 8 digits later.

In this way, loop...

Take out all 20 digits of the number.
Then the code for the integer part is done..
You can also handle the decimal part by yourself..
知,不觉多。不知,乃求知
Floor 29 Posted 2007-04-02 09:49 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
I made a mistake above;

It's impossible to solve the problem where the divisor is greater than 10^9;

As for the dividend, the idea used in my last code is the same as flyinspace's;

That code is applicable to any number of digits of the dividend

[ Last edited by bjsh on 2007-4-1 at 08:51 PM ]
Floor 30 Posted 2008-01-28 16:28 ·  中国 江西 九江 电信
初级用户
★★
Credits 127
Posts 59
Joined 2007-11-13 00:41
18-year member
UID 102491
Gender Female
Status Offline
;)Large number. I use cyclic interception to get numbers, but there are still problems. For example, 123456789123456. First take the first nine digits for calculation, then add zeros at the end, and then add the results. There is a problem with adding very large numbers, and the result is not very correct. There is a decimal problem. When magnified, there is a big difference in numbers. It seems that cmd is not suitable for calculation. I can only do this much~~~
----The previous ones are all experts. I will study hard and work harder: )

[ Last edited by nanhui112 on 2008-1-28 at 04:43 PM ]
Forum Jump: