Domanda

I have the following vector

vec = [ 255     0   255     0   255     0   255     0   255     0   255     0   255     0   255     0]

vec 1x16 double

and using the following command

polyval(vec', 256);

I get

ans = 3.3896e+038

but when I try to get back my original vector

vec2 = decimal2base(ans, 256)

I get

vec2 = 255     0   255     0   255     1     0     0     0     0     0     0     0     0     0     0

and this is clearly not my original vector.

Whats more if again I run polyval in this vector

polyval(vec2', 256); 

I get

ans=

  3.3896e+038

I am not entirely sure what sort of mistake I am making as I know that my conversion functions are ok, so it must be a number precision thing.

È stato utile?

Soluzione

Ah, large numbers. The value 3.3896e+038 is higher than the maximum integer that can be represented by a double without loss of accuracy.

That maximum number is 2^53 or

>> flintmax('double')
ans =
   9.0072e+15

So you are losing accuracy and you cannot reverse the computation.


Doing the computations with uint64 values only:

>> pows = uint64(fliplr(0:numel(vec)-1));
>> sum(uint64(vec).*(uint64(256).^pows),'native')
ans =
 18446744073709551615

That's about 1.84e+19. Just a little different from what you get if you use doubles. But wait... that number looks familiar:

>> intmax('uint64')
ans =
 18446744073709551615

So, you've maxed out unsigned 64-bit integers too:

>> uint64(256).^pows
ans =
  Columns 1 through 5
 18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615
  Columns 6 through 10
 18446744073709551615 18446744073709551615 18446744073709551615    72057594037927936      281474976710656
  Columns 11 through 15
        1099511627776           4294967296             16777216                65536                  256
  Column 16

When you get above 255^8 or so, you're passing intmax('uint64') and you can't manage numbers this large, at least not with MATLAB's built-in data types.

Altri suggerimenti

see if this returns '1':

polyval(vec(6:end),256)==polyval(vec2(6:end),256);

If so, then it's just a property of '255+1' for that special 'vec'.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top