Skip to content

Documentation should use more specific types #4525

Open

Description

@matthijskooijman

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).

Activity

  1. added
    Component: Documentation
    on Feb 4, 2016
  2. agdl commented on Feb 5, 2016

    @agdl
    Member

    @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

  3. agdl commented on Feb 5, 2016

    @agdl
    Member

    BTW once upon a time someone from the high levels told me that for beginners standard data types are not too friendly...

  4. matthijskooijman commented on Feb 5, 2016

    @matthijskooijman
    CollaboratorAuthor

    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?

  5. agdl commented on Feb 5, 2016

    @agdl
    Member

    I think we need @tigoe

  6. tigoe commented on Feb 5, 2016

    @tigoe
    Member

    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.

  7. matthijskooijman commented on Feb 5, 2016

    @matthijskooijman
    CollaboratorAuthor

    @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)?

  8. tigoe commented on Feb 5, 2016

    @tigoe
    Member

    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.

  9. matthijskooijman commented on Feb 5, 2016

    @matthijskooijman
    CollaboratorAuthor

    @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...

  10. tigoe commented on Feb 5, 2016

    @tigoe
    Member

    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.

  11. Breidenbach commented on Feb 5, 2016

    @Breidenbach

    Oooh - back to COBOL! :-)

    On Feb 5, 2016, at 8:49 AM, tigoe [email protected] wrote:

    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).

  12. cousteaulecommandant commented on Feb 7, 2016

    @cousteaulecommandant
    Contributor

    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.

    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).

  13. tigoe commented on Feb 7, 2016

    @tigoe
    Member

    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.

  14. q2dg commented on Feb 9, 2016

    @q2dg

    Well...six months ago...#3801

  15. shiftleftplusone commented on Feb 19, 2016

    @shiftleftplusone

    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) !!

  16. 44 remaining items

  17. matthijskooijman commented on Jul 10, 2017

    @matthijskooijman
    CollaboratorAuthor

    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...

    If we want to go for the "fastest type with at least x bits", we could be using the int_fast16_t type from http://en.cppreference.com/w/c/types/integer. On AVR, the uint_fast8_t actually 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 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).

  18. cmaglie commented on Jul 10, 2017

    @cmaglie
    Member

    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.

  19. cousteaulecommandant commented on Jul 10, 2017

    @cousteaulecommandant
    Contributor

    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.)

  20. matthijskooijman commented on Jul 10, 2017

    @matthijskooijman
    CollaboratorAuthor

    Good point. byte is indeed nice, though less portable.

  21. bperrybap commented on Jul 10, 2017

    @bperrybap

    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.

  22. datenheim commented on Feb 5, 2020

    @datenheim

    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.

  23. cousteaulecommandant commented on Feb 5, 2020

    @cousteaulecommandant
    Contributor

    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).

  24. datenheim commented on Feb 5, 2020

    @datenheim

    "DUE and other SAM/SAMD based boards"

    or maybe

    "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?

    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

  25. cousteaulecommandant commented on Feb 7, 2020

    @cousteaulecommandant
    Contributor

    or maybe

    "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.

  26. datenheim commented on Feb 9, 2020

    @datenheim

    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 :)

  27. added
    feature request
    Library: Other
    on Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: DocumentationLibrary: Otherfeature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions