gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] gnash ChangeLog server/asobj/Number.cpp


From: Martin Guy
Subject: [Gnash-commit] gnash ChangeLog server/asobj/Number.cpp
Date: Thu, 08 Feb 2007 16:39:37 +0000

CVSROOT:        /sources/gnash
Module name:    gnash
Changes by:     Martin Guy <martinwguy> 07/02/08 16:39:37

Modified files:
        .              : ChangeLog 
        server/asobj   : Number.cpp 

Log message:
        Only do string-to-number conversion when text value is required;
        moved number to text conversion out of class definition into its own 
function;
        added text conversion of NaN and Infinities.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.2276&r2=1.2277
http://cvs.savannah.gnu.org/viewcvs/gnash/server/asobj/Number.cpp?cvsroot=gnash&r1=1.13&r2=1.14

Patches:
Index: ChangeLog
===================================================================
RCS file: /sources/gnash/gnash/ChangeLog,v
retrieving revision 1.2276
retrieving revision 1.2277
diff -u -b -r1.2276 -r1.2277
--- ChangeLog   8 Feb 2007 16:23:28 -0000       1.2276
+++ ChangeLog   8 Feb 2007 16:39:37 -0000       1.2277
@@ -1,3 +1,10 @@
+2007-02-07 Martin Guy <address@hidden>
+
+       * server/asobj/Number.cpp: Only do string-to-number conversion when
+         text value is required, not on every math operation; moved text
+         conversion code out of class definition into its own function.
+       * server/asobj/Number.cpp: Added text conversion of NaN and Infinities.
+
 2007-02-08 Sandro Santilli <address@hidden>
 
        * server/event_id.h: add id() accessor, properly use the id_code enum.

Index: server/asobj/Number.cpp
===================================================================
RCS file: /sources/gnash/gnash/server/asobj/Number.cpp,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- server/asobj/Number.cpp     6 Feb 2007 18:16:41 -0000       1.13
+++ server/asobj/Number.cpp     8 Feb 2007 16:39:37 -0000       1.14
@@ -18,7 +18,7 @@
 //
 //
 
-/* $Id: Number.cpp,v 1.13 2007/02/06 18:16:41 martinwguy Exp $ */
+/* $Id: Number.cpp,v 1.14 2007/02/08 16:39:37 martinwguy Exp $ */
 
 // Implementation of ActionScript Number class.
 
@@ -31,10 +31,12 @@
 #include "builtin_function.h" // need builtin_function
 
 #include <sstream>
+#include <cmath>
 
 namespace gnash {
 
 // Forward declarations
+static void number_val_to_str(double val, char *str);
 //static void number_to_string(const fn_call& fn);
 
 static void
@@ -82,6 +84,32 @@
                as_object(getNumberInterface()),
                _val(val)
        {
+       }
+
+       // override from as_object
+       const char* get_text_value() const
+       {
+               number_val_to_str(_val, _str);
+               return _str;
+       }
+
+       // override from as_object
+       double get_numeric_value() const
+       {
+               return _val;
+       }
+
+       as_value get_primitive_value() const 
+       {
+               return _val;
+       }
+
+};
+
+// Convert numeric value to string value
+static void
+number_val_to_str(double _val, char *_str)
+{
                // Printing formats:
                //
                // If _val > 1, Print up to 15 significant digits, then switch
@@ -152,21 +180,19 @@
                // 0.00009999999999999995 and
                // 0.000009999999999999995 instead.
 
-               if (std::fabs(_val) >= 0.0001 ||
-                   std::fabs(_val) < 0.00001) {
-                       char *cp;
-
-                       sprintf(_str, "%.15g", _val);
-                       // Remove a leading zero from 2-digit exponent if any
-                       if ((cp = strchr(_str, 'e')) != NULL &&
-                           cp[2] == '0') {
-                               // We can't use strcpy() cos its src&dest can't
-                               // overlap. However, this can only be "...e+0n"
-                               // or ...e-0n;  3+digit exponents never have
-                               // leading 0s.
-                               cp[2] = cp[3]; cp[3] = '\0';
-                       }
-               } else {
+       // Handle non-numeric values.
+       // "printf" gives "nan", "inf", "-inf", so we check explicitly
+       switch (std::fpclassify(_val)) {
+       case FP_NAN:
+               strcpy(_str, "NaN");
+               break;
+       case FP_INFINITE:
+               // isinf() cannot be relied on to distinguish -Infinity
+               strcpy(_str, _val < 0 ? "-Infinity" : "Infinity");
+               break;
+       default:        // FP_ZERO, FP_NORMAL and FP_SUBNORMAL
+               if (std::fabs(_val) < 0.0001 &&
+                   std::fabs(_val) >= 0.00001) {
                        // This is the range for which %.15g gives scientific
                        // notation but for which we must give decimal.
                        // We can't easily use %f bcos it prints a fixed number
@@ -180,7 +206,7 @@
                        
                        sprintf(_str, "%.15g", _val * 10.0);
                        if ((cp = strchr(_str, '.')) == NULL || cp[1] != '0') {
-                               log_error("Internal error: cannot find \".0\" 
in %s for %.15g\n", _str, _val);
+                               log_error("Internal error: Cannot find \".0\" 
in \%s for %.15g\n", _str, _val);
                                // Just give it to them raw instead
                                sprintf(_str, "%.15g", _val);
                        } else {
@@ -206,29 +232,23 @@
                                *cp = c;
 #endif
                        }
+               } else {
+                       // Regular case
+                       char *cp;
                        
+                       sprintf(_str, "%.15g", _val);
+                       // Remove a leading zero from 2-digit exponent if any
+                       if ((cp = strchr(_str, 'e')) != NULL &&
+                           cp[2] == '0') {
+                               // We can't use strcpy() cos its src&dest can't
+                               // overlap. However, this can only be "...e+0n"
+                               // or ...e-0n;  3+digit exponents never have
+                               // leading 0s.
+                               cp[2] = cp[3]; cp[3] = '\0';
                }
        }
-
-       // override from as_object
-       const char* get_text_value() const
-       {
-               return _str;
-       }
-
-       // override from as_object
-       double get_numeric_value() const
-       {
-               return _val;
-       }
-
-       as_value get_primitive_value() const 
-       {
-               return get_numeric_value();
        }
-
-};
-
+}
 
 static void
 number_ctor(const fn_call& fn)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]