Jelajahi Sumber

Add getters for Decimal attributes, when creating Decimal without a value set it to zero.

mingodad 13 tahun lalu
induk
melakukan
1e1c6d6905
2 mengubah file dengan 52 tambahan dan 10 penghapusan
  1. 47 8
      ext/sq_decimal.cpp
  2. 5 2
      samples/test-decimal.nut

+ 47 - 8
ext/sq_decimal.cpp

@@ -184,33 +184,32 @@ static SQRESULT sq_Decimal_release_hook(SQUserPointer p, SQInteger size, HSQUIRR
 static SQRESULT sq_Decimal_constructor (HSQUIRRELVM v) {
     SQ_FUNC_VARS(v);
     GET_Decimal_INSTANCE(v, 1);
+    uint32_t status;
+    mpd_context_t *ctx = sq_get_global_ctx(v, 1);
     if(_top_ > 1){
         switch(sq_gettype(v, 2)){
+/*
             case OT_INSTANCE:{
                 GET_DecimalCtx_INSTANCE(v, 2);
                 dec = mpd_new(ctx);
+                mpd_qset_i32(dec, 0, ctx, &status);
             }
+*/
             case OT_INTEGER:{
                 SQ_GET_INTEGER(v, 2, iparam);
-                mpd_context_t *ctx = sq_get_global_ctx(v, 1);
                 dec = mpd_new(ctx);
-                uint32_t status;
                 mpd_qset_i32(dec, iparam, ctx, &status);
             }
             break;
             case OT_STRING:{
                 SQ_GET_STRING(v, 2, str);
-                mpd_context_t *ctx = sq_get_global_ctx(v, 1);
                 dec = mpd_new(ctx);
-                uint32_t status;
                 mpd_qset_string(dec, str, ctx, &status);
             }
             break;
             case OT_FLOAT:{
                 SQ_GET_FLOAT(v, 2, fparam);
-                mpd_context_t *ctx = sq_get_global_ctx(v, 1);
                 dec = mpd_new(ctx);
-                uint32_t status;
                 char buf[32];
                 snprintf(buf, sizeof(buf), "%f", fparam);
                 mpd_qset_string(dec, buf, ctx, &status);
@@ -218,7 +217,10 @@ static SQRESULT sq_Decimal_constructor (HSQUIRRELVM v) {
             break;
         }
     }
-    else dec = mpd_qnew();
+    else {
+        dec = mpd_new(ctx);
+        mpd_qset_i32(dec, 0, ctx, &status);
+    }
     sq_setinstanceup(v, 1, dec);
     sq_setreleasehook(v, 1, sq_Decimal_release_hook);
     return 1;
@@ -422,7 +424,31 @@ static SQRESULT sq_Decimal_next_toward(HSQUIRRELVM v)
     uint32_t status = 0;
     mpd_qnext_toward(result, dec, dec2, ctx, &status);
 	return sq_Decimal_new_for_dec(v, result, ctx, status);
-}
+}
+
+#define DECIMAL_IS(fn) \
+static SQRESULT sq_Decimal_##fn(HSQUIRRELVM v)\
+{\
+    SQ_FUNC_VARS_NO_TOP(v);\
+    GET_Decimal_INSTANCE(v, 1);\
+    sq_pushbool(v, mpd_##fn(dec));\
+	return 1;\
+}
+
+DECIMAL_IS(isfinite);
+DECIMAL_IS(isinfinite);
+DECIMAL_IS(isnan);
+DECIMAL_IS(isnegative);
+DECIMAL_IS(ispositive);
+DECIMAL_IS(isqnan);
+DECIMAL_IS(issigned);
+DECIMAL_IS(issnan);
+DECIMAL_IS(isspecial);
+DECIMAL_IS(iszero);
+DECIMAL_IS(isinteger);
+DECIMAL_IS(isodd);
+DECIMAL_IS(iseven);
+
 
 #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),sq_Decimal_##name,nparams,tycheck}
 static SQRegFunction Decimal_methods[] =
@@ -443,6 +469,19 @@ static SQRegFunction Decimal_methods[] =
     _DECL_FUNC(next_minus, 1,_SC("x")),
     _DECL_FUNC(next_plus, 1,_SC("x")),
     _DECL_FUNC(next_toward, 2,_SC("xx")),
+    _DECL_FUNC(isfinite, 1,_SC("x")),
+    _DECL_FUNC(isinfinite, 1,_SC("x")),
+    _DECL_FUNC(isnan, 1,_SC("x")),
+    _DECL_FUNC(isnegative, 1,_SC("x")),
+    _DECL_FUNC(ispositive, 1,_SC("x")),
+    _DECL_FUNC(isqnan, 1,_SC("x")),
+    _DECL_FUNC(issigned, 1,_SC("x")),
+    _DECL_FUNC(issnan, 1,_SC("x")),
+    _DECL_FUNC(isspecial, 1,_SC("x")),
+    _DECL_FUNC(iszero, 1,_SC("x")),
+    _DECL_FUNC(isinteger, 1,_SC("x")),
+    _DECL_FUNC(isodd, 1,_SC("x")),
+    _DECL_FUNC(iseven, 1,_SC("x")),
     {0,0}
 };
 #undef _DECL_FUNC

+ 5 - 2
samples/test-decimal.nut

@@ -6,6 +6,9 @@ f1 = 0.1;
 f2 = 0.3;
 print(f1+f1+f1-f2);
 
+local d = Decimal();
+print(d.isnan(), d.iszero(), d.isodd(), d.iseven());
+
 local dec1 = Decimal("1.1");
 local dec2 = Decimal("2.2");
 print(dec1 + dec2)
@@ -56,7 +59,7 @@ for(local i=0; i < count; ++i){
 	result = dec1 * dec2;
 	result = dec1 / dec2;
 	//if(i%1000) collectgarbage();
-	if(i%1000) dummy();
+	if(i%1000) dummy(); //dummy function call to trigger release hook garbage collection
 }
 
 print("Decimal took", os.clock()-now);
@@ -67,7 +70,7 @@ for(local i=0; i < count; ++i){
 	result = f1 - f2;
 	result = f1 * f2;
 	result = f1 / f2;
-	if(i%1000) dummy();
+	if(i%1000) dummy(); // only to make this loop with the same extra load of a function call
 }
 print("Float took", os.clock()-now);