Search code examples
javascriptfirefox-addonjsctypes

Doing math on UInt64 is unwrapping it


I have a var blah = ctypes.UInt64('65'), the 65 is really anything. I need to do math on that, but when I do:

console.log(blah * ctypes.UInt64('2') it removes the ctypes.UInt64 wrap. I need to keep that wrap so I can pass it into something else, I know I can re-wrap it, but whats the proper way to do math on UInt64 and Int64 please.


Solution

  • There is no API/library to do math on js-ctypes Int64 types.

    You'll need to unwrap and re-wrap as needed and of course do enough error checking and handling to know when it is safe to unwrap stuff and what math operations might lose precision (>53bit) and/or overflow numbers.

    If you cannot use regular JS double precision float math, because you really need those 64-bit, then you'll need read up on bignum math and how to do it with small-numbers, or find a suitable bignum library. However, the numbers from your example do not require this.

    And avoid doing int64 math in the first place, if possible; but of course, that is not always possible.