Skip to content

[DOC] Vagueness in data types explanation #3797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
q2dg opened this issue Sep 15, 2015 · 9 comments
Closed

[DOC] Vagueness in data types explanation #3797

q2dg opened this issue Sep 15, 2015 · 9 comments
Assignees
Labels
Component: Documentation Related to Arduino's documentation content Type: Duplicate Another item already exists for this topic
Milestone

Comments

@q2dg
Copy link

q2dg commented Sep 15, 2015

In https://www.arduino.cc/en/Reference/Word is written: "A word stores a 16-bit unsigned number, from 0 to 65535. Same as an unsigned int. " But in https://www.arduino.cc/en/Reference/UnsignedInt is written "The Due stores a 4 byte (32-bit) value, ranging from 0 to 4,294,967,295 (2^32 - 1)." So...

*Does the Due also store 4 bytes in the word type?
*If so...maybe it should be noted that then, in Due the word type and the unsigned long type are the same and also the int type and the long type.
*What about the Zero? It's the same than the Due?

In https://www.arduino.cc/en/Reference/Double only mentions Due has 8-byte precision. Zero too?

@shiftleftplusone
Copy link

I'm not quite sure if I understand you question correctly - AFAIK, on most 8bit cpus (edited)
8 bit values are char, byte, and actually word,
16 bit values are short, maybe word, int
32 bit values are long

on 32 bit system even
16 bit values are short and maybe word, or even int
32 bit values may be int or long or even word
64 bit values are long or long long

moreover,
on Arduino AVR char is 8-bit signed, while
on the 32-bit Due char is 8-bit UNSIGNED !

Because all these values are ambiguous I'm only using the exact-size values
int8_t, uint8_t
int16_t, uint16_t
int32_t, uint32_t

for char in strings to be used for Serial it's not possible though:
in this case for all platforms only char works, even if its signed for AVRs and unsigned for Due.

This indeed is a bug.

@q2dg
Copy link
Author

q2dg commented Sep 15, 2015

Well, I'll rewrite the issue putting "?" where I consider there's no a clear statement.
According to "Data Types" pages from https://www.arduino.cc/en/Reference/HomePage:

char : 1 signed byte (AVR & ARM) --> It's clear
byte (unsigned char) : 1 unsigned byte (AVR & ARM) --> It's clear
int : 2 signed bytes (AVR) ; 4 signed bytes (Due and...Zero??)
short : 2 signed bytes (AVR & ARM --> so for AVR it's the same than int)
word (unsigned int) : 2 unsigned bytes (AVR) ; 4 unsigned bytes (Due and...Zero??)
long : 4 signed bytes (AVR & ARM?? --> so for ARM it'd be the same than int)
unsigned long : 4 unsigned bytes (AVR & ARM?? --> so for ARM it'd be the same than word)
float : 4 bytes (AVR & ARM)
double : 4 bytes (AVR) ; 8 bytes (Due and...Zero??)

@shiftleftplusone
Copy link

char is unsigned (!) for the Due !!
(edit) word: not sure

int is 16 bit signed on AVR
long is 32 bit signed on AVR
int and long is 32 bit signed on Due

double is not double on AVR, its just an alias for float on AVR Arduinos,
by definition double is always 2x precision of float - except for Arduino AVR workarounds.

for the Zero I don't know.

@q2dg
Copy link
Author

q2dg commented Sep 15, 2015

Well, from https://www.arduino.cc/en/Reference/Char: "The char datatype is a signed type, meaning that it encodes numbers from -128 to 127. For an unsigned, one-byte (8 bit) data type, use the byte data type". It not distinct AVR from ARM. Who is right, then???

And as I said at first message: in https://www.arduino.cc/en/Reference/Word is written: "A word stores a 16-bit unsigned number, from 0 to 65535. Same as an unsigned int." But in https://www.arduino.cc/en/Reference/UnsignedInt is written "The Due stores a 4 byte (32-bit) value, ranging from 0 to 4,294,967,295 (2^32 - 1)." So I infer Due (and Zero?) also store 4 bytes in the word type. Who is right, then??

@shiftleftplusone
Copy link

try it: _targeting the DUE char is definitely unsigned!_
I ran in many issues because of that!

from systematic reasons, a word is the amount of data that a machine can process at one time and equals the register size....:
"
Word Size and Data Types

A word is the amount of data that a machine can process at one time. This fits into the document analogy that includes characters (usually eight bits) and pages (many words, often 4 or 8KB worth) as other measurements of data. A word is an integer number of bytes for example, one, two, four, or eight. When someone talks about the "n-bits" of a machine, they are generally talking about the machine's word size. For example, when people say the Pentium is a 32-bit chip, they are referring to its word size, which is 32 bits, or four bytes.

The size of a processor's general-purpose registers (GPR's) is equal to its word size. The widths of the components in a given architecture for example, the memory bus are usually at least as wide as the word size. Typically, at least in the architectures that Linux supports, the memory address space is equal to the word size[2]. Consequently, the size of a pointer is equal to the word size. Additionally, the size of the C type long is equal to the word size, whereas the size of the int type is sometimes less than that of the word size. For example, the Alpha has a 64-bit word size. Consequently, registers, pointers, and the long type are 64 bits in length. The int type, however, is 32 bits long. The Alpha can access and manipulate 64 bits, one word at a time.
"
http://stackoverflow.com/questions/16863333/what-is-the-point-word-type-in-c

So on 8-bit AVR, a "word" is supposed to have 8bits=1 byte, and 32-bit-ARM core has a 32-bit (4-byte) "word".
But for sure Arduino has it's own systematics.

@q2dg
Copy link
Author

q2dg commented Sep 15, 2015

Well, tha't why I've opened this issue...to clarify the use of the right words in defining the data types on both architectures

@shiftleftplusone
Copy link

ok, what _IS_ is easy to find out:



//=====================================================================================
//=====================================================================================
void setup() {
   int32_t  i;

   // Serial terminal window
   i=115200;
   Serial.begin(i);  

}


//=====================================================================================
void loop(){
  char     sbuf[128];

  sprintf(sbuf, "sizeof(char) == %d", sizeof(char)); Serial.println(sbuf);
  sprintf(sbuf, "sizeof(short) == %d", sizeof(short)); Serial.println(sbuf);
  sprintf(sbuf, "sizeof(word) == %d", sizeof(word)); Serial.println(sbuf);
  sprintf(sbuf, "sizeof(int) == %d", sizeof(int)); Serial.println(sbuf);
  sprintf(sbuf, "sizeof(long) == %d", sizeof(long)); Serial.println(sbuf);
  sprintf(sbuf, "sizeof(float) == %d", sizeof(float)); Serial.println(sbuf);
  sprintf(sbuf, "sizeof(double) == %d", sizeof(double)); Serial.println(sbuf);
  sprintf(sbuf, "sizeof(long double) == %d", sizeof(long double)); Serial.println(sbuf);
  sprintf(sbuf, "sizeof(long long) == %d", sizeof(long long)); Serial.println(sbuf);


  while(1); 


}
//=====================================================================================

on Mega:

sizeof(char) == 1
sizeof(short) == 2
sizeof(word) == 2
sizeof(int) == 2
sizeof(long) == 4
sizeof(float) == 4
sizeof(double) == 4
sizeof(long double) == 4
sizeof(long long) == 8

on Due:

sizeof(char) == 1
sizeof(short) == 2
sizeof(word) == 4
sizeof(int) == 4
sizeof(long) == 4
sizeof(float) == 4
sizeof(double) == 8
sizeof(long double) == 8
sizeof(long long) == 8

@shiftleftplusone
Copy link

but that does not always apply to C standards, nevertheless they also are ambiguous!
IMO Arduino has to fix that, esp. for word (see above) and float !
http://www.cplusplus.com/doc/tutorial/variables/
http://stackoverflow.com/questions/16863333/what-is-the-point-word-type-in-c
http://www.tutorialspoint.com/cprogramming/c_data_types.htm

Floating-Point Types
Following table gives you details about standard floating-point types...:
Type      Storage size    Value range            Precision
float          4 byte   1.2E-38 to 3.4E+38      6 decimal places
double         8 byte   2.3E-308 to 1.7E+308    15 decimal places
long double   10 byte   3.4E-4932 to 1.1E+4932  19 decimal places

@q2dg
Copy link
Author

q2dg commented Sep 16, 2015

Well, if you don't mind, i'll close this issue and I'll reopen a new one with a more concise text as this one has become a little hard to follow

@q2dg q2dg closed this as completed Sep 16, 2015
@ffissore ffissore modified the milestone: Release 1.6.6 Sep 16, 2015
@per1234 per1234 added Component: Documentation Related to Arduino's documentation content Type: Duplicate Another item already exists for this topic labels Feb 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Documentation Related to Arduino's documentation content Type: Duplicate Another item already exists for this topic
Projects
None yet
Development

No branches or pull requests

5 participants