Calculator Dll - Help
Made by Matrebatre
With this dll you can calculate the result of an expression. You can use it to allow users to input calculations when you ask for a real value. It supports:
- Standard operators: +, -, * and /
- Scientific notation (1.52e8)
- Brackets
- Functions (see function list below)
- Constants
The dll also contains functions for converting strings to reals and reals to strings, as the standard GM functions are very limited and often crash if the numbers are too large.
The syntax and functions supported by the dll work the same as in GM. The following functions are supported:
abs(x)
sign(x)
round(x)
floor(x)
ceil(x)
frac(x)
sqrt(x)
sqr(x)
power(x,n)
exp(x)
ln(x)
log2(x)
log10(x)
logn(n,x)
sin(x)
cos(x)
tan(x)
arcsin(x)
arccos(x)
arctan(x)
arctan2(y,x)
degtorad(x)
radtodeg(x)
mod(x,divisor)
div(x,divisor)
min(x1,x2,x3,...)
max(x1,x2,x3,...)
mean(x1,x2,x3,...)
median(x1,x2,x3,...)
point_distance(x1,y1,x2,y2)
point_direction(x1,y1,x2,y2)
lengthdir_x(len,dir)
lengthdir_y(len,dir)
random(x)
make_color_rgb(red,green,blue)
color_get_red(col)
color_get_green(col)
color_get_blue(col)
merge_color(col1,col2,amount)
The dll also supports constants pi and e.
If you are not using the extension, you should call calculatordll_init
once before using the other functions.
Functions:
calc_setparametercount(parametercount)
- Sets the number of parameters you want to use. Set this to 0 if you are not using parameters. Parameters are almost the same as constants, but they can be changed.
calc_setparameter(index,name,value)
- Sets the name and value of a parameter. The first parameter has index 1. The number of parameters should be set before using this function.
calc_calculate(expression)
- Calculates the result of an expression. Return values:
1 - Successful (use calc_result to get the result)
0 - Memory error (not enough free memory)
-1 - Divide by zero
-2 - Out of domain
-3 - Out of range
-4 - Unexpected character
-5 - Unexpected end
-6 - Operator expected
-7 - Wrong argument count
-8 - Unknown function
-9 - Unknown constant
If an error occurred, you can use calc_errorpos and calc_errorlen to get the part of the expression that caused the error.
calc_result()
- Returns the result of the last expression calculated.
calc_errorpos()
- Returns the position of the error in the last expression calculated. 0 is the first position.
calc_errorlen()
- Returns the length of the error in the last expression calculated. You can use this to get the part of the expression that caused the error.
calc_str2real(string)
- Converts a string to a real number without crashing if the number is too big. Returns 0 if an error occurred.
calc_real2str(real,length,mindecimals,maxdecimals,padzeroes,useplussign)
- Converts a real number to a string with more options than the standard GM function. Length indicates the minimum length of the resulting string (max 1000). If it is shorter, spaces will be added left of it. Mindecimals and maxdecimals indicate the minimum and maximum number of decimals. If padzeroes is true, the string will be padded with zeroes instead of spaces. If useplussign is true, a sign will be added even for positive numbers.
calc_real2str_e(real,length,mindecimals,maxdecimals,padzeroes,useplussign)
- Converts a real number to a string using the scientific notation. The function has the same arguments as the previous function.
calc_real2str_best(real,length,mindecimals,maxdecimals,padzeroes,useplussign)
- Converts a real number to a string, trying to use the best notation (decimal or scientific). The function has the same arguments as the previous function.
Examples:
Using the calculator:
var expression,a,result,error,errorpos,errorlen;
expression = get_string('Expression:','');
a = calc_calculate(expression);
if a=1 {
result = calc_result();
show_message('Calculation was successful.##'+expression+' = '+calc_real2str_best(result,0,0,8,false,false));
} else {
if(a=0) then error = 'Memory error';
if(a=-1) then error = 'Divide by zero';
if(a=-2) then error = 'Out of domain';
if(a=-3) then error = 'Out of range';
if(a=-4) then error = 'Unexpected character';
if(a=-5) then error = 'Unexpected end';
if(a=-6) then error = 'Operator expected';
if(a=-7) then error = 'Wrong argument count';
if(a=-8) then error = 'Unknown function';
if(a=-9) then error = 'Unknown constant';
errorpos = calc_errorpos();
errorlen = calc_errorlen();
show_message('Calculation failed.#'+error+' at position '+string(errorpos+1)+': "'+string_copy(expression,errorpos+1,errorlen)+'"##Expression: '+expression);
}
Good luck using this dll!