0

I have 2 numbers

a = 1548764548675465486;
b = 4535154875433545787;

when I sum these number they are rounded to

a => 1548764548675465500
b => 4535154875433545700

and a + b returns 6083919424109011000 while it should return 6083919424109011273

is there a javascript solution to solve this problem witout the use of a library ?

Khalid
  • 4,730
  • 5
  • 27
  • 50
  • You'd need a 64-bit machine... – Shahar Jul 31 '14 at 02:02
  • does it mean that in a web application, users with 32-bit machines won't get exact results ? – Khalid Jul 31 '14 at 02:03
  • 5
    No, don't listen to @Shahar. JS Numbers are 64-bit double precision floating point numbers regardless of architecture. Without using a library, you'd have to implement BigInteger (arbitrary precision arithemetic) yourself. – univerio Jul 31 '14 at 02:04
  • @Shahar I'm creating a calculator that supports long numbers – Khalid Jul 31 '14 at 02:08
  • 1
    This has nothing to do with summing. Look at the value of `a` after you set it, and you'll see `1548764548675465500`. This is simply a precision issue -- IEEE double-precision floating point (which is what JavaScript numbers always use) can represent at most 15-16 digits of precision. If you want better precision, you need to use something more sophisticated than JavaScript's numeric type. – Joe White Jul 31 '14 at 02:16

3 Answers3

3

To work around the precision limitations associated with JavaScript's numbers, you will need to use a BigInteger library like the popular one offered here: http://silentmatt.com/biginteger/

Usage:

var a = BigInteger("1548764548675465486");
var b = BigInteger("4535154875433545787");
var c = a.add(b);

alert(a.toString() + ' + ' + b.toString() + ' = ' + c.toString());
// Alerts "1548764548675465486 + 4535154875433545787 = 6083919424109011273"

Demo: http://jsfiddle.net/69AEg/1/

techfoobar
  • 65,616
  • 14
  • 114
  • 135
1

There are no integers in Javascript, all numbers are double precision floating point.

That gives you a precision of around 15-16 digits, which is what you are seeing.

as per this question and potential solution i.e. use a library

Personally, I would not use javascript, never been great at numbers. Just try typing 0.1 + 0.2 into any browsers console window. Result is 0.30000000000000004.

Send the calculation to your server side language (as a string) and do the work there, you should have a better outcome.

Technical article on the nuances of floating point numbers here, if you interested

Community
  • 1
  • 1
OJay
  • 4,763
  • 3
  • 26
  • 47
  • The floating point number trouble you point out with `0.1 + 0.2` is true of _any_ language that uses it, not just javaScript... – jdphenix Jul 31 '14 at 02:48
0

Well, here is a solution I found witout the use of any external library, all I need to do is to define a class that had a property value wich should be a string, and define the function plus

    function LongNumber()
    {
        // it takes the argument and remove first zeros
        this.value = arguments[0].toString();
        while(this.value[0]==="0")
            this.value = this.value.substr(1);

        // this function adds the numbers as string to another string and returns result as LongNumber
        this.plus = function (Num)
        {
            var num1 = pad(Num.value.length, this.value);
            var num2 = pad(this.value.length, Num.value);
            var numIndex = num1.length;
            var rest = 0;
            var resultString = "";
            while (numIndex)
            {
                var number1 = parseInt(num1[(numIndex)-1]);
                var number2 = parseInt(num2[(numIndex--)-1]);
                var addition = (number1+number2+rest)%10;
                rest = parseInt((number1+number2+rest)/10);
                resultString = addition.toString() + resultString;
            }
            return new LongNumber((rest?rest.toString():"") + resultString);
        }
        function pad(width, string) 
        { 
            return (width <= string.length) ? string : pad(width, '0' + string)
        }
    }

All i need to do now is to declare 2 LongNombers and use the function plus

var Number1 = new LongNumber("1548764548675465486");
var Number2 = new LongNumber("4535154875433545787");
var Result = Number1.plus(Number2);

Result.value // returns "6083919424109011273"
Khalid
  • 4,730
  • 5
  • 27
  • 50