1

Because of the floating point error 2^(log(63)/log(2)) isn't equal to 63. Check the results below:

format long;
>> 2^(log(63)/log(2))

ans =

  63.000000000000014

And unfortunatelly i can't use vpa on a logarithm according to the Matlab documents:

Unlike exact symbolic values, double-precision values inherently contain round-off errors. When you call vpa on a double-precision input, vpa cannot restore the lost precision, even though it returns more digits than the double-precision value. However, vpa can recognize and restore the precision of expressions of the form p/q, pπ/q, (p/q)1/2, 2q, and 10q, where p and q are modest-sized integers.

So how can i solve this issue ? I have very big numbers like 2^200 and i get very big errors.

Edit: I'm not asking why it is happening. I'm asking how to make this work as 100% accurate so this isn't a duplicate.

The best solution so far:

Unfortunatelly the solution that is suggested by @Sardar_Usama isn't always working as intended. Check the results below:

>> sym(2^(log(2251799813685247)/log(2)))

ans =

2251799813685259

On the other hand

>> 2^(log(vpa(2251799813685247))/log(vpa(2)))

ans =

2.2517998136852470000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0*10^0*10^15

is much much more closer to 2251799813685247 = 2^51. It's error is around ~9.491*10^-494 which makes this the best solution so far but there is still some error.

Kitiara
  • 343
  • 6
  • 21

1 Answers1

4

If you cannot use round or vpa, there is a slower way of dealing this, if you have Symbolic Math Toolbox, by creating symbolic numbers . i.e.

a = sym(2^(log(63)/log(2)))

This will give you sym class 63 which you can later convert to double using:

double(a)

This is what you'll get:

>> format long
>> a = sym(2^(log(63)/log(2)))

a =

63

>> double(a)

ans =

    63
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • Unfortunatelly this isn't always working as intended. Check this out `sym(2^(log(2251799813685247)/log(2)))` = `2251799813685259`. (btw 2251799813685247 is 2^51) On the other hand, `2^(log(vpa(2251799813685247))/log(vpa(2)))` is much much more closer to `2251799813685247`. It's error is around `~2.252*10^-192` which is not causing any trouble for my calculations but there is still some error. – Kitiara Feb 24 '17 at 00:19
  • @Kitiara There are only two options: *symbolic arithmetic* and *numeric arithmetic* (which includes *variable precision* and *double precision*). There *is* no other option. Getting 100% accuracy is not always possible. Read this article: https://www.mathworks.com/help/symbolic/recognize-and-avoid-roundoff-errors.html – Sardar Usama Feb 24 '17 at 06:22