ejolson,

Nice work. It seems node.js really is your favourite programming language!

Thanks.

Well, depends what I'm doing I guess. Things like JS and even Python are great for getting your head around and playing with some new algorithm without getting distracted by weird syntax and superfluous crap. When you feel the need for speed it's time to recast in C/C++ or whatever.

Just now my other infatuation is Scala. The easy way to describe hardware for FPGAs.

Does

let k = (n / 2) | 0

mean integer division? Does Math.floor do something different? Is there a different notation for integer division?

It's complicated. But simple...

Numbers in JS are 64 bit floats. That means you can do integer correct arithmetic up to 2 to the power 53 - 1. Something like 9007199254740991.

Of course n/2 might not be an integer so what to do:

1) Use Math.floor. That will give you a correct integer value up to 53 bits:

Code: Select all

```
> Number.MAX_SAFE_INTEGER
9007199254740991
> Number.MAX_SAFE_INTEGER / 2
4503599627370495.5
> Math.floor(Number.MAX_SAFE_INTEGER / 2)
4503599627370495
```

2) Use a logical operator like |

Code: Select all

```
> Number.MAX_SAFE_INTEGER
9007199254740991
> Number.MAX_SAFE_INTEGER / 2
4503599627370495.5
> (Number.MAX_SAFE_INTEGER / 2)|0
-1
```

Ouch! What happened there?

Thing is logical operators in JS all work on 32 bit values. So the integer in your float gets chopped to 32 bits then the logical operator applied.

That might sound crazy but it makes a lot of sense. It's efficient in the run time, and it enables one to transpile C/C++ code to JS and have it run very efficiently. See Emscripten.

In the case of my n/2 I think 32 bits is enough for the result.

Thanks for the potential optimization tips. I will look at that later. Now it's back to C++.... God rest my soul.