Scalar Multiplication

5 views (last 30 days)
somesh bandari
somesh bandari 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

Accepted Answer

Walter Roberson
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
  6 Comments
James Tursa
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
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.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!