How can we write unsigned long value in matlab?

22 次查看(过去 30 天)
Hi there, I am trying to implement some steps from a C code and there is this "(double)" "(unsigned long)" thing which I really don't have any idea about. Can anyone please guide me if I have to write the exact code in matlab what would be the syntax?
Here is the example of the line that I wanted to implement

采纳的回答

Walter Roberson
Walter Roberson 2022-2-24
Technically, a C compiler on a 64 bit OS can be completely compliant if it uses 32 bits for unsigned long . There is no requirement in C that 64 bits be used if they are available.
gpssim.h says that
typedef struct
{
int week; /*!< GPS week number (since January 1980) */
double sec; /*!< second inside the GPS \a week */
} gpstime_t;
The maximum number of seconds in one week is 604800 (or 604801 perhaps with a leap second), so using either uint32 or uint64 would work for this purpose.
However: In C, when you divide integers, you truncate towards zero; https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division . This is not the same as in MATLAB: when you divide 32 or fewer bit integer data types in MATLAB, the operation is carried out "as if" the values were converted to double, the operation done on double, and then the result cast to the integer data type. This may sound the same, but it is not: in MATLAB, when you cast a floating point number to an integer data type, the value is rounded . Observe:
uint32(3)/uint32(2)
ans = uint32 2
This is because it was carried out "as if" 3.0/2.0 were calculated, giving 1.5, and then uint32(1.5) rounds to uint32(2)
Because of this, equivalent code in MATLAB to the C could would be
g0.week = g.week;
g0.sec = floor(floor(g.sec + 0.5)/30) * 30;
wn = uint32(mod(g0.week, 1024)); %or uint64 depending on compiler
tow = uint32(floor(floor(g0.sec) / 6)); %or uint64 depending on compiler
On the other hand, because of the way you constructed g0.sec you can be sure that it is the floating point representation of an integer, and is exactly divisible by 6 (since it is an exact multiple of 30), so the last line could be optimized to
tow = uint32(g0.sec / 6); %or uint64 depending on compiler
  2 个评论
Imtiaz nabi
Imtiaz nabi 2022-2-24
what if I use uint32(g.sec+0.5/30) instead of using the floor floor?
Walter Roberson
Walter Roberson 2022-2-24
No, definitely not. You do not want to divide only the 0.5 by 30 .
uint32(g.sec+0.5)/30
would not be the same either.
Consider the range of seconds from 0 to 60. Add 0.5 to get 0.5 to 60.5 . (unsigned long) truncates so in C the result would be 0, 1, 2, 3, ... 60 . The C code then / by 30UL which in C always truncates, so the C results would be 0 for 0 to 29, then 1 for 30 to 59, then 2 for 60. Then the C code casts to double and multiplies by 30; that would give you 0 for entries corresponding to 0 to 29, then 30 for entries corresponding to 30 to 59, then 60 for the entry corresponding to 60 input.
Now consider your proposed MATLAB code uint32(g.sec+0.5)/30 . You again add 0.5 to get 0.5 to 60.5 . Then the uint32() rounds so those would round to 1, 2, ... 61 as uint32 values. Divide by 30 is carried out as-if double() that and divide so 1/30, 2/30, ... 61/30 intermediate results, then cast to uint32 with rounding active, getting 0 for entries 1/30 up to 14/30 [which were originally seconds 0 to 13], then 1 for entries 15/30 to 44/30 [which were originally seconds 14 to 43], then 2 for entries 45/30 to 61/30 [which were originally seconds 44 to 60] .
You need the double floor() that I proposed to get the code right. 0.0+0.5 need to collapse to 0 when converted to integer, not rounded to 1. And 15 --> 15.5 --> 15 --> 15/30 needs to map to uint32(0) not to uint32(1)

请先登录,再进行评论。

更多回答(1 个)

Rik
Rik 2022-2-24
编辑:Rik 2022-2-24
As I understand it, this would be a casting operation, equivalent to the Matlab function cast. (note that typecast changes the data type, but not the underlying bits, while cast will try to find the closest equivalent)
Since an unsigned long is an 8 byte integer, on systems with 8 bits per byte (virtually every computer nowadays), that amounts to 64 bits. So that would make it a uint64 in Matlab.
doc cast
doc typecast
  2 个评论
Imtiaz nabi
Imtiaz nabi 2022-2-24
ok so what is this /30UL and if I want to implement this in matlab do I just need to use uint64(my values); that's it?
Rik
Rik 2022-2-24
I suspect that's correct.
Your question doesn't actually seem a Matlab question. I looks like you don't understand the C code.
I hardly ever work with C. I suspect that '30UL' is casting the value 30 to an unsigned long, but I don't know.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by