math with int and floats doesn't work?

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

    math with int and floats doesn't work?

    I could have sworn this would be a FAQ, but it doesn't appear to be in
    there. You must get this all the time, or I'm just totally doing something
    wrong.

    Math with int's and floats appears to just not work at all, much to my
    frustration. :-( I'm trying to write a simple calculation and it just plain
    outputs wrong information. Something like this:

    function calculateValue( kWh) {
    dollar_amount = kWh * .033;
    // dollar_amount = StrS(dollar_amo unt, 1, 2);
    result.innerHTM L = dollar_amount;
    }

    I even jumped through hoops to try to enforce type, and convert strings to
    float format, but it still doesn't work (which is wierd in itself).

    function calculateValue( inputValue) {
    var kWh = new String(inputVal ue);
    kWh = parseFloat(kWh) ;
    dollar_amount = kWh * .033;
    dollar_amount = StrS(dollar_amo unt, 1, 2);
    result.innerHTM L = dollar_amount;
    }

    I've seen float errors in calculators, and other languages, but nothing that
    was just totally wrong everytime by a power of ~10, it's a little insane!

    Eric


  • Lasse Reichstein Nielsen

    #2
    Re: math with int and floats doesn't work?

    "Eric Gibson" <aithien@bellso uth.net> writes:
    [color=blue]
    > I could have sworn this would be a FAQ, but it doesn't appear to be in
    > there. You must get this all the time, or I'm just totally doing something
    > wrong.[/color]

    If your results are so surpricing, my bet is that you are doing
    something wrong. Javascript isn't significantly different from other
    languages in how it handles numbers.
    [color=blue]
    > Math with int's and floats appears to just not work at all, much to my
    > frustration. :-([/color]

    I'm *pretty* sure you are doing something wrong then :).
    [color=blue]
    > I'm trying to write a simple calculation and it just plain outputs
    > wrong information. Something like this:
    >
    > function calculateValue( kWh) {
    > dollar_amount = kWh * .033;
    > // dollar_amount = StrS(dollar_amo unt, 1, 2);
    > result.innerHTM L = dollar_amount;
    > }[/color]

    The three questions that needs answers before anyone can help you:
    What do you do? What is it supposed to do? What does it do?
    The first is almost answered by the code, except: Where is it called
    and with what as an argument?
    [color=blue]
    > I even jumped through hoops to try to enforce type, and convert strings to
    > float format, but it still doesn't work (which is wierd in itself).[/color]

    The multiplication should convert its other arguments to numbers, similar
    to using the Number conversion function.
    [color=blue]
    > function calculateValue( inputValue) {
    > var kWh = new String(inputVal ue);
    > kWh = parseFloat(kWh) ;
    > dollar_amount = kWh * .033;
    > dollar_amount = StrS(dollar_amo unt, 1, 2);
    > result.innerHTM L = dollar_amount;
    > }
    >
    > I've seen float errors in calculators, and other languages, but nothing that
    > was just totally wrong everytime by a power of ~10, it's a little insane![/color]

    I don't know the function StrS, but the rest looks fine.

    If it is off by a factor of 10, are you sure the input is correct, and
    that .033 is the correct multiplyer (and it shouldn't have been 0.33)?

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dr John Stockton

      #3
      Re: math with int and floats doesn't work?

      JRS: In article <tzndb.443$5u5. 357@bignews5.be llsouth.net>, seen in
      news:comp.lang. javascript, Eric Gibson <aithien@bellso uth.net> posted at
      Sat, 27 Sep 2003 17:49:16 :-
      [color=blue]
      >I could have sworn this would be a FAQ, but it doesn't appear to be in
      >there. You must get this all the time, or I'm just totally doing something
      >wrong.
      >
      >Math with int's and floats appears to just not work at all, much to my
      >frustration. :-( I'm trying to write a simple calculation and it just plain
      >outputs wrong information. Something like this:
      >
      >function calculateValue( kWh) {
      > dollar_amount = kWh * .033;
      > // dollar_amount = StrS(dollar_amo unt, 1, 2);
      > result.innerHTM L = dollar_amount;
      >}[/color]

      The arithmetic works for me. I trust that you actually supplied the
      function StrS, and the functions Sign and StrU which it calls.


      One should never write a number with the first or last character a
      decimal point, since that is liable to human error. But, assuming that
      you meant 0.033, that's not your problem; but your electricity is too
      cheap.

      For something like this, you should always give a complete "non-working"
      answer, with data, together with what you wish the function to do and
      the results that you expect.

      --
      © 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

      • Dr John Stockton

        #4
        Re: math with int and floats doesn't work?

        JRS: In article <he2x6f5x.fsf@h otpop.com>, seen in
        news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
        posted at Sun, 28 Sep 2003 00:27:06 :-[color=blue]
        >
        >I don't know the function StrS, but the rest looks fine.
        >[/color]

        Even you should read the FAQ !

        StrS is also in <URL:http://www.merlyn.demo n.co.uk/js-round.htm>.

        --
        © 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...