1
votes

I stumbled over the following strange thing while using matlab's symbolic toolbox

/ >> syms e

/>> y=11111111111111111^e

y = 11111111111111112^e

Seems like there is a limitation when working with large numbers. Can this be solved without changing to a completely different system, like sage?

2

2 Answers

2
votes

I think the problem is that Matlab parses the number into a double before it converts it to a symbolic expression. As a double has a 52-bit mantissa, you have approximately 16 significant digits but your number is longer.

As an alternative, you could try to create the number directly from a string:

y=sym('11111111111111111')^e

Unfortunately, I do not have Matlab available right now, so this answer is untested.

2
votes

It's not a bug, it's a feature. And it's called "round-off error"

matlab uses a double format to store normal variable, just like the double in C programming language, and many other languages like C++.

Actually, the "bug" has nothing to do with the "^x",as we can see:

>> clear
>> syms y
>> format bank
>> y=11111111111111111
y =
 11111111111111112.00

Even a simple assign triggers the "bug".

And we can see how a double variable is really stored in memory in VS, using debug mode:

enter image description here

As you can see in the screenshot, both a and b are stored as "2ea37c58cccccccc" in the memory, which means the computer can't tell one from the other.

And that's the reason of the "bug" you found.

To avoid this, you can use symbolic constant instead:

>> y=sym('11111111111111111')
y =
11111111111111111

In this way, the computer will store the "y" in memory in a different format, which will avoid round-off error and cost more memory.