0

I've been using matlab to solve some boundary value problems lately, and I've noticed an annoying quirk. Suppose I start with the interval [0,1], and I want to search inside it. Naturally, one would perform a binary search, so I would subdivide the interval into [0,0.5] and [0.5,1]. Excellent: let's now suppose we narrow down our search to [0.5,1]. Now we divide the interval [0.5,0.75] and [0.75,1]. No apparent problem yet. However, as we keep going, representation of powers of 2 in base 10 becomes less and less natural. For example, 2^-22 in binary is just 22 bits, while in decimal it is 16 digits. However, keep in mind that each digit of decimal is really encoding ~ 4 bits. In other words, representing these fractions as decimal is extremely inefficient.

Matlab's precision only extends to 16 digit decimal floats, so a binary search going to 2^-22 is as good as you can do. However, 2^-22 ~ 10^-7, which is much bigger than 10^-16, so the best search strategy in matlab seems to be a decimal search! In any case, this is what I have done so far: to take full advantage of the 16 digit precision, I've had to subdivide the interval [0,1] into 10 pieces.

Hopefully I've made my problem clear. So, my question is: how do I make matlab count in native binary? I want to work with 64 bit binary floats!

Guy
  • 123
  • 4
  • 1
    16 decimal digits means you can go down to about 10^-16, or 2^-53. You may find [this](https://stackoverflow.com/a/686454/2586922) useful – Luis Mendo Nov 18 '20 at 18:08
  • 2
    MATLAB certainly expresses numbers internally in binary. The decimal representation is only for display. – Cris Luengo Nov 18 '20 at 18:15
  • 3
    Try `format hex`, and then look at your numbers: `2^-22 = 3e90000000000000`, `2^-40 = 3d70000000000000`, etc. Any power of two is represented with only an exponent, the mantissa is always 1. – Cris Luengo Nov 18 '20 at 18:21
  • 1
    _"how do I make matlab count in native binary"_ It already does. Floating point numbers are represented in memory as binary. Not sure what you're asking. – Pranav Hosangadi Nov 18 '20 at 18:56
  • I'll just add that in case you need to go beyond 10^-16 (or 2^-53) you can use Matlab's VPA option. `vpa(x,d)` will use variable-precision floating-point arithmetic (VPA) to evaluate `x` to up to `d` significant digits precision. – bla Nov 18 '20 at 21:18
  • @bla I would love to go beyond the usual precision, but it seems to significantly slow down my code :( – Guy Nov 19 '20 at 02:01
  • @CrisLuengo Thank you I'll give it a shot! – Guy Nov 19 '20 at 02:01
  • @PranavHosangadi My motivation for writing this question was that I tried doing the search in decimal and binary, and the binary search ran out of precision before my solution was fully converged, while the decimal search had no problem. Clearly there's something going on here! – Guy Nov 19 '20 at 02:04
  • “Binary search ran out of precision” Is that under the assumption that you can’t go below 2^-22? Because you easily can. Precision doesn’t change if you use binary or decimal representation, internally it’s all 64-bit IEEE floating-point values. – Cris Luengo Nov 19 '20 at 02:27
  • @CrisLuengo concretely: I'm solving a boundary value problem by "shooting," and if I run the shooting algorithm by subdividing my interval into two parts instead of 10, it does not converge. Perhaps the problem is my code, which I could share if my problem is still not clear. – Guy Nov 19 '20 at 16:42
  • Negative powers of 10 cannot be represented exactly, so maybe the rounding error is causing your algorithm to converge? Weird, but I cannot see another difference. Unless you use a limited number of steps. Dividing the interval by 10 means you get to 1/1000 in 3 steps, in the binary case this is 10 steps. So you need more steps in the binary case. But each step is less work (only two parts to work on instead of 10), so you end up doing the same amount of work. Do post your code, I'm hoping it will clarify what you are seeing, because I don't get it. – Cris Luengo Nov 19 '20 at 16:52

0 Answers0