Abhranil Das
Abhranil Das

Reputation: 5916

Represent tiny numbers in Matlab using sym and vpa

I need to store some really tiny positive and negative numbers in Matlab. One way is to store their signs separately, and the log10 of their magnitude, which lets me store numbers as small as 10^(-realmax)=10^(-10^308), effectively in double precision.

But is there a way to use Matlab's sym and vpa to store numbers as small as this, with the magnitude and sign together, not separately, such that later I can extract their sign, and their order of magnitude, i.e. log10 of their absolute value, in double precision?

I tried something like x=sym('-10^(-1e308)'), but this produces an error.

Upvotes: 0

Views: 40

Answers (1)

Wolfie
Wolfie

Reputation: 30146

The error you get is quite explicit, and tells you to try using str2sym:

Error using sym>convertChar (line 1557)

Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.

Doing that gives us an output of 0, so we should heed the comment from Cris and use 10^ instead of 1e, as well as str2sym:

x=str2sym('-10^(-10^308)')

That gives us a different error:

Error using str2sym (line 66)

Unable to convert string to symbolic expression: Integer too large in context.

This has already been asked about elsewhere

Quoting the MathWorks staff member from the linked post:

This error means the computation is trying to compute something like integer^integer with an exponent larger than 2^32. The resulting integer would take up several Gigabytes of memory and take a long time to compute – and be pretty much useless for virtually all applications. So the symbolic engine flat out refuses to start computing it.


Aside: I would take a moment to consider if you actually require both exponents, that would be an incredibly tiny number (or large if the exponent sign was positive), even 10^(10^10) is large enough for the symbolic math engine to refuse creation...

Upvotes: 2

Related Questions