6

Given the C code below:

int nSum       = 0;
// pNumber is 9109190866037
int nDigits    = strlen(pNumber);
int nParity    = (nDigits-1) % 2;
char cDigit[2] = "\0";
for (int i = nDigits; i > 0 ; i--)
{
  cDigit[0]  = pNumber[i-1];
  int nDigit = atoi(cDigit);

  if (nParity == i % 2) {
    nDigit = nDigit * 2;
  }

  nSum += nDigit/10;
  nSum += nDigit%10;
  printf("NUMBER: %d\n", nSum);
}

Outputs:

NUMBER: 13
NUMBER: 13
NUMBER: 16
NUMBER: 22
NUMBER: 29
NUMBER: 29
NUMBER: 38
NUMBER: 39
NUMBER: 48
NUMBER: 48
NUMBER: 50
NUMBER: 59
NUMBER: 59

And this JavaScript code (written in TypeScript, so there actually is typing involved here as well, but it is mostly inferred):

let nSum = 0;
let nDigits = partialIdNumber.length;
let nParity = (nDigits - 1) % 2;
let cDigit = "\0";
for (let i = nDigits; i > 0; i--) {

  cDigit = partialIdNumber[i - 1];
  let nDigit = parseInt(cDigit);

  if (nParity == i % 2) {
    nDigit  = nDigit * 2;
  }
  nSum += nDigit / 10;
  nSum += nDigit % 10;
  console.log("NUMBER: %d", nSum);
}

Outputs:

NUMBER: 14.3
NUMBER: 14.3
NUMBER: 17.5
NUMBER: 24.1
NUMBER: 31.700000000000003
NUMBER: 31.700000000000003
NUMBER: 41.5
NUMBER: 42.6
NUMBER: 52.4
NUMBER: 52.4
NUMBER: 54.6
NUMBER: 64.5

NOTE: Both these implementations are the same, just different languages.

The C code produces the expected results and JavaScript doesn't.

Questions

  1. What assumptions does JavaScript make in order to produce that output?
  2. What part of my JavaScript code would I have to change to produce the desired output?
Siya Mzam
  • 4,655
  • 1
  • 26
  • 44
  • 1
    The difference is mainly in strict typing in C and type casting in JavaScript. – VisioN Jan 31 '19 at 14:54
  • 7
    In JavaScript, all numbers are implemented as IEEE-754 floating point values. Integers are even implemented as such. See https://stackoverflow.com/a/4703752/47589 –  Jan 31 '19 at 14:56
  • 2
    Javascript division is producing fractional numbers unlike `c` which is performing integer division in case of integer operands. – Eugene Sh. Jan 31 '19 at 14:56
  • 1
    @epascarello: The question you marked this as a duplicate of addresses rounding issues and other aspects of floating-point arithmetic. This question is about the semantics of expressions in C versus JavaScript. In particular, the most relevant information is when integer arithmetic is used versus when floating-point is used, not the details of rounding in floating-point arithmetic. Please do not promiscuously mark questions as duplicates. – Eric Postpischil Jan 31 '19 at 16:00

1 Answers1

3

You need to convert numbers after using division to an integer value.

var partialIdNumber ='9109190866037'

let nSum = 0;
let nDigits = partialIdNumber.length;
let nParity = (nDigits - 1) % 2;
let cDigit = "\0";
for (let i = nDigits; i > 0; i--) {
    cDigit = partialIdNumber[i - 1];
    let nDigit = +cDigit;
    if (nParity == i % 2) {
        nDigit  = nDigit * 2;
    }
    nSum += Math.floor(nDigit / 10); // int
    nSum += nDigit % 10;
    console.log("NUMEBR: %d", nSum);
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392