Go Down

### Topic: documented length of Double appears to be incorrec (Read 3770 times)previous topic - next topic

#### jon_bondy

##### Dec 30, 2008, 03:20 am
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.

#### halley

#1
##### Dec 30, 2008, 04:08 amLast Edit: Dec 30, 2008, 04:16 am by halley Reason: 1
"Dear Mr. Owl, how many licks does it take to get to the center of a Tootsie Roll Tootsie Pop?"

"Let's find out."

Code: [Select]
`void setup(){    Serial.begin(9600);    Serial.println(sizeof(float));    Serial.println(sizeof(double));}void loop() { ; }`

Quote
4
4

#### jon_bondy

#2
##### Dec 30, 2008, 04:13 am
Why would one call something a "Double" when it is no larger than a Float, then?  Confusing.  To me at least!

#### halley

#3
##### Dec 30, 2008, 04:17 am

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?

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.

#### jon_bondy

#4
##### Dec 30, 2008, 04:24 am
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.

Jon

#### dcb

#5
##### Dec 31, 2008, 08:17 pm
I found out the hard way that double didn't mean double too  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.

#### mikalhart

#6
##### Jan 05, 2009, 02:32 am
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.

#### kg4wsv

#7
##### Jan 05, 2009, 03:31 pm
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

#### paulb

#8
##### Jan 25, 2009, 05:54 amLast Edit: Jan 25, 2009, 05:54 am by paulb Reason: 1
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

#### mem

#9
##### Jan 25, 2009, 06:04 am
Paulb. it is true.

There is a reference to float and double being 32 bits in the compiler user manual: http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage  (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.

#### halley

#10
##### Jan 25, 2009, 06:05 am
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.

#### mem

#11
##### Jan 25, 2009, 06:12 am
I posted a request to clarify the docs here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1232860278/0#0

Go Up