Go Down

### Topic: Integers use much less space then Floats?! (Read 2574 times)previous topic - next topic

#### mixania

##### Oct 08, 2012, 08:32 pm
Hi Guys!

Recently i've been doing a project using my Arduino Uno R3, which is very simple. It is basically just a photoresistor that is used as a motion detector and a 8-ohm speaker from Radioshack. I want to make a burglar alarm that turns on when it spots significant changes in light.

So my code is very short, just above 30 lines of code. You don't really have to study it. But maybe by looking at it would help you answer my question.
Code: [Select]
`int sensor = 14; // sensor pinint sound = 9;   // speaker pinvoid setup() {  Serial.begin(9600); // Serial for Debuging}void loop() {  Serial.println(analogRead(sensor)); // Print out to Serial  int comp = (analogRead(sensor));    // Comp integer which is used later on in the "for" statement to find by how much the light intensity has chaged.   Serial.print("Sensor Read = ");     // Debug it  Serial.println(comp);    for (int i=0; i<50; i++) {          // Comparing 50 times    int eye = (analogRead(sensor));    int dif = comp - eye;             // if is diffirence between the comp light light measure and the current measure         if (dif < -10 || dif > 10) {   // If there is an aceptable diffirence in this case 10 then do a sound       tone(sound, 6000);       delay(3000);       noTone(sound);       break;       Serial.println("------------------------------------");       Serial.print("Diffirence = ");       Serial.println(dif);       Serial.println("------------------------------------");     }      delay(100);                      // do this comparing 10 times per second  }}`

But here is a Mystery (or a fact?)  When I change the
Code: [Select]
`int eye = (analogRead(sensor));int dif = comp - eye;            `

To

Code: [Select]
`float eye = (analogRead(sensor));int dif = comp - eye;             `

The Sketch size increase from 4,502 bytes to 5,178 bytes, that somewhere 600 bytes. Half a Kilobyte!!

Now i know that longs range from something like -2,123,424,234, to 2,123,453,345 but why do they use so much space?
Would've the programmers from the 60's killed their self if they found out that just one number took the whole computer disk space at that time?!

#### AWOL

#1
##### Oct 08, 2012, 08:35 pm
The processor has no floating point hardware, so fp operations have to be emulated in software.
This takes up program memory.
A float is four bytes, an int is two on the Arduino.
Variables reside in RAM, not program memory.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### PaulS

#2
##### Oct 08, 2012, 08:41 pm
But here is a Mystery (or a fact?)  smiley-roll-sweat When I change the

Code: [Select]
`int eye = (analogRead(sensor));int dif = comp - eye;`

To

Code: [Select]
`float eye = (analogRead(sensor));int dif = comp - eye;`

Why would you do that? The analogRead() function returns an int. Storing the two byte int in a 4 byte float wastes space and adds no value.

#### mixania

#3
##### Oct 08, 2012, 08:46 pm

Why would you do that? The analogRead() function returns an int. Storing the two byte int in a 4 byte float wastes space and adds no value.

True, But i just did it as to show an example. Maybe i'll use a light sensor or other hardware later on requiring longs.

#### AWOL

#4
##### Oct 08, 2012, 08:47 pm
Quote
Maybe i'll use a light sensor or other hardware later on requiring longs.

But analogRead will only ever return a two byte result.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### mixania

#5
##### Oct 08, 2012, 08:50 pm

The processor has no floating point hardware, so fp operations have to be emulated in software.
This takes up program memory.
A float is four bytes, an int is two on the Arduino.
Variables reside in RAM, not program memory.

Thanks, but how does it only resides in RAM if 600bytes of program memory are being used up?

#### mixania

#6
##### Oct 08, 2012, 08:52 pm

But analogRead will only ever return a two byte result.

Oh... Thats true. My bad

#### AWOL

#7
##### Oct 08, 2012, 08:56 pm
Quote
Thanks, but how does it only resides in RAM if 600bytes of program memory are being used up?
The AVR processor core has NO native support for floating point.
If you want to add two floating point numbers, you have to write a sequence of assembler instruction (which reside in program memory) to fetch, operate on and store the two lots of four bytes of RAM which represent the two floating point numbers.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### MarkT

#8
##### Oct 08, 2012, 09:00 pm
The Arduino software is clever enough to only compile into the binary sketch the code that is actually used - so if you don't use floats you don't get any of the float code incorporated.  Ditto all the other stuff and libraries.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### fungus

#9
##### Oct 08, 2012, 09:08 pm

Now i know that longs range from something like -2,123,424,234, to 2,123,453,345 but why do they use so much space?

What increased most was your program size because the compiler pulled in extra code for the floating point operations (which are done in software).
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#### tuxduino

#10
##### Oct 08, 2012, 10:00 pm

The processor has no floating point hardware, so fp operations have to be emulated in software.
This takes up program memory.
A float is four bytes, an int is two on the Arduino.
Variables reside in RAM, not program memory.

Thanks, but how does it only resides in RAM if 600bytes of program memory are being used up?

Program memory is physically different from RAM.

http://www.arduino.cc/playground/Learning/Memory

Go Up

Please enter a valid email to subscribe