Archive

Archive for the ‘javascript’ Category

[nodejs] working with big integers

November 15, 2015 Leave a comment

Python
Under Python it’s very easy to work with big integers (called ‘long’ type in Python 2). The programmer doesn’t have to worry about the overflow error, the interpreter takes care of it. For instance, let’s calculate 2128:

>>> 2 ** 128
340282366920938463463374607431768211456

NodeJS
In JavaScript there is just a ‘number’ type, which is floating point. Actually, there is no integer type in JS :) And big integers are not supported, so you need a library if you want to work with huge numbers.

There is a very nice package called big-integer. It has an excellent documentation with lots of examples. Its usage is quite simple, similar to Java:

#!/usr/bin/env node

"use strict";

var bigInt = require("big-integer");

/*
 * 2 ** 128 == 340282366920938463463374607431768211456
 */

function main() {
    console.log(bigInt(2).pow(128).toString());
}

main();
Categories: javascript, nodejs Tags: ,

Dive into JavaScript

November 7, 2015 3 comments

I’ve decided to dive into JavaScript. I had some superficial knowledge of it, but nothing serious. I’ve used Flask for my pet projects, but they should also look nice on the client side. So expect some JavaScript and Node.js related posts in the future. But fear not, my favourite programming language is still Python :)

js

Categories: javascript, nodejs
Design a site like this with WordPress.com
Get started