On Jul 12, 2013, at 1:48 PM, Mark S. Miller wrote:

> On Fri, Jul 12, 2013 at 11:00 AM, Tab Atkins Jr. <[email protected]> wrote:
> On Fri, Jul 12, 2013 at 10:48 AM, Allen Wirfs-Brock
> <[email protected]> wrote:
> > 
> > In other words you want to define Number.isInteger to return true only if 
> > it's argument is an integer number in the range -(2^53-1)..2^53-1
> >
> > That's a useful test and a plausible definition of Number.isInteger but it 
> > will also probably be surprising to programmers who are familiar with the 
> > esoterics of IEEE floats.
> 
> On the other hand, people (like me) who just want to be able to tell
> when something is a freaking integer will be better served by these
> semantics.  ^_^
> 
> I fully agree. It will also help some programmers avoid the mistake I made -- 
> of including 2**53 in their own home-rolled test.
> 
>  
> 
> > If we went that direction what should Number.toInteger do for values 
> > outside that range?
> 
> For consistency, it should wrap mod 2**53 of course.
> 
> Just kidding. I have no coherent opinion yet of what it should do. What are 
> the use cases we imagine toInteger may be useful for?
> 
> 
Well in the spec.  the equivalent operation is used  in various algorithms that 
access collections that are Uint32 length limited.  For example, accessing 
elements of TypedArrays

For example, checking a a typed array index against the length of the array:

function get(ta, index) {
     let intIndex = Number.toInteger(index);   //why isn't this 
index.toInteger(); ??
     let len = ta.length;
     if (!Number.isFinite(len)) return undefined
     if (intIndex >= len) return undefined;
     if (intIndex < len) return undefined;
     return ta[intIndex];
}

Note that this is a situation where it would be most convenient if toInteger 
just passed through infinities in which case the explicit isFinite test could 
be eliminated.

Also, in the above, I'm assuming the NaN converts to 0.

Allen
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to