# Scalar Multiplication

Asked by somesh bandari on 26 May 2012
Latest activity Commented on by James Tursa on 26 May 2012

Hi there, I tried to multiply 1000000000 (9 zeros) and 100000000000000 (14 zeros). The answer ideally should be 1 followed by 23 zeros, but MATLAB is showing 99999999999999992000000.00.

I tried to check by subtracting 1*e23 - 99999999999999992000000.00, the answer was surprisingly zero. This means that 99999999999999992000000.00 is equal to 1*e23.

Can you please explain the reason. Also, I want my answer to display 1 followed by 23 zeros exactly as it has to be. Could you please help me to achieve this.

Regards, Somesh

## Products

Answer by Walter Roberson on 26 May 2012

You need to use the Symbolic Toolbox, or the MATLAB File Exchange contribution "vpi" (John D'Errico)

The immediate problem you are encountering is that 10^23 requires 77 bits to represent, but IEEE 754 double precision floating point numbers have only 53 bits of precision. Switching to uint64() will not work for you because uint64() would have only 64 bits and you need 77 bits.

The largest power of 10 that MATLAB can store exactly in an IEEE 754 double precision number is 10^15.

The largest power of 10 that can be stored exactly in a uint64() unsigned 64 bit integer is 10^18

Walter Roberson on 26 May 2012

Perhaps it is the 2^53 limit, James? I know that just a few releases ago they changed the parser so that uint64() around a decimal constant would be stored correctly, but perhaps it hasn't caught up to the case where the expression inside the uint64() is a constant.

uint64(uint64(10)^19)

I wonder... ?

James Tursa on 26 May 2012

E.g.,

>> num2strexact(10^16)
ans =
1e16
>> num2strexact(10^17)
ans =
1e17
>> num2strexact(10^18)
ans =
1e18
>> num2strexact(10^19)
ans =
1e19
>> num2strexact(10^20)
ans =
1e20
>> num2strexact(10^21)
ans =
1e21
>> num2strexact(10^22)
ans =
1e22
>> num2strexact(10^23)
ans =
9.9999999999999991611392e22

James Tursa on 26 May 2012

Regarding the 2^53 limit, I would say that is a likely guess. I just haven't checked. Also, I would agree that this may not be consistent across different MATLAB versions, particularly when TMW implemented new uint64 arithmetic recently.