• Advertisement
Sign in to follow this  

math utilities

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i tried to register math functions but always fail. r = engine->RegisterGlobalFunction("float sin (float)", asFUNCTION(sin), asCALL_CDECL); assert( r >= 0 ); i wonder if there is any available math functions in angelscript.

Share this post


Link to post
Share on other sites
Advertisement
Regular "sin" returns double and wants a double argument. I'm not sure but this might make the registering fail since you declare it as returning float and wanting a float argument.

Try to register with "double sin(double)" instead, or you could register "sinf" with asFUNCTION(sinf). sinf is just like sin except that it returns float and accepts a float argument.

Share this post


Link to post
Share on other sites
What will be the return value on success/failure?

(Ie is the assert correct with >= instead of >)

Share this post


Link to post
Share on other sites
This works (I just verified it):


int r = engine->RegisterGlobalFunction("float sin(float)", asFUNCTION(sinf), asCALL_CDECL); assert( r >= 0 );


You'll have to do '#include <math.h>' of course.

Kitt3n:

The return value is negative on error. So the correct test to do is 'r >= 0'.

Share this post


Link to post
Share on other sites
Here's my math-collection, just in case anyone might find a use for it.
I really can't remember if I wrote it myself, if somebody else wrote it or even if it's straight from the AS-source :)
Comments & suggestions are always welcome ofcourse.


//---------------------------------------------------------------------------------------------------------------------
// Math utils
//---------------------------------------------------------------------------------------------------------------------
const float asPI = M_PI;
float asRandF() { return float(rand()) / float(RAND_MAX); } // return a random float in range [0..1]
float asAbs(float inVal) { return ::fabs(inVal); }
float asMin(float inA, float inB) { return inA <= inB ? inA : inB; }
float asMax(float inA, float inB) { return inA >= inB ? inA : inB; }
float asFloor(float inVal) { return float(floor(inVal)); }
float asCeil(float inVal) { return float(ceil(inVal)); }
float asRound(float inVal) { return float(floor(inVal + 0.5f)); }
float asClamp(float inVal, float inMin, float inMax) { return (inVal < inMin ? inMin : (inVal > inMax ? inMax : inVal)); }


void as_RegisterMathUtils(asIScriptEngine * outEngine)
{
// stupidity check
if (outEngine == NULL) return;

// A few common math functions, taken directly from the C runtime library
int r;
r = outEngine->RegisterGlobalProperty("const float PI", const_cast<float *>(&asPI)); assert(r >= 0);
r = outEngine->RegisterGlobalFunction("float Abs(float)", asFUNCTION(asAbs), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Sin(float)", asFUNCTION(::sinf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Cos(float)", asFUNCTION(::cosf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Sqrt(float)", asFUNCTION(::sqrtf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float ArcSin(float)", asFUNCTION(::asinf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float ArcCos(float)", asFUNCTION(::acosf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Tan(float)", asFUNCTION(::tanf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float ArcTan(float)", asFUNCTION(::atanf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float ArcTan2(float, float)", asFUNCTION(::atan2f), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Log(float)", asFUNCTION(::logf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Pow(float, float)", asFUNCTION(::powf), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Rand()", asFUNCTION(asRandF), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Min(float, float)", asFUNCTION(asMin), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Max(float, float)", asFUNCTION(asMax), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Floor(float)", asFUNCTION(asFloor), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Ceil(float)", asFUNCTION(asCeil), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Round(float)", asFUNCTION(asRound), asCALL_CDECL); assert(r>=0);
r = outEngine->RegisterGlobalFunction("float Clamp(float, float, float)", asFUNCTION(asClamp), asCALL_CDECL); assert(r>=0);
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement