Eval Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tozeina

    Eval Function

    Can any one help in this please.
    I'm using eval function in JavaScript, But when the eval method return
    a big big number the result will be a number with "E"
    for example :
    Eval(1000000000 000000000000) will result 1e+21
    I need to get the number as it is . can i ??

    Thanks in advance

  • Baconbutty

    #2
    Re: Eval Function

    Generally no.

    Javascript numbers have limited precision (i.e. they can only contain a
    finite number of digits), following the IEEE standards. See:-



    You will need to research arbitrary precision mathematics if you really
    need greater precision.

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Eval Function

      "tozeina" <tozeina@gmail. com> writes:
      [color=blue]
      > I'm using eval function in JavaScript,[/color]

      Probably a bad idea.
      <URL:http://jibbering.com/faq/#FAQ4_40>
      [color=blue]
      >But when the eval method return
      > a big big number the result will be a number with "E"
      > for example :
      > Eval(1000000000 000000000000) will result 1e+21[/color]

      That would be the default string representation of that number for
      your browser. The notation "1e+21" means 1 * 10^21, i.e., 1 with
      21 zeroes after it, which is exactly the number you entered.

      You should also be aware, that numbers that big can't all be represented
      exactly. E.g., (1e+21 == (1e+21 + 1)) gives true, because the 64 bit
      floating point numbers used by Javascript does not have the resolution
      to represent (1e+21 + 1).
      [color=blue]
      > I need to get the number as it is . can i ??[/color]

      You can compute your own string representation of the number.
      Maybe something like:
      ---
      function numberToString( number, base) {
      base = base || 10;
      var res = [];
      while (number > 0) {
      var digit = number % base;
      res.push((digit < 10) ? digit : String.fromChar Code(digit - 10 + 97));
      number = (number - digit) / base;
      }
      return res.reverse().j oin("");
      }
      ---
      Call it as:
      numberToString( 1e+21, 10)
      Make sure base is an integer between 2 and 36!

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Danny

        #4
        Re: Eval Function



        The number should come out as is by default, it should't use the Euler's
        format, I think the issue is in your using eval(), eval() is outputting
        such, instead, skip eval() altogether.


        Danny
        --
        Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

        Comment

        • Dr John Stockton

          #5
          Re: Eval Function

          JRS: In article <1123677937.150 597.85710@f14g2 000cwb.googlegr oups.com>,
          dated Wed, 10 Aug 2005 05:45:37, seen in news:comp.lang. javascript,
          tozeina <tozeina@gmail. com> posted :[color=blue]
          >Can any one help in this please.
          >I'm using eval function in JavaScript, But when the eval method return
          >a big big number the result will be a number with "E"
          >for example :
          >Eval(100000000 0000000000000) will result 1e+21
          >I need to get the number as it is . can i ??[/color]

          No doubt you should not in fact be using eval - see newsgroup FAQ 4.40.

          In javascript, Numbers are stored as IEEE Doubles, a binary floating-
          point format. If you want to see that, which you don't, consider
          <URL:http://www.merlyn.demo n.co.uk/js-misc0.htm#IEEE> .

          If you really do need to express a large number in fixed-point, you will
          need to accept rounding errors (though it may be possible to mask the
          obvious ones) - see BigStrOfMN in
          <URL:http://www.merlyn.demo n.co.uk/js-round.htm#CAI>.

          Alternatively, if you want results accurate past one part in about 1e15,
          you can implement your own arithmetic using arrays of digits to
          represent numbers. I've done it, for Pascal/Delphi integer work, in
          <URL:http://www.merlyn.demo n.co.uk/programs/longcalc.pas>.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...