Scalar Multiplication

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

0 votes

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

Thank you Walter Roberson.
So if I use Symbolic Toolbox or "vpi", will this solve my problem and just give me 10^23 as my result. I specifically need the answer to be 10^23 (only 1s and 0s). Also my numbers can go a little higher as well, upto (10^16)*(10^16).
Thank you for your answer.
Regards,
Somesh
Yes, the Symbolic Toolbox should give you 1 followed by 23 zeros. I would think it likely that vpi would as well.
Additional clarification:
IEE 754 double precision can store 10^16, 10^17, ..., 10^22 exactly, but these numbers do not have enough precision to do an operation such as 10^22-1 and get an exact result in the 1's digit. That is, you can't do integral arithmetic with numbers in these ranges exactly. The 10^15 is the largest IEEE 754 double precision number that can be stored exactly *and* have enough precision to do an operation like 10^15-1 exactly. That is, 10^15 is the largest power of 10 such that eps(10^15) <= 1.
Also, a uint64 can go up to 10^19:
>> log10(double(uint64(realmax)))
ans =
19.2659
However, MATLAB does not do the uint64/double conversion correctly for numbers in this range. E.g.,
>> uint64(10^19)
ans =
9999999999999999999
Neither does MSVC 2008 in a mex routine:
>> uint64double(10^19)
ans =
9223372036854775808
So whatever algorithms are used behind the scenes for these conversions do not appear to work correctly beyond some max double value (presumably the largest value for which integral arithmetic can still be done exactly using the underlying algorithm). I haven't investigated what that limit is for the various conversion methods.
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... ?
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
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)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!