Batch, Plugin, Programming, Uncategorized

Evaluate complex math expressions in Batch with AC 1.0/1.1

One of the drawbacks of Batch is it’s lack of support for some complex math functions. This plugin, developed by Michael Bukowski can alleviate this restriction for you. While it has two dependencies, it is still very small (under 100kb) and unlocks powerful functions to use in your applications, including floating point and logarithm operations. In this article, we will discuss it’s benefits and use cases.

RELATED VIDEO

Plugin demonstrated and recorded by Karanveer Chouhan (Kvc)

FEATURE COMPARISON – AC 1.0 VS 1.1

AC 1.0 has a smaller feature set than AC 1.1 but is still useful for developers who do not want to utilize some of the more advanced features of this plugin. Both plugins still process floating point numeric values, but AC 1.1 unlocks a variety of math functions.

AC 1.1 TABLE

AC 1.0 TABLE

As you can see, AC 1.1 is the improved version of AC 1.0, but AC 1.0 is still mentioned in this article for compatibility purposes. Both allow you to utilize the above functions using floating point numbers to a high level of precision. AC uses the BC compiler for all of these functions. BC is an arbitrary precision numeric processing language, and this plugin allows batch to interface into it, hence why it is a dependency.

USING AC IN YOUR PROJECTS TO EVALUATE EXPRESSIONS

Let’s demonstrate how AC is effective by using it to calculate and round the value of Pi to three points of precision. Let’s assume we have it downloaded and linked to our path, so we can use it anywhere. There are two modes, specified by the first parameter. C in this case means we are running a calculation with the expression. R means we are rounding the expression (in the below case, to the third decimal point of precision). The below code block demonstrates it’s usage for calculating in this case. case:

@echo off
Echo. Calculating value of 'PI'
Call AC C "355/113" Pi
Echo. Pi = %Pi%
Echo.
Echo. Rounding off 'Pi'
Call AC R "%Pi%" 3 _New_Pi
Echo. _New_Pi = %_New_pi%

As you can probably see, we are actually storing the result in a environment variable! This is one of the few plugins that will automatically store the result in an environment variable so that we are able to use it immediately, as if we had done set /a (the original, limited method of math evaluation)

CALL AC.BAT [MODE] [EXPRESSION] [OUTPUT ENV VARIABLE]

Let’s run that test program we gave above, and we should expect this output ( round(pi, 3) ):

C:\>test.bat
Calculating value of 'PI'
 Pi = "3.1415929......" // did not continue for sake of clarity

 Rounding off 'Pi'
 _New_Pi = 3.142

As you can see, this alone is extremely powerful for operations in CMD, as before floating point values were nearly impossible to implement. We can also utilize mathematical functions such as log_n, sin, cos and radian conversions in the proper form of func(x) right in the expression! I encourage you to try it out in your own project. An expression like: “1+2+5^13+sin(4)+log_n(8)+sqrt(300)” is completely evaluated and returned using this plugin.

CONCLUSION/DOWNLOAD

You can download this plugin on our GitHub page, as well as see basic documentation and the table of functions I gave at the beginning of this article. Michael Bukowski is credited for developing this plugin as well as the creators of the BC and DC language. I hope this article has demonstrated how powerful this plugin can be for CMD applications, as these mathematical operations were once completely blocked off from use in the command-line. Thank you for reading.

One thought on “Evaluate complex math expressions in Batch with AC 1.0/1.1

  1. I would rename the article because it’s not that it allows complex math expressions (with a non-zero imaginary part), but rather calculate advanced math.

Leave a Reply