core/smn_float.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Mar 22 21:50:20 2007 +0000 (2007-03-22)
changeset 653 ef60870067ff
parent 537 3fc99cb190ce
child 831 1995c0dd5c04
permissions -rw-r--r--
More consistency of some sort...

Oh god, what has possessed me to do this?
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include <math.h>
    16 #include <string.h>
    17 #include <stdlib.h>
    18 #include "sm_globals.h"
    19 
    20 /****************************************
    21 *                                       *
    22 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    23 *                                       *
    24 ****************************************/
    25 
    26 
    27 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    28 {
    29 	float val = static_cast<float>(params[1]);
    30 
    31 	return sp_ftoc(val);
    32 }
    33 
    34 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    35 {
    36 	float val = sp_ctof(params[1]);
    37 	val = (val >= 0) ? val : -val;
    38 
    39 	return sp_ftoc(val);
    40 }
    41 
    42 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    43 {
    44 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    45 
    46 	return sp_ftoc(val);
    47 }
    48 
    49 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    50 {
    51 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    52 
    53 	return sp_ftoc(val);
    54 }
    55 
    56 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    57 {
    58 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    59 
    60 	return sp_ftoc(val);
    61 }
    62 
    63 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    64 {
    65 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    66 
    67 	return sp_ftoc(val);
    68 }
    69 
    70 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    71 {
    72 	float val1 = sp_ctof(params[1]);
    73 	float val2 = sp_ctof(params[2]);
    74 
    75 	if (val1 > val2)
    76 	{
    77 		return 1;
    78 	} else if (val1 < val2) {
    79 		return -1;
    80 	}
    81 
    82 	return 0;
    83 }
    84 
    85 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
    86 {
    87 	float val = sp_ctof(params[1]);
    88 	float base = sp_ctof(params[2]);
    89 
    90 	if ((val <= 0) || (base <= 0))
    91 	{
    92 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
    93 	}
    94 	if (base == 10.0)
    95 	{
    96 		val = log10(val);
    97 	} else {
    98 		val = log(val) / log(base);
    99 	}
   100 
   101 	return sp_ftoc(val);
   102 }
   103 
   104 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   105 {
   106 	float val = sp_ctof(params[1]);
   107 
   108 	return sp_ftoc(exp(val));
   109 }
   110 
   111 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   112 {
   113 	float base = sp_ctof(params[1]);
   114 	float exponent = sp_ctof(params[2]);
   115 
   116 	return sp_ftoc(pow(base, exponent));
   117 }
   118 
   119 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   120 {
   121 	float val = sp_ctof(params[1]);
   122 
   123 	if (val < 0.0)
   124 	{
   125 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
   126 	}
   127 
   128 	return sp_ftoc(sqrt(val));
   129 }
   130 
   131 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   132 {
   133 	float val = sp_ctof(params[1]);
   134 
   135 	switch (params[2])
   136 	{
   137 	case 1:
   138 		{
   139 			val = floor(val);
   140 			break;
   141 		}
   142 	case 2:
   143 		{
   144 			val = ceil(val);
   145 			break;
   146 		}
   147 	case 3:
   148 		{
   149 			if (val >= 0.0)
   150 			{
   151 				val = floor(val);
   152 			} else {
   153 				val = ceil(val);
   154 			}
   155 			break;
   156 		}
   157 	default:
   158 		{
   159 			val = (float)floor(val + 0.5);
   160 			break;
   161 		}
   162 	}
   163 
   164 	return static_cast<int>(val);
   165 }
   166 
   167 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   168 {
   169 	float val = sp_ctof(params[1]);
   170 	val = val - floor(val);
   171 
   172 	return sp_ftoc(val);
   173 }
   174 
   175 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   176 {
   177 	float val = sp_ctof(params[1]);
   178 	val = sin(val);
   179 
   180 	return sp_ftoc(val);
   181 }
   182 
   183 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   184 {
   185 	float val = sp_ctof(params[1]);
   186 	val = cos(val);
   187 
   188 	return sp_ftoc(val);
   189 }
   190 
   191 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   192 {
   193 	float val = sp_ctof(params[1]);
   194 	val = tan(val);
   195 
   196 	return sp_ftoc(val);
   197 }
   198 
   199 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   200 {
   201 	float val = sp_ctof(params[1]);
   202 	val = asin(val);
   203 
   204 	return sp_ftoc(val);
   205 }
   206 
   207 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   208 {
   209 	float val = sp_ctof(params[1]);
   210 	val = acos(val);
   211 
   212 	return sp_ftoc(val);
   213 }
   214 
   215 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   216 {
   217 	float val = sp_ctof(params[1]);
   218 	val = atan(val);
   219 
   220 	return sp_ftoc(val);
   221 }
   222 
   223 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   224 {
   225 	float val1 = sp_ctof(params[1]);
   226 	float val2 = sp_ctof(params[2]);
   227 	val1 = atan2(val1, val2);
   228 
   229 	return sp_ftoc(val1);
   230 }
   231 
   232 REGISTER_NATIVES(floatnatives)
   233 {
   234 	{"float",			sm_float},
   235 	{"FloatMul",		sm_FloatMul},
   236 	{"FloatDiv",		sm_FloatDiv},
   237 	{"FloatAdd",		sm_FloatAdd},
   238 	{"FloatSub",		sm_FloatSub},
   239 	{"FloatFraction",	sm_FloatFraction},
   240 	{"FloatRound",		sm_FloatRound},
   241 	{"FloatCompare",	sm_FloatCompare},
   242 	{"SquareRoot",		sm_SquareRoot},
   243 	{"Pow",				sm_Pow},
   244 	{"Exponential",		sm_Exponential},
   245 	{"Logarithm",		sm_Logarithm},
   246 	{"Sine",			sm_Sine},
   247 	{"Cosine",			sm_Cosine},
   248 	{"Tangent",			sm_Tangent},
   249 	{"FloatAbs",		sm_FloatAbs},
   250 	{"ArcTangent",		sm_ArcTangent},
   251 	{"ArcCosine",		sm_ArcCosine},
   252 	{"ArcSine",			sm_ArcSine},
   253 	{"ArcTangent2",		sm_ArcTangent2},
   254 	{NULL,				NULL}
   255 };