Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #28707 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2015-09-25 07:31 -0700 |
| Last post | 2015-09-27 09:02 -0700 |
| Articles | 20 on this page of 44 — 8 participants |
Back to article view | Back to comp.lang.javascript
How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-25 07:31 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-25 17:09 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-25 14:43 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 08:54 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-09-30 15:04 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-30 21:13 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-10-01 10:44 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-01 13:56 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? John Harris <niam@jghnorth.org.uk.invalid> - 2015-10-02 11:09 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-02 13:55 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-02 16:34 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Danny <dann90038@gmail.com> - 2015-09-25 15:11 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-25 17:50 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 08:59 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 04:25 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 15:00 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 06:15 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 15:34 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 08:06 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Tim Streater <timstreater@greenbee.net> - 2015-09-26 18:10 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 22:53 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Tim Streater <timstreater@greenbee.net> - 2015-09-26 22:37 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-27 10:05 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-27 08:42 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Tim Streater <timstreater@greenbee.net> - 2015-09-27 17:18 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-26 10:59 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 13:15 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-26 14:58 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 16:57 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 04:28 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 15:11 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-09-26 17:19 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 22:51 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-09-27 13:50 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-26 20:51 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-28 22:45 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-29 12:36 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-29 12:48 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-09-26 14:43 +0200
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 06:06 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? John Harris <niam@jghnorth.org.uk.invalid> - 2015-09-27 14:06 +0100
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-27 08:50 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-27 08:53 -0700
Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-27 09:02 -0700
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-09-26 22:53 +0200 |
| Message-ID | <XnsA521E8EF9A070eejj99@194.109.6.166> |
| In reply to | #28735 |
Tim Streater <timstreater@greenbee.net> wrote on 26 Sep 2015 in comp.lang.javascript: > In article <XnsA5219E7CBAC2eejj99@194.109.6.166>, Evertjan. > <exxjxw.hannivoort@inter.nl.net> wrote: > >>You should be glad that Javascript does not crash or error out, >>but gives you the NaN-value, but it stays bad programming. > > Personally I'd prefer if it were possible to have javascript give an > exception when NaN is generated, for debugging purposes. if (isNaN(...)) Xception(); -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2015-09-26 22:37 +0100 |
| Message-ID | <260920152237182932%timstreater@greenbee.net> |
| In reply to | #28738 |
In article <XnsA521E8EF9A070eejj99@194.109.6.166>, Evertjan. <exxjxw.hannivoort@inter.nl.net> wrote: >Tim Streater <timstreater@greenbee.net> wrote on 26 Sep 2015 in >comp.lang.javascript: > >> In article <XnsA5219E7CBAC2eejj99@194.109.6.166>, Evertjan. >> <exxjxw.hannivoort@inter.nl.net> wrote: >> >>>You should be glad that Javascript does not crash or error out, >>>but gives you the NaN-value, but it stays bad programming. >> >> Personally I'd prefer if it were possible to have javascript give an >> exception when NaN is generated, for debugging purposes. > >if (isNaN(...)) Xception(); Very funny. So every time I'm about to modify a variable containing what ought be be a non-NaN value, and after every such computation, I need to check all variables for the NaN value, e.g. if (isNaN(a)) Xception (); if (isNaN(b)) Xception (); c= a + b; if (isNaN(c)) Xception (); and so on. That what you're saying ?? -- "Please stop telling us what you feel. Please stop telling us what your intuition is. Your intuitive feelings are of no interest whatsoever, and nor are mine. I don't give a bugger what you feel, or what I feel. I want to know what the evidence shows." -- Richard Dawkins
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-09-27 10:05 +0200 |
| Message-ID | <XnsA52266AC5331Aeejj99@194.109.6.166> |
| In reply to | #28739 |
Tim Streater <timstreater@greenbee.net> wrote on 26 Sep 2015 in
comp.lang.javascript:
> In article <XnsA521E8EF9A070eejj99@194.109.6.166>, Evertjan.
> <exxjxw.hannivoort@inter.nl.net> wrote:
>
>>Tim Streater <timstreater@greenbee.net> wrote on 26 Sep 2015 in
>>comp.lang.javascript:
>>
>>> In article <XnsA5219E7CBAC2eejj99@194.109.6.166>, Evertjan.
>>> <exxjxw.hannivoort@inter.nl.net> wrote:
>>>
>>>>You should be glad that Javascript does not crash or error out,
>>>>but gives you the NaN-value, but it stays bad programming.
>>>
>>> Personally I'd prefer if it were possible to have javascript give an
>>> exception when NaN is generated, for debugging purposes.
>>
>>if (isNaN(...)) Xception();
>
> Very funny. So every time I'm about to modify a variable containing
> what ought be be a non-NaN value, and after every such computation, I
> need to check all variables for the NaN value, e.g.
>
> if (isNaN(a)) Xception ();
> if (isNaN(b)) Xception ();
These two are not necessary,
they will be automagically catched by the third.
> c= a + b;
> if (isNaN(c)) Xception ();
>
> and so on. That what you're saying ??
Yes, because Javascript is not a faith,
that the user [you] can change to your desire.
You will have to do with what the different engine-builders
rightly or wrongly did with the W3-concepts.
Ofcourse you can submit your idea's,
but it will take perhaps 10 years for them to surface, if at all.
===================
However, you could build your own function,
perhaps something like:
function add(x,y) {
var temp = x + y
if (isNaN(temp)) Xception ();
return temp;
};
===================
I would think it is not that difficult to KNOW,
that a and b are legitimate numbers,
unless they are user-input,
in which case they would have to be pre-tested anyway.
The case posted by Jonas, that a NaN could appear unexpectedly from his own
code, seems ludicrous to me. If you cannot trust your own code, better not
write it, or think and build more modular.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-09-27 08:42 -0700 |
| Message-ID | <811e9b74-c010-4468-bde8-980a5611e88b@googlegroups.com> |
| In reply to | #28741 |
Den söndag 27 september 2015 kl. 10:05:46 UTC+2 skrev Evertjan.:
> Tim Streater <timstreater@greenbee.net> wrote on 26 Sep 2015 in
> comp.lang.javascript:
>
> > In article <XnsA521E8EF9A070eejj99@194.109.6.166>, Evertjan.
> > <exxjxw.hannivoort@inter.nl.net> wrote:
> >
> >>Tim Streater <timstreater@greenbee.net> wrote on 26 Sep 2015 in
> >>comp.lang.javascript:
> >>
> >>> In article <XnsA5219E7CBAC2eejj99@194.109.6.166>, Evertjan.
> >>> <exxjxw.hannivoort@inter.nl.net> wrote:
> >>>
> >>>>You should be glad that Javascript does not crash or error out,
> >>>>but gives you the NaN-value, but it stays bad programming.
> >>>
> >>> Personally I'd prefer if it were possible to have javascript give an
> >>> exception when NaN is generated, for debugging purposes.
> >>
> >>if (isNaN(...)) Xception();
> >
> > Very funny. So every time I'm about to modify a variable containing
> > what ought be be a non-NaN value, and after every such computation, I
> > need to check all variables for the NaN value, e.g.
> >
> > if (isNaN(a)) Xception ();
> > if (isNaN(b)) Xception ();
>
> These two are not necessary,
> they will be automagically catched by the third.
>
> > c= a + b;
> > if (isNaN(c)) Xception ();
> >
> > and so on. That what you're saying ??
>
> Yes, because Javascript is not a faith,
> that the user [you] can change to your desire.
>
> You will have to do with what the different engine-builders
> rightly or wrongly did with the W3-concepts.
>
> Ofcourse you can submit your idea's,
> but it will take perhaps 10 years for them to surface, if at all.
>
> ===================
>
> However, you could build your own function,
> perhaps something like:
>
> function add(x,y) {
> var temp = x + y
> if (isNaN(temp)) Xception ();
> return temp;
> };
>
> ===================
>
> I would think it is not that difficult to KNOW,
> that a and b are legitimate numbers,
> unless they are user-input,
> in which case they would have to be pre-tested anyway.
>
> The case posted by Jonas, that a NaN could appear unexpectedly from his own
> code, seems ludicrous to me. If you cannot trust your own code, better not
> write it, or think and build more modular.
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
I do not know what is the right approach, maybe you can tell i see no way to work around the check? You can not know the carry operations before doing the arithmetic.
/*ADD TWO VARIABLES USING CHOSEN BASE*/
function naiveAdd(base, addOne, addTwo)
{
abigArr = addOne.length;
asmallArr = addTwo.length;
addResult = [];
remainder = 0;
for (i = 0; i < asmallArr; i ++ )
{
addOne[i] = parseInt(addOne[i]);
addTwo[i] = parseInt(addTwo[i]);
addOne[i] = addOne[i] + addTwo[i] + remainder;
if (addOne[i] >= base)
{
addOne[i] = addOne[i] - base;
remainder = 1;
}
else
{
remainder = 0;
}
}
//If strings of equal length but there is a remainder;
while (remainder == 1 && addOne[i]==(base-1))
{
addOne[i] = 0;
i++;
}
if (remainder == 1) {if (isNaN(addOne[i])) addOne[i] = 0;addOne[i] = addOne[i]+1;}
return addOne;
}
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2015-09-27 17:18 +0100 |
| Message-ID | <270920151718406007%timstreater@greenbee.net> |
| In reply to | #28741 |
In article <XnsA52266AC5331Aeejj99@194.109.6.166>, Evertjan. <exxjxw.hannivoort@inter.nl.net> wrote: >Tim Streater <timstreater@greenbee.net> wrote on 26 Sep 2015 in >comp.lang.javascript: > >> In article <XnsA521E8EF9A070eejj99@194.109.6.166>, Evertjan. >> <exxjxw.hannivoort@inter.nl.net> wrote: >> >>>Tim Streater <timstreater@greenbee.net> wrote on 26 Sep 2015 in >>>comp.lang.javascript: >>> >>>> In article <XnsA5219E7CBAC2eejj99@194.109.6.166>, Evertjan. >>>> <exxjxw.hannivoort@inter.nl.net> wrote: >>>> >>>>>You should be glad that Javascript does not crash or error out, >>>>>but gives you the NaN-value, but it stays bad programming. >>>> >>>> Personally I'd prefer if it were possible to have javascript give an >>>> exception when NaN is generated, for debugging purposes. >>> >>>if (isNaN(...)) Xception(); >> >> Very funny. So every time I'm about to modify a variable containing >> what ought be be a non-NaN value, and after every such computation, I >> need to check all variables for the NaN value, e.g. >> >> if (isNaN(a)) Xception (); >> if (isNaN(b)) Xception (); > >These two are not necessary, >they will be automagically catched by the third. caught and if I haven't tested a and b, then I don't know which of them was faulty. >> c= a + b; >> if (isNaN(c)) Xception (); >You will have to do with what the different engine-builders >rightly or wrongly did with the W3-concepts. Of course. -- "The idea that Bill Gates has appeared like a knight in shining armour to lead all customers out of a mire of technological chaos neatly ignores the fact that it was he who, by peddling second-rate technology, led them into it in the first place." - Douglas Adams
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-09-26 10:59 +0100 |
| Message-ID | <87wpvdec2d.fsf@bsb.me.uk> |
| In reply to | #28716 |
jonas.thornvall@gmail.com writes: > Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny: >> to jonas.t >> >> in short, if the value is an integer, it will never return NaN. >> What if may be happening in your case, is that the object gets >> something else other than a numeric value. >> >> but a solution depends on what the context for all that is >> and you haven't given us context or relevant code of the matter > > Well sometimes the index of the array is empty for the leading > position, it is an carry operation. As yet unassigned positions in an array are undefined but the isNaN function returned true for undefined values. It's likely that you don't have a numeric NaN in addOne[i], but undefined instead. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-09-26 13:15 +0200 |
| Message-ID | <XnsA52186DAF427Deejj99@194.109.6.166> |
| In reply to | #28721 |
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote on 26 Sep 2015 in comp.lang.javascript: >> Well sometimes the index of the array is empty for the leading >> position, it is an carry operation. > > As yet unassigned positions in an array are undefined but the isNaN > function returned true for undefined values. It's likely that you don't > have a numeric NaN in addOne[i], but undefined instead. You are mixing things up. The index[!] of an array can never be "empty", only the array item[!] can be null or unassignesd. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-09-26 14:58 +0100 |
| Message-ID | <87r3lle0z9.fsf@bsb.me.uk> |
| In reply to | #28722 |
"Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: > Ben Bacarisse <ben.usenet@bsb.me.uk> wrote on 26 Sep 2015 in > comp.lang.javascript: > >>> Well sometimes the index of the array is empty for the leading >>> position, it is an carry operation. >> >> As yet unassigned positions in an array are undefined but the isNaN >> function returned true for undefined values. It's likely that you don't >> have a numeric NaN in addOne[i], but undefined instead. > > You are mixing things up. How so? > The index[!] of an array can never be "empty", > only the array item[!] can be null or unassignesd. Yes. I never said they could be. I re-worded what had been said by the OP to get something a bit closer to the truth. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-09-26 16:57 +0200 |
| Message-ID | <XnsA521AC820A22Beejj99@194.109.6.166> |
| In reply to | #28731 |
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote on 26 Sep 2015 in comp.lang.javascript: >> The index[!] of an array can never be "empty", >> only the array item[!] can be null or unassignesd. > > Yes. I never said they could be. I re-worded what had been said by the > OP to get something a bit closer to the truth. ;-) -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-09-26 04:28 -0700 |
| Message-ID | <64092fce-7ef7-48d7-9e0d-27a2220fcfe0@googlegroups.com> |
| In reply to | #28721 |
Den lördag 26 september 2015 kl. 11:59:23 UTC+2 skrev Ben Bacarisse: > jonas.thornvall@gmail.com writes: > > > Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny: > >> to jonas.t > >> > >> in short, if the value is an integer, it will never return NaN. > >> What if may be happening in your case, is that the object gets > >> something else other than a numeric value. > >> > >> but a solution depends on what the context for all that is > >> and you haven't given us context or relevant code of the matter > > > > Well sometimes the index of the array is empty for the leading > > position, it is an carry operation. > > As yet unassigned positions in an array are undefined but the isNaN > function returned true for undefined values. It's likely that you don't > have a numeric NaN in addOne[i], but undefined instead. > > -- > Ben. Yes that is the problem Ben when you use array elements as digits places. If it is an add operation that will carry extra digit you will add to an index where the element may be a NaN value.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-09-26 15:11 +0200 |
| Message-ID | <XnsA5219A84E49A9eejj99@194.109.6.166> |
| In reply to | #28724 |
jonas.thornvall@gmail.com wrote on 26 Sep 2015 in comp.lang.javascript: > Yes that is the problem Ben when you use array elements as digits > places. If it is an add operation that will carry extra digit you will > add to an index where the element may be a NaN value. Wrong. An Array-element may "hold" someting, it "is" not something. There is only one specific NaN-value, like there is only one value 37. What you probably mean is that the element may be EITHER undefined OR hold null OR a string OR an object. When testing with isNaN(element) it will return "true" because isNaN() returns true in all these cases. > If it is an add operation that will carry extra digit you will > add to an index where the element may be a NaN value. You cannot add a digit to an index, what exactly do you mean? Do you ad an element to an array of elements containing "digits"? -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-09-26 17:19 +0200 |
| Message-ID | <4175397.OLgykfH8RQ@PointedEars.de> |
| In reply to | #28728 |
Evertjan. wrote:
> What you probably mean is that the element may be
> EITHER undefined
> OR hold null OR a string OR an object.
>
> When testing with
>
> isNaN(element)
>
> it will return "true" because isNaN() returns true in all these cases.
isNaN("42") === false
isNaN({valueOf: function () { return 42; }}) === false
isNaN({toString: function () { return "42"; }}) === false
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-09-26 22:51 +0200 |
| Message-ID | <XnsA521E8950918Ceejj99@194.109.6.166> |
| In reply to | #28734 |
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 26 Sep 2015 in
comp.lang.javascript:
> Evertjan. wrote:
>
>> What you probably mean is that the element may be
>> EITHER undefined
>> OR hold null OR a string OR an object.
>>
>> When testing with
>>
>> isNaN(element)
>>
>> it will return "true" because isNaN() returns true in all these cases.
>
> isNaN("42") === false
> isNaN({valueOf: function () { return 42; }}) === false
> isNaN({toString: function () { return "42"; }}) === false
Indeed,
"42" being a string,
the other two are objects.
There must be a point of no return somewhere,
because otherwise what would be the point of it all?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-09-27 13:50 +0200 |
| Message-ID | <5607D7F5.2000607@PointedEars.de> |
| In reply to | #28737 |
Evertjan. wrote:
> Thomas 'PointedEars' Lahn […] wrote […]:
>> Evertjan. wrote:
>>> What you probably mean is that the element may be
>>> EITHER undefined
>>> OR hold null OR a string OR an object.
>>>
>>> When testing with
>>>
>>> isNaN(element)
>>>
>>> it will return "true" because isNaN() returns true in all these cases.
>>
>> isNaN("42") === false
>> isNaN({valueOf: function () { return 42; }}) === false
>> isNaN({toString: function () { return "42"; }}) === false
>
> Indeed,
> "42" being a string,
> the other two are objects.
Exactly. (_Object references_, to be precise.)
> There must be a point of no return somewhere,
> because otherwise what would be the point of it all?
I have no idea what you mean.
You did not realize, and my examples show, that isNaN(value) returns “false”
if ToNumber(value) is not NaN. For object references, this is the primitive
representation of the object referred to by “value”, with a preference on
value.valueOf(), and value.toString() as fallback. For string values (which
can also be returned by .toString()) it suffices if “value” can be
interpreted as an expansion of /StringNumericLiteral/. Therefore, in
general your statement is wrong.
Another example:
isNaN(["42"]) === isNaN([42]) === false
<http://www.ecma-international.org/ecma-262/6.0/index.html#sec-isnan-number>
Number.isNaN() was introduced in ECMAScript 2015 because isNaN() does this
conversion and interpretation.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-09-26 20:51 +0100 |
| Message-ID | <87fv21dkmy.fsf@bsb.me.uk> |
| In reply to | #28724 |
jonas.thornvall@gmail.com writes: > Den lördag 26 september 2015 kl. 11:59:23 UTC+2 skrev Ben Bacarisse: >> jonas.thornvall@gmail.com writes: >> >> > Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny: >> >> to jonas.t >> >> >> >> in short, if the value is an integer, it will never return NaN. >> >> What if may be happening in your case, is that the object gets >> >> something else other than a numeric value. >> >> >> >> but a solution depends on what the context for all that is >> >> and you haven't given us context or relevant code of the matter >> > >> > Well sometimes the index of the array is empty for the leading >> > position, it is an carry operation. >> >> As yet unassigned positions in an array are undefined but the isNaN >> function returned true for undefined values. It's likely that you don't >> have a numeric NaN in addOne[i], but undefined instead. >> > > Yes that is the problem Ben when you use array elements as digits > places. If it is an add operation that will carry extra digit you will > add to an index where the element may be a NaN value. You say "yes" but I don't think you got what I said. An as yet unassigned array position is undefined -- it won't have NaN as its value. However, isNaN (the global function by that name) converts it's argument in such a way that undefined becomes NaN. $ nodejs > var a = []; undefined > a[1] undefined > a[1] === undefined true > isNaN(a[1]) This may be why you think you have NaN in the "extra" digit places. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-09-28 22:45 -0700 |
| Message-ID | <7a92063a-1369-4a31-ac81-1d69e0bf8ad8@googlegroups.com> |
| In reply to | #28736 |
Den lördag 26 september 2015 kl. 21:51:43 UTC+2 skrev Ben Bacarisse: > jonas.thornvall@gmail.com writes: > > > Den lördag 26 september 2015 kl. 11:59:23 UTC+2 skrev Ben Bacarisse: > >> jonas.thornvall@gmail.com writes: > >> > >> > Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny: > >> >> to jonas.t > >> >> > >> >> in short, if the value is an integer, it will never return NaN. > >> >> What if may be happening in your case, is that the object gets > >> >> something else other than a numeric value. > >> >> > >> >> but a solution depends on what the context for all that is > >> >> and you haven't given us context or relevant code of the matter > >> > > >> > Well sometimes the index of the array is empty for the leading > >> > position, it is an carry operation. > >> > >> As yet unassigned positions in an array are undefined but the isNaN > >> function returned true for undefined values. It's likely that you don't > >> have a numeric NaN in addOne[i], but undefined instead. > >> > > > > Yes that is the problem Ben when you use array elements as digits > > places. If it is an add operation that will carry extra digit you will > > add to an index where the element may be a NaN value. > > You say "yes" but I don't think you got what I said. An as yet > unassigned array position is undefined -- it won't have NaN as its > value. However, isNaN (the global function by that name) converts it's > argument in such a way that undefined becomes NaN. > > $ nodejs > > var a = []; > undefined > > a[1] > undefined > > a[1] === undefined > true > > isNaN(a[1]) > > This may be why you think you have NaN in the "extra" digit places. > > -- > Ben. From what i understand an array, is a storage bin/stack of items. Now i have a stack of items that in most case do have an initiated element at index, but in some cases it is indeed empty. Since it is integer elements i do perform opeations upon performing carry operations, i think it is kind of weird that there is no good way to deal with this. This must be the most common problem programming statistics. I do not see why i should not be able to add to an undefined value, it is basicly like adding to zero. And since the type of the elements stored are integers, i just can't see the point i have to do a check everytime an assign a zero, it is ridiculous. A suggestion to fill the slot ahead with zeroes do not work either because then the array length will not report correct. It is kind of strange that such operation report NaN back.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-09-29 12:36 +0200 |
| Message-ID | <XnsA524804A0547Ceejj99@194.109.6.166> |
| In reply to | #28753 |
jonas.thornvall@gmail.com wrote on 29 Sep 2015 in comp.lang.javascript: > From what i understand an array, is a storage bin/stack of items. Now i > have a stack of items that in most case do have an initiated element at > index, but in some cases it is indeed empty. Wrong understanding! There are NO empty elements. Javascript arrays are infinite collections of pointers to variable storage, where the pointers are identified by their positive integer index-values Only those elements that are defined exist. var myArray = []; is an empty array, but NONE of the elements are empty, because there ARE NO elements [yet]. myArray.length is 0 here, but is set to the highest defined element, unless specifically set to a higher element. var myArray = []; alert(myArray.length); // 0 myArray[17] = 'b'; alert(myArray.length); // 18 myArray.length = 5555; alert(myArray.length); // 5555 myArray.length = 2; alert(myArray.length); // 2 alert(myArray[17]); // undefined So myArray[17] is NOT empty, it does not EXIST anymore! > Now i have a stack of items Not so, a "stack" [in casu strictior] is a push-pull memory heap, where ONLY the top-most element is ACCESSABLE, and all elements below the top one are EXISTING. Not so in a Javascript array. [You could use an array for simulating a stack, but that is another story.] -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-09-29 12:48 +0100 |
| Message-ID | <87oaglcuoq.fsf@bsb.me.uk> |
| In reply to | #28753 |
jonas.thornvall@gmail.com writes: > Den lördag 26 september 2015 kl. 21:51:43 UTC+2 skrev Ben Bacarisse: <snip> >> ... An as yet >> unassigned array position is undefined -- it won't have NaN as its >> value. However, isNaN (the global function by that name) converts it's >> argument in such a way that undefined becomes NaN. >> >> $ nodejs >> > var a = []; >> undefined >> > a[1] >> undefined >> > a[1] === undefined >> true >> > isNaN(a[1]) >> >> This may be why you think you have NaN in the "extra" digit places. >> > > From what i understand an array, is a storage bin/stack of items. Now > i have a stack of items that in most case do have an initiated element > at index, but in some cases it is indeed empty. Your terminology is wrong. An array is not a stack and the metaphor of describing as yet unused elements as empty is leading you astray. They are nether full nor empty -- an expression that accesses an unknown property of an object has the value called undefined. That's a value like any other, and you can even assign it to an array element -- a[i] may have been set to undefined. > Since it is integer > elements i do perform opeations upon performing carry operations, i > think it is kind of weird that there is no good way to deal with this. There is a way to deal with this. You don't consider it a good way, but there's nothing that can be done about that. > This must be the most common problem programming statistics. I do not > see why i should not be able to add to an undefined value, it is > basicly like adding to zero. And since the type of the elements stored > are integers, i just can't see the point i have to do a check > everytime an assign a zero, it is ridiculous. A suggestion to fill the > slot ahead with zeroes do not work either because then the array > length will not report correct. Not everyone has the same view of what is ridiculous. I think what ECMAScript does here is perfectly sensible. > It is kind of strange that such operation report NaN back. It's a reasonably choice for the language designers to have made. You might prefer to switch to Perl. In Perl, undef() + 1 == 1 although it is considered bad style to rely on that fact. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-09-26 14:43 +0200 |
| Message-ID | <3550184.eD00NEl37L@PointedEars.de> |
| In reply to | #28721 |
Ben Bacarisse wrote:
> jonas.thornvall@gmail.com writes:
>> Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny:
>>> in short, if the value is an integer, it will never return NaN.
>>> What if may be happening in your case, is that the object gets
>>> something else other than a numeric value.
>>>
>>> but a solution depends on what the context for all that is
>>> and you haven't given us context or relevant code of the matter
>>
>> Well sometimes the index of the array is empty for the leading
>> position, it is an carry operation.
It is not the index that is "empty" then, but the array element. The index
is a number used for addressing the elements of an array instead.
> As yet unassigned positions in an array are undefined but the isNaN
> function returned true for undefined values. It's likely that you don't
> have a numeric NaN in addOne[i], but undefined instead.
The Number.isNaN() method of ECMAScript Ed. 6 can solve this problem.
It can be emulated as follows:
if (typeof Number.isNaN != "function")
{
Number.isNaN = function (value) {
return (typeof value == "number") && (value != value);
};
}
This makes use of the fact that the IEEE 754 “Not-a-number” value is the
only Number value that is not equal to itself by definition.
See also, and compare:
<https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN>
PointedEars
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-09-26 06:06 -0700 |
| Message-ID | <07e16207-5a28-4507-a2de-a4ee33184cae@googlegroups.com> |
| In reply to | #28725 |
Den lördag 26 september 2015 kl. 14:43:32 UTC+2 skrev Thomas 'PointedEars' Lahn:
> Ben Bacarisse wrote:
>
> > jonas.thornvall@gmail.com writes:
> >> Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny:
> >>> in short, if the value is an integer, it will never return NaN.
> >>> What if may be happening in your case, is that the object gets
> >>> something else other than a numeric value.
> >>>
> >>> but a solution depends on what the context for all that is
> >>> and you haven't given us context or relevant code of the matter
> >>
> >> Well sometimes the index of the array is empty for the leading
> >> position, it is an carry operation.
>
> It is not the index that is "empty" then, but the array element. The index
> is a number used for addressing the elements of an array instead.
>
> > As yet unassigned positions in an array are undefined but the isNaN
> > function returned true for undefined values. It's likely that you don't
> > have a numeric NaN in addOne[i], but undefined instead.
>
> The Number.isNaN() method of ECMAScript Ed. 6 can solve this problem.
> It can be emulated as follows:
>
> if (typeof Number.isNaN != "function")
> {
> Number.isNaN = function (value) {
> return (typeof value == "number") && (value != value);
> };
> }
>
> This makes use of the fact that the IEEE 754 "Not-a-number" value is the
> only Number value that is not equal to itself by definition.
>
> See also, and compare:
> <https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN>
>
>
> PointedEars
> --
> PointedEars
> FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
> Please do not cc me. / Bitte keine Kopien per E-Mail.
What is the reason for programming languages to not allow adding to null values NaN, it seem like a problem that will come up in many situations. And to me it seem reasonable that the programer expect it to be a carry operation or some arithmetic where the variable not initialised?
What could actually go wrong by allowing add to a NaN, that is presumably a zero *startvalue*, it seem as something unavoidable programming for example statistics?
Jonas Thörnvall
[toc] | [prev] | [next] | [standalone]
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web