-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Documentation should use more specific types #4525
Comments
@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 |
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 think we need @tigoe |
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 mean the ones listed here, as used in Arduino sketches over the last ten years: 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 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:
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 |
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. |
Oooh - back to COBOL! :-)
|
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 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). |
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. |
Well...six months ago...#3801 |
about cousteaulecommandant's and tigoe's statements: the C standard data types listed in < stdint.h > are |
@shiftleftplusone From what I have seen, Arduino is not intended as a tool to teach C or C++ and their standard libraries but rather to teach programming in general and to be "easy to use". For example, it introduces For really learning C and how to use microcontrollers, the Arduino IDE is probably not the best choice. PS: sorry for wall of text. I really need to learn to summarize. |
Objection! |
@cousteaulecommandant pretty much sums up our philosophy in a nutshell. I don't think ANSI C is such a perfect standard that it must be adhered to at the expense of legibility. People can learn the standards after they've learned the more basic concepts, and so far, Arduino's proven pretty good for teaching those basic concepts, in part because it doesn't adhere to the standards. If you want to use the standards in your own examples, go ahead. They'll compile just fine. |
Once you have trained someone with bad habits it takes longer to unlearn the bad habits, even with someone leaning over you watching every key press. I am regularly involved with learners fro age 14 to 30, bad habits and wrong ways stick until they waste a day tracking down an issue which was their bad habit, no matter how many times you tell them, show them, smack them around the head they keep on. Don't even get me on exam boards and bad habits..... |
I don't mind if Arduino types definition must follow a standard or not...but please: give it some consistency. As I said in #3801: *"word" type is a 2-byte data type only in AVR boards but in Due (and Zero??) it is a 4-byte data type. Not very logical. *"char" type in a signed type in AVR boards but in Due (and Zero??) is unsigned. OMG!! *I repeat here: maybe should be added a table somewhere in web simply showing three columns for each data type (row): the bytes stored in both architectures (AVR & ARM) if it's signed or not and their resepective numerical boundaries. This would help to see, for instance, that in Due (and Zero??) the "word" type and the "unsigned long" type are the same and also the "int" type and the "long" type, whereas in AVR the "short" type is equal to "int" type. What a mess. Thanks |
apart from logic or not it is a matter of libs design if there will happen a lot of mess or not. But e.g., if a AVR lib is designed to handle char as signed then bullshit can be reliably expected to happen if the same code will be compiled to unsigned char when targeting an ARM. So for variable definitions (at user interface level) working on either platforms unambiguous datatype dimensions are essential, i.e. the C11 stdint datatypes. Simply re-define in all existing libs former int as int16_t or former long as int32_t, and everything will be fine. Ok, perhaps not everything , but most of it. (char for Serial class is another issue... ) |
char/unsigned char for nearly any 8 bit interface, LCD, SPI, I2C/TWI..... The main issue is the Arduino documentation does not say like C/C++ that these types may vary across platforms, FOR EXAMPLE it states on 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. " No platform dependency it states it is ALWAYS this when it is not |
I don't see your point - who is missing something? (or are you just confirming what I already wrote?) |
You and I know that char is platform dependent new users and many others do NOT so either - Leave the current DOCUMENTATION mess or - Change compiler settings to match documentation for ALL platforms or - Change documentation to say exactly what each type is for EACH platform or - Change types to a standard set that are EXPLAINED to new users and DOCUMENTED Current documentation mess is STILL AVR specific, and most neweer platforms are ARM, so many gotchas for new users and new hobbyists let alone students. |
ok, thank you for clarification, your point was not clear to me. |
Yes please, let's do this.
Bug. Either a "documentation bug" or an Arduino bug, you choose. Either fix the documentation or use
(1) definitely no, yuck. |
cousteau wrote:
Agreed
Hence various people's comments
Might do, but the compiler for each has been set to a standard for norms and
But is the table method you agreed with up top, if you make a table you must
Remembering that char is SIGNED on AVR .. Strings should never really be
char is signed on AVR not on ARM there is the nub of the problem.
If you create a cross platform library you need to be sure what you are |
Sure, internally I'd use those types, but externally (function parameter and return types) I'd prefer to go with "normal" types as the thing the end user will have to deal with. BTW, I wasn't thinking of Unicode nor UTF-8 for characters, just of "smallest unit of 'text' in a string" (you can't do UTF-8 based indexing or accessing on strings anyway). Maybe the right words would have been "piece of a string", to emphasize that I don't refer to raw data in general but to a specific usage (elements of text strings). |
The length of |
Indeed, for example (And it will be even funnier if it moves to weirder architectures that don't have 8-bit bytes, such as some TI microcontrollers, and therefore no |
https://github.com/01org/corelibs-arduino101 |
OK, so it's not part of the arduino/Arduino repository... is it some sort of fork of the Arduino IDE, or is this going to be in the main arduino/Arduino repository in a future? In any case, if this .c is considered as part of "the standard Arduino family" covered by the documentation, then some action should be taken. Either define a In any case, this kinda shows either that having the same type with different sizes for different platforms is a good idea (a good C or C++ program shouldn't rely on special types having a specific size anyway, but use macros to determine it; that's why By the way,
actually since C99, but they weren't adopted by C++ until C++11 so they're "relatively modern" there (and they are only mandatory for implementations that support those types or equivalent, which is almost always the case). |
So...then? |
Let me recap a bit... (I'll send one post for each problem for clarity) About
|
About using fixed datatypes for every API of ArduinoSomeone asked to change the Arduino API to use only fixed-size types, like I don't think this a good idea unless there is a real need for the type to be fixed size. Standard types in C are defined with a minimum size, that is exactly what we need for the Arduino API. For example, look at the
the In this case the proper choice is http://www.cplusplus.com/doc/tutorial/variables/
So, IMHO, the solution here is to keep the API as-is unless there is a real need for fixed-size types. |
About fixing docs for
|
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 If we want to go for the "fastest type with at least x bits", we could be using the Agreed on all other points. As for the |
It's not the fastest type in general, but the fastest to handle at least 16 bits, that's what counts here. |
Personally I like |
Good point. |
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. i.e. if size/width of the data matters, use stdint types. |
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. |
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 |
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? For the ESP families the data have been stated here: |
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. |
You are right. That is a good solution.
At least there could be a warning at compile time :) |
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).The text was updated successfully, but these errors were encountered: