Go Down

Topic: Integers use much less space then Floats?! (Read 1 time) previous topic - next topic

mixania

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.
Code: [Select]

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


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

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?!  :smiley-eek:  :smiley-surprise:
Arduino Uno R3
Mac OSX Lion

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

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


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.
Arduino Uno R3
Mac OSX Lion

AWOL

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



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?
Arduino Uno R3
Mac OSX Lion

mixania


But analogRead will only ever return a two byte result.


Oh... Thats true. My bad
Arduino Uno R3
Mac OSX Lion

AWOL

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

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 won't respond to messages, use the forum please ]

fungus


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




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