Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
error in dec2hex

Subject: error in dec2hex

From: Leyo Joseph

Date: 15 May, 2012 16:49:06

Message: 1 of 7

Hello,
I am trying this simple dec2hex and gets the error==>
 k=200*2^47/38400

k =

  7.3301e+011

>> dec2hex(k)
??? Error using ==> dec2hex at 27
First argument must contain non-negative integers.

But if I try ==>
 dec2hex(7.3301e+011,12)

ans =

00AAAACCF880

looks like the number Matlab calculating is wrong. Python gives the answer as
0X00AAAAAAAAAA
Is this some precision issue?

Subject: error in dec2hex

From: Bruno Luong

Date: 15 May, 2012 17:16:07

Message: 2 of 7

"Leyo Joseph" wrote in message <jou1e2$h1$1@newscl01ah.mathworks.com>...
> Hello,
> I am trying this simple dec2hex and gets the error==>
> k=200*2^47/38400
>

% Requirement

>> help dec2hex
 dec2hex Convert decimal integer to hexadecimal string.
    dec2hex(D) returns a 2-D string array where each row is the
    hexadecimal representation of each decimal integer in D.
    D must contain non-negative integers smaller than 2^52.

   [...]


>> k==round(k)

ans =

     0

Thus k does not meet the requirement.

Bruno

Subject: error in dec2hex

From: Barry Williams

Date: 15 May, 2012 17:29:09

Message: 3 of 7

"Leyo Joseph" wrote in message <jou1e2$h1$1@newscl01ah.mathworks.com>...
> Hello,
> I am trying this simple dec2hex and gets the error==>
> k=200*2^47/38400
>
> k =
>
> 7.3301e+011
>
> >> dec2hex(k)
> ??? Error using ==> dec2hex at 27
> First argument must contain non-negative integers.
>
> But if I try ==>
> dec2hex(7.3301e+011,12)
>
> ans =
>
> 00AAAACCF880
>
> looks like the number Matlab calculating is wrong. Python gives the answer as
> 0X00AAAAAAAAAA
> Is this some precision issue?

Note from doc dec2hex: 'd must be a nonnegative integer smaller than 2^52.'
The hexadecimal representation of the integer 7.3301e+11 is 00AAAACCF880.
It looks as if 47/38400 starts repeating after the 9th decimal position. You would need infinite precision to evaluate your expression exactly.
Python is giving you the hexadecimal representation of the integer 733007751850. If you input that integer into ML, you will get the same answer as in Python: AAAAAAAAAA.
Barry

Subject: error in dec2hex

From: Roger Stafford

Date: 15 May, 2012 17:31:07

Message: 4 of 7

"Leyo Joseph" wrote in message <jou1e2$h1$1@newscl01ah.mathworks.com>...
> Hello,
> I am trying this simple dec2hex and gets the error==>
> k=200*2^47/38400
>
> k =
>
> 7.3301e+011
>
> >> dec2hex(k)
> ??? Error using ==> dec2hex at 27
> First argument must contain non-negative integers.
>
> But if I try ==>
> dec2hex(7.3301e+011,12)
>
> ans =
>
> 00AAAACCF880
>
> looks like the number Matlab calculating is wrong. Python gives the answer as
> 0X00AAAAAAAAAA
> Is this some precision issue?
- - - - - - - -
  Yes, it is a precision issue, Leyo. 200*2^47/38400 is not an integer but 7.3301e+011 is. The latter is only matlab's displayed approximation to five decimal places. The two numbers are actually not the same. You get different results from Python because you gave them different numbers. If you first round off 200*2^47/38400 to an integer and give it to 'dec2hex' you should get the same results (aside from the intial '0X' of course.)

Roger Stafford

Subject: error in dec2hex

From: Leyo Joseph

Date: 15 May, 2012 17:36:06

Message: 5 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jou30n$8a9$1@newscl01ah.mathworks.com>...
> "Leyo Joseph" wrote in message <jou1e2$h1$1@newscl01ah.mathworks.com>...
> > Hello,
> > I am trying this simple dec2hex and gets the error==>
> > k=200*2^47/38400
> >
>
> % Requirement
>
> >> help dec2hex
> dec2hex Convert decimal integer to hexadecimal string.
> dec2hex(D) returns a 2-D string array where each row is the
> hexadecimal representation of each decimal integer in D.
> D must contain non-negative integers smaller than 2^52.
>
> [...]
>
>
> >> k==round(k)
>
> ans =
>
> 0
>
> Thus k does not meet the requirement.
>
> Bruno
Thnaks,
But is the following answer correct?

 dec2hex( 7.3301e+011,12)

ans =

00AAAACCF880

I am getting different answer with Python?.
Thanks

Subject: error in dec2hex

From: Bruno Luong

Date: 15 May, 2012 17:56:19

Message: 6 of 7

"Leyo Joseph" wrote in message <jou466$dk3$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jou30n$8a9$1@newscl01ah.mathworks.com>...
> > "Leyo Joseph" wrote in message <jou1e2$h1$1@newscl01ah.mathworks.com>...
> > > Hello,
> > > I am trying this simple dec2hex and gets the error==>
> > > k=200*2^47/38400
> > >
> >
> > % Requirement
> >
> > >> help dec2hex
> > dec2hex Convert decimal integer to hexadecimal string.
> > dec2hex(D) returns a 2-D string array where each row is the
> > hexadecimal representation of each decimal integer in D.
> > D must contain non-negative integers smaller than 2^52.
> >
> > [...]
> >
> >
> > >> k==round(k)
> >
> > ans =
> >
> > 0
> >
> > Thus k does not meet the requirement.
> >
> > Bruno
> Thnaks,
> But is the following answer correct?

It is correct

>> dec2hex(733010000000) % 7.3301e11

ans =

AAAACCF880

>> dec2hex(733007751851) % round k

ans =

AAAAAAAAAB

>> dec2hex(733007751850) % floor(k); obviously value used by Python after cast

ans =

AAAAAAAAAA

% Bruno

>
> dec2hex( 7.3301e+011,12)
>
> ans =
>
> 00AAAACCF880
>
> I am getting different answer with Python?.
> Thanks

Subject: error in dec2hex

From: Steven_Lord

Date: 15 May, 2012 18:06:03

Message: 7 of 7



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:jou3sr$cbc$1@newscl01ah.mathworks.com...
> "Leyo Joseph" wrote in message <jou1e2$h1$1@newscl01ah.mathworks.com>...
>> Hello,
>> I am trying this simple dec2hex and gets the error==>
>> k=200*2^47/38400
>>
>> k =
>>
>> 7.3301e+011
>>
>> >> dec2hex(k)
>> ??? Error using ==> dec2hex at 27
>> First argument must contain non-negative integers.
>>
>> But if I try ==>
>> dec2hex(7.3301e+011,12)
>>
>> ans =
>>
>> 00AAAACCF880
>>
>> looks like the number Matlab calculating is wrong. Python gives the
>> answer as 0X00AAAAAAAAAA
>> Is this some precision issue?
> - - - - - - - -
> Yes, it is a precision issue, Leyo. 200*2^47/38400 is not an integer but
> 7.3301e+011 is. The latter is only matlab's displayed approximation to
> five decimal places. The two numbers are actually not the same. You get
> different results from Python because you gave them different numbers. If
> you first round off 200*2^47/38400 to an integer and give it to 'dec2hex'
> you should get the same results (aside from the intial '0X' of course.)

Actually it looks like the number used to generate the Python answer is
floor(200*2^47/38400), not round().

num1 = 7.3301e+011;
num2 = hex2dec('00AAAAAAAAAA');
num3 = floor(200*2^47/38400);
v = [num1, num2, num3];
differences = bsxfun(@minus, v, v.')
num2 == num3

Using ROUND instead of FLOOR would make num2 and num3 differ by 1.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us