In general, I have a few questions about calling C functions from within NEURON.
Specifically, I am looking for a NEURON function that takes a floating point number, and returns the 32-bit representation (sign bit + exponent + mantissa; 64 bits for doubles) of that number in the computer. In C, this can be done easily by defining the function
Code: Select all
int float2hex(float theFloat) {
int hexval;
hexval = *(int *)&theFloat;
return hexval;
}
Code: Select all
float theFloat;
int bit_rep;
theFloat = 1.5;
bit_rep = float2hex(theFloat);
Q1: Is there a way to do this float-address-to-int typecasting in HOC? I didn't think so, which is why I tried to implement this as a C function in the nrn/src/oc/ directory:
Code: Select all
/**************************** FILE src/oc/float2hex.c ********/
#include <stdio.h>
#include <ctype.h>
int float2hex(float);
int float2hex(float theFloat) {
int hexval;
hexval = *(int *)&theFloat;
return hexval;
}
/* Hoc interface */
extern double hoc_pushx();
int hoc_float2hex(x)
double x;
{
int hexval;
fprintf(stderr,"in hoc_float2hex()\nfound x = %g\n",x);
hexval = float2hex(x);
fprintf(stderr,"Float %f = 0x%x = %g\n",x,hexval,(double)hexval);
hoc_pushx((double)hexval);
return 0;
}
Code: Select all
oc>val = float2hex(1.5)
first instance of val
in hoc_float2hex()
found x = 1.5
Float 1.500000 = 0x3fc00000 = 1.06955e+09
oc>print val
NaN
Q2: It looks to me like the number that I want returned (1.06955e+09) is too large to be returned properly. Is this a correct assessment, or have I made some programming error? Might there be a different way to work around this problem?
Also, a few general questions on calling C functions within NEURON:
Q3: I want this function to be available whenever I run my version of Neuron. Is there a different place in the source code (rather than its current home of nrn/src/oc) where I should put this file?
Q4: Do I need to use the functions hoc_pgetarg(), getarg(), or hoc_ret()? Looking at the way other C functions in src/oc interface with HOC, I should probably use them, but I couldn't quite figure out why they were needed. When I did call these functions in my code, I kept getting ambiguous segmentation violations; so, I left them out.
Thanks in advance for any suggestions.
Best,
Christina