![]() |
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:54 |
47,816 topics / 349,916 posts / today 0 new / 48,265 members |
| DOS批处理 & 脚本技术(批处理室) » [Joint Participation] [Challenge Ideas] [Batch Processing Floating-Point Operations] |
| Printable Version 44,245 / 136 |
| Floor1 redtek | Posted 2006-10-04 04:10 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
) This post is meant to throw out a brick to attract jade; everyone expand your thinking and interactively take part in learning batch files together~ :)
) [Using DOS internal commands to calculate the sum of two floating-point numbers] Example: Sum.Bat 12.99 438.76 Result: 451.75 Rules: Negative numbers are not considered Possible errors from deliberately or accidentally entering non-numeric characters are not considered for now Only DOS internal commands may be used (no DOS version restriction); external commands are forbidden. Only consider adding the two floating-point numbers appearing in %1 and %2; adding more floating-point numbers is not considered for now. Allowed number formats: 0.91 0.10 2.12 1092.90 Formats not allowed for now: .91 .9 (Decimal precision: two digits after the decimal point) Purpose: Broaden thinking, increase interest in batch files, increase everyone’s interactive learning and hands-on playing, through our continuous interaction, let everyone participate and progress together in the use of batch files :) Direction: This is only one item; more content needs to be discovered together by netizens, with moderator support :) Everyone can use any wild idea whatsoever to implement the problem above~ :) Why this post exists: Only with interaction and everyone participating can one’s own level improve faster. Turn the way of asking only when encountering problems and not actively learning batch files normally into everyone actively improving together step by step :) Reward: For excellent solutions and solutions with different lines of thought, the moderators will give godlike guidance and demonstrations, then give everyone points as encouragement~ :) Other: Waiting for everyone to have better ideas and very practical topics for interactive learning and application, with everyone participating together :) =================================== |
|
| Floor2 electronixtar | Posted 2006-10-04 04:20 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
| Floor3 redtek | Posted 2006-10-04 04:31 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
Originally posted by electronixtar at 2006-10-4 04:20: Wonderful~~ Added 6 points~ :) |
|
| Floor4 pengfei | Posted 2006-10-04 04:47 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
The code in the first section of this post is the latest modification. It is more powerful; everyone is welcome to test it...
The section below is also general-purpose, but the decimal part can only calculate 8 digits, and it does not have the function of judging erroneous input or judging whether the entered value meets the requirements. Related problem discussion: About the radix problem during numerical calculations! [ Last edited by pengfei on 2006-10-9 at 05:01 ] |
|
| Floor5 redtek | Posted 2006-10-04 05:12 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Addictive!!! Added 6 points for brother pengfei~ :)
|
|
| Floor6 redtek | Posted 2006-10-04 05:18 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
The works by the two experts are very wonderful~~
Personally I feel that to learn batch files and DOS batch files well, as well as the application of scripts, is actually equivalent to walking step by step toward the road of programming :) Or developers use a kind of program thinking and a way of understanding data structures to bring batch files to a higher level :) This is one of the reasons why many netizens, after learning DOS batch file applications, still cannot improve any further after many years :) But some experts really are moving upward every day and every step, skills are no longer important; the data structures applied can solve many problems :) Whether in batch files or in code development, they are all connected :) [ Last edited by redtek on 2006-10-4 at 05:21 ] |
|
| Floor7 pengfei | Posted 2006-10-04 10:15 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Finally solved the limitation on decimal digit count; it can calculate various types of values, and the number of decimal digits is not restricted. The problem of erroneous input has not been solved for now.
The code is updated on the 4th floor! |
|
| Floor8 namejm | Posted 2006-10-04 11:18 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Posting a section of code. In the rush I did not have time to do error-checking on the data or optimize the code; it can adapt to cases where the number of decimal digits is inconsistent. The basic idea is to calculate the integer part and decimal part of the value separately:
[ Last edited by namejm on 2006-10-4 at 11:20 ] |
|
| Floor9 electronixtar | Posted 2006-10-04 20:17 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
Scripts are still simpler ^_^
|
|
| Floor10 pengfei | Posted 2006-10-04 23:50 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Solved the problem where the first digit after the decimal point is 0 and an 8 or 9 appears after it, causing the value to be an invalid number.
A brand-new algorithm; the rough idea is: read the decimal digit count and pad zeros where insufficient. After removing the . sign, calculate the decimal and integer parts together, and after the calculation is complete, accurately insert the decimal point into the corresponding place according to the recorded decimal point position, thereby completing the floating-point value calculation. This section of code has better generality; time was tight and I did not have time to optimize it: [ Last edited by pengfei on 2006-10-6 at 03:51 ] |
|
| Floor11 namejm | Posted 2006-10-05 00:44 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
The code by pengfei on the “10th floor” first pads the floating-point numbers with 0 to make the digit counts equal, then removes the decimal point, processes the new number, and then restores the decimal point position. This idea is quite novel and goes one step beyond my idea on the “8th floor”. After optimization, the code will be more concise than my section. It seems that lots of discussion really can easily spark flashes of wisdom ^_^
|
|
| Floor12 pengfei | Posted 2006-10-05 01:43 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
^_^ Hehe~ Brother namejm’s code idea on the 8th floor is the same as mine on the 4th floor. If the first decimal digit is zero and followed by 8 or 9, an error will occur; it is still a radix problem, while the code on the 10th floor will not.
|
|
| Floor13 不得不爱 | Posted 2006-10-05 04:44 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
I tried it; if the first digit is 0, any 8 or 9 after it will cause an error! Even the 10th floor’s is the same!
|
|
| Floor14 pengfei | Posted 2006-10-05 04:49 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Moderator, you are adding zero to the highest digit of the integer, right!
Try adding zero to the first digit after the decimal point, then compare the calculation results of the two code sections. |
|
| Floor15 不得不爱 | Posted 2006-10-05 05:09 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
The one below automatically removes 0 when the first digit is 0, automatically adds 0 when there is no digit before the decimal point, and automatically removes 0 when there is 0 at the end of the decimal part!
[ Last edited by qwe1234567 on 2006-10-6 at 07:40 ] |
|
| 1 2 3 4 … 10 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |