You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Documentation should use more specific types #4525
Currently, the documention uses types like "unsigned long" (for example here: https://www.arduino.cc/en/Reference/millis). However, this is only correct for the AVR architecture, on e.g. the Due a long is (AFAIK) 64 bit, but the code explicity uses uint32_t. It would make sense to adopt the same explicit approach in the documentation (and, while we're at it, probably apply this to the code of the AVR port as well).
Reacted by Rotzbua, Henry Gabryjelski, Stefan Langer, Carl Mai and Andy2No
@matthijskooijman +1 I think that actually all the documentation and software should use standard data types like uint8_t, int32_t etc. In this way it is immediately clear the "dimension" of a variable and code can be optimized and standardized
Reacted by Henry Gabryjelski, Stefan Langer and Andy2No
BTW once upon a time someone from the high levels told me that for beginners standard data types are not too friendly...
They might be a bit scary-looking, that's true, but the using platform-dependent types of unknown size looks nicer but doesn't work as well. We could consider introducing more friendly alternative names, but then Arduino code becomes even less portable, and things people learn will also be less reusable.
Perhaps a decent alternative is to use the standard names in the docs, but make them a link to a page explaining what these types mean? Anyone that is confused about a typename can click it and get an explanation?
I would rather we adopt the Arduino types to new platforms as we go. As you said, Arturo, the point in making them in the first place was to make the language more readable for a non-technical audience. I'd like to maintain that approach as we move to new platforms.
@tigoe, what "Arduino types" do you mean exactly? There is the custom "byte" type, but AFAIK no other custom integer types are defined. The AVR core and documentation use int and long for the bigger integers, but those can't be portably used on other platforms, since their sizes will be different.
Or is your suggestion to introduce new types (e.g. word and dword in addition to byte or something)?
I'm suggesting that if they don't exist for other platforms, we introduce them, to make those platforms compatible with Arduino as it currently exists.
@tigoe, Oh good point. Seems I totally missed the short and word types (though "word" is a bit of an ambiguous term in general - I think it originally referred to a machine's word size, for x86 it was used to refer to 16-bit, with double-word being 64-bit, arduino seems to use it for 16-bit on AVR and 32-bit on Due, which doesn't actually make it very useful here). On additional complication with these types is that currently short is signed and word is unsigned, and I don't think C supports just adding "signed" or "unsigned" to a custom type to change its signedness.
So, regarding types, it seems we don't have any Arduino-specific / user-friendly set of types that is complete (e.g. has signed and unsigned integers of 8, 16, 32 and possibly 64 bits wide) and portable. So that leaves us with two options for making this consistent:
Use the existing, standard types like uint32_t.
Introduce new types to make the existing types complete.
I'm not so sure what the best option is here. Using standard types makes code, as well as learned concepts, more portable. These types also clearly and unambiguously indicate the signedness and size of the type, at least once you've learned how they work. I'm inclined to stick with the standard types, also because it is probably hard to define a new set of typenames that are clear and unambiguous enough. "byte" is well-defined, "short" is less obvious and "word" is both unobvious as well as ambiguous.
For lack of any well-defined names for specific bit widths, using the number of bits in the type (like the standard types) might be useful. To make the names less cryptic, they could be more verbose, an drop the not-so-useful _t postfix. E.g. types like signed_16bit_int or unsigned_8bit_int could work, though I'm afraid they're so long and verbose nobody will want to type them...
I'd be more inclined to introduce new typws to make the existing types complete. I have always found the _t extension irritating, and uint8, uint16, uint32, etc, too terse, and too easy to mis-type as unit (not to mention how auto-correct does the same to them).
As for being so long and verbose, nobody wanting to type them: that hasn't been my experience, outside of people who've been classically trained in C programming. Tersness is a weakness, in my opinion. It's one of the things I'm trying to fight against, to get people to use natural language more in code, so it's readable to people from many backgrounds. If we really want programming to be a common literacy, then the grammar of it has to be articulate.
I'd be more inclined to introduce new typws to make the existing types complete. I have always found the _t extension irritating, and uint8, uint16, uint32, etc, too terse, and too easy to mis-type as unit (not to mention how auto-correct does the same to them).
As for being so long and verbose, nobody wanting to type them: that hasn't been my experience, outside of people who've been classically trained in C programming. Tersness is a weakness, in my opinion. It's one of the things I'm trying to fight against, to get people to use natural language more in code, so it's readable to people from many backgrounds. If we really want programming to be a common literacy, then the grammar of it has to be articulate.
—
Reply to this email directly or view it on GitHub #4525 (comment).
I agree that the [u]intX_t types are a bit cryptic and unnecessarily scary. I have seen implementations that use custom types such as u8 or s32 for unsigned/signed types; maybe that's excessively short though. As for verbosity, what about unsigned8 or signed_32b? (not sure if it looks better with or without the underscore, and with or without the b; I think without either)
As for the documentation, I'd just say "a 32-bit signed integer" when necessary.
about cousteaulecommandant's and tigoe's statements:
the C standard data types listed in < stdint.h > are
int8_t, int16_t, int32_t, int_64_t, uint8_t, uint16_t, uint32_t, uint64_t, (...),
and Arduino should apply to the standards!!
why establish a non-standard-C-compliant datatype just for Arduino?
C is C is C, and either C user who is learning C for Arduino should learn to use the correct rules for standard C from the start - or one day he will fail terrificly in "real world" environments (e.g., gpp on Raspberry Pi) !!
or AVR is surely int16_t, but this type is not a good choice for ARM, where the best type is int32_t (because ARM MCUs are designed to better handle memory transfers of 32-bits)
In general, using "int" as the "fastest" type seems nice, except that this does not really apply for AVR. There, 8-bit is faster than 16-bit, but int is 16-bit anyway...
Agreed on all other points. As for the word and byte types, we should probably discourage people from using them (e.g. mark as deprecated in the docs and remove them from examples).
In general, using "int" as the "fastest" type seems nice, except that this does not really apply for AVR. There, 8-bit is faster than 16-bit, but int is 16-bit anyway...
It's not the fastest type in general, but the fastest to handle at least 16 bits, that's what counts here.
In the example above we need 16 bit, because we need to distinguish 0xFF as byte read from the stream from -1 that means "no data available". This is only possible using a signed type that has >=16 bits.
As for the word and byte types, we should probably discourage people from using them
Personally I like byte though; I think it's a useful type with an intuitive name, whereas unsigned char is long and potentially confusing for some users (why would you use a "character" to store an integer or a bunch of bytes?). The documentation could explain that it's just defined as unsigned char though (or have a side note in the char docs for what are signed/unsigned char and how the latter is the same as byte).
(Not word though; word is useless and confusing.)
As for the word and byte types, we should probably discourage people from using them (e.g. mark as deprecated in the docs and remove them from examples).
I tend to agree (at least for word - which is not an obvious width). While I did use custom local types like that back through the 80's and 90's to gain some local portability. This was done because there was nothing portable that was standardized back then.
The ANSI committee finally caved and created the stdint types back in 1999. My preference would be to see Arduino start encourage the use of standardized types vs creating their own - after-all it has been nearly 20 years since these standardized types were created and they are portable across platforms.
i.e. if size/width of the data matters, use stdint types.
The stdint types and names are pretty short/simple and obvious.
Perhaps it would be useful to include them in the documentation.
It should probably be changed to "DUE and other SAM/SAMD based boards" as for int. Same for unsigned int. I guess this is because the DUE was the only official ARM-based board when the documentation was written.
I still think a table would be easier to read, and maybe combine the documentation for int/short int/long int (and maybe also signed char) into a single article (same for the unsigned counterparts).
"On boards with 32-Bit processors (such as Arduino Due, SAM(D), ESP...) doubles have 8-byte (64 bit) precision".
For me the fact that AVR does not have a "real" double is an implementation error and should be fat and red and blinking ;-)
IMO arduino (IDE) does not only focus on the original arduino boards - the major strength is availability of so many different platform which are quite popular under arduino-fans.
A table that is referenced on all adequate pages (also covering the floating point types!) might be a solution.
Maybe also at least one motivating sentence recommending usage of un-ambigous types is not too disturbing to beginners.
What is the list of cores based on popularity?
AVR/Cortex/SAM(D)/ESP/STM/LPC? 5-6 families aren't too much, are they?
"On boards with 32-Bit processors (such as Arduino Due, SAM(D), ESP...) doubles have 8-byte (64 bit) precision".
IMO arduino (IDE) does not only focus on the original arduino boards - the major strength is availability of so many different platform which are quite popular under arduino-fans.
That implies stating that every possible architecture, officially supported or not, current or future, that is labeled as "32-bit" defines doubles as 64-bit in its ABI, which I'm not sure can be done. I'd limit the statement to all the officially supported cores, maybe suggesting that this probably applies to other 32- and 64-bit architectures as well.
For me the warning that AVR does not have no double is an implementation error and should be fat and red and blinking ;-)
Yeah, is this even allowed by the C++ standard? At least in C it goes below the minimum required precision for a double.
That implies stating that every possible architecture, officially supported or not, current or future, that is labeled as "32-bit" defines doubles as 64-bit in its ABI, which I'm not sure can be done. I'd limit the statement to all the officially supported cores, maybe suggesting that this probably applies to other 32- and 64-bit architectures as well.
You are right. That is a good solution.
For me the fact that AVR does not have a "real" double is an implementation error and should be fat and red and blinking ;-)
Yeah, is this even allowed by the C++ standard? At least in C it goes below the minimum required precision for a double.
At least there could be a warning at compile time :)
Activity
agdl commentedon Feb 5, 2016
@matthijskooijman +1 I think that actually all the documentation and software should use standard data types like uint8_t, int32_t etc. In this way it is immediately clear the "dimension" of a variable and code can be optimized and standardized
agdl commentedon Feb 5, 2016
BTW once upon a time someone from the high levels told me that for beginners standard data types are not too friendly...
matthijskooijman commentedon Feb 5, 2016
They might be a bit scary-looking, that's true, but the using platform-dependent types of unknown size looks nicer but doesn't work as well. We could consider introducing more friendly alternative names, but then Arduino code becomes even less portable, and things people learn will also be less reusable.
Perhaps a decent alternative is to use the standard names in the docs, but make them a link to a page explaining what these types mean? Anyone that is confused about a typename can click it and get an explanation?
agdl commentedon Feb 5, 2016
I think we need @tigoe
tigoe commentedon Feb 5, 2016
I would rather we adopt the Arduino types to new platforms as we go. As you said, Arturo, the point in making them in the first place was to make the language more readable for a non-technical audience. I'd like to maintain that approach as we move to new platforms.
matthijskooijman commentedon Feb 5, 2016
@tigoe, what "Arduino types" do you mean exactly? There is the custom "byte" type, but AFAIK no other custom integer types are defined. The AVR core and documentation use int and long for the bigger integers, but those can't be portably used on other platforms, since their sizes will be different.
Or is your suggestion to introduce new types (e.g. word and dword in addition to byte or something)?
tigoe commentedon Feb 5, 2016
I mean the ones listed here, as used in Arduino sketches over the last ten years:
https://www.arduino.cc/en/Reference/HomePage
I'm suggesting that if they don't exist for other platforms, we introduce them, to make those platforms compatible with Arduino as it currently exists.
matthijskooijman commentedon Feb 5, 2016
@tigoe, Oh good point. Seems I totally missed the
shortandwordtypes (though "word" is a bit of an ambiguous term in general - I think it originally referred to a machine's word size, for x86 it was used to refer to 16-bit, with double-word being 64-bit, arduino seems to use it for 16-bit on AVR and 32-bit on Due, which doesn't actually make it very useful here). On additional complication with these types is that currentlyshortis signed andwordis unsigned, and I don't think C supports just adding "signed" or "unsigned" to a custom type to change its signedness.So, regarding types, it seems we don't have any Arduino-specific / user-friendly set of types that is complete (e.g. has signed and unsigned integers of 8, 16, 32 and possibly 64 bits wide) and portable. So that leaves us with two options for making this consistent:
uint32_t.I'm not so sure what the best option is here. Using standard types makes code, as well as learned concepts, more portable. These types also clearly and unambiguously indicate the signedness and size of the type, at least once you've learned how they work. I'm inclined to stick with the standard types, also because it is probably hard to define a new set of typenames that are clear and unambiguous enough. "byte" is well-defined, "short" is less obvious and "word" is both unobvious as well as ambiguous.
For lack of any well-defined names for specific bit widths, using the number of bits in the type (like the standard types) might be useful. To make the names less cryptic, they could be more verbose, an drop the not-so-useful
_tpostfix. E.g. types likesigned_16bit_intorunsigned_8bit_intcould work, though I'm afraid they're so long and verbose nobody will want to type them...tigoe commentedon Feb 5, 2016
I'd be more inclined to introduce new typws to make the existing types complete. I have always found the _t extension irritating, and uint8, uint16, uint32, etc, too terse, and too easy to mis-type as unit (not to mention how auto-correct does the same to them).
As for being so long and verbose, nobody wanting to type them: that hasn't been my experience, outside of people who've been classically trained in C programming. Tersness is a weakness, in my opinion. It's one of the things I'm trying to fight against, to get people to use natural language more in code, so it's readable to people from many backgrounds. If we really want programming to be a common literacy, then the grammar of it has to be articulate.
Breidenbach commentedon Feb 5, 2016
Oooh - back to COBOL! :-)
cousteaulecommandant commentedon Feb 7, 2016
I agree that the [u]intX_t types are a bit cryptic and unnecessarily scary. I have seen implementations that use custom types such as
u8ors32for unsigned/signed types; maybe that's excessively short though. As for verbosity, what aboutunsigned8orsigned_32b? (not sure if it looks better with or without the underscore, and with or without the b; I think without either)As for the documentation, I'd just say "a 32-bit signed integer" when necessary.
re: the original comment, I'm not sure a long is 64 bits in ARM; according to http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472l/chr1359125009502.html it is 32 bits just like int (the only platform I've heard about that uses 64 bit longs is x86_64/amd64).
tigoe commentedon Feb 7, 2016
I think @damellis should weigh in on this, as he's more informed than I when it comes to the choices for data type names originally.
q2dg commentedon Feb 9, 2016
Well...six months ago...#3801
shiftleftplusone commentedon Feb 19, 2016
about cousteaulecommandant's and tigoe's statements:
the C standard data types listed in < stdint.h > are
int8_t, int16_t, int32_t, int_64_t, uint8_t, uint16_t, uint32_t, uint64_t, (...),
and Arduino should apply to the standards!!
why establish a non-standard-C-compliant datatype just for Arduino?
C is C is C, and either C user who is learning C for Arduino should learn to use the correct rules for standard C from the start - or one day he will fail terrificly in "real world" environments (e.g., gpp on Raspberry Pi) !!
44 remaining items
matthijskooijman commentedon Jul 10, 2017
In general, using "int" as the "fastest" type seems nice, except that this does not really apply for AVR. There, 8-bit is faster than 16-bit, but
intis 16-bit anyway...If we want to go for the "fastest type with at least x bits", we could be using the
int_fast16_ttype from http://en.cppreference.com/w/c/types/integer. On AVR, theuint_fast8_tactually is 8 bits wide, while I suspect it is 32-bits on ARM (haven't checked). Using these types might be a bit more complex for novice users, though.Agreed on all other points. As for the
wordandbytetypes, we should probably discourage people from using them (e.g. mark as deprecated in the docs and remove them from examples).cmaglie commentedon Jul 10, 2017
It's not the fastest type in general, but the fastest to handle at least 16 bits, that's what counts here.
In the example above we need 16 bit, because we need to distinguish
0xFFas byte read from the stream from-1that means "no data available". This is only possible using a signed type that has >=16 bits.cousteaulecommandant commentedon Jul 10, 2017
Personally I like
bytethough; I think it's a useful type with an intuitive name, whereasunsigned charis long and potentially confusing for some users (why would you use a "character" to store an integer or a bunch of bytes?). The documentation could explain that it's just defined asunsigned charthough (or have a side note in thechardocs for what aresigned/unsigned charand how the latter is the same asbyte).(Not
wordthough;wordis useless and confusing.)Fix prototypes to strictly match Arduino API
matthijskooijman commentedon Jul 10, 2017
Good point.
byteis indeed nice, though less portable.bperrybap commentedon Jul 10, 2017
I tend to agree (at least for word - which is not an obvious width). While I did use custom local types like that back through the 80's and 90's to gain some local portability. This was done because there was nothing portable that was standardized back then.
The ANSI committee finally caved and created the stdint types back in 1999. My preference would be to see Arduino start encourage the use of standardized types vs creating their own - after-all it has been nearly 20 years since these standardized types were created and they are portable across platforms.
i.e. if size/width of the data matters, use stdint types.
The stdint types and names are pretty short/simple and obvious.
Perhaps it would be useful to include them in the documentation.
Fix prototypes to strictly match Arduino API
Fix prototypes to strictly match Arduino API
datenheim commentedon Feb 5, 2020
Somehow the floting point numbers may be affected too, docs not clear enough on all occasions:
https://www.arduino.cc/reference/en/language/variables/data-types/double/
Double is not only on the Due 64 bits long but on all 32bit cores and also 64bit cores.
While I do not want to revive the discussion for the integer types arduino should be prepared to swap over to 64bit cores ... IMHO.
cousteaulecommandant commentedon Feb 5, 2020
It should probably be changed to "DUE and other SAM/SAMD based boards" as for int. Same for unsigned int. I guess this is because the DUE was the only official ARM-based board when the documentation was written.
I still think a table would be easier to read, and maybe combine the documentation for
int/short int/long int(and maybe alsosigned char) into a single article (same for the unsigned counterparts).datenheim commentedon Feb 5, 2020
or maybe
For me the fact that AVR does not have a "real" double is an implementation error and should be fat and red and blinking ;-)
IMO arduino (IDE) does not only focus on the original arduino boards - the major strength is availability of so many different platform which are quite popular under arduino-fans.
A table that is referenced on all adequate pages (also covering the floating point types!) might be a solution.
Maybe also at least one motivating sentence recommending usage of un-ambigous types is not too disturbing to beginners.
What is the list of cores based on popularity?
AVR/Cortex/SAM(D)/ESP/STM/LPC? 5-6 families aren't too much, are they?
For the ESP families the data have been stated here:
espressif/arduino-esp32#1745 (comment)
https://www.arduinoforum.de/arduino-Thread-L%C3%A4nge-der-Datentypen-beim-ESP8266?pid=45102#pid45102
cousteaulecommandant commentedon Feb 7, 2020
That implies stating that every possible architecture, officially supported or not, current or future, that is labeled as "32-bit" defines doubles as 64-bit in its ABI, which I'm not sure can be done. I'd limit the statement to all the officially supported cores, maybe suggesting that this probably applies to other 32- and 64-bit architectures as well.
Yeah, is this even allowed by the C++ standard? At least in C it goes below the minimum required precision for a double.
datenheim commentedon Feb 9, 2020
You are right. That is a good solution.
At least there could be a warning at compile time :)