VPC: Variable precision calculator

This software can be redistributed under GNU Lesser General Public License.
Every source code of this software can be obtained through GitHub.
Copylight (c) 2024 Shigeo Kobayashi. All rights reserved.
Japanese

Windows binary files(32-bit:vpc.exe and bigdecimal.dll) can be downloaded. Only 2 files are necessary. Copy 2 files to any folder,and just run vpc.exe.

To quit the progrmam,just enter quit.

Basics Functions Rounding Rounding methods Iterative calculations Environment settings Input/Outout Special symbols

VPC(Variable precision calculator)is a simple but a programmable calculator. Using Bigdecimal,VPC can offer any number of effective digits you want as far as your computer memories installed allows. Following example computes the ratio of the circumference of a circle to its diameter(π). You can obtain 100 digits in default. But you can specify '$precision=200' explained later to get 200(or more) effective digits.
 c:\Test>vpc
VPC(Variable Precision Calculator V2) of Bigdecimal(V11)
  Copyright (c) 2024 by Shigeo Kobayashi. Allrights reserved.

Enter command
>a = pi();?a
 a =  0.3141592653 5897932384 6264338327 9502884197 1693993751 0582097494 4592307816 4062862089 9862803482 5342117068 E1

>$precision = 200
>b = pi()
>?b
 b =  0.3141592653 5897932384 6264338327 9502884197 1693993751 0582097494 4592307816 4062862089 9862803482 5342117067 9821480865 1328230664 7093844609 5505822317 2535940812 8481117450 2841027019 3852110555 9644622948 9549303819 6E1
>

'a' and 'b' are predefined variables and pi() is the function to compute π(detailed explanations given later). a=pi() means,π is computed by the function pi() and the computation result(π) is stored to the variable a. In default,100 digits of π are computed,then y '$precision = 200' means 200 digits of π are computed. If you specify $precision = 1000,then 1000 digits can be obtained of course. Also,the setting $precision affects arithmetic operations + - * /. ';' is the statement delimiter which enables you to enter more than one statements in a line. To print value of variable,you can use '?' as ?a. '...68E1' means '...68*101'.

Basics

  1. Arithmetics using variables and functions:
    In VPC,26 variables,a,b,c,...,x,y,z, are predefined(no more variable can be created). Arithmetic calculations can be done by using variables and functions(sin,cos,...).
    VPC starts processing when one line is read. One line will be divided by ';' into statements. VPC executes each statement from left to right to the end of line. If any error is found in the statement,then all statements after that statement to the end of line will be skipped.
    Any computation result must be stored to any variable. And any variable's value can be printed by the operator ?,as ?a. Intermediate value of computation can be longer than the digits specified by $precision to keep exact result. The value will be rounded according to the specification of $round when it is stored to any variable.
    Enter command
    >a=pi()/4;b= power(sin(a),2) + power(cos(a),2);?a;?b
     a =  0.7853981633 9744830961 5660845819 8757210492 9234984377 6455243736 1480769541 0157155224 9657008706 3355292669 9554E0
     b =  0.1E1
    
  2. Environment settings:
    Environment settings are set at any time and affect through computations. $precision affects total digits held by any variable,$round affects all rounding operations. ? can also be used to print any environment setting. Any environment setting is kept in environment setting variable starting '$' like $precision and printed as ?$precision. To set environment setting variable's value, '= ' can be used. But only simple substitution is allowed,and can not be used in any equation(calculation).
  3. Value printing:
    Value of a variable or a environment setting can be printed by ?. But,only a variable or a environment setting can be placed after ?. Any equation or computation can not be placed after ?.
    After printing any value, '?' prints new-line, but it prints '; ' if '+' is added at the end instead.
    Example: a=1;b=2;?a+;?b ==> a = 1; b = 2
    Special usage:
    ?* ... Value of all valrables and settings are printed.
    ?$ ... Value of all settings are printed.
  4. Processing unit(statement):
    One line can be divided into statements by ';'. VPC processes a statement at a time. For instance,a = pi();?a, a = pi() is computed first,and the result is printed by ?a next. This is the same as writing 2 lines like
    a = pi()
    ?a
    But writing 'a = pi() ?a' without using ';' will be an error.
  5. Quotation marks( ' or " ):
    Any character string having space or spcial character can be quoted by ( ' or " ). Quoted string is treated as a single token. Numerical value representaion can be long and can be divided by spaces ' ' or by commnas ',' for readability. In such a case it should by quoted by ' or " as '1,000' for example. If it is not quoted as 1,000, VPC recognizes it as 1 and 000 (separated tokens) which is a syntax error. ' can be used for a string having " in it," can be used for a string having ' in it. But any string having both " and ' is not allowed.
    Example:
    a='pi'() is correct, but a=' pi '() is an error('pi' is a function name,but ' pi ' is not).
    a=1000 is correct. a= " - 10.0 1" is regarded as a=-10.01 and is also correct. Spaces(and characters specified by $format) in a numerical expression are ignored.
  6. Iterations:
    repeat or while (repeated to the end of line)can be used.
  7. Input/Output:
    write ".\result.txt" saves all variable values and settings to the file ".\result.txt" which can be restored by read ".\result.txt".
  8. Comment:
    # means the start of comment and any character from # to the end of line will be ignored,

Functions

Every functions bellow(except for iterations()) return digits specified by $precision at least after successful computation.
Function nameDescriptionExampleRemarks
atan(a)computes arctangent of a
c = tan-1(a)
>a=0.5;c=atan(a);?a
a = 0.8414709848 0789650665 ... 5435E0
|a|<=1
sin(a)/cos(a)computes trigonometric function >a=sin(1);?a;b=cos(1);?b;c=a*a+b*b;?c
a = 0.8414709848 0789650665 ... 5435E0
b = 0.5403023058 6813971740 ... 4357E0
c = 0.1E1
the argument value must be radian, not degree, and must be small enough.
If the argument(a) is greater than 3.14...,then you must adjust a as a=a-3.14.. before calling sin(a) or cos(a).
exp(a)computes exponentiation of Napier's number(base of natural logarithm:ea). >a=exp(1);?a
a = 0.2718281828 4590452353 ... 4E1
ln(a)computes natural logarithm(base of Napier's number).
Logea
>a=ln(0.5);?a;b=exp(a);?b
a = -0.6931471805 5994530941 723... 42E0
b = 0.5E0
0 < a <= 2
pi()computes ratio of a circle's circumference(π). >a=pi();?a
a = 0.3141592653 5897932384 ...068 E1
brackets () are necessary.
sqrt(a)computes square root
a=b1/2
>a=sqrt(5);?a
a = 0.2236067977 4997896964 ...275 E1
a >= 0
iterations() returns total number of iteration count just before. >a=sqrt(2);b=iterations();?a;?b
a = 0.1414213562 3730950488 0168872420 ...7E1
b = 0.2E2
iterations() <= $max_iterations
Notes:

Function nameDescriptionExampleRemarks
abs(a) computes absolute value of a. >a=-1;b=abs(a);?b
b = 0.1E1
power(a,n)computes n power of a
b=power(a,n) => b=an
>b=power(2,-2);?b
b = 0.25E0
n must be a positiove or negative integer.
b = power(2,0.5) is an error.
int(a)extracts integer part of a. >a=(1/3)*100;b=int(a);?b
b = 0.33E2
frac(a) extracts fraction part of a. >a=(1/3)*100;b=frac(a);?b
b = 0.3333333333 3333333333 ... 33 E0
digits(a)returns number of effective digits of a. >a=(1/3)*2;?a;b=digits(a);?b
a = 0.6666666666 6666666666 ... 666 6667E0
b = 0.104E3
exponent(a)returns an integer number of exponent of a.
a=0.xxxx*10n exponent(a) returns n.
>a=power(10,3);?a;b=exponent(a);?b
a = 0.1E4
b = 0.4E1

Rounding

Rounding operation is implicitly performed when computation result is stored to any variable. If the computation result has more than $precision digits,then it is rounded by the method specified by $round. Rounding operation can also be done explicitly by calling one of two funtions(trim() or round()) listed bellow.
Function nameDescriptionExample
trim(a,n)trim(a,n) means,rounding operation is done on the (n+1)th digit counted from the left most position of a according to the specification of $round. After rounding, a consists of n digits at most. >$format=F;a=1234.5678;b=trim(a,4);c=round(a,1);d=round(a,-1);e=round(a,0);?a;?b;?c;?d;?e # results are as follows
round(a,i) round(a,i) rounds a at the i-th position where i is the relative position counted from the decimal point.
If i>=0,then the degit at (i+1)th position from the decimal point to the right is rounded. The total count of digits after the decimal point will be i at most.
If i<0 then,the i-th digit from the decimal point to the left is rounded. As the result,at least i zeros appear from the decimal point to the left.
a = 1234.5678
b = 1235
c = 1234.6
d = 1230
e = 1235
Rounding methods:
The list of round methods which can be assigned to $round(default: $round = half_up).
Round methodMeaningExample
upround away from zero. >$round=up;a=-1/3;b=round(a,0);?b
b = -0.1E1
downround towards zero(truncate). >$round=down;a=-1/3;b=round(a,0);?b
b = -0.0
half_upround up if the digit >= 5 otherwise truncated(default). >a=-2/3;b=round(a,3);?b
b = -0.667E0
half_downround up if the digit >= 6 otherwise truncated. >$round=half_down;a=round(5.5555,3);?a
a = 0.5555E1
ceilround towards positive infinity(ceil). >$round=ceil;a=trim(1.2345,3);?a;b=trim(-1.2345,3);?b
a = 0.124E1
b = -0.123E1
floorround towards negative infinity(floor). >$round=floor;a=trim(1.2345,3);?a;b=trim(-1.2345,3);?b
a = 0.123E1
b = -0.124E1
half_evenround towards the even neighbor(Banker's rounding). >$round=half_even
>a=trim(2.125,3);?a
a = 0.212E1
>a=trim(2.135,3);?a
a = 0.214E1

Iterative calculations(I/O) and condition statement

MethodDescriptionExample
repeat n;Statements after 'repeat n' to the line end will be repeated n times. n must be positive integer. Factorial of 10(10!)
>n=0;s=1;repeat 10;n=n+1;s=s*n
>?s
s = 0.36288E7
while a op b;Statements after 'while a op b' to the end of line will be repeated while the condition 'a op b' is satisfied(true). a or b must be a variable or any numerical number. One of a or b must be a variable at least. op can be the one listetd bellow.
  • a > b ..... a is greater than b
  • a < b ..... a is less than b
  • a >= b ... a is greater or equal to b
  • a <= b .... a is less than or equal to b
  • a == b ... a is equal to b
  • a != b .... a is not equal to b
Factorial of 10(10!)
>n=10;s=1;while n>0;s=s*n;n=n-1
>?s
s = 0.36288E7
if a op b;If the condition 'a op b' is satisfied,then all statements to the end of line followed will be executed,otherwise skipped. 'break' can be followed by the 'if' to stop execution followed and exit iteration if it is executing. Factorial of 10(10!)
>n=0;s=1;repeat 100;n=n+1;s=s*n;if n>=10;break
>?s
s = 0.36288E7
load 'file' V1 V2 ... VnOpens 'file', and reads all lines from the file. After reading each one line,numerical values(which VPC can regard as it is a numeric) in the line will be stored to the variables listed from left to right. So every line must contain n numerics at least. Any numeric after n-th position will be ignored. Contents of 'data.txt'
1 a2 b 2 ;;3 4 5 6
'-11' ( 12) - 13;;
21,, 22 ,23

Execution results
>load data.txt a b c;?a;?b;?c
a = 0.1E1
b = 0.2E1
c = 0.3E1
a = -0.11E2
b = 0.12E2
c = -0.13E2
a = 0.21E2
b = 0.22E2
c = 0.23E2

Environment settings

Setting nameDescriptionExample
$format Controls how the numeric number is printed.
Each character of the character string at the right hand of $format will be processed from left to right. >?$format # default value
$format = '10*E q'
  • '10':Any integer. Numerical string is divided by the delimitter(' ' or ',' as explained bellow) at every n(10 in this case) characters for readability.
  • '*' :'*','+',or '-'. controls the first character of the number printed. The following character can be given.
    • '*' ... One space ' ' is output for positive number(default). '-' is output for negative number.
    • '-' ... No space is output for positive number. '-' is output for negative number.
    • '+' ... '+' is output for positive number. '-' is output for negative number.

  • 'E' :'E' or 'F'. when 'E'is specified,then number is output as 0.123...E3,where E3 means 103. 'F' means to output number in normal format without power of 10 specification.
  • ' ' :' ' or ','. specify the delimitter of the number string. A space or comma can be specified.
  • 'q' :'q' or 'Q'. when 'Q' is specified, then the numeric string output is quoted by '. 'q' is not.
  • If the same specifications are found,then later specification will be adopted.
Default value: '10*E q'



>a=(1/3)*1000;?a
a = 0.3333333333 3333333333 ... 333 333E3
>$format='QF,';?a
a = '333.3333333333,33333...333,3333333333'

$max_iterationsmaximum number of iteration count for computing sin() cos() ...etc. Default value: 10000
$precision Maximum number of digits that each variable a,b,...z can hold. In case like a=b*c+d,the right hand side of the equation will be performed as many digits as to keep the exact result(computation like 1/3 is a special case ). And finaly rounded when stored to the left hand side variable when the total digits are greater than $precision according to the $round specification. Default value: 100
$roundMethod for the rounding operation. Default value: half_up
$titleAny character string can be specified.Default value: ""
$a,$b,...$y,$z Any character string can be specified. Any comments about each variable may be useful. Default value: ""

vpc.ini file

Before VPC is running, if you prepare the file vpc.ini at the current directory(usually,the place where vpc.exe is placed),it can be read and executed if you want. This may be convenient if you alway prefer to set $format,$precision,... before actual execution.

Input/Output

I/O statementDescription
read 'file path'Specified file is read and executed. If the file is output by 'write' command,then everything will be restored to the state when the 'write' command is executed.
write 'file path'Every value of variables and environment setting will be output to the specified file. Every thing can be restored by read command to the state when write is executed.

Special symbols

Special symbols (Infinity and NaN) is used for unrepresentable resulted by division by zero. The value of division by zero cannnot be represented by any numerical symbol. After division by zero, 'a' will be stored special symbol listed above. Division by zero is itself an error. And referencing to 'a' after the division is also an error. NaN is not a number and never coincident with anything including itself.
Shigeo Kobayashi 2024-3-15