Soar Kernel  9.3.2 08-06-12
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Macros | Functions
rhsfun_math.cpp File Reference
#include <portability.h>
#include <stdlib.h>
#include "rhsfun_math.h"
#include "symtab.h"
#include "kernel.h"
#include "mem.h"
#include "print.h"
#include "lexer.h"
#include "rhsfun.h"
#include "soar_rand.h"
#include <math.h>

Go to the source code of this file.

Macros

#define PI   3.141592653589
#define PI_OVER_TWO   (PI/2)
#define RAD_TO_DEG(X)   ((X*180)/PI)
#define TWO_PI   (PI*2)
#define X   0
#define Y   1
#define Z   2

Functions

uint64_t _dice_binom (uint64_t n, uint64_t m)
double _dice_prob_atleast (int64_t dice, int64_t sides, int64_t count)
double _dice_prob_exact (int64_t dice, int64_t sides, int64_t count)
double _dice_zero_tolerance (double in)
Symbolabs_rhs_function_code (agent *thisAgent, list *args, void *)
int64_t air_soar_round_off_angle (int64_t n, int64_t m)
Symbolatan2_rhs_function_code (agent *thisAgent, list *args, void *)
double bracket_rad_to_deg (double var)
Symbolcompute_heading_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolcompute_range_rhs_function_code (agent *thisAgent, list *args, void *)
int64_t convert (double flo)
double convert_to_soar_angle (double heading_in_rads)
Symbolcos_rhs_function_code (agent *thisAgent, list *args, void *)
Symboldice_prob_rhs_function_code (agent *thisAgent, list *args, void *)
Symboldiv_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolfloat_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolfp_divide_rhs_function_code (agent *thisAgent, list *args, void *)
int64_t heading_to_point (int64_t current_x, int64_t current_y, int64_t x, int64_t y)
void hrl_xydof_to_heading (double xydof[3], double *output)
void init_built_in_rhs_math_functions (agent *thisAgent)
Symbolint_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolminus_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolmod_rhs_function_code (agent *thisAgent, list *args, void *)
double normalize_heading_float (double n)
int64_t normalize_heading_int (int64_t n)
Symbolplus_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolrand_float_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolrand_int_rhs_function_code (agent *thisAgent, list *args, void *)
void remove_built_in_rhs_math_functions (agent *thisAgent)
Symbolround_off_air_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolround_off_heading_air_rhs_function_code (agent *thisAgent, list *args, void *)
double round_off_heading_float (double n, double m)
int64_t round_off_heading_int (int64_t n, int64_t m)
Symbolsin_rhs_function_code (agent *thisAgent, list *args, void *)
Symbolsqrt_rhs_function_code (agent *thisAgent, list *args, void *)
Symboltimes_rhs_function_code (agent *thisAgent, list *args, void *)
void vec2_norm (double v[3], double r[3], int abort)
void vector_from_to_position (double pos1[3], double pos2[3], double vector[3])

Macro Definition Documentation

#define PI   3.141592653589

Definition at line 760 of file rhsfun_math.cpp.

Referenced by convert_to_soar_angle().

#define PI_OVER_TWO   (PI/2)

Definition at line 761 of file rhsfun_math.cpp.

Referenced by convert_to_soar_angle().

#define RAD_TO_DEG (   X)    ((X*180)/PI)

Definition at line 763 of file rhsfun_math.cpp.

Referenced by bracket_rad_to_deg().

#define TWO_PI   (PI*2)

Definition at line 762 of file rhsfun_math.cpp.

Referenced by convert_to_soar_angle().

#define X   0

Definition at line 764 of file rhsfun_math.cpp.

Referenced by hrl_xydof_to_heading(), vec2_norm(), and vector_from_to_position().

#define Y   1

Definition at line 765 of file rhsfun_math.cpp.

Referenced by hrl_xydof_to_heading(), vec2_norm(), and vector_from_to_position().

#define Z   2

Definition at line 766 of file rhsfun_math.cpp.

Referenced by vector_from_to_position().

Function Documentation

uint64_t _dice_binom ( uint64_t  n,
uint64_t  m 
)

Definition at line 1066 of file rhsfun_math.cpp.

Referenced by _dice_prob_exact().

{
uint64_t* b = new uint64_t[ n+1 ];
uint64_t i, j, ret;
b[0] = 1;
for ( i=1; i<=n; ++i )
{
b[i] = 1;
for ( j=(i-1); j>0; --j )
{
b[j] += b[j-1];
}
}
ret = b[m];
delete[] b;
return ret;
}
double _dice_prob_atleast ( int64_t  dice,
int64_t  sides,
int64_t  count 
)

Definition at line 1119 of file rhsfun_math.cpp.

References _dice_prob_exact().

Referenced by dice_prob_rhs_function_code().

{
// makes no sense
if ( dice < 0 )
return 0;
if ( count < 0 )
return 0;
if ( sides < 1 )
return 0;
double result = 0.0;
for ( int64_t i=0; ( count + i <= dice ); ++i )
{
result += _dice_prob_exact( dice, sides, count+i );
}
return result;
}
double _dice_prob_exact ( int64_t  dice,
int64_t  sides,
int64_t  count 
)

Definition at line 1086 of file rhsfun_math.cpp.

References _dice_binom().

Referenced by _dice_prob_atleast(), and dice_prob_rhs_function_code().

{
// makes no sense
if ( dice < 0 )
return 0;
if ( count < 0 )
return 0;
if ( sides < 1 )
return 0;
// if there are no dice, probability is zero unless count is also zero
if ( dice == 0 )
{
if ( count == 0 )
return 1;
return 0;
}
if ( count > dice )
return 0;
double p1kd = pow( static_cast< double >( sides ), static_cast< double >( count ) );
double p2nkn = pow( static_cast< double >( sides - 1 ), static_cast< double >( dice - count ) );
double p2nkd = pow( static_cast< double >( sides ), static_cast< double >( dice - count ) );
double result = static_cast< double >( _dice_binom( static_cast< uint64_t >( dice ), static_cast< uint64_t >( count ) ) );
result *= ( static_cast< double >( 1.0 ) / p1kd );
result *= ( p2nkn / p2nkd );
return result;
}
double _dice_zero_tolerance ( double  in)
inline

Definition at line 1060 of file rhsfun_math.cpp.

Referenced by dice_prob_rhs_function_code().

{
return ( ( fabs( in ) <= 0.00001 )?( 0 ):( in ) );
}
Symbol* abs_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 444 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), make_int_constant(), NIL, print(), print_with_symbols(), int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg,
*return_value;
if (!args) {
print(thisAgent, "Error: 'abs' function called with no arguments\n");
return NIL;
}
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
return_value = make_float_constant(thisAgent, fabs(arg->fc.value));
else if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
return_value = make_int_constant(thisAgent, (arg->ic.value<0) ? -arg->ic.value : arg->ic.value);
else {
print_with_symbols(thisAgent, "Error: 'abs' function called with non-numeric argument %y\n", arg);
return NIL;
}
return return_value;
}
int64_t air_soar_round_off_angle ( int64_t  n,
int64_t  m 
)

Definition at line 813 of file rhsfun_math.cpp.

Referenced by bracket_rad_to_deg().

{
int64_t unbounded_rounded, bounded_rounded;
/* need to round the first (n) to the nearest second (m) */
if (n < 0)
unbounded_rounded = m * ((n - (m / 2L)) / m);
else
unbounded_rounded = m * ((n + (m / 2L)) / m);
/* we need to make sure that -180 < value <= 180. */
// FIXME replace this code with the much faster mod2pi in ed olson's linalg library
bounded_rounded = (unbounded_rounded % 360);
if (bounded_rounded > 180)
bounded_rounded -= 360;
if (bounded_rounded <= -180)
bounded_rounded += 360;
return bounded_rounded;
}
Symbol* atan2_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 391 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
cons *c;
double numer_value,
denom_value;
if (!args) {
print(thisAgent, "Error: 'atan2' function called with no arguments\n");
return NIL;
}
for (c=args; c!=NIL; c=c->rest) {
arg = static_cast<symbol_union *>(c->first);
if ( (arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE)
&& (arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols (thisAgent, "Error: non-number (%y) passed to atan2\n",
arg);
return NIL;
}
}
if (!args->rest) {
print(thisAgent, "Error: 'atan2' function called with only one argument\n");
return NIL;
}
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
numer_value = arg->fc.value;
else
numer_value = static_cast<double>(arg->ic.value) ;
c = args->rest;
if (c->rest) {
print(thisAgent, "Error: 'atan2' function called with more than two arguments.\n");
return NIL;
}
arg = static_cast<symbol_union *>(c->first);
if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
denom_value = arg->fc.value;
else
denom_value = static_cast<double>(arg->ic.value) ;
return make_float_constant(thisAgent, atan2(numer_value, denom_value));
}
double bracket_rad_to_deg ( double  var)

Definition at line 835 of file rhsfun_math.cpp.

References air_soar_round_off_angle(), and RAD_TO_DEG.

Referenced by heading_to_point().

{
return static_cast<double>(air_soar_round_off_angle(static_cast<int64_t>(RAD_TO_DEG(var)), 1)) ;
}
Symbol* compute_heading_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 871 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, heading_to_point(), symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_int_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
int64_t current_x, current_y;
int64_t waypoint_x, waypoint_y;
int count;
cons *c;
if (!args) {
print(thisAgent, "Error: 'compute-heading' function called with no arguments\n");
return NIL;
}
for (c = args; c != NIL; c = c->rest) {
arg = static_cast<Symbol *>(c->first);
if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
(arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols(thisAgent, "Error: non-number (%y) passed to - compute-heading\n", arg);
return NIL;
}
}
count = 1;
for (c = args->rest; c != NIL; c = c->rest) {
arg = static_cast<Symbol *>(c->first);
if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
(arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols(thisAgent, "Error: non-number (%y) passed to compute-heading function.\n", arg);
return NIL;
} else {
count++;
}
}
if (count != 4) {
print(thisAgent, "Error: 'compute-heading' takes exactly 4 arguments.\n");
return NIL;
}
arg = static_cast<Symbol *>(args->first);
current_x = (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? arg->ic.value : static_cast<int64_t>(arg->fc.value);
arg = static_cast<Symbol *>(args->rest->first);
current_y = (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? arg->ic.value : static_cast<int64_t>(arg->fc.value);
arg = static_cast<Symbol *>(args->rest->rest->first);
waypoint_x = (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? arg->ic.value : static_cast<int64_t>(arg->fc.value);
arg = static_cast<Symbol *>(args->rest->rest->rest->first);
waypoint_y = (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? arg->ic.value : static_cast<int64_t>(arg->fc.value);
return make_int_constant(thisAgent, heading_to_point(current_x, current_y, waypoint_x, waypoint_y));
}
Symbol* compute_range_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 932 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_int_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
double current_x, current_y;
double waypoint_x, waypoint_y;
int count;
cons *c;
if (!args) {
print(thisAgent, "Error: 'compute-range' function called with no arguments\n");
return NIL;
}
for (c = args; c != NIL; c = c->rest) {
arg = static_cast<Symbol *>(c->first);
if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE)
&& (arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols(thisAgent, "Error: non-number (%y) passed to - compute-range\n", arg);
return NIL;
}
}
count = 1;
for (c = args->rest; c != NIL; c = c->rest) {
arg = static_cast<Symbol *>(c->first);
if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE)
&& (arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols(thisAgent, "Error: non-number (%y) passed to compute-range function.\n", arg);
return NIL;
} else {
count++;
}
}
if (count != 4) {
print(thisAgent, "Error: 'compute-range' takes exactly 4 arguments.\n");
return NIL;
}
arg = static_cast<Symbol *>(args->first);
current_x = (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? static_cast<double>(arg->ic.value) : arg->fc.value;
arg = static_cast<Symbol *>(args->rest->first);
current_y = (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? static_cast<double>(arg->ic.value) : arg->fc.value;
arg = static_cast<Symbol *>(args->rest->rest->first);
waypoint_x = (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? static_cast<double>(arg->ic.value) : arg->fc.value;
arg = static_cast<Symbol *>(args->rest->rest->rest->first);
waypoint_y = (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? static_cast<double>(arg->ic.value) : arg->fc.value;
return make_int_constant(thisAgent, static_cast<int64_t>(sqrt((current_x - waypoint_x)
* (current_x - waypoint_x)
+ (current_y - waypoint_y)
* (current_y - waypoint_y))));
}
int64_t convert ( double  flo)

Definition at line 840 of file rhsfun_math.cpp.

Referenced by heading_to_point().

{
return static_cast<int64_t>(flo);
}
double convert_to_soar_angle ( double  heading_in_rads)

Definition at line 789 of file rhsfun_math.cpp.

References PI, PI_OVER_TWO, and TWO_PI.

Referenced by hrl_xydof_to_heading().

{
double heading;
/* Not only correct, but more efficient! */
heading = heading_in_rads - PI_OVER_TWO;
if (heading < 0.0)
heading += TWO_PI;
heading = TWO_PI - heading;
if (heading > PI)
heading -= TWO_PI;
return heading;
}
Symbol* cos_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 334 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), NIL, print(), print_with_symbols(), int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
double arg_value;
if (!args) {
print(thisAgent, "Error: 'cos' function called with no arguments\n");
return NIL;
}
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
arg_value = arg->fc.value;
else if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
arg_value = static_cast<double>(arg->ic.value) ;
else {
print_with_symbols(thisAgent, "Error: 'cos' function called with non-numeric argument %y\n", arg);
return NIL;
}
return make_float_constant(thisAgent, cos(arg_value));
}
Symbol* dice_prob_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 1140 of file rhsfun_math.cpp.

References _dice_prob_atleast(), _dice_prob_exact(), _dice_zero_tolerance(), symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), sym_constant_struct::name, NIL, print_with_symbols(), cons_struct::rest, symbol_union::sc, SYM_CONSTANT_SYMBOL_TYPE, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
int64_t dice;
int64_t sides;
int64_t count;
enum pred_type { eq, ne, lt, gt, le, ge, bad };
pred_type pred = bad;
// parse + validate
{
Symbol* temp_sym;
// dice
temp_sym = static_cast< Symbol* >( args->first );
if ( ( temp_sym->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE ) &&
( temp_sym->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE ) )
{
print_with_symbols( thisAgent, "Error: non-number (%y) passed as 'dice' to - compute-dice-probability\n", temp_sym );
return NIL;
}
dice = ( ( temp_sym->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE )?( temp_sym->ic.value ):( static_cast< int64_t >( temp_sym->fc.value ) ) );
// sides
temp_sym = static_cast< Symbol* >( args->rest->first );
if ( ( temp_sym->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE ) &&
( temp_sym->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE ) )
{
print_with_symbols( thisAgent, "Error: non-number (%y) passed as 'sides' to - compute-dice-probability\n", temp_sym );
return NIL;
}
sides = ( ( temp_sym->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE )?( temp_sym->ic.value ):( static_cast< int64_t >( temp_sym->fc.value ) ) );
// count
temp_sym = static_cast< Symbol* >( args->rest->rest->first );
if ( ( temp_sym->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE ) &&
( temp_sym->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE ) )
{
print_with_symbols( thisAgent, "Error: non-number (%y) passed as 'count' to - compute-dice-probability\n", temp_sym );
return NIL;
}
count = ( ( temp_sym->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE )?( temp_sym->ic.value ):( static_cast< int64_t >( temp_sym->fc.value ) ) );
// pred
temp_sym = static_cast< Symbol* >( args->rest->rest->rest->first );
if ( temp_sym->common.symbol_type != SYM_CONSTANT_SYMBOL_TYPE )
{
print_with_symbols( thisAgent, "Error: non-string (%y) passed as 'pred' to - compute-dice-probability\n", temp_sym );
return NIL;
}
if ( strcmp( temp_sym->sc.name, "eq" ) == 0 )
{
pred = eq;
}
else if ( strcmp( temp_sym->sc.name, "ne" ) == 0 )
{
pred = ne;
}
else if ( strcmp( temp_sym->sc.name, "lt" ) == 0 )
{
pred = lt;
}
else if ( strcmp( temp_sym->sc.name, "gt" ) == 0 )
{
pred = gt;
}
else if ( strcmp( temp_sym->sc.name, "le" ) == 0 )
{
pred = le;
}
else if ( strcmp( temp_sym->sc.name, "ge" ) == 0 )
{
pred = ge;
}
if ( pred == bad )
{
print_with_symbols( thisAgent, "Error: invalid string (%y) passed as 'pred' to - compute-dice-probability\n", temp_sym );
return NIL;
}
}
double ret = 0;
if ( pred == eq )
{
if ( ( count < 0 ) || ( count > dice ) )
{
ret = 0;
}
else
{
ret = _dice_zero_tolerance( _dice_prob_exact( dice, sides, count ) );
}
}
else if ( pred == ne )
{
if ( ( count < 0 ) || ( count > dice ) )
{
ret = 1;
}
else
{
ret = _dice_zero_tolerance( 1 - _dice_prob_exact( dice, sides, count ) );
}
}
else if ( pred == lt )
{
if ( count <= 0 )
{
ret = 0;
}
else if ( count > dice )
{
ret = 1;
}
else
{
ret = _dice_zero_tolerance( 1 - _dice_prob_atleast( dice, sides, count ) );
}
}
else if ( pred == gt )
{
if ( count < 0 )
{
ret = 1;
}
else if ( count >= dice )
{
ret = 0;
}
else
{
ret = _dice_zero_tolerance( _dice_prob_atleast( dice, sides, count ) - _dice_prob_exact( dice, sides, count ) );
}
}
else if ( pred == le )
{
if ( count < 0 )
{
ret = 0;
}
else if ( count >= dice )
{
ret = 1;
}
else
{
ret = _dice_zero_tolerance( ( 1 - _dice_prob_atleast( dice, sides, count ) ) + _dice_prob_exact( dice, sides, count ) );
}
}
else if ( pred == ge )
{
if ( count <= 0 )
{
ret = 1;
}
else if ( count > dice )
{
ret = 0;
}
else
{
ret = _dice_zero_tolerance( _dice_prob_atleast( dice, sides, count ) );
}
}
return make_float_constant(thisAgent, ret);
}
Symbol* div_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 238 of file rhsfun_math.cpp.

References cons_struct::first, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_int_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, and int_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg1, *arg2;
arg1 = static_cast<symbol_union *>(args->first);
arg2 = static_cast<symbol_union *>(args->rest->first);
if (arg1->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: non-integer (%y) passed to div function\n",
arg1);
return NIL;
}
if (arg2->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: non-integer (%y) passed to div function\n",
arg2);
return NIL;
}
if (arg2->ic.value == 0) {
print (thisAgent, "Error: attempt to divide ('div') by zero.\n");
return NIL;
}
return make_int_constant (thisAgent, arg1->ic.value / arg2->ic.value);
/* Warning: ANSI doesn't say precisely what happens if one or both of the
two args is negative. */
}
Symbol* float_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 531 of file rhsfun_math.cpp.

References FALSE, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, IDENTIFIER_SYMBOL_TYPE, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, SYM_CONSTANT_SYMBOL_TYPE, symbol_add_ref(), symbol_to_string(), int_constant_struct::value, and VARIABLE_SYMBOL_TYPE.

Referenced by init_built_in_rhs_math_functions().

{
Symbol * sym;
if (!args) {
print (thisAgent, "Error: 'float' function called with no arguments.\n");
return NIL;
}
if (args->rest) {
print (thisAgent, "Error: 'float' takes exactly 1 argument.\n");
return NIL;
}
sym = static_cast<Symbol *>(args->first);
if (sym->common.symbol_type == VARIABLE_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: variable (%y) passed to 'float' RHS function.\n",
sym);
return NIL;
} else if (sym->common.symbol_type == IDENTIFIER_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: identifier (%y) passed to 'float' RHS function.\n",
sym);
return NIL;
} else if (sym->common.symbol_type == SYM_CONSTANT_SYMBOL_TYPE) {
double float_val;
errno = 0;
float_val = strtod(symbol_to_string (thisAgent, sym, FALSE, NIL, 0), NULL);
if (errno) {
print (thisAgent, "Error: bad float (%y) given to 'float' RHS function\n",
sym);
return NIL;
}
return make_float_constant (thisAgent, float_val);
} else if (sym->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
return sym;
} else if (sym->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
return make_float_constant(thisAgent, static_cast<double>(sym->ic.value) );
}
print (thisAgent, "Error: unknown symbol type (%y) given to 'float' RHS function\n",
sym);
return NIL;
}
Symbol* fp_divide_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 185 of file rhsfun_math.cpp.

References rhs_function_struct::f, symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
double f;
cons *c;
if (!args) {
print (thisAgent, "Error: '/' function called with no arguments\n");
return NIL;
}
for (c=args; c!=NIL; c=c->rest) {
arg = static_cast<symbol_union *>(c->first);
if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
(arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols (thisAgent, "Error: non-number (%y) passed to / function\n",
arg);
return NIL;
}
}
if (! args->rest) {
/* --- only one argument --- */
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type==INT_CONSTANT_SYMBOL_TYPE) f = static_cast<double>(arg->ic.value);
else f = arg->fc.value;
if (f != 0.0) return make_float_constant (thisAgent, 1.0 / f);
print (thisAgent, "Error: attempt to divide ('/') by zero.\n");
return NIL;
}
/* --- two or more arguments --- */
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type==INT_CONSTANT_SYMBOL_TYPE) f = static_cast<double>(arg->ic.value);
else f = arg->fc.value;
for (c=args->rest; c!=NIL; c=c->rest) {
arg = static_cast<symbol_union *>(c->first);
if (arg->common.symbol_type==INT_CONSTANT_SYMBOL_TYPE) {
if (arg->ic.value) f /= arg->ic.value;
else { print (thisAgent, "Error: attempt to divide ('/') by zero.\n"); return NIL; }
} else {
if (arg->fc.value != 0.0) f /= arg->fc.value;
else { print (thisAgent, "Error: attempt to divide ('/') by zero.\n"); return NIL; }
}
}
return make_float_constant (thisAgent, f);
}
int64_t heading_to_point ( int64_t  current_x,
int64_t  current_y,
int64_t  x,
int64_t  y 
)

Definition at line 845 of file rhsfun_math.cpp.

References bracket_rad_to_deg(), convert(), FALSE, hrl_xydof_to_heading(), vec2_norm(), and vector_from_to_position().

Referenced by compute_heading_rhs_function_code().

{
double plane_pos[3], waypoint_pos[3], dir[3];
double heading;
plane_pos[0] = static_cast<double>(current_x) ;
plane_pos[1] = static_cast<double>(current_y) ;
plane_pos[2] = 0;
waypoint_pos[0] = static_cast<double>(x) ;
waypoint_pos[1] = static_cast<double>(y) ;
waypoint_pos[2] = 0;
vector_from_to_position(plane_pos, waypoint_pos, dir);
vec2_norm(dir, dir, FALSE);
hrl_xydof_to_heading(dir, &heading);
return convert(bracket_rad_to_deg(heading));
}
void hrl_xydof_to_heading ( double  xydof[3],
double *  output 
)

Definition at line 804 of file rhsfun_math.cpp.

References convert_to_soar_angle(), X, and Y.

Referenced by heading_to_point().

{
double heading_in_rads;
heading_in_rads = convert_to_soar_angle(atan2(xydof[Y], xydof[X]));
(*output) = heading_in_rads;
}
void init_built_in_rhs_math_functions ( agent thisAgent)

Definition at line 1315 of file rhsfun_math.cpp.

References abs_rhs_function_code(), add_rhs_function(), atan2_rhs_function_code(), compute_heading_rhs_function_code(), compute_range_rhs_function_code(), cos_rhs_function_code(), dice_prob_rhs_function_code(), div_rhs_function_code(), FALSE, float_rhs_function_code(), fp_divide_rhs_function_code(), int_rhs_function_code(), make_sym_constant(), minus_rhs_function_code(), mod_rhs_function_code(), plus_rhs_function_code(), rand_float_rhs_function_code(), rand_int_rhs_function_code(), round_off_air_rhs_function_code(), round_off_heading_air_rhs_function_code(), sin_rhs_function_code(), sqrt_rhs_function_code(), times_rhs_function_code(), and TRUE.

Referenced by init_built_in_rhs_functions().

{
-1, TRUE, FALSE, 0);
-1, TRUE, FALSE, 0);
-1, TRUE, FALSE, 0);
-1, TRUE, FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "div"), div_rhs_function_code,
2, TRUE, FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant (thisAgent, "mod"), mod_rhs_function_code,
2, TRUE, FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "sin"),
1,
FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "cos"),
1,
FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "atan2"),
2,
FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "sqrt"),
1,
FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "abs"),
1,
FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "int"),
1,
FALSE, 0);
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "float"),
1,
FALSE, 0);
/* voigtjr 6/12/2007: added these built in functions on laird's request
these are straight out of the <8.6 kernel */
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "round-off-heading"),
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "round-off"),
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "compute-heading"),
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "compute-range"),
// NLD: 11/11 (ditto voigtjr's motivation above)
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "compute-dice-probability"),
// Bug 800: implement rhs rand functions
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "rand-int"),
add_rhs_function (thisAgent, make_sym_constant(thisAgent, "rand-float"),
}
Symbol* int_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 475 of file rhsfun_math.cpp.

References FALSE, symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, IDENTIFIER_SYMBOL_TYPE, INT_CONSTANT_SYMBOL_TYPE, make_int_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, SYM_CONSTANT_SYMBOL_TYPE, symbol_add_ref(), symbol_to_string(), float_constant_struct::value, and VARIABLE_SYMBOL_TYPE.

Referenced by init_built_in_rhs_math_functions().

{
Symbol * sym;
if (!args) {
print (thisAgent, "Error: 'int' function called with no arguments.\n");
return NIL;
}
if (args->rest) {
print (thisAgent, "Error: 'int' takes exactly 1 argument.\n");
return NIL;
}
sym = static_cast<Symbol *>(args->first);
if (sym->common.symbol_type == VARIABLE_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: variable (%y) passed to 'int' RHS function.\n",
sym);
return NIL;
} else if (sym->common.symbol_type == IDENTIFIER_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: identifier (%y) passed to 'int' RHS function.\n",
sym);
return NIL;
} else if (sym->common.symbol_type == SYM_CONSTANT_SYMBOL_TYPE) {
int64_t int_val;
errno = 0;
int_val = strtol(symbol_to_string (thisAgent, sym, FALSE, NIL, 0), NULL, 10);
if (errno) {
print (thisAgent, "Error: bad integer (%y) given to 'int' RHS function\n",
sym);
return NIL;
}
return make_int_constant (thisAgent, int_val);
} else if (sym->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
return sym;
} else if (sym->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
double int_part;
modf(sym->fc.value, &int_part);
return make_int_constant(thisAgent, static_cast<int64_t>(int_part) );
}
print (thisAgent, "Error: unknown symbol type (%y) given to 'int' RHS function\n",
sym);
return NIL;
}
Symbol* minus_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 126 of file rhsfun_math.cpp.

References rhs_function_struct::f, FALSE, symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), make_int_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, TRUE, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
double f = 0; /* For gcc -Wall */
int64_t i = 0; /* For gcc -Wall */
cons *c;
Bool float_found;
if (!args) {
print (thisAgent, "Error: '-' function called with no arguments\n");
return NIL;
}
for (c=args; c!=NIL; c=c->rest) {
arg = static_cast<symbol_union *>(c->first);
if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
(arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols (thisAgent, "Error: non-number (%y) passed to - function\n",
arg);
return NIL;
}
}
if (! args->rest) {
/* --- only one argument --- */
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type==INT_CONSTANT_SYMBOL_TYPE)
return make_int_constant (thisAgent, - arg->ic.value);
return make_float_constant (thisAgent, - arg->fc.value);
}
/* --- two or more arguments --- */
arg = static_cast<symbol_union *>(args->first);
float_found = FALSE;
if (arg->common.symbol_type==INT_CONSTANT_SYMBOL_TYPE) i = arg->ic.value;
else { float_found = TRUE; f = arg->fc.value; }
for (c=args->rest; c!=NIL; c=c->rest) {
arg = static_cast<symbol_union *>(c->first);
if (arg->common.symbol_type==INT_CONSTANT_SYMBOL_TYPE) {
if (float_found) f -= arg->ic.value;
else i -= arg->ic.value;
} else {
if (float_found) f -= arg->fc.value;
else { float_found = TRUE; f = i - arg->fc.value; }
}
}
if (float_found) return make_float_constant (thisAgent, f);
return make_int_constant (thisAgent, i);
}
Symbol* mod_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 272 of file rhsfun_math.cpp.

References cons_struct::first, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_int_constant(), NIL, print(), print_with_symbols(), cons_struct::rest, and int_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg1, *arg2;
arg1 = static_cast<symbol_union *>(args->first);
arg2 = static_cast<symbol_union *>(args->rest->first);
if (arg1->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: non-integer (%y) passed to mod function\n",
arg1);
return NIL;
}
if (arg2->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) {
print_with_symbols (thisAgent, "Error: non-integer (%y) passed to mod function\n",
arg2);
return NIL;
}
if (arg2->ic.value == 0) {
print (thisAgent, "Error: attempt to divide ('mod') by zero.\n");
return NIL;
}
return make_int_constant (thisAgent, arg1->ic.value % arg2->ic.value);
/* Warning: ANSI guarantees this does the right thing if both args are
positive. If one or both is negative, it only guarantees that
(a/b)*b + a%b == a. */
}
double normalize_heading_float ( double  n)

Definition at line 600 of file rhsfun_math.cpp.

Referenced by round_off_heading_air_rhs_function_code().

{
/* we need to make sure that -180 < value <= 180 so we modify */
/* the original rounded value using the fact that for heading, */
/* for any integer value of x, the following holds: */
/* heading1 = x*360 + heading2 */
while (n <= -180.0)
n += 360.0;
while (n > 180.0)
n -= 360.0;
return n;
}
int64_t normalize_heading_int ( int64_t  n)

Definition at line 585 of file rhsfun_math.cpp.

Referenced by round_off_heading_air_rhs_function_code().

{
/* we need to make sure that -180 < value <= 180 so we modify */
/* the original rounded value using the fact that for heading, */
/* for any integer value of x, the following holds: */
/* heading1 = x*360 + heading2 */
while (n <= -180)
n += 360;
while (n > 180)
n -= 360;
return n;
}
Symbol* plus_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 42 of file rhsfun_math.cpp.

References rhs_function_struct::f, FALSE, symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), make_int_constant(), NIL, print_with_symbols(), cons_struct::rest, TRUE, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Bool float_found;
int64_t i;
double f = 0;
Symbol *arg;
cons *c;
for (c=args; c!=NIL; c=c->rest) {
arg = static_cast<symbol_union *>(c->first);
if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
(arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols (thisAgent, "Error: non-number (%y) passed to + function\n",
arg);
return NIL;
}
}
i = 0;
float_found = FALSE;
while (args) {
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type==INT_CONSTANT_SYMBOL_TYPE) {
if (float_found) f += arg->ic.value;
else i += arg->ic.value;
} else {
if (float_found) f += arg->fc.value;
else { float_found = TRUE; f = arg->fc.value + i; }
}
args = args->rest;
}
if (float_found) return make_float_constant (thisAgent, f);
return make_int_constant (thisAgent, i);
}
Symbol* rand_float_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 997 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), NIL, print_with_symbols(), SoarRand(), int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
double n = 0;
if (args)
{
cons* c = args;
Symbol* arg = static_cast<Symbol*>(c->first);
if (arg) {
if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
n = static_cast<double>(arg->ic.value);
} else if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
n = arg->fc.value;
} else {
print_with_symbols(thisAgent, "Error: non-number (%y) passed to - rand-float\n", arg);
return NIL;
}
} else {
// assume default behavior (no arg)
// possibly warn? when can this happen?
}
}
if (n > 0) {
return make_float_constant(thisAgent, SoarRand(n));
}
return make_float_constant(thisAgent, SoarRand());
}
Symbol* rand_int_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 1032 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_int_constant(), NIL, print_with_symbols(), SoarRandInt(), int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
int64_t n = 0;
if (args)
{
cons* c = args;
Symbol* arg = static_cast<Symbol*>(c->first);
if (arg) {
if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
n = arg->ic.value;
} else if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
n = static_cast<int64_t>(arg->fc.value);
} else {
print_with_symbols(thisAgent, "Error: non-number (%y) passed to - rand-int\n", arg);
return NIL;
}
} else {
// assume default behavior (no arg)
// possibly warn? when can this happen?
}
}
if (n > 0) {
return make_int_constant(thisAgent, static_cast<int64_t>(SoarRandInt(static_cast<uint32_t>(n))));
}
return make_int_constant(thisAgent, SoarRandInt());
}
void remove_built_in_rhs_math_functions ( agent thisAgent)

Definition at line 1393 of file rhsfun_math.cpp.

References find_sym_constant(), and remove_rhs_function().

Referenced by remove_built_in_rhs_functions().

{
// DJP-FREE: These used to call make_sym_constant, but the symbols must already exist and if we call make here again we leak a reference.
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "+"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "*"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "-"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "/"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "div"));
remove_rhs_function (thisAgent, find_sym_constant (thisAgent, "mod"));
remove_rhs_function (thisAgent, find_sym_constant(thisAgent, "sin"));
remove_rhs_function (thisAgent, find_sym_constant(thisAgent, "cos"));
remove_rhs_function (thisAgent, find_sym_constant(thisAgent, "atan2"));
remove_rhs_function (thisAgent, find_sym_constant(thisAgent, "sqrt"));
remove_rhs_function (thisAgent, find_sym_constant(thisAgent, "abs"));
remove_rhs_function (thisAgent, find_sym_constant(thisAgent, "int"));
remove_rhs_function (thisAgent, find_sym_constant(thisAgent, "float"));
/* voigtjr 6/12/2007: added these built in functions on laird's request
these are straight out of the <8.6 kernel */
remove_rhs_function(thisAgent, find_sym_constant(thisAgent, "round-off-heading"));
remove_rhs_function(thisAgent, find_sym_constant(thisAgent, "round-off"));
remove_rhs_function(thisAgent, find_sym_constant(thisAgent, "compute-heading"));
remove_rhs_function(thisAgent, find_sym_constant(thisAgent, "compute-range"));
// NLD: 11/11
remove_rhs_function(thisAgent, find_sym_constant(thisAgent, "compute-dice-probability"));
// Bug 800: implement rand
remove_rhs_function(thisAgent, find_sym_constant(thisAgent, "rand-int"));
remove_rhs_function(thisAgent, find_sym_constant(thisAgent, "rand-float"));
}
Symbol* round_off_air_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 712 of file rhsfun_math.cpp.

References FALSE, symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), make_int_constant(), NIL, print(), cons_struct::rest, round_off_heading_float(), round_off_heading_int(), TRUE, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
double n = 0, f_m = 0;
int64_t i_m = 0;
cons *c;
bool float_found = FALSE;
if (!args) {
print(thisAgent, "Error: 'round_off' function called with no arguments\n");
return NIL;
}
if (!args->rest) {
/* --- only one argument --- */
print(thisAgent, "Error: 'round_off' function called with only one argument.\n");
return NIL;
}
/* --- two or more arguments --- */
arg = static_cast<Symbol *>(args->first);
if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
n = static_cast<double>(arg->ic.value) ;
else if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
n = arg->fc.value;
}
c = args->rest;
if (c->rest) {
/* --- more than two arguments --- */
print(thisAgent, "Error: 'round_off' function called with more than two arguments.\n");
return NIL;
}
arg = static_cast<Symbol *>(c->first);
if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
i_m = arg->ic.value;
else if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
float_found = TRUE;
f_m = arg->fc.value;
}
/* Now, deal with the arguments based on type and return result */
if (float_found)
return make_float_constant(thisAgent, round_off_heading_float(n, f_m));
else
return make_int_constant(thisAgent, round_off_heading_int(static_cast<int64_t>(n), i_m));
}
Symbol* round_off_heading_air_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 656 of file rhsfun_math.cpp.

References FALSE, symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), make_int_constant(), NIL, normalize_heading_float(), normalize_heading_int(), print(), cons_struct::rest, round_off_heading_float(), round_off_heading_int(), TRUE, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
double n = 0, f_m = 0;
int64_t i_m = 0;
cons *c;
bool float_found = FALSE;
if (!args) {
print(thisAgent, "Error: 'round_off_heading' function called with no arguments\n");
return NIL;
}
if (!args->rest) {
/* --- only one argument --- */
print(thisAgent, "Error: 'round_off_heading' function called with only one argument.\n");
return NIL;
}
/* --- two or more arguments --- */
arg = static_cast<Symbol *>(args->first);
if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
n = static_cast<double>(arg->ic.value) ;
else if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
n = arg->fc.value;
}
c = args->rest;
if (c->rest) {
/* --- more than two arguments --- */
print(thisAgent, "Error: 'round_off_heading' function called with more than two arguments.\n");
return NIL;
}
arg = static_cast<Symbol *>(c->first);
if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
i_m = arg->ic.value;
else if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
float_found = TRUE;
f_m = arg->fc.value;
}
/* Now, deal with the arguments based on type and return result */
if (float_found)
else
return make_int_constant(thisAgent, normalize_heading_int(round_off_heading_int(static_cast<int64_t>(n) , i_m)));
}
double round_off_heading_float ( double  n,
double  m 
)

Definition at line 627 of file rhsfun_math.cpp.

Referenced by round_off_air_rhs_function_code(), and round_off_heading_air_rhs_function_code().

{
double n_10, m_10, unbounded_rounded;
double ip;
double ip2;
/* OK. Both n and m can have tenths, so multiply by 10 and treat
as integers */
modf((n * 10.0), &ip);
n_10 = ip;
modf((m * 10.0), &ip);
m_10 = ip;
if (n_10 < 0.0) {
modf((m_10 / 2.0), &ip2);
modf(((n_10 - ip2) / m_10), &ip);
unbounded_rounded = (m_10 * ip);
} else {
modf((m_10 / 2.0), &ip2);
modf(((n_10 + ip2) / m_10), &ip);
unbounded_rounded = (m_10 * ip);
}
/* Divide by 10 to get tenths back and return */
return unbounded_rounded / 10.0;
}
int64_t round_off_heading_int ( int64_t  n,
int64_t  m 
)

Definition at line 614 of file rhsfun_math.cpp.

Referenced by round_off_air_rhs_function_code(), and round_off_heading_air_rhs_function_code().

{
int64_t unbounded_rounded;
/* need to round the first (i_n) to the nearest second (i_m) */
if (n < 0)
unbounded_rounded = m * ((n - (m / 2L)) / m);
else
unbounded_rounded = m * ((n + (m / 2L)) / m);
return unbounded_rounded;
}
Symbol* sin_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 305 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), NIL, print(), print_with_symbols(), int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
double arg_value;
if (!args) {
print(thisAgent, "Error: 'sin' function called with no arguments\n");
return NIL;
}
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
arg_value = arg->fc.value;
else if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
arg_value = static_cast<double>(arg->ic.value) ;
else {
print_with_symbols(thisAgent, "Error: 'sin' function called with non-numeric argument %y\n", arg);
return NIL;
}
return make_float_constant(thisAgent, sin(arg_value));
}
Symbol* sqrt_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 362 of file rhsfun_math.cpp.

References symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), NIL, print(), print_with_symbols(), int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Symbol *arg;
double arg_value;
if (!args) {
print(thisAgent, "Error: 'sqrt' function called with no arguments\n");
return NIL;
}
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
arg_value = arg->fc.value;
else if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
arg_value = static_cast<double>(arg->ic.value);
else {
print_with_symbols(thisAgent, "Error: 'sqrt' function called with non-numeric argument %y\n", arg);
return NIL;
}
return make_float_constant(thisAgent, sqrt(arg_value));
}
Symbol* times_rhs_function_code ( agent thisAgent,
list args,
void *   
)

Definition at line 83 of file rhsfun_math.cpp.

References rhs_function_struct::f, FALSE, symbol_union::fc, cons_struct::first, FLOAT_CONSTANT_SYMBOL_TYPE, symbol_union::ic, INT_CONSTANT_SYMBOL_TYPE, make_float_constant(), make_int_constant(), NIL, print_with_symbols(), cons_struct::rest, TRUE, int_constant_struct::value, and float_constant_struct::value.

Referenced by init_built_in_rhs_math_functions().

{
Bool float_found;
int64_t i;
double f = 0;
Symbol *arg;
cons *c;
for (c=args; c!=NIL; c=c->rest) {
arg = static_cast<symbol_union *>(c->first);
if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
(arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
print_with_symbols (thisAgent, "Error: non-number (%y) passed to * function\n",
arg);
return NIL;
}
}
i = 1;
float_found = FALSE;
while (args) {
arg = static_cast<symbol_union *>(args->first);
if (arg->common.symbol_type==INT_CONSTANT_SYMBOL_TYPE) {
if (float_found) f *= arg->ic.value;
else i *= arg->ic.value;
} else {
if (float_found) f *= arg->fc.value;
else { float_found = TRUE; f = arg->fc.value * i; }
}
args = args->rest;
}
if (float_found) return make_float_constant (thisAgent, f);
return make_int_constant (thisAgent, i);
}
void vec2_norm ( double  v[3],
double  r[3],
int  abort 
)

Definition at line 775 of file rhsfun_math.cpp.

References X, and Y.

Referenced by heading_to_point().

{
double mag, mag2;
mag2 = v[X] * v[X] + v[Y] * v[Y];
mag = sqrt(mag2);
if (!abort && (mag < 0.01)) {
r[0] = 1.0;
r[1] = 0.0;
return;
}
r[0] = v[0] / mag;
r[1] = v[1] / mag;
}
void vector_from_to_position ( double  pos1[3],
double  pos2[3],
double  vector[3] 
)

Definition at line 768 of file rhsfun_math.cpp.

References X, Y, and Z.

Referenced by heading_to_point().

{
vector[X] = pos2[X] - pos1[X];
vector[Y] = pos2[Y] - pos1[Y];
vector[Z] = pos2[Z] - pos1[Z];
}