script with modulus

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
doscode
Posts: 175
Joined: 15 Feb 2012 14:02

script with modulus

#1 Post by doscode » 03 Aug 2012 03:01

In a file, I have series of values like these:
0,093333333
0,011666667
0,35
0,02
0,186666667
0,021666667
0,14
0,013333333
0,163333333
0,02
0,35

(there are periodical decimals)

These values per second. I need to get full value per x seconds.

I tried to do some script for it:

Code: Select all

@echo off
Setlocal EnableDelayedExpansion

for /F "usebackq" %%V in (".\values_per_second.txt") DO (
SET V=%%V
echo !V!

 For /L %%T IN (1,1,7200) DO (
 SET M=!V!%%T
 echo !V!%%T = !M!
 if !M! EQU 0 (
  pause
  break
  )
 )
 
)
pause


But it does not seem to work. Do I have the modulus and condition right? If remains is 0, then the number is fully divisible by the x seconds (without remains).

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: script with modulus

#2 Post by Ed Dyreen » 03 Aug 2012 03:25

'
What values are returned by your debugger ( after the breakpoint ) ?

Where is the modulus ?

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: script with modulus

#3 Post by doscode » 03 Aug 2012 04:29

I thought !V!%%T as modulus.

I find that this is not the way I should go to get my results. I find different approach. You can delete this thread.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: script with modulus

#4 Post by Squashman » 03 Aug 2012 20:57

you need to use SET /a to use the modulus operator and the percent signs need to be doubled up just like FOR variables.

Also remember that set /a cannot do floating point math and any number with a leadng zero will be treated as an octal value.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: script with modulus

#5 Post by trebor68 » 11 Aug 2012 05:34

You will want two integer value for this value with decimal numbers.
value = x / y


I will use the format that we use in Germany.
in Germany format: 1.234,567
One thousand two hundred thirty-four comma five six seven

in English format: 1,234.567
One thousand two hundred thirty-four point five six seven


For the example value you can get y = 600 for solution.

0,aabbbbbbb * 100 = aa,bbbbbbb
with 0 <= aa < 100
and bbbbbbb with 6 different value (0000000 1666667 3333333 5000000 6666667 8333333) for 0 to 5.

x = 6 * aa + [0..5]
value is than x / 600

The last number 7 is round then 6,6666666... = 7

Here my code for the Test27.bat:

Code: Select all

@echo off
Setlocal EnableDelayedExpansion
set t0=000000000
set value2=600
for /F "usebackq" %%V in ("PeriodenZahlen.txt") DO (
SET V=%%V
echo !V!
set t3=!V!%t0%
:: value 0,aabbbbbbb :: with t1 as aa and t2 as bbbbbbb
set /a t1="10!t3:~2,2! %% 1000">nul
set t2=!t3:~4,7!
if !t2!==0000000 set /a value1=!t1! * 6
if !t2!==1666667 set /a value1=!t1! * 6 + 1
if !t2!==3333333 set /a value1=!t1! * 6 + 2
if !t2!==5000000 set /a value1=!t1! * 6 + 3
if !t2!==6666667 set /a value1=!t1! * 6 + 4
if !t2!==8333333 set /a value1=!t1! * 6 + 5
:: echo ## !t1! ## !t2! ### !value1! / %value2%
echo !t3:~0,11! = !value1! / %value2%
call Test27a !value1! %value2%
echo.
)


But you can have value that not will have the smallest value.
The gratest cammon divide number is 12.
12 / 600 = 1 / 50

This will make the batch Test27a.bat:

Code: Select all

@echo off
if #%1#==## (echo Error with the value1 and value2.)&goto :eof
if #%2#==## (echo Error with the value2.)&goto :eof
set v1=%1
set v2=%2
if %v1% lss %v2% (set v3=%v1%) & (set v1=%v2%) & (set v2=!v3!)
:here
:: echo %v1% %v2%
set v3=
set /a v3=v1 - (v2 * (v1 / v2))
:: echo %v3%
if not %v3%==0 (set v1=%v2%) & (set v2=%v3%) & goto :here
:: here the results
set v3=%v2%
set /a v1=%1 / v3
set /a v2=%2 / v3
echo %1 / %2 = %v1% / %v2%


If you want to check with other value as the 600.
Here the mathematic:

for /l %g in (2, 1, %f%) do
for /l %h in (1, 1, !g!) do

you will check with this value
1/2
1/3
2/3
1/4
2/4
3/4
...

Here is a example for calculate the digits value with xx numbers.
1.234,000 000 / 6

1234 / 6 = 205
1234 - 6 * 205 = 4

4 * 1000 = 4000
4000 / 6 = 666
4000 - 6 * 666 = 4

4 * 1000 = 4000
4000 / 6 = 666
4000 - 6 * 666 = 4

The last number is NOT rounded.

The solution with 6 digitals not rounded is:
1.234 / 6 = 205,666 666

If you want rounded after 3 digits numbers.
666 + 500 = 1166
1166 / 1000 = 1
205,666 + 0,001 = 205,667

I will not write this batch in this time.

Post Reply