Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #13674

Re: Javascript Math problem

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Javascript Math problem
Date 2012-04-28 21:10 +0200
Organization albasani.net
Message-ID <jnhfar$7la$1@news.albasani.net> (permalink)
References <jnf7b8$7d0$1@wieslauf.sub.de> <6507721.320.1335573024044.JavaMail.geo-discussion-forums@ynen8> <jnfivf$gj5$1@news.albasani.net> <1416612.AFRqVoOGUt@PointedEars.de>

Show all headers | View raw


On 2012-04-28 13:04, Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
> 
>> On 2012-04-28 02:30, Michael Haufe (TNO) wrote:
>>> Simple enough for the mathematically inclined:
>>> 
>>> function f(n){ return (n-(n%3))/3 }
>> 
>> Ah yes, I forgot to add that one. 
> 
> However, that approach requires three floating-point operations (and one 
> call of a user-defined function), whereas
> 
>   Math.floor(n / 3)
> 
> only requires two (and one property access and one built-in function call).  
> ISTM that therefore the cost and potential rounding error is greater with 
> the former than with the latter, and that the latter should be preferred,
> at least for n >= 0.

I also had the suspicion that rounding could introduce errors here, and
I did a half-hearted search for such a case, but couldn't find one.
Things might get interesting at the 2^32 boundary for bit operations,
and 2^53 for arithmetic operations, but for calculations with numbers of
that magnitude we need to take special precautions anyway.

As for performance, function calls (built-in or user-defined) are
generally much more expensive than numeric operations (unless the
functions are inlined by the compiler). Without testing, I'd expect
(a/b)|0 or (a/b)>>0 to perform best out of the expressions mentioned in
this thread. Math.floor() would still be my choice for the "normal"
case, small positive integers and few repetitions, because the intent of
the code is immediately obvious.

>> In JS, -5.5 % 3 is -2.5 ...
> 
> You mean _in conforming implementations of ECMAScript_, of course.

No, Thomas, I don't. Anywhere else, I would have written "JavaScript"
and people would automatically understand it as a pars pro toto. Only in
this group do I write "JS", because up until now, this has spared me
your there-is-no-javascript replies, which I find incredibly tedious.

You are the only person who ever complains about others using
"JavaScript", "Javascript", "javascript", or "JS". It would be nice if
you could get it into your head that people need a catch-all term for
this language and its variants. When there's a relevant distinction to
be made between engines, dialects, or versions, we'll mention it. I'm
certainly not going to write "conforming implementations of ECMAScript"
every time. If you have a better suggestion, please let us hear it. If
you don't, please accept what has become general usage. You're fighting
against windmills here.

- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Javascript Math problem Tom <thart@vicon.net> - 2012-04-27 15:41 -0700
  Re: Javascript Math problem Stefan Weiss <krewecherl@gmail.com> - 2012-04-28 01:33 +0200
    Re: Javascript Math problem Tom <thart@vicon.net> - 2012-04-27 18:02 -0700
    Re: Javascript Math problem Dr J R Stockton <reply1217@merlyn.demon.co.uk.not.invalid> - 2012-04-29 21:07 +0100
      Re: Javascript Math problem "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-04-30 08:06 +0000
        Re: Javascript Math problem Tom <thart@vicon.net> - 2012-04-30 01:33 -0700
        Re: Javascript Math problem Dr J R Stockton <reply1218@merlyn.demon.co.uk.not.invalid> - 2012-05-01 19:30 +0100
  Re: Javascript Math problem "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-04-27 17:30 -0700
    Re: Javascript Math problem Tom <thart@vicon.net> - 2012-04-27 18:05 -0700
    Re: Javascript Math problem Stefan Weiss <krewecherl@gmail.com> - 2012-04-28 04:00 +0200
      Re: Javascript Math problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-28 13:04 +0200
        Re: Javascript Math problem Stefan Weiss <krewecherl@gmail.com> - 2012-04-28 21:10 +0200
          Re: Javascript Math problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-28 22:23 +0200
            Re: Javascript Math problem "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-04-28 23:39 +0300
            JS/Terminology (was: Javascript Math problem) Stefan Weiss <krewecherl@gmail.com> - 2012-04-29 05:59 +0200
              Re: JS/Terminology Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-29 16:15 +0200
                Re: JS/Terminology Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-29 16:20 +0200
                Re: JS/Terminology Puzzled <scratching.head@example.com> - 2012-05-07 16:22 -0400
                Re: JS/Terminology Gene Wirchenko <genew@ocis.net> - 2012-05-07 14:36 -0700
                Re: JS/Terminology "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-05-08 08:40 +0000
                Re: JS/Terminology Gene Wirchenko <genew@ocis.net> - 2012-05-08 09:29 -0700
            Re: Javascript Math problem John G Harris <john@nospam.demon.co.uk> - 2012-04-29 17:30 +0100
          Re: Javascript Math problem Scott Sauyet <scott.sauyet@gmail.com> - 2012-04-30 06:19 -0700
            Re: Javascript Math problem Dr J R Stockton <reply1218@merlyn.demon.co.uk.not.invalid> - 2012-05-01 19:48 +0100
              Re: Javascript Math problem Gene Wirchenko <genew@ocis.net> - 2012-05-01 19:30 -0700
      Re: Javascript Math problem "Mel Smith" <med_cutout_syntel@aol.com> - 2012-04-28 09:00 -0600
    Re: Javascript Math problem "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-04-28 09:02 +0000

csiph-web