adding two quantities

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr John Stockton

    #31
    Re: I have the answer! (was: Re: adding two quantities)

    JRS: In article <bo1evo$153r1a$ 1@ID-174912.news.uni-berlin.de>, seen in
    news:comp.lang. javascript, Fabian <lajzar@hotmail .com> posted at Sun, 2
    Nov 2003 08:16:52 :-[color=blue]
    >Douglas Crockford hu kiteb:
    >[color=green]
    >> You probably meant to write
    >>
    >> var sum = eval(s1 + " + " + s2);
    >>
    >> which is less wrong than your suggestion, but which does two
    >> concatenations before invoking the compiler. This is a very bad way
    >> to perform addition on two strings.[/color]
    >
    >I just checked my manual. Apparently, the following should work:
    >
    >var sum = parseFloat(s1) + parseFloat(s2);
    >
    >Im just a disinterested observer though. I just tested it and it seems
    >to work too. Note the capitalisation is important.[/color]

    One should not choose a method merely on the basis that it seems to
    work. That condition is necessary but not sufficient.

    If there is any prospect of the string having been supplied by an
    unreliable system (including a user) without subsequent full format
    verification, one should consider what happens with invalid strings.
    Different methods give different results.

    F.X0.value = "6.7 tons"
    Q = [parseFloat(F.X0 .value), +F.X0.value] // result : 6.7,NaN

    "3,14159" gives 3,NaN and three is clearly not what was hoped for;
    yet it will give similar numerical results later.

    "0XIDE" gives 0,NaN. "0XE" gives 0,14.

    And

    F.X0.value = "0077"
    Q = [parseInt(F.X0.v alue), +F.X0.value] // result : 63,77

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

    Comment

    • Albert Wagner

      #32
      Re: I have the answer! (was: Re: adding two quantities)

      On Sun, 2 Nov 2003 14:15:07 +0000
      Dr John Stockton <spam@merlyn.de mon.co.uk> wrote:
      <snip>[color=blue]
      > If there is any prospect of the string having been supplied by an
      > unreliable system (including a user) without subsequent full format
      > verification, one should consider what happens with invalid strings.
      > Different methods give different results.[/color]
      <snip>
      Excellent point. What would you recommend? I would suspect that any
      conversion from date, time, or number to string should have a mirror
      method that converts the other direction for comparison.
      --
      Life is an offensive, directed against the repetitious mechanism of the
      Universe.
      --Alfred North Whitehead (1861-1947)

      Comment

      • Dr John Stockton

        #33
        Re: I have the answer! (was: Re: adding two quantities)

        JRS: In article <20031102150501 .6f765cd1.alwag ner@tcac.net>, seen in
        news:comp.lang. javascript, Albert Wagner <alwagner@tcac. net> posted at
        Sun, 2 Nov 2003 15:05:01 :-[color=blue]
        >On Sun, 2 Nov 2003 14:15:07 +0000
        >Dr John Stockton <spam@merlyn.de mon.co.uk> wrote:
        ><snip>[color=green]
        >> If there is any prospect of the string having been supplied by an
        >> unreliable system (including a user) without subsequent full format
        >> verification, one should consider what happens with invalid strings.
        >> Different methods give different results.[/color]
        ><snip>
        >Excellent point. What would you recommend? I would suspect that any
        >conversion from date, time, or number to string should have a mirror
        >method that converts the other direction for comparison.[/color]

        It depends on what s1 and s2 represent.

        Must they be all-digit strings? Is there an upper size limit? Can
        there be a leading + or -? Are leading and/or trailing spaces to be
        allowed? Is Hex to be allowed, or Octal?

        Can one accept E-notation for integer quantities? If a Saturn V costs a
        gigabuck, can one write 1e9 or must one use 1000000000?

        If they can be fractional, what is the separator? How about thousands
        separators? Is 1 000 000 allowed for a million?

        It also depends on how important error actually is.

        I would, generally, verify the format of the string with a RegExp -
        OK = /(d{1,6})/.test(S) // crude example
        Ans = +RegExp.$1

        The RegExp must be tested both for accepting what should be accepted and
        rejecting what should be rejected.

        Note that eval(s1) has the possible advantage of allowing the entry of
        expressions. A short American, asked to enter his height in
        centimetres, may like to be able to type (5*12+3)*2.54 instead of
        doing the arithmetic himself.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        Working...