Batch Calcs.. Not the same as usual - batch-file

I'm trying to make a desktop binary calculator and can't seem to get values to calculate properly.
http://pastebin.com/embed_js.php?i=mtsJRys8
:: functions for 128
if userValue GEQ 128 (
set bin128 = 1
goto 64
)
if not userValue GEQ 128 (
set bin128 = 0
)
Trying to get the Variable 'bin128' to end with a value 1 or 0

Decimal to binary converter?
I see in your paste you're computing the binary result by comparing the entered number with powers of two. There's a more efficient algorithm for converting dec to bin, and it'll handle numbers from 0 to 2147483647.
#echo off
setlocal
if "%~1"=="" (
echo usage: %~nx0 integer
goto :EOF
)
set /a dec = %~1
:/2
set /a mod = dec %% 2, dec /= 2
set "bin=%mod%%bin%"
if %dec% gtr 0 goto :/2
echo %bin%
I also see paulsm4's comment recommending another (suspicious quote) "real" language. Truthfully, though, not counting languages that already have a direct int-to-bin conversion method, the batch scripting language can actually save you a few steps because all math is integer math, with decimals inherently truncated. This saves you from having to Math.floor or similar on every iteration. This is one situation where batch is better-suited for the task than many other languages.
If you prefer to do the power-of-two comparison method, here's another solution. It loops 31 times regardless of the length of the numeral you supply, but for /L is very efficient. This may actually be faster than the method above for larger numbers for many iterations. Not sure.
#echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
echo usage: %~nx0 integer
goto :EOF
)
set /a dec = %~1, pow = 1073741824
for /L %%I in (1,1,31) do (
if !dec! geq !pow! (
set "bin=!bin!1"
set /a dec -= pow
) else (
if defined bin set "bin=!bin!0"
)
set /a pow /= 2
)
if not defined bin set bin=0
echo %bin%
And just because I felt like it, here's the same thing but using bitwise operations. This is the most efficient method. (Edit: or at least it was, until Aacini posted his solution.)
#echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
echo usage: %~nx0 integer
goto :EOF
)
for /L %%I in (0,1,30) do (
set /a "mask = 1 << %%I, bit = ^!^!(%~1 & mask)"
if !mask! gtr %~1 goto break
set "bin=!bit!!bin!"
)
:break
if not defined bin set bin=0
echo %bin%
Efficiency:
I ran a series of tests to determine which method is most efficient. For each method, I sent the output to NUL, looped the script for 1000 iterations, then took an average of the run times over 3 runnings. I did this first with a small value of 5, then a large value of 2147483646. Results:
goto method, input=small: 7.37 seconds
goto method, input=large: 33.77 seconds
powers-of-two method, input=small: 8.38 seconds
powers-of-two method, input=large: 12.44 seconds
bitwise method, input=small: 6.35 seconds
bitwise method, input=large: 11.42 seconds
These results are not surprising, as for /L is generally faster than a goto loop, and bitwise operations occur faster than integer math.

Just to complete rojo's answer, I think that this is the most efficient method to convert a decimal number to binary:
#echo off
setlocal EnableDelayedExpansion
if "%~1"=="" (
echo usage: %~nx0 integer
goto :EOF
)
set "decimal=%~1"
set "binary="
for /L %%i in (1,1,32) do (
set /A "bit=decimal&1, decimal>>=1"
set "binary=!bit!!binary!"
if !decimal! equ 0 goto break
)
:break
echo %binary%
Note that previous method correctly convert negative numbers!
At this post there is a Batch file that can convert very large decimal numbers to binary. The result is stored in a Batch variable, so the binary number may have a maximum of 8 K digits. This means that the maximum decimal number that can be converted is 2^8192 - 1.

Related

Basic BAT calculator command

I am trying to do a very basic 2 value input BAT file but I am struggling. I used a free template from Wikihow and re-adjusted but something is not right.
I want to calculate 100/(A/B+1) and I know for a fact that the Batch calculation ignores the "+1" for some reason and basically just calculates "100/(A/B). Please help. Thanks.
TITLE Calculator
ECHO OFF
CLS
:TOP
color 3f
Cls
:SUM
CLS
ECHO ---------------------------------------------------
ECHO[
ECHO Division
ECHO[
ECHO ---------------------------------------------------
ECHO[
set /p A=" Enter First Amount = "
ECHO[
set /p B=" Enter Second Amount = "
SET /A C=100/(A/B+1)
ECHO[
ECHO ---------------------------------------------------
ECHO Result %C%
PAUSE
GOTO:TOP
I suspect this is an order of operations issue.
Remember that plus comes after division.
You might need this instead:
SET /A C=100/(A/(B+1))
This will allow the B+1 to come before the division.
SET /A doesn't ignore the +1:
E.g. SET /A 100/(10/2+1) outputs 16; the problem you most likely have is this:
Any SET /A calculation that returns a fractional result will be
rounded down to the nearest whole integer.
(Source)
However, it is possible to work around this issue:
SETLOCAL EnableDelayedExpansion
SET /A "var=100000/(10/2+1)"
SET "fraction=%var:~-3%" & SET /A "var/=1000"
IF !fraction:~-1! GEQ 5 SET /A "fraction+=10"
SET "fraction=!fraction:~0,-1!"
ECHO %var%.%fraction%
Basically you multiply the number to be divided by 10^(n) and - after you set the fraction (%var:~-n%) - divide it by 10^(n) again. Then you check the last digit of the fraction for proper rounding and remove it (read more).
I am assuming you were looking for:
SET /A C=(A/B)+1
SET /A C=100/(C)
This the correct formula, but it will not return the correct results for fractioned numbers.
So if A = 10 and B = 2
10 / 2 = 5 +1 =6
100/6 = 16,6 but batch rounds off to 16.
Then A = 453 and B = 178
453 / 178 = 2,544943820224719
2,544943820224719 + 1 = 3,544943820224719 but Batch rounds off to 3
100/3 = 33
So if you use numbers that will cause fractions, you MUST use a solution that include fractions.

Update running unsigned long long sum in bat file

Is there a simple way to sum two numbers potentially >= 2*31 in a .BAT file?
I have a running sum, and argument %1 that is the name of an existing file.
set sum=4123456789
set fsize=%~z1
I'd like to add fsize to sum. Unfortunately fsize (and sum) can be as tiny as zero or 10's of gigabytes (%~z1 accurately reports >= 2*31 file sizes).
I know a program could do it, and I'll go that route if necessary, but I'd prefer to do it with a few added lines of .BAT logic.
I think the easiest way is to split the summands into two parts – integer and fractional Gigas (multiples of 1000000000), add the respective parts individually, then recombine them. See the following example script, which contains a lot of explanatory remarks:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Initialise variables:
set /A "GIGASUM=0, FRACSUM=0"
:LOOP
rem // Get command line argument:
shift
set "NUM=%~0"
if not defined NUM goto :NEXT
rem // Check number for validity:
(for /F "delims=0123456789" %%N in ("%NUM%") do rem/) && (
echo ERROR: non-numeric characters encountered!
exit /B 1
)
rem // Split number into two parts, integer and fractional Gigas:
set "NUM=000000000%NUM%"
set "GIGA=%NUM:~,-9%" & set "FRAC=%NUM:~-9%"
rem // Remove leading zeros from integer Gigas:
for /F "tokens=* delims=0" %%N in ("%GIGA%") do set "GIGA=%%N"
rem // Sum up fractional Gigas and determine carry:
set /A "FRACSUM+=(1%FRAC%-1000000000)"
set "CARRY=%FRACSUM:~,-9%" & set "FRACSUM=000000000%FRACSUM%"
set "FRACSUM=%FRACSUM:~-9%"
rem // Sum up integer Gigas and regard carry:
set /A "GIGASUM+=GIGA+CARRY"
rem // Loop back to next summand:
goto :LOOP
:NEXT
rem // Remove leading zeros:
for /F "tokens=* delims=0" %%N in ("%GIGASUM%%FRACSUM%") do set "SUM=%%N"
if not defined SUM set "SUM=0"
rem // Return resulting sum:
echo %SUM%
endlocal
exit /B
The greatest possible sum amounts to 231 * 109 – 1 = 2147483647999999999, an overflow is not detected.
The required logic is not so complicated. Here it is one version:
#echo off
setlocal
:loop
set /p "pair=Enter two numbers separated by plus sign: "
if errorlevel 1 goto :EOF
for /F "tokens=1,2 delims=+" %%a in ("%pair%") do set "num1=%%a" & set "num2=%%b"
set "sum="
set "carry=0"
:nextDigit
set /A sum1=%num1:~-1%+%num2:~-1%+carry
set "sum=%sum1:~-1%%sum%"
set /A carry=sum1/10
set "num1=%num1:~0,-1%"
if not defined num1 set "num1=0"
set "num2=%num2:~0,-1%"
if not defined num2 set "num2=0"
if "%carry%%num1%%num2%" neq "000" goto nextDigit
echo The sum is: %sum%
goto loop

How to count the ones in a binary representation of a given hex in a cmd Batch?

i am trying to write a batch file that asks the user to type a hexadcimal number and then counts the number of ones in the binary representation , like if i typed A the program echos 1 .
i have this code that uses a look up table to convert from binary to hex and answered by Aacini here ...
#echo off
setlocal
set "bin=110111101010110110111110111011111100101011111110"
call :bin2hex hex=%bin%
echo hex: %hex%
goto :EOF
:bin2hex hexVar=binValue
setlocal EnableDelayedExpansion
for %%a in (0000-0;0001-1;0010-2;0011-3;0100-4;0101-5;0110-6;0111-7;1000-8;1001-9;1010-A;1011-B;1100-C;1101-D;1110-E;1111-F) do (
for /F "tokens=1,2 delims=-" %%b in ("%%a") do (
set "hextable[%%b]=%%c"
)
)
set "hex="
set "bin=000%~2"
:bin2hexloop
set "hex=!hextable[%bin:~-4%]!%hex%"
set "bin=%bin:~0,-4%"
if defined bin if "%bin:~3%" neq "" goto bin2hexloop
endlocal & set "%~1=%hex%"
goto :EOF
i tried to inverse the way this code works , but it didn't work !
here is my attempt
#echo off
setlocal
set "hex=ABCDEF"
call :hex2bin bin=%bin%
echo : %bin%
pause;
goto :EOF
:hex2bin binVar=hexValue
setlocal EnableDelayedExpansion
for %%a in (0-0000;1-0001;2-0010;3-0011;4-0100;5-0101;6-0110;7-0111;8-1000;9-1001;A-1010;B-1011;C-1100;D-1101;E-1110;F-1111) do (
for /F "tokens=1,2 delims=-" %%b in ("%%a") do (
set "bintable[%%b]=%%c"
)
)
set "bin="
set "hex=000%~16"
:hex2binloop
set "bin=!bintable[%hex:~-4%]!%bin%"
set "hex=%hex:~0,-4%"
if defined hex if "%hex:~3%" neq "" goto hex2binloop
endlocal & set "%~4=%bin%"
goto :EOF
anybody can help me ?
If I understood you correctly, you want not to "convert a hexadecimal number to binary", but to "count the ones that each hex digit have" and accumulate they (for example, for A the number is 1). This way, the solution must work with "The number of ones each hex digit have".
#echo off
setlocal
set "hex=ABCDEF"
call :countOnesInHex ones=%hex%
echo There are %ones% ones in %hex%
pause
goto :EOF
:countOnesInHex ones=hexValue
setlocal
for %%a in (0-0;1-1;2-1;3-2;4-1;5-2;6-2;7-3;8-1;9-2;A-2;B-3;C-2;D-3;E-3;F-4) do (
for /F "tokens=1,2 delims=-" %%b in ("%%a") do (
set "onesIn[%%b]=%%c"
)
)
set ones=0
set "hex=%~2"
:hexCountLoop
set /A ones+=onesIn[%hex:~0,1%]
set "hex=%hex:~1%"
if defined hex goto hexCountLoop
endlocal & set "%1=%ones%"
exit /B
Convert the number to decimal and do the basic math: divide by 2 and sum the remainders.
Do it in chunks of 7 hexadecimal digits because batch file calculations support only 31 bits (2^31-1 or 2147483647 or 0x7FFFFFFF).
set hex=ABCDEFABCDEFABCDEF
set ones=0
:loopchunks
set /a decimal=0x%hex:~0,7%
set hex=%hex:~7%
:loopdigits
set /a ones+=decimal %% 2, decimal/=2
if not %decimal%==0 goto loopdigits
if defined hex goto loopchunks
echo %ones%
Output:
51
#ECHO OFF
SETLOCAL
FOR %%a IN (F37ABD abcdef 123 321 99 100 f11f 0 cafe) DO CALL :mainproc %%a
GOTO :eof
:mainproc
SET hexnum=%1
SET /a count1=0
:loop
SET /a hex1=0x%hexnum:~0,1%
:bitloop
SET /a count1+=%hex1% %% 2
SET /a hex1/=2
IF %hex1% gtr 0 GOTO bitloop
SET hexnum=%hexnum:~1%
IF DEFINED hexnum GOTO loop
ECHO %count1% 1s detected IN %1
GOTO :EOF
The for loop simply assigns the values in the list to the variable %%a in turn and executes the main part of the procedure with a parameter (%1) of that item.
Within the main procedure, initialise hexnum as the number to be analysed and count1 with the accumulated number of 1s
Then set hex1 to 0xstrung before (copy the first digit of hexnum) which will be a hex numeric, 0x0 to 0xf. SInce this is the format for cmd to accept a hex number, it sets hex1 to decimal 0..15
next add (hex1 mod 2) to count, that is 1 or zero if odd/even
next halve hex1. Since cmd calculates in integer mode, the result is truncated, hence 6=>3 and 7+>3
the result is >0, do the next binary digit. repeat until 0.
Toss out the first character of hexnum (assign the substring starting at character 1, given that it starts counting at "character 0")
If hexnum has characters left, repeat for the next hex digit
otherwise, report.
result:
17 1s detected IN F37ABD
17 1s detected IN abcdef
4 1s detected IN 123
4 1s detected IN 321
4 1s detected IN 99
1 1s detected IN 100
10 1s detected IN f11f
0 1s detected IN 0
11 1s detected IN cafe

Batch: count the number of digits in a variable

I want to find a way to know the number of digits in variable. For now, I'm trying to use this code. In this example, %var% is the variable that I need to know the number of digits it has.
set x=1
set var=12345
:LOOP
set temp=%var:~0,%x%%
if %temp%==%var% goto END
set x=%x%+1
goto LOOP
:END
Theoretically, at the end of the code %x% would be the number of digits %var% has. However, it doesn't work. I found out the problem is at the 3rd line. I modified the code to diagnose:
set x=1
set var=12345
:LOOP
set temp=%var:~0,%x%%
echo %temp%
pause
if %temp%==%var% goto END
set x=%x%+1
goto LOOP
:END
The result echoed was:
x%%
Can anyone pinpoint my mistake or give an alternative solution to determine the number of digits in a variable?
Here's a short solution, that only works for numeric variables:
set /a Log=1%var:~1%-%var:~1% -0
set /a Len=%Log:0=+1%
The variable %Len% will contain the number of digits in %var%.
Explanation
The basic idea is to convert the first digit to 1, and the rest of them (the 'trailing' digits) to 0's. Then we can use the string replacement function to replace all the 0's with +1 giving us 1+1+1.... and evaluate the string as an arithmetic expression. This will give us the total number of digits.
The 'trailing' digits can be gotten using %var:~1% and we convert them to 0 by subtracting them from the variable itself: 45678 - 5678 gives 40000 etc. However, the above code subtracts them from 1%var:~1% instead, in order to replace the first digit with 1 (i.e. 1 followed by the 'trailing' digits).
The reason for the extra -0 is in case %var% only has one digit, for example 7. In that case, the expression 1%var:~1%-%var:~1% would evaluate to 1- and the shell would complain: Missing operand. The -0 ensures that we always have a valid expression.
Now that we've converted the variable in to the proper form into %Log%, we can replace every occurrence of 0 with +1using %Log:0=+1% and evaluate the resulting expression using set /a, giving us our final result.
The main problem with your code is
set temp=%var:~0,%x%%
This does not work. The parser is not able to properly determine what percent sign belongs to what variable. You can enable delayed expansion and write it as
set "temp=!var:~0,%x%!"
For alternative versions, to handle any length string, any of the posted answers will work.
For a simpler solution, if you are sure the string is under 10 characters, then this is an alternative
set "x=0123456789%var%"
set "x=%x:~-10,1%"
As there is no build in function for string length, you can write your own function.
#echo off
setlocal
set "myString=abcdef!%%^^()^!"
call :strlen result myString
echo %result%
goto :eof
:strlen <resultVar> <stringVar>
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)
This function needs always 13 loops, instead of a simple strlen function which needs strlen-loops.
It handles all characters.
Source: How do you get the string length in a batch file?
Your are trying to do this loop :
#Echo Off
Set /P VrStr=Enter your string :
:Loop
If "%VrStr%" EQU "" Goto EndLoop
Set VrStr=%VrStr:~0,-1%
Set /A VrLgr+=1
Goto Loop
:EndLoop
Echo Number of char: %VrLgr%
Pause
You can use this to :
#echo off
setlocal EnableDelayedExpansion
Set /P $Tstring=Enter your string:
for /l %%a in (0,1,9000) do (set $t=!$Tstring:~%%a,1!&if not defined $t (echo [NB OF CHAR =] %%a&pause&exit /b))
pause

Floating point division in a batch file

I need to do a floating-point division in a dos batch.
I didn't find a way to do it. Something like this :
SET /A Res=10/3
returns a integer number.
Is it possible to do it ?
I know this is a very old topic, but I can't found a simple Batch method in all previous answers, so I post here a pure Batch solution that is very simple to use.
Perform operations using fixed point arithmetic in Batch is simple. "Fixed point" means that you must set a number of decimals in advance and keep it throughout the operations. Add and subtract operations between two Fixed Point numbers are performed directly. Multiply and division operations requires an auxiliary variable, that we can call "one", with the value of 1 with the right number of decimals (as "0" digits). After multiply, divide the product by "one"; before division, multiply the dividend by "one". Here it is:
#echo off
setlocal EnableDelayedExpansion
set decimals=2
set /A one=1, decimalsP1=decimals+1
for /L %%i in (1,1,%decimals%) do set "one=!one!0"
:getNumber
set /P "numA=Enter a number with %decimals% decimals: "
if "!numA:~-%decimalsP1%,1!" equ "." goto numOK
echo The number must have a point and %decimals% decimals
goto getNumber
:numOK
set numB=2.54
set "fpA=%numA:.=%"
set "fpB=%numB:.=%"
set /A add=fpA+fpB, sub=fpA-fpB, mul=fpA*fpB/one, div=fpA*one/fpB
echo %numA% + %numB% = !add:~0,-%decimals%!.!add:~-%decimals%!
echo %numA% - %numB% = !sub:~0,-%decimals%!.!sub:~-%decimals%!
echo %numA% * %numB% = !mul:~0,-%decimals%!.!mul:~-%decimals%!
echo %numA% / %numB% = !div:~0,-%decimals%!.!div:~-%decimals%!
For example:
Enter a number with 2 decimals: 3.76
3.76 + 2.54 = 6.30
3.76 - 2.54 = 1.22
3.76 * 2.54 = 9.55
3.76 / 2.54 = 1.48
Batch files as such do not support the floating point arithmetic. However, this article suggests a workaround that uses an external script file to do calculations. The script file should use some sort of eval function to evaluate the expression passed as an argument and return the result. Here's a sample VBScript file (eval.vbs) that does this:
WScript.Echo Eval(WScript.Arguments(0))
You can call this external script from your batch file, specify the expression to be evaluated and get the result back. For example:
#echo off
for /f %%n in ('cscript //nologo eval.vbs "10/3"') do (
set res=%%n
)
echo %res%
Of course, you'll get the result as a string, but it's better than nothing anyway, and you can pass the obtained result to the eval script as part of another expression.
According to this reference, there is no floating point type in DOS batch language:
Although variables do exist in the DOS batch programming language, they are extremely limited. There are no integer, pointer or floating point variable types, only strings.
I think what you are trying to do will be impossible without implementing your own division scheme to calculate the remainder explicitly.
I recently came across this batch file to compute an approximation of Pi.
There is a DivideByInteger label that might be useful to you: Stupid-Coding-Tricks-A-Batch-of-Pi
It uses a set of MaxQuadIndex variables, each containing a four-digit number (quadruple), in order to store the entire result. The code allows division by an integer between 1 and 10000, inclusive.
:DivideByInteger
if defined PiDebug echo.DivideByInteger %1 %2
set /a DBI_Carry = 0
for /L %%i in (!MaxQuadIndex!, -1, 0) do (
set /a DBI_Digit = DBI_Carry*10000 + %1_%%i
set /a DBI_Carry = DBI_Digit %% %2
set /a %1_%%i = DBI_Digit / %2
)
goto :EOF
A Print label is also available…
try this
SETLOCAL EnableExtensions EnableDelayedExpansion
call :calc_ 1 (99-(100*5/100^)^)
echo !calc_v!
goto :EOF
:calc_
set scale_=1
set calc_v=
for /l %%i in (1,1,%1) do set /a scale_*=10
set /a "calc_v=!scale_!*%2"
set /a calc_v1=!calc_v!/!scale_!
set /a calc_v2=!calc_v!-!calc_v1!*!scale_!
set calc_v=!calc_v1!.!calc_v2!
goto :EOF
just change
call :calc_ decimalpoint equataion
in the example
decimalpoint is 1
equataion is (99-(100*5/100^)^) ;make sure if you use () that you insert ^ before ) as in ^)
the answer is 94.0
if decimalpoint is 2
and equataion is 22/7 ;π pi
the answer is 3.14
I wrote a pure batch file specifically to do division. It takes the first number you input, and then divides it by the second one, and displays the result with as many decimal points as you specify.
Echo off
cls
if NOT "%3" == "" (
set n1=%1
set n2=%2
set max=%3
goto :begin
)
set counter=2
set n1=1
set n2=1
set ans=
:start
Echo.
Echo. 1 / 2
Echo.
Set /p N1= 1?
set /p N2= 2?
Set /p Max= Out how many Decimal Points?
:begin
set /a TmpAns=%N1%/%N2%
set ans=%TmpAns%.
:: Echo.%ans%.>Answer.txt
<nul set /p "=%Tmpans%."
set /a TmpSub=%N2%*%TmpAns%
set /a N1=%N1%-%TmpSub%
set N1=%N1%0
If NOT "%n1%" == "00" (
if %n1% LSS %N2% (
set N1=%N1%0
set ans=%ans%0
)
) else (
Goto :Finished
)
set count=0
:loop
If "%count%" == "%max%" (
Goto :Finished
)
set /a TmpAns=%N1%/%N2%
set ans=%ans%%TmpAns%
<nul set /p "=%Tmpans%"
set /a TmpSub=%N2%*%TmpAns%
set /a N1=%N1%-%TmpSub%
set N1=%N1%0
If NOT "%n1%" == "00" (
if %n1% LSS %N2% (
set N1=%N1%0
set ans=%ans%0
)
) else (
Goto :Finished
)
set /a count=%count%+1
goto :loop
:finished
cls
Echo.
Echo.
Echo.The Number
Echo.%ans%
Echo.
Echo.
set n1=1
set n2=1
pause
goto :eof
:eof
The answer put into the variable %Ans%. It can also be called with parameters. ("Divide.bat 50 27 5" would give you 50/27 out 5 decimal points.)
Since nowadays PowerShell is present on almost all machines, I would let PowerShell do the math and return the result to the batch.
Example:
set divident=10
set divisor=3
for /f "delims=" %%a in ('powershell -Command %divident%/%divisor%') do set result=%%a
#echo %result%
Explanation:
Input variables: Use set variables to define divident and divisor.
Calling powershell and assign result to a batch variable: for /f "delims=" %%a in ('powershell -Command ...) do set result=%%a (you may also check here: How to put a single PowerShell output string into a cmd variable?)
Note the above code will only work with integer input variables.
To support floating point input variables, we need to send the variables as strings inside quotations ("%variable%") and convert the strings within PowerShell back to Double, otherwise batch would interpret the commas as delimiters and PowerShell could not interpret the numbers.
Example:
set divident=10,5
set divisor=3,4
for /f "delims=" %%a in ('powershell -Command [convert]::ToDouble^(\"%divident%\"^)
/[convert]::ToDouble^(\"%divisor%\"^)') do set result=%%a
#echo %result%
Explanation:
Note in PowerShell you would do this like [convert]::ToDouble("10,5")/[convert]::ToDouble("3,5"). However in batch we need to escape the quotes using backslash, and we also need to add a "^" sign before and after the quoted parts: [convert]::ToDouble^("%divident%"^)/[convert]::ToDouble^("%divisor%"^)
If you're running in a command shell on Windows (rather than DOS), you can use VBScript to evaluate complex expressions including floating point math for you.
I have written a small helper library you can call to do this.
EvalBat Library on GitHub

Resources