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


Groups > comp.lang.javascript > #27076

Re: Reach a digit of numerical value in array index.

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Reach a digit of numerical value in array index.
Date 2015-03-19 15:59 +0100
Organization PointedEars Software (PES)
Message-ID <2641901.31sAzN5gEV@PointedEars.de> (permalink)
References <7bb55099-4d9b-46e0-b4b7-3785304be4f4@googlegroups.com> <medgv0$gjb$1@dont-email.me> <49b891b6-283a-45ed-beec-3cdc19983595@googlegroups.com> <9k5lgatqlm6esvojkhsntbcdpk58ma2gka@4ax.com>

Show all headers | View raw


John Harris wrote:

> […] jonas.thornvall@gmail.com wrote:
>> Well i know howto do it, but its insane,
>> i questioning why one can not read/reach the digit place
>> of a number stored in an array, without going via temp variables.
>> There should be a way reading them straight out of the index.

> Why should there be?
> 
> Remember that what's stored are binary numbers. Do you really want to
> fetch a binary digit?

ACK.  That said, there are (at least) three ways to retrieve the decimal 
digit from a Number value without a "temporary variable"; just error-prone 
and not very efficient ones (so kids, don’t do this at home):

a) n.toString().charAt(i)
b) n.toString().slice(-j, -j + 1)
c) (n % Math.pow(10, j)).toString().charAt(0)

where “n” is (a variable storing) the Number value (preferably a numeric 
integer), “i” is (a variable storing) the 0-based index of the digit counted 
from the left, and “j” is (a variable storing) the 1-based index of the 
digit counted from the right.

I do not think I need to explain why these approaches are not very 
efficient.  They are also error-prone because of limited precision of the 
IEEE 754 double-precision floating-point format as such, and of the string 
representation of its values in ECMAScript.

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

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


Thread

Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-17 14:15 -0700
  Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-17 14:34 -0700
    Re: Reach a digit of numerical value in array index. JJ <jj4public@vfemail.net> - 2015-03-18 13:38 +0700
      Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-18 06:57 -0700
  Re: Reach a digit of numerical value in array index. Norman Peelman <npeelman@cfl.rr.com> - 2015-03-18 23:49 -0400
    Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-19 01:16 -0700
      Re: Reach a digit of numerical value in array index. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-19 09:28 +0000
        Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-19 05:00 -0700
          Re: Reach a digit of numerical value in array index. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-19 16:23 +0000
            Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-19 10:43 -0700
              Re: Reach a digit of numerical value in array index. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-19 19:51 +0100
              Re: Reach a digit of numerical value in array index. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-22 11:20 +0000
                Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-22 08:18 -0700
        Re: Reach a digit of numerical value in array index. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-19 15:59 +0100
          Re: Reach a digit of numerical value in array index. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-19 16:13 +0100
          Re: Reach a digit of numerical value in array index. Dr J R Stockton <reply1500@merlyn.demon.co.uk.invalid> - 2015-03-20 23:50 +0000
            Re: Reach a digit of numerical value in array index. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-22 16:29 +0100
              Re: Reach a digit of numerical value in array index. Scott Sauyet <scott@sauyet.com> - 2015-03-22 17:36 -0400
              Re: Reach a digit of numerical value in array index. Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-03-22 21:26 -0300
                Re: Reach a digit of numerical value in array index. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-23 05:54 +0100

csiph-web