core/smn_float.cpp
author Borja Ferrer <faluco@alliedmods.net>
Fri Jan 05 00:42:22 2007 +0000 (2007-01-05)
changeset 268 d6fed4f05e07
parent 260 110d1d014b0c
child 351 7c219cde57e7
permissions -rw-r--r--
Fixed CPlayer init, it was called too early.
nothing important on the logger
     1 #include <math.h>
     2 #include <string.h>
     3 #include "sm_autonatives.h"
     4 #include "sp_vm_api.h"
     5 #include "sp_typeutil.h"
     6 
     7 using namespace SourcePawn;
     8 
     9 /****************************************
    10 *                                       *
    11 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    12 *                                       *
    13 ****************************************/
    14 
    15 
    16 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    17 {
    18 	float val = static_cast<float>(params[1]);
    19 
    20 	return sp_ftoc(val);
    21 }
    22 
    23 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    24 {
    25 	float val = sp_ctof(params[1]);
    26 	val = (val >= 0) ? val : -val;
    27 
    28 	return sp_ftoc(val);
    29 }
    30 
    31 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    32 {
    33 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    34 
    35 	return sp_ftoc(val);
    36 }
    37 
    38 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    39 {
    40 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    41 
    42 	return sp_ftoc(val);
    43 }
    44 
    45 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    46 {
    47 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    48 
    49 	return sp_ftoc(val);
    50 }
    51 
    52 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    53 {
    54 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    55 
    56 	return sp_ftoc(val);
    57 }
    58 
    59 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    60 {
    61 	float val1 = sp_ctof(params[1]);
    62 	float val2 = sp_ctof(params[2]);
    63 
    64 	if (val1 > val2)
    65 	{
    66 		return 1;
    67 	} else if (val1 < val2) {
    68 		return -1;
    69 	}
    70 
    71 	return 0;
    72 }
    73 
    74 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
    75 {
    76 	float val = sp_ctof(params[1]);
    77 	float base = sp_ctof(params[2]);
    78 
    79 	if ((val <= 0) || (base <= 0))
    80 	{
    81 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f).", val, base);
    82 	}
    83 	if (base == 10.0)
    84 	{
    85 		val = log10(val);
    86 	} else {
    87 		val = log(val) / log(base);
    88 	}
    89 
    90 	return sp_ftoc(val);
    91 }
    92 
    93 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
    94 {
    95 	float val = sp_ctof(params[1]);
    96 
    97 	return sp_ftoc(exp(val));
    98 }
    99 
   100 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   101 {
   102 	float base = sp_ctof(params[1]);
   103 	float exponent = sp_ctof(params[2]);
   104 
   105 	return sp_ftoc(pow(base, exponent));
   106 }
   107 
   108 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   109 {
   110 	float val = sp_ctof(params[1]);
   111 
   112 	if (val < 0.0)
   113 	{
   114 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f).", val);
   115 	}
   116 
   117 	return sp_ftoc(sqrt(val));
   118 }
   119 
   120 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   121 {
   122 	float val = sp_ctof(params[1]);
   123 
   124 	switch (params[2])
   125 	{
   126 	case 1:
   127 		{
   128 			val = floor(val);
   129 			break;
   130 		}
   131 	case 2:
   132 		{
   133 			val = ceil(val);
   134 			break;
   135 		}
   136 	case 3:
   137 		{
   138 			if (val >= 0.0)
   139 			{
   140 				val = floor(val);
   141 			} else {
   142 				val = ceil(val);
   143 			}
   144 			break;
   145 		}
   146 	default:
   147 		{
   148 			val = (float)floor(val + 0.5);
   149 			break;
   150 		}
   151 	}
   152 
   153 	return static_cast<int>(val);
   154 }
   155 
   156 static cell_t sm_FloatStr(IPluginContext *pCtx, const cell_t *params)
   157 {
   158 	char *str;
   159 
   160 	pCtx->LocalToString(params[1], &str);
   161 	if (!strlen(str))
   162 	{
   163 		return 0;
   164 	}
   165 
   166 	return sp_ftoc((float)atof(str));
   167 }
   168 
   169 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   170 {
   171 	float val = sp_ctof(params[1]);
   172 	val = val - floor(val);
   173 
   174 	return sp_ftoc(val);
   175 }
   176 
   177 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   178 {
   179 	float val = sp_ctof(params[1]);
   180 	val = sin(val);
   181 
   182 	return sp_ftoc(val);
   183 }
   184 
   185 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   186 {
   187 	float val = sp_ctof(params[1]);
   188 	val = cos(val);
   189 
   190 	return sp_ftoc(val);
   191 }
   192 
   193 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   194 {
   195 	float val = sp_ctof(params[1]);
   196 	val = tan(val);
   197 
   198 	return sp_ftoc(val);
   199 }
   200 
   201 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   202 {
   203 	float val = sp_ctof(params[1]);
   204 	val = asin(val);
   205 
   206 	return sp_ftoc(val);
   207 }
   208 
   209 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   210 {
   211 	float val = sp_ctof(params[1]);
   212 	val = acos(val);
   213 
   214 	return sp_ftoc(val);
   215 }
   216 
   217 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   218 {
   219 	float val = sp_ctof(params[1]);
   220 	val = atan(val);
   221 
   222 	return sp_ftoc(val);
   223 }
   224 
   225 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   226 {
   227 	float val1 = sp_ctof(params[1]);
   228 	float val2 = sp_ctof(params[2]);
   229 	val1 = atan2(val1, val2);
   230 
   231 	return sp_ftoc(val1);
   232 }
   233 
   234 REGISTER_NATIVES(floatnatives)
   235 {
   236 	{"float",			sm_float},
   237 	{"FloatStr",		sm_FloatStr},
   238 	{"FloatMul",		sm_FloatMul},
   239 	{"FloatDiv",		sm_FloatDiv},
   240 	{"FloatAdd",		sm_FloatAdd},
   241 	{"FloatSub",		sm_FloatSub},
   242 	{"FloatFraction",	sm_FloatFraction},
   243 	{"FloatRound",		sm_FloatRound},
   244 	{"FloatCompare",	sm_FloatCompare},
   245 	{"SquareRoot",		sm_SquareRoot},
   246 	{"Pow",				sm_Pow},
   247 	{"Exponential",		sm_Exponential},
   248 	{"Logarithm",		sm_Logarithm},
   249 	{"Sine",			sm_Sine},
   250 	{"Cosine",			sm_Cosine},
   251 	{"Tangent",			sm_Tangent},
   252 	{"FloatAbs",		sm_FloatAbs},
   253 	{"ArcTangent",		sm_ArcTangent},
   254 	{"ArcCosine",		sm_ArcCosine},
   255 	{"ArcSine",			sm_ArcSine},
   256 	{"ArcTangent2",		sm_ArcTangent2},
   257 	{NULL,				NULL}
   258 };