Integers use much less space then Floats?!

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

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.

int sensor = 14; // sensor pin
int sound = 9;   // speaker pin

void 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?) :cold_sweat: When I change the

int eye = (analogRead(sensor));
int dif = comp - eye;

To

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!! :astonished:

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?! :astonished: :open_mouth:

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.

But here is a Mystery (or a fact?) smiley-roll-sweat When I change the

int eye = (analogRead(sensor));
int dif = comp - eye;

To

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.

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

Maybe i’ll use a light sensor or other hardware later on requiring longs.

But analogRead will only ever return a two byte result.

AWOL: 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?

AWOL: But analogRead will only ever return a two byte result.

Oh... Thats true. My bad

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.

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.

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

mixania:

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