Acme-ExtUtils-XSOne-Test-Calculator
view release on metacpan or search on metacpan
Calculator.xs view on Meta::CPAN
static double basic_safe_divide(double a, double b, int *error) {
if (b == 0.0) {
*error = 1;
return 0.0;
}
*error = 0;
return a / b;
}
static double basic_clamp(double value, double min_val, double max_val) {
if (value < min_val) return min_val;
if (value > max_val) return max_val;
return value;
}
static double basic_percent(double value, double percent) {
return value * percent / 100.0;
}
/* C code from: Calculator/Memory.xs */
#line 1 "lib/Acme/ExtUtils/XSOne/Test/Calculator/Memory.xs"
/*
* Acme::ExtUtils::XSOne::Test::Calculator::Memory - Memory and history functions
*
* This module accesses the shared state defined in _header.xs
*/
/* Memory package helpers - need access to memory_slots */
static int mem_is_valid_slot(int slot) {
return (slot >= 0 && slot < MAX_MEMORY_SLOTS);
}
static int mem_get_used_slots(void) {
int count = 0;
for (int i = 0; i < MAX_MEMORY_SLOTS; i++) {
if (memory_slots[i] != 0.0) count++;
}
return count;
}
static double mem_sum_all(void) {
double sum = 0.0;
for (int i = 0; i < MAX_MEMORY_SLOTS; i++) {
sum += memory_slots[i];
}
return sum;
}
static void mem_add_to_slot(int slot, double value) {
if (mem_is_valid_slot(slot)) {
memory_slots[slot] += value;
}
}
/* C code from: Calculator/Scientific.xs */
#line 1 "lib/Acme/ExtUtils/XSOne/Test/Calculator/Scientific.xs"
/*
* Acme::ExtUtils::XSOne::Test::Calculator::Scientific - Scientific/advanced operations
*/
/* C helper functions for Scientific package */
static double sci_safe_log(double a, int *error) {
if (a <= 0.0) {
*error = 1;
return 0.0;
}
*error = 0;
return log(a);
}
static double sci_safe_sqrt(double a, int *error) {
if (a < 0.0) {
*error = 1;
return 0.0;
}
*error = 0;
return sqrt(a);
}
static double sci_ipow(double base, int exp) {
/* Integer power - faster than pow() for integer exponents */
if (exp == 0) return 1.0;
int neg = 0;
if (exp < 0) {
neg = 1;
exp = -exp;
}
double result = 1.0;
while (exp > 0) {
if (exp & 1) result *= base;
base *= base;
exp >>= 1;
}
return neg ? 1.0 / result : result;
}
static double sci_combination(int n, int r) {
if (r > n || r < 0) return 0.0;
if (r == 0 || r == n) return 1.0;
double result = 1.0;
for (int i = 0; i < r; i++) {
result = result * (n - i) / (i + 1);
}
return result;
}
/* C code from: Calculator/Trig.xs */
#line 1 "lib/Acme/ExtUtils/XSOne/Test/Calculator/Trig.xs"
/*
* Acme::ExtUtils::XSOne::Test::Calculator::Trig - Trigonometric functions
*/
/* C helper functions for Trig package */
static double trig_normalize_angle(double radians) {
/* Normalize angle to [-PI, PI] */
while (radians > M_PI) radians -= 2.0 * M_PI;
while (radians < -M_PI) radians += 2.0 * M_PI;
return radians;
}
static int trig_is_valid_asin_arg(double x) {
return (x >= -1.0 && x <= 1.0);
}
static double trig_sec(double x) {
return 1.0 / cos(x);
}
Calculator.xs view on Meta::CPAN
"max_memory_slots", "max_history_entries",
"is_valid_slot", "used_slots", "sum_all_slots", "add_to"
};
do_import(aTHX_ "Acme::ExtUtils::XSOne::Test::Calculator::Memory",
memory_exports, 12, items, ax);
}
MODULE = Acme::ExtUtils::XSOne::Test::Calculator PACKAGE = Acme::ExtUtils::XSOne::Test::Calculator::Scientific
PROTOTYPES: DISABLE
double
power(base, exp)
double base
double exp
CODE:
RETVAL = pow(base, exp);
add_to_history('^', base, exp, RETVAL);
OUTPUT:
RETVAL
double
sqrt_val(a)
double a
CODE:
if (a < 0.0) {
croak("Cannot take square root of negative number");
}
RETVAL = sqrt(a);
add_to_history('r', a, 0.5, RETVAL);
OUTPUT:
RETVAL
double
cbrt_val(a)
double a
CODE:
RETVAL = cbrt(a);
add_to_history('r', a, 1.0/3.0, RETVAL);
OUTPUT:
RETVAL
double
nth_root(a, n)
double a
double n
CODE:
if (n == 0.0) {
croak("Cannot take 0th root");
}
if (a < 0.0 && fmod(n, 2.0) == 0.0) {
croak("Cannot take even root of negative number");
}
RETVAL = pow(a, 1.0/n);
add_to_history('r', a, n, RETVAL);
OUTPUT:
RETVAL
double
log_natural(a)
double a
CODE:
if (a <= 0.0) {
croak("Cannot take log of non-positive number");
}
RETVAL = log(a);
add_to_history('l', a, M_E, RETVAL);
OUTPUT:
RETVAL
double
log10_val(a)
double a
CODE:
if (a <= 0.0) {
croak("Cannot take log of non-positive number");
}
RETVAL = log10(a);
add_to_history('L', a, 10, RETVAL);
OUTPUT:
RETVAL
double
log_base(a, base)
double a
double base
CODE:
if (a <= 0.0 || base <= 0.0 || base == 1.0) {
croak("Invalid logarithm arguments");
}
RETVAL = log(a) / log(base);
add_to_history('L', a, base, RETVAL);
OUTPUT:
RETVAL
double
exp_val(a)
double a
CODE:
RETVAL = exp(a);
add_to_history('e', a, 0, RETVAL);
OUTPUT:
RETVAL
double
factorial(n)
int n
CODE:
if (n < 0) {
croak("Cannot take factorial of negative number");
}
if (n > 170) {
croak("Factorial overflow (max 170)");
}
RETVAL = 1.0;
for (int i = 2; i <= n; i++) {
RETVAL *= i;
}
add_to_history('!', (double)n, 0, RETVAL);
OUTPUT:
RETVAL
double
ipow(base, exp)
double base
int exp
CODE:
RETVAL = sci_ipow(base, exp);
add_to_history('^', base, (double)exp, RETVAL);
OUTPUT:
RETVAL
double
safe_sqrt(a)
double a
CODE:
int error;
RETVAL = sci_safe_sqrt(a, &error);
if (!error) {
add_to_history('r', a, 0.5, RETVAL);
}
OUTPUT:
RETVAL
double
safe_log(a)
double a
CODE:
int error;
RETVAL = sci_safe_log(a, &error);
if (!error) {
add_to_history('l', a, M_E, RETVAL);
}
OUTPUT:
RETVAL
double
combination(n, r)
int n
int r
CODE:
RETVAL = sci_combination(n, r);
add_to_history('C', (double)n, (double)r, RETVAL);
OUTPUT:
RETVAL
double
permutation(n, r)
int n
int r
CODE:
if (r > n || r < 0 || n < 0) {
RETVAL = 0.0;
} else {
RETVAL = sci_combination(n, r);
for (int i = 2; i <= r; i++) {
RETVAL *= i;
}
}
add_to_history('P', (double)n, (double)r, RETVAL);
OUTPUT:
RETVAL
void
import(...)
CODE:
{
static const char *scientific_exports[] = {
"power", "sqrt_val", "cbrt_val", "nth_root",
"log_natural", "log10_val", "log_base", "exp_val",
"factorial", "ipow", "safe_sqrt", "safe_log",
"combination", "permutation"
};
do_import(aTHX_ "Acme::ExtUtils::XSOne::Test::Calculator::Scientific",
scientific_exports, 14, items, ax);
}
MODULE = Acme::ExtUtils::XSOne::Test::Calculator PACKAGE = Acme::ExtUtils::XSOne::Test::Calculator::Trig
PROTOTYPES: DISABLE
double
sin_val(a)
double a
CODE:
RETVAL = sin(a);
add_to_history('s', a, 0, RETVAL);
OUTPUT:
RETVAL
double
cos_val(a)
double a
CODE:
RETVAL = cos(a);
add_to_history('c', a, 0, RETVAL);
OUTPUT:
RETVAL
double
tan_val(a)
double a
CODE:
RETVAL = tan(a);
add_to_history('t', a, 0, RETVAL);
OUTPUT:
RETVAL
double
asin_val(a)
double a
CODE:
if (a < -1.0 || a > 1.0) {
croak("asin argument must be in [-1, 1]");
}
RETVAL = asin(a);
add_to_history('S', a, 0, RETVAL);
OUTPUT:
RETVAL
double
acos_val(a)
double a
CODE:
if (a < -1.0 || a > 1.0) {
croak("acos argument must be in [-1, 1]");
}
RETVAL = acos(a);
add_to_history('C', a, 0, RETVAL);
OUTPUT:
( run in 0.861 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )