Ignore:
Timestamp:
Dec 30, 2005, 12:14:12 AM (19 years ago)
Author:
mjs
Message:

Reviewed and committed by Maciej.

(Merged from KJS, original work by Harri Porten)

  • kjs/number_object.cpp: (NumberProtoFunc::callAsFunction): rewrote Number.toString(radix) to work with negative numbers, floating point and very large numbers.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/number_object.cpp

    r11807 r11831  
    22/*
    33 *  This file is part of the KDE libraries
    4  *  Copyright (C) 1999-2000 Harri Porten ([email protected])
     4 *  Copyright (C) 1999-2000,2003 Harri Porten ([email protected])
    55 *
    66 *  This library is free software; you can redistribute it and/or
     
    141141    if (dradix >= 2 && dradix <= 36 && dradix != 10) { // false for NaN
    142142      int radix = static_cast<int>(dradix);
    143       unsigned i = v->toUInt32(exec);
    144       char s[33];
    145       char *p = s + sizeof(s);
    146       *--p = '\0';
     143      const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
     144      // INT_MAX results in 1024 characters left of the dot with radix 2
     145      // give the same space on the right side. safety checks are in place
     146      // unless someone finds a precise rule.
     147      char s[2048 + 3];
     148      double x = v->toNumber(exec);
     149      if (isNaN(x) || isInf(x))
     150        return jsString(UString::from(x));
     151
     152      // apply algorithm on absolute value. add sign later.
     153      bool neg = false;
     154      if (x < 0.0) {
     155        neg = true;
     156        x = -x;
     157      }
     158      // convert integer portion
     159      double f = floor(x);
     160      double d = f;
     161      char *dot = s + sizeof(s) / 2;
     162      char *p = dot;
     163      *p = '\0';
    147164      do {
    148         *--p = "0123456789abcdefghijklmnopqrstuvwxyz"[i % radix];
    149         i /= radix;
    150       } while (i);
     165        *--p = digits[static_cast<int>(fmod(d, radix))];
     166        d /= radix;
     167      } while ((d <= -1.0 || d >= 1.0) && p > s);
     168      // any decimal fraction ?
     169      d = x - f;
     170      const double eps = 0.001; // TODO: guessed. base on radix ?
     171      if (d < -eps || d > eps) {
     172        *dot++ = '.';
     173        do {
     174          d *= radix;
     175          *dot++ = digits[static_cast<int>(d)];
     176          d -= static_cast<int>(d);
     177        } while ((d < -eps || d > eps) && dot - s < static_cast<int>(sizeof(s)) - 1);
     178        *dot = '\0';
     179      }
     180      // add sign if negative
     181      if (neg)
     182        *--p = '-';
    151183      return jsString(p);
    152184    } else
     
    224256              x = cx;
    225257         
    226           decimalAdjust = int(logx);
     258          decimalAdjust = static_cast<int>(logx);
    227259      }
    228260     
     
    311343     
    312344      if (x != 0) {
    313           e = int(log10(x));
     345          e = static_cast<int>(log10(x));
    314346          double n = floor(x / pow(10.0, e - p + 1));
    315347          if (n < pow(10.0, p - 1)) {
Note: See TracChangeset for help on using the changeset viewer.