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


Groups > comp.lang.javascript > #18637 > unrolled thread

Why is this returning NaN

Started by"Tony" <tony.johansson@inport.com>
First post2013-02-26 11:54 +0100
Last post2013-02-28 09:19 +0100
Articles 20 on this page of 79 — 17 participants

Back to article view | Back to comp.lang.javascript


Contents

  Why is this returning NaN "Tony" <tony.johansson@inport.com> - 2013-02-26 11:54 +0100
    Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-26 12:09 +0100
    Re: Why is this returning NaN "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-02-26 13:45 +0200
      Re: Why is this returning NaN "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-01 00:45 -0300
        Re: Why is this returning NaN "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-01 01:13 -0300
          Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-01 12:48 +0100
        Re: Why is this returning NaN "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-01 10:06 +0100
        Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-01 12:46 +0100
          Re: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-01 14:19 +0000
          Re: Why is this returning NaN "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br> - 2013-03-01 12:01 -0300
            Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-01 21:03 +0100
              Re: Why is this returning NaN "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br> - 2013-03-01 18:25 -0300
                Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-01 22:35 +0100
                  Re: Why is this returning NaN Patricia Shanahan <pats@acm.org> - 2013-03-01 20:31 -0800
                    Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-02 12:49 +0100
                    Re: Why is this returning NaN "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-03 12:32 -0300
                      Re: Why is this returning NaN "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-03 18:03 +0100
                        Re: Why is this returning NaN Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-03-03 10:16 -0700
                        OT: "javascript" was: Why is this returning NaN Christoph Becker <cmbecker69@gmx.de> - 2013-03-03 20:35 +0100
                          Re: OT: "javascript" was: Why is this returning NaN "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-03-03 21:43 +0200
                          Re: OT: "javascript" was: Why is this returning NaN Tim Streater <timstreater@greenbee.net> - 2013-03-03 19:45 +0000
                          "javascript" (was: OT: "javascript") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-03 21:09 +0100
                            Re: "javascript" "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-03 23:19 -0300
                              Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-04 11:27 +0100
                              Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-04 13:09 +0000
                                Re: "javascript" Christoph Becker <cmbecker69@gmx.de> - 2013-03-10 20:50 +0100
                                  Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-10 20:29 +0000
                                    Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-10 23:23 +0100
                                      Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-10 22:48 +0000
                                        Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-10 23:53 +0100
                                          Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-10 23:23 +0000
                                            Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-11 02:57 +0100
                                              Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-11 08:55 +0000
                                                Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-11 17:38 +0100
                                              Re: "javascript" Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-12 19:29 +0000
                                                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-13 14:36 +1100
                                                  Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-13 15:20 +0000
                                                    Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-14 11:18 +1100
                                                      Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-14 10:43 +0000
                                                        Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-15 07:02 +1100
                                                          Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-15 10:28 +0000
                                                            Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-16 07:17 +1100
                                                              Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-16 12:04 +0000
                                                                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-17 08:17 +1100
                                                                  Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-17 11:13 +0000
                                                                    Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-17 23:15 +1100
                                                                      Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-17 16:42 +0000
                                                                        Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-18 07:47 +1100
                                                                          Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-18 15:01 +0000
                                                                            Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-19 07:34 +1100
                                                                              Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-19 15:04 +0000
                                                                                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-20 07:40 +1100
                                                Re: "javascript" Mike Duffy <Use.web.form@website_in_sig.com> - 2013-03-13 12:03 +0000
                                                  Re: "javascript" Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-14 21:22 +0000
                                                    Re: "javascript" Mike Duffy <Use.web.form@website_in_sig.com> - 2013-03-15 01:44 +0000
                                                      Re: "javascript" Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-16 19:42 +0000
                                                        Re: "javascript" Mike Duffy <Use.web.form@website_in_sig.com> - 2013-03-17 18:25 +0000
                                                          Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-17 19:01 +0000
                                                    Re: "javascript" Gene Wirchenko <genew@telus.net> - 2013-03-15 10:55 -0700
                                            Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-13 15:21 +0000
                            Re: "javascript" (was: OT: "javascript") Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-04 13:07 +0000
                            Re: "javascript" Christoph Becker <cmbecker69@gmx.de> - 2013-03-10 20:34 +0100
                              Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-10 19:39 +0000
                          Re: OT: "javascript" was: Why is this returning NaN "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-03 22:41 -0300
                            Re: OT: "javascript" was: Why is this returning NaN "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-04 09:57 +0100
                            Re: OT: "javascript" was: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-04 11:25 +0100
                            Re: OT: "javascript" was: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-04 14:52 +0000
                              Re: OT: "javascript" was: Why is this returning NaN Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-10 01:02 +0100
                                Re: OT: "javascript" was: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-10 10:52 +0000
                                  Re: OT: "javascript" was: Why is this returning NaN Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-10 12:44 +0100
                                    Re: OT: "javascript" was: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-10 17:49 +0000
                                      Re: OT: "javascript" was: Why is this returning NaN Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-20 01:21 +0100
                                        Re: OT: "javascript" was: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-20 15:25 +0000
                                          Re: OT: "javascript" was: Why is this returning NaN Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-30 01:28 +0100
    Re: Why is this returning NaN Tim Streater <timstreater@greenbee.net> - 2013-02-26 12:00 +0000
      Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-26 18:07 +0100
    Re: Why is this returning NaN Gregor Kofler <usenet@gregorkofler.com> - 2013-02-26 13:42 +0100
    Re: Why is this returning NaN Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-02-27 20:43 +0000
      Re: Why is this returning NaN "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-02-28 09:19 +0100

Page 1 of 4  [1] 2 3 4  Next page →


#18637 — Why is this returning NaN

From"Tony" <tony.johansson@inport.com>
Date2013-02-26 11:54 +0100
SubjectWhy is this returning NaN
Message-ID<kgi46g$ivc$1@dont-email.me>
var diff = Math.abs(new Date() - "2013/02/26 10:24:00");
This should be perfect legal I believe.

//Tony

[toc] | [next] | [standalone]


#18639

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-02-26 12:09 +0100
Message-ID<1679878.FDbs7fZfY5@PointedEars.de>
In reply to#18637
Tony wrote:

> var diff = Math.abs(new Date() - "2013/02/26 10:24:00");
> This should be perfect legal I believe.

Fortunately, your beliefs do not enter into what constitutes proper 
programming.  Go away.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18640

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2013-02-26 13:45 +0200
Message-ID<kgi75q$2lf$1@dont-email.me>
In reply to#18637
2013-02-26 12:54, Tony wrote:

> var diff = Math.abs(new Date() - "2013/02/26 10:24:00");
> This should be perfect legal I believe.

When you use new Date(), you get an object. And "2013/02/26 10:24:00" is 
as such just a string. You cannot perform subtractions on objects and 
strings.

Instead, use e.g.

var now = new Date();
var other = new Date("2013/02/26 10:24:00");
var diff = Math.abs(now.getTime() - other.getTime());

Better, use a suitable library to parse the date stamp or construct the 
Date object using suitable numeric parameters. There is no guarantee 
that all implementations will parse a notation like 2013/02/26 10:24:00 
(the same way).

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

[toc] | [prev] | [next] | [standalone]


#18673

From"J.R." <groups_jr-1@yahoo.com.br>
Date2013-03-01 00:45 -0300
Message-ID<kgp88e$p8o$1@speranza.aioe.org>
In reply to#18640
On 26/02/2013 08:45, Jukka K. Korpela wrote:
> 2013-02-26 12:54, Tony wrote:
>
>> var diff = Math.abs(new Date() - "2013/02/26 10:24:00"); This
>> should be perfect legal I believe.
>
> When you use new Date(), you get an object. And "2013/02/26
> 10:24:00" is as such just a string. You cannot perform subtractions
> on objects and strings.

Yes, you can, because JavaScript (JS) does an implicit coercion with
objects: first JS tries .toString(), then valueOf() methods.

Therefore, the OP is legal because new Date() is implicitly coerced
before subtracting the string. You can test this in console:

new Date() - "250" // is legal and works.

The OP'c code fails because converting new Date() to a string might
produce give a different format than the string being used. E.g. new
Date().toString() results "Fri Mar 01
2013 00:36:34 GMT-0300" on my computer, so it is insane to do:

"Fri Mar 01 2013 00:36:34 GMT-0300" - "2013/02/26 10:24:00" // NaN


> Instead, use e.g.
>
> var now = new Date(); var other = new Date("2013/02/26 10:24:00");
> var diff = Math.abs(now.getTime() - other.getTime());
>
> Better, use a suitable library to parse the date stamp or construct
> the Date object using suitable numeric parameters. There is no
> guarantee that all implementations will parse a notation like
> 2013/02/26 10:24:00 (the same way).
>

ACK.

-- 
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#18674

From"J.R." <groups_jr-1@yahoo.com.br>
Date2013-03-01 01:13 -0300
Message-ID<kgp9sq$spe$1@speranza.aioe.org>
In reply to#18673
On 01/03/2013 00:45, J.R. wrote:
> On 26/02/2013 08:45, Jukka K. Korpela wrote:
>> 2013-02-26 12:54, Tony wrote:
>>
>>> var diff = Math.abs(new Date() - "2013/02/26 10:24:00"); This
>>> should be perfect legal I believe.
>>
>> When you use new Date(), you get an object. And "2013/02/26
>> 10:24:00" is as such just a string. You cannot perform
>> subtractions on objects and strings.
>
> Yes, you can, because JavaScript (JS) does an implicit coercion with
> objects: first JS tries .toString(), then valueOf() methods.
>
> Therefore, the OP is legal because new Date() is implicitly coerced
> before subtracting the string. You can test this in console:
>
> new Date() - "250" // is legal and works.
>
> The OP'c code fails because converting new Date() to a string might
> produce give a different format than the string being used. E.g. new
> Date().toString() results "Fri Mar 01 2013 00:36:34 GMT-0300" on my
> computer, so it is insane to do:
>
> "Fri Mar 01 2013 00:36:34 GMT-0300" - "2013/02/26 10:24:00" // NaN
>

Sorry, a correction is necessary here: the OP's code fails because there
is no meaning in subtracting:

"Fri Mar 01 2013 00:36:34 GMT-0300" - "2013/02/26 10:24:00" // NaN

It is the same as:

"three" - "one" // NaN

But if you do:

"3" - "1" // produces 2.

Anyway, beware of implicit coercions in JavaScript.

>
>> Instead, use e.g.
>>
>> var now = new Date(); var other = new Date("2013/02/26 10:24:00");
>> var diff = Math.abs(now.getTime() - other.getTime());
>>
>> Better, use a suitable library to parse the date stamp or
>> construct the Date object using suitable numeric parameters. There
>> is no guarantee that all implementations will parse a notation
>> like 2013/02/26 10:24:00 (the same way).
>>
>
> ACK.
>


-- 
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#18679

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-03-01 12:48 +0100
Message-ID<18170635.egMJfb6KeS@PointedEars.de>
In reply to#18674
J.R. wrote:

> On 01/03/2013 00:45, J.R. wrote:
>> On 26/02/2013 08:45, Jukka K. Korpela wrote:
>>> 2013-02-26 12:54, Tony wrote:
>>>
>>>> var diff = Math.abs(new Date() - "2013/02/26 10:24:00"); This
>>>> should be perfect legal I believe.
>>
>> […]
> 
> Sorry, a correction is necessary here: the OP's code fails because there
> is no meaning in subtracting:
> 
> "Fri Mar 01 2013 00:36:34 GMT-0300" - "2013/02/26 10:24:00" // NaN

Nonsense.  The operands are converted to _Number_, not to String.
 
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18677

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-03-01 10:06 +0100
Message-ID<XnsA17666DD2FCD7eejj99@194.109.133.133>
In reply to#18673
J.R. wrote on 01 mrt 2013 in comp.lang.javascript:

> new Date() - "250" // is legal and works.

s = new Date() - "250";
document.write(s);

... resulting in 250 milliseconds before now?

No, it does not, 
it returns the present number of miliseconds-250 after zero date:

1362128394463

==========================================

s = new Date() - 2013/03/01;
document.write(s);

... resulting in 671 milliseconds before now?

No, it does not, 
it returns the present number of miliseconds-671 after zero date:

1362128508071

=========================================

s = new Date() - "2013/03/01";
document.write(s);

... resulting in the present number of milliseconds in today?

No, it does not, 
it returns NaN.

[Chrome tested]

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#18678

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-03-01 12:46 +0100
Message-ID<6062718.vzGJ4vAIZo@PointedEars.de>
In reply to#18673
J.R. wrote:

> On 26/02/2013 08:45, Jukka K. Korpela wrote:
>> 2013-02-26 12:54, Tony wrote:
>>> var diff = Math.abs(new Date() - "2013/02/26 10:24:00"); This
>>> should be perfect legal I believe.
>>
>> When you use new Date(), you get an object. And "2013/02/26
>> 10:24:00" is as such just a string. You cannot perform subtractions
>> on objects and strings.
> 
> Yes, you can, because JavaScript (JS) does 

… and any other conforming ECMAScript implementations do … 

> an implicit coercion with objects: first JS tries .toString(), then
> valueOf() methods.

No, this (ToString()) would only happen if the operator was “+”.
 
> Therefore, the OP is legal

The term “legal” is very much misplaced in programming when it does not 
refer to legislation (such as copyright).  A programming language has a 
syntax, a definition of sequences of tokens that a parser accepts.  A 
program is either *syntactically valid* or it is not.  In addition to 
syntax, source code consist of implementations of algorithms.  Source code 
then either makes sense (has meaning/is logical) or it does not (has not/is 
not).

> because new Date() is implicitly coerced before subtracting the string.

No, it is not.

> You can test this in console:
> 
> new Date() - "250" // is legal and works.
                           ^^^^^
(There is that word again.)

But the result is _not_ NaN as it would be with

  (new Date()).toString() - "250"

Instead, it is an integer Number, for example

  -250

for

  new Date(0) - "250"

Because both the Date instance and the primitive String value "250" are 
type-converted to Number by the “-” operator:

(1) §  11.6.2:

    ToNumber(new Date(0)) - ToNumber("250")

(2) §§ 9.3 and 9.3.1:

    ToNumber(ToPrimitive(new Date(0), Number)) - 250

(3) § 9.1:

    ToNumber([[DefaultValue]](new Date(0), Number) - 250

(4) § 8.12.8:

    ToNumber(new Date(0).valueOf()) - 250

(5) § 15.9.6:

    ToNumber(Date.prototype.valueOf.call(new Date(0))) - 250

(6) § 15.9.5.8:

    ToNumber((new Date(0))[[time value]]) - 250

(7) § 15.9.5.8:

    ToNumber(0) - 250

(8) § 9.1:

    0 - 250

(9) § 11.6.2:

    -250
 
> The OP'c code fails because converting new Date() to a string might
> produce give a different format than the string being used. […]

Nonsense.

>> Instead, use e.g.
>>
>> var now = new Date(); var other = new Date("2013/02/26 10:24:00");
>> var diff = Math.abs(now.getTime() - other.getTime());
>>
>> Better, use a suitable library to parse the date stamp or construct
>> the Date object using suitable numeric parameters. There is no
>> guarantee that all implementations will parse a notation like
>> 2013/02/26 10:24:00 (the same way).
> 
> ACK.

A Sith^WWannabe cries for their Master^W^Wlibraries when they get stuck.
A Jedi^W^WThe knowledgable uses the power of the Force^Wlanguages instead:

  var s = "2013/02/26 10:24:00";
  var m = s.match(/^\s*(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)\s*$/);
  var d;

  if (m)
  {
    /* 
     * But see also Function.prototype.construct() in JSX:object.js
     *
     * --m[2];
     * d = Date.construct(m.slice(1));
     */
    d = new Date(m[1], m[2] - 1, m[3], m[4], m[5], m[6]);
  }

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18682

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2013-03-01 14:19 +0000
Message-ID<O8jGfXDLkLMRFwt5@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD>
In reply to#18678
In message <6062718.vzGJ4vAIZo@PointedEars.de>, Thomas 'PointedEars'
Lahn wrote:

  <snip>
>The term “legal” is very much misplaced in programming when it does not
>refer to legislation (such as copyright).
  <snip>

So it's wrong to throw an exception called "Illegal_Operation".

I don't believe you.

Furthermore, ECMA 262 v5 uses the word "illegal" in three places, and
none of them are to do with civil or criminal law.

  John
-- 
John Harris

[toc] | [prev] | [next] | [standalone]


#18683

From"Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br>
Date2013-03-01 12:01 -0300
Message-ID<kgqfsg$vdj$1@speranza.aioe.org>
In reply to#18678
On 01/03/2013 08:46, Thomas 'PointedEars' Lahn wrote:
> J.R. wrote:
>
>> On 26/02/2013 08:45, Jukka K. Korpela wrote:
>>> 2013-02-26 12:54, Tony wrote:
>>>> var diff = Math.abs(new Date() - "2013/02/26 10:24:00"); This
>>>> should be perfect legal I believe.
>>>
>>> When you use new Date(), you get an object. And "2013/02/26
>>> 10:24:00" is as such just a string. You cannot perform
>>> subtractions on objects and strings.
>>
>> Yes, you can, because JavaScript (JS) does
>
> … and any other conforming ECMAScript implementations do …
>
>> an implicit coercion with objects: first JS tries .toString(), then
>> valueOf() methods.
>
> No, this (ToString()) would only happen if the operator was “+”.
>
>> Therefore, the OP is legal
>
> The term “legal” is very much misplaced in programming when it does
> not refer to legislation (such as copyright).  A programming
> language has a syntax, a definition of sequences of tokens that a
> parser accepts.  A program is either *syntactically valid* or it is
> not.  In addition to syntax, source code consist of implementations
> of algorithms.  Source code then either makes sense (has meaning/is
> logical) or it does not (has not/is not).
>
>> because new Date() is implicitly coerced before subtracting the
>> string.
>
> No, it is not.
>
>> You can test this in console:
>>
>> new Date() - "250" // is legal and works.
> ^^^^^ (There is that word again.)
>
> But the result is _not_ NaN as it would be with
>
> (new Date()).toString() - "250"
>
> Instead, it is an integer Number, for example
>
> -250
>
> for
>
> new Date(0) - "250"
>
> Because both the Date instance and the primitive String value "250"
> are type-converted to Number by the “-” operator:
>
> (1) §  11.6.2:
>
> ToNumber(new Date(0)) - ToNumber("250")
>
> (2) §§ 9.3 and 9.3.1:
>
> ToNumber(ToPrimitive(new Date(0), Number)) - 250
>
> (3) § 9.1:
>
> ToNumber([[DefaultValue]](new Date(0), Number) - 250
>
> (4) § 8.12.8:
>
> ToNumber(new Date(0).valueOf()) - 250
>
> (5) § 15.9.6:
>
> ToNumber(Date.prototype.valueOf.call(new Date(0))) - 250
>
> (6) § 15.9.5.8:
>
> ToNumber((new Date(0))[[time value]]) - 250
>
> (7) § 15.9.5.8:
>
> ToNumber(0) - 250
>
> (8) § 9.1:
>
> 0 - 250
>
> (9) § 11.6.2:
>
> -250
>

ACK. Thanks!


>> The OP'c code fails because converting new Date() to a string might
>> produce give a different format than the string being used. […]
>
> Nonsense.

I mistook the subtraction with the "==" operator. Then I made a
correction in my follow-up post, but it still was incomplete (forgive me
as it was past midnight). Okay. Thanks.

>
>>> Instead, use e.g.
>>>
>>> var now = new Date(); var other = new Date("2013/02/26
>>> 10:24:00"); var diff = Math.abs(now.getTime() -
>>> other.getTime());
>>>
>>> Better, use a suitable library to parse the date stamp or
>>> construct the Date object using suitable numeric parameters.
>>> There is no guarantee that all implementations will parse a
>>> notation like 2013/02/26 10:24:00 (the same way).
>>
>> ACK.
>
> A Sith^WWannabe cries for their Master^W^Wlibraries when they get
> stuck. A Jedi^W^WThe knowledgable uses the power of the
> Force^Wlanguages instead:
>
> var s = "2013/02/26 10:24:00"; var m =
> s.match(/^\s*(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)\s*$/); var d;
>
> if (m) { /* * But see also Function.prototype.construct() in
> JSX:object.js * * --m[2]; * d = Date.construct(m.slice(1)); */ d =
> new Date(m[1], m[2] - 1, m[3], m[4], m[5], m[6]); }
>

I think that he was talking about a personal library some developers 
create for their own use; I mean he was advising the use of neither 
date.js nor any other bloated library.

--
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#18684

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-03-01 21:03 +0100
Message-ID<1652233.QWaZaUHVxr@PointedEars.de>
In reply to#18683
Joao Rodrigues (J.R.) wrote:

> On 01/03/2013 08:46, Thomas 'PointedEars' Lahn wrote:
>> J.R. wrote:
>>> The OP'c code fails because converting new Date() to a string might
>>> produce give a different format than the string being used. […]
>> Nonsense.
> 
> I mistook the subtraction with the "==" operator.

If so, there is another misconception on your part.  In short:

1. new Date() == "2013/02/26 10:24:00"
2. ToPrimitive(new Date()) == "2013/02/26 10:24:00"
3. [[DefaultValue]](new Date()) == "2013/02/26 10:24:00"
4. Number(…) == "2013/02/26 10:24:00"
5. Number(…) == ToNumber("2013/02/26 10:24:00")
6. Number(…) == NaN
7. false

(“NaN” is not equal to anything, not even itself.  It can only be detected 
with isNaN(…)).

> Then I made a correction in my follow-up post, but it still was incomplete
> (forgive me as it was past midnight). Okay. Thanks.

Your “correction” was not incomplete; it was wrong.  The result “NaN” with 
the original code also is because the operands of “-” are converted to 
_Number_ (instead of String).  In short:

1. Math.abs(new Date() - "2013/02/26 10:24:00")
2. Math.abs(Number(new Date()) - Number("2013/02/26 10:24:00"))
3. Math.abs(Number(…) - NaN)
4. Math.abs(NaN)
5. NaN

(“NaN” is said to be “toxic”.)

There is another statement that I did not comment on but should have.  You 
said in your “correction”:

| Anyway, beware of implicit coercions in JavaScript.

To which I reply:

No; *learn* about implicit type _conversion_ in ECMAScript and its 
implementations, and *learn to leverage* its expressive power.

No offense meant or taken.
 
>>>> Better, use a suitable library to parse the date stamp or
>>>> construct the Date object using suitable numeric parameters.
>>>> There is no guarantee that all implementations will parse a
>>>> notation like 2013/02/26 10:24:00 (the same way).
>>>
>>> ACK.
>>
>> A Sith^WWannabe cries for their Master^W^Wlibraries when they get
>> stuck. A Jedi^W^WThe knowledgable uses the power of the
>> Force^Wlanguages instead:
>>
>> var s = "2013/02/26 10:24:00"; var m =
>> s.match(/^\s*(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)\s*$/); var d;
>>
>> if (m) { /* * But see also Function.prototype.construct() in
>> JSX:object.js * * --m[2]; * d = Date.construct(m.slice(1)); */ d =
>> new Date(m[1], m[2] - 1, m[3], m[4], m[5], m[6]); }
> 
> I think that he was talking about a personal library some developers
> create for their own use; I mean he was advising the use of neither
> date.js nor any other bloated library.

I do not know what he meant, but I doubt it was a “personal library”.  
Indeed, given his record, it is likely that he did mean such bloat.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18685

From"Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br>
Date2013-03-01 18:25 -0300
Message-ID<kgr6co$5l3$1@speranza.aioe.org>
In reply to#18684
On 01/03/2013 17:03, Thomas 'PointedEars' Lahn wrote:
> Joao Rodrigues (J.R.) wrote:
>
>> On 01/03/2013 08:46, Thomas 'PointedEars' Lahn wrote:
>>> J.R. wrote:
>>>> The OP'c code fails because converting new Date() to a string
>>>> might produce give a different format than the string being
>>>> used. […]
>>> Nonsense.
>>
>> I mistook the subtraction with the "==" operator.
>
> If so, there is another misconception on your part.  In short:

You are inferring what I thought based on what I wrote.

>
> 1. new Date() == "2013/02/26 10:24:00" 2. ToPrimitive(new Date()) ==
> "2013/02/26 10:24:00" 3. [[DefaultValue]](new Date()) == "2013/02/26
> 10:24:00" 4. Number(…) == "2013/02/26 10:24:00" 5. Number(…) ==
> ToNumber("2013/02/26 10:24:00") 6. Number(…) == NaN 7. false
>
> (“NaN” is not equal to anything, not even itself.

Yes!

> It can only be detected with isNaN(…)).

No, isNaN() is not reliable, because it fails badly with other "non NaN" 
or "not coercible to NaN" values, e.g.

isNaN("Lahn"); // true
isNaN({}); // true

So, we would be better off if we used:

function isReallyNaN(x) {
   return x !== x; // “NaN” is not equal to anything, not even itself
}

Guess who wrote "isNaN is broken and should effectively be deprecated"?
<https://mail.mozilla.org/pipermail/es5-discuss/2009-May/002529.html>


>
>> Then I made a correction in my follow-up post, but it still was
>> incomplete (forgive me as it was past midnight). Okay. Thanks.
>
> Your “correction” was not incomplete; it was wrong.  The result “NaN”
> with the original code also is because the operands of “-” are
> converted to _Number_ (instead of String).  In short:
>
> 1. Math.abs(new Date() - "2013/02/26 10:24:00") 2.
> Math.abs(Number(new Date()) - Number("2013/02/26 10:24:00")) 3.
> Math.abs(Number(…) - NaN) 4. Math.abs(NaN) 5. NaN
>
> (“NaN” is said to be “toxic”.)

At the risk of being repetitive...

>
> There is another statement that I did not comment on but should have.
> You said in your “correction”:
>
> | Anyway, beware of implicit coercions in JavaScript.
>
> To which I reply:
>
> No; *learn* about implicit type _conversion_ in ECMAScript and its
> implementations, and *learn to leverage* its expressive power.

That's the OP's case, not mine. However, I have appreciated very much 
all the references to the ECMAScript documentation for every explanation 
of yours. A very good lecture on the subject.

>
> No offense meant or taken.
>

I believe in you, but sometimes your writing sounds a bit rude while you 
do not intend to be so. Anyway, it is always worth to follow-up your 
explanations although it may hurt one's feelings.

>>>>> Better, use a suitable library to parse the date stamp or
>>>>> construct the Date object using suitable numeric parameters.
>>>>> There is no guarantee that all implementations will parse a
>>>>> notation like 2013/02/26 10:24:00 (the same way).
>>>>
>>>> ACK.
>>>
>>> A Sith^WWannabe cries for their Master^W^Wlibraries when they
>>> get stuck. A Jedi^W^WThe knowledgable uses the power of the
>>> Force^Wlanguages instead:
>>>
>>> var s = "2013/02/26 10:24:00"; var m =
>>> s.match(/^\s*(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)\s*$/); var
>>> d;
>>>
>>> if (m) { /* * But see also Function.prototype.construct() in
>>> JSX:object.js * * --m[2]; * d = Date.construct(m.slice(1)); */ d
>>> = new Date(m[1], m[2] - 1, m[3], m[4], m[5], m[6]); }
>>
>> I think that he was talking about a personal library some
>> developers create for their own use; I mean he was advising the use
>> of neither date.js nor any other bloated library.
>
> I do not know what he meant, but I doubt it was a “personal
> library”. Indeed, given his record, it is likely that he did mean
> such bloat.
>

--
Joao Rodrigues (J. R.)

[toc] | [prev] | [next] | [standalone]


#18686

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-03-01 22:35 +0100
Message-ID<3738380.KODd8mRBzQ@PointedEars.de>
In reply to#18685
Joao Rodrigues (J.R.) wrote:

> On 01/03/2013 17:03, Thomas 'PointedEars' Lahn wrote:
>> Joao Rodrigues (J.R.) wrote:
>>> On 01/03/2013 08:46, Thomas 'PointedEars' Lahn wrote:
>>>> J.R. wrote:
>>>>> The OP'c code fails because converting new Date() to a string
>>>>> might produce give a different format than the string being
>>>>> used. […]
>>>> Nonsense.
>>>
>>> I mistook the subtraction with the "==" operator.
>> If so, there is another misconception on your part.  In short:
> 
> You are inferring what I thought based on what I wrote.

Yes.  So?

>> 1. new Date() == "2013/02/26 10:24:00" 2. ToPrimitive(new Date()) ==
>> "2013/02/26 10:24:00" 3. [[DefaultValue]](new Date()) == "2013/02/26
>> 10:24:00" 4. Number(…) == "2013/02/26 10:24:00" 5. Number(…) ==
>> ToNumber("2013/02/26 10:24:00") 6. Number(…) == NaN 7. false
>>
>> (“NaN” is not equal to anything, not even itself.
> 
> Yes!
> 
>> It can only be detected with isNaN(…)).
> 
> No, isNaN() is not reliable, because it fails badly with other "non NaN"
> or "not coercible to NaN" values, e.g.
> 
> isNaN("Lahn"); // true
> isNaN({}); // true
> 
> […]

Straw man.  I did not say that isNaN(…) was reliable with non-Number values; 
only that NaN, a Number value, can only be detected with isNaN(…).

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18688

FromPatricia Shanahan <pats@acm.org>
Date2013-03-01 20:31 -0800
Message-ID<OOidnXA2nqcEHazMnZ2dnUVZ_r-dnZ2d@earthlink.com>
In reply to#18686
On 3/1/2013 1:35 PM, Thomas 'PointedEars' Lahn wrote:
...
> Straw man.  I did not say that isNaN(…) was reliable with non-Number values;
> only that NaN, a Number value, can only be detected with isNaN(…).
>

That is surprising. In environments that did not have an isNaN
equivalent, or when implementing the equivalent of isNaN for a library,
I used to use the property that a NaN is not equal to itself
but all other numbers are:

if(!(x == x))

Does something go wrong with that?

Of course, isNaN is the clearest, most readable way of detecting a NaN,
regardless of whether it is the only way.

Patricia

[toc] | [prev] | [next] | [standalone]


#18689

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-03-02 12:49 +0100
Message-ID<2081372.r8gXD5iYSO@PointedEars.de>
In reply to#18688
Patricia Shanahan wrote:

> On 3/1/2013 1:35 PM, Thomas 'PointedEars' Lahn wrote:
>> […]  I did not say that isNaN(…) was reliable with non-Number
>> values; only that NaN, a Number value, can only be detected with
>> isNaN(…).
> 
> That is surprising. In environments that did not have an isNaN
> equivalent, or when implementing the equivalent of isNaN for a library,
> I used to use the property that a NaN is not equal to itself
> but all other numbers are:
> 
> if(!(x == x))
> 
> Does something go wrong with that?

No, I do not think so.  It appears to be equivalent to

  if (isNaN(x))

based on the premise that the value of x is of the Number type (an 
implementation of IEEE-754 floating-point values).  This can be expressed 
as:

  if (typeof x == "number" && x != x)

(Those who prefer “!==” here may do so with no obvious performance gain but 
with reduced backwards compatibility.)
 
> Of course, isNaN is the clearest, most readable way of detecting a NaN,
> regardless of whether it is the only way.

With the provision that one must make sure first that the value is of the 
Number type, as the ECMAScript isNaN(…) is about whether the argument can be 
*interpreted* as a Number value.  But I have yet to come across a case where 
I would use isNaN(…) and could not reasonably assume that the argument was a 
Number or something used as a Number later on.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18692

From"J.R." <groups_jr-1@yahoo.com.br>
Date2013-03-03 12:32 -0300
Message-ID<kgvqf1$3q0$1@speranza.aioe.org>
In reply to#18688
On 02/03/2013 01:31, Patricia Shanahan wrote:
<snip>
>
> That is surprising. In environments that did not have an isNaN
> equivalent, or when implementing the equivalent of isNaN for a library,
> I used to use the property that a NaN is not equal to itself
> but all other numbers are:
>
> if(!(x == x))
>
> Does something go wrong with that?
>
> Of course, isNaN is the clearest, most readable way of detecting a NaN,
> regardless of whether it is the only way.

Brendan Eich, the creator of JavaScript, wrote about problems with 
isNaN() and isFinite() a few years ago:
<https://mail.mozilla.org/pipermail/es5-discuss/2009-May/002529.html>
and <https://brendaneich.com/>

Number isNaN and isFinite are worthwhile to avoid implicit conversion 
mistakes in specifying the isNaN and isFinite global functions which 
date from ES1 days. For instance:

Number.isNaN("foo"); // false

But:

isNaN("foo"); // true


Then, if you need a generic function for your own library that always 
does the test correctly, then go with isReallyNaN():

function isReallyNaN(x) {
   return x !== x; // “NaN” is unequal to itself
}

-- 
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#18693

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-03-03 18:03 +0100
Message-ID<XnsA178B7B7451BDeejj99@194.109.133.133>
In reply to#18692
J.R. wrote on 03 mrt 2013 in comp.lang.javascript:

> Brendan Eich, the creator of JavaScript, 

According to sources on this NG, 
he cannot have created that
that as it did and does not exist.

Does Brendan Eich exist?

Did Javascript just evolve?

Are these sources wrong?

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#18694

FromMartin Leese <please@see.Web.for.e-mail.INVALID>
Date2013-03-03 10:16 -0700
Message-ID<kh00ed$959$1@dont-email.me>
In reply to#18693
Evertjan. wrote:
> J.R. wrote on 03 mrt 2013 in comp.lang.javascript:
> 
>> Brendan Eich, the creator of JavaScript, 
> 
> According to sources on this NG, 
> he cannot have created that
> that as it did and does not exist.
> 
> Does Brendan Eich exist?
> 
> Did Javascript just evolve?

It is JavaScript that does not exist.  Try
to keep up.

> Are these sources wrong?

They probably don't exist either.  Good
read though.

-- 
Regards,
Martin Leese
E-mail: please@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

[toc] | [prev] | [next] | [standalone]


#18695 — OT: "javascript" was: Why is this returning NaN

FromChristoph Becker <cmbecker69@gmx.de>
Date2013-03-03 20:35 +0100
SubjectOT: "javascript" was: Why is this returning NaN
Message-ID<kh08k8$e5v$1@speranza.aioe.org>
In reply to#18693
Evertjan. wrote:

>> Brendan Eich, the creator of JavaScript, 
> According to sources on this NG, 
> he cannot have created that
> that as it did and does not exist.

You're probably referring to Thomas Lahn as these sources, but he'd
never said, that JavaScript doesn't exist, but that there is neither
javascript nor Javascript.  Note the capitalization.

As ECMAScript is a case-sensitive language, and `JavaScript' and
`javascript' designate different identifiers, one can easily agree with
Thomas.  OTOH it's contradictory to read in this NG's FAQ:

| The term "javascript" is used as a common name for all dialects of
| ECMAScript.[1][2]

[1] <http://jibbering.com/faq/#ecma>
[2] <http://pointedears.de/scripts/faq/cljs/#ecma>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#18696 — Re: OT: "javascript" was: Why is this returning NaN

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2013-03-03 21:43 +0200
SubjectRe: OT: "javascript" was: Why is this returning NaN
Message-ID<kh092f$qjn$1@dont-email.me>
In reply to#18695
2013-03-03 21:35, Christoph Becker wrote:

> As ECMAScript is a case-sensitive language, and `JavaScript' and
> `javascript' designate different identifiers, one can easily agree with
> Thomas.

I hope you are just trying to be ironic, or otherwise funny.

The name of a programming language is not part of the language.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

[toc] | [prev] | [next] | [standalone]


Page 1 of 4  [1] 2 3 4  Next page →

Back to top | Article view | comp.lang.javascript


csiph-web