documented length of Double appears to be incorrec

I just downloaded the IDE and have been reading the documentation (which I assume came with the IDE). The definition of a Float is 4 bytes; the definition of a Double is ... 4 bytes. I assume the latter should be 8 bytes.

"Dear Mr. Owl, how many licks does it take to get to the center of a Tootsie Roll Tootsie Pop?"

"Let's find out."

void setup()
{
    Serial.begin(9600);
    Serial.println(sizeof(float));
    Serial.println(sizeof(double));
}

void loop() { ; }

4
4

Why would one call something a "Double" when it is no larger than a Float, then? Confusing. To me at least!

:slight_smile:

First, the easiest way to answer some questions is to ask the computer. It's not hard to download a program to the Arduino, so, why not? :slight_smile:

Second, the reasoning for this decision is pretty clear. To save space and reduce complexity. The C language defines the sizes of char and long. It leaves most of the others up to the particular implementation on the particular platform. The size of an int should be a convenient size for the registers of the platform, and may be the same as long, or even bigger than long.

Some platforms might not even support float or double at all. Now, when you use a particular math operator, say, adding int to int, the compiler needs to know what instructions to implement that operator for the target machine. If you start using floating point arithmetic, a large number of subroutines must be included in your program, eating up valuable space. If it supported double as a different size and precision, it would bloat the program space even more.

The AVR platform chose to make just ONE precision of float, and double is just an alias. This is to maintain compatibility with math.h, which traditionally uses double for all arguments.

Documentation says it is "double precision", but I gather it occupies the same storage as a "single precision" value. Strange.

That said, I suppose I should stop beating the horse, since it clearly is dead.

Thanks for your time!

:slight_smile:

Jon

I found out the hard way that double didn't mean double too :slight_smile: Wasn't real happy either when I found out, as it meant I had a lot of work to do to get the desired precision.

Funny stuff indeed. Just about the only thing you are guaranteed in C is that a long is at least as long as a short, a short at least as long as a char, and a double no less precise than a float. :slight_smile:

sometimes confusing, but nothing is broken or wrong with it - that's the way the language is defined, at least in the original version (I'm not so sure about ANSII, but I think it still holds).

In the original C "specification" (the K&R book), 4 example machines were listed describing the size of various standard types: only two were 8 bit ASCII, one was 9 bit ASCII, and the other was 8 bit EBCDIC. At least we don't have to deal with those last two anymore. (:

This sort of flexibility is one reason C is used so widely.

-j

If this is true, it seems we should just delete the docs for the double, since it appears to just be redundant with float.

I'd like to confirm this by doing the math outside of
Serial.print because I've had other math fail. But it seems like others have confirmed this too.

Is this documented in AVR-GCC too?

paulb

Paulb. it is true.

There is a reference to float and double being 32 bits in the compiler user manual: Frequently Asked Questions (see data types)

The arduino reference also implies that a double is the same size as a float although it doesn't come right out and say they are the same. I will post a request to add this in the bugs and suggestions threads.

paulb, they're the same thing. But it's an extremely old and pervasive convention to use double for standard math headers. Neglecting to mention it in the docs would confuse people who saw that in the header files. Though I'd say it could be made more obvious in the Arduino docs that they're identical.

I posted a request to clarify the docs here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1232860278/0#0