# My math works in spreadsheet, but not in code

I’m not getting the expected output. I’m sure it’s a syntax or typing problem, but I can’t figure it out. This is my function. It oversamples an diode-type temperature sensor.

``````float getTemp(){

for (int i=0; i<15; i++){
delay(100);
steps+=dummy;
Serial.println(dummy);
}
Serial.println(steps);
steps/=4;
Serial.println(steps);

temp=(5*steps*100)/(4096)) + offset);
return temp;

}//getTemp
``````

This returns bizzare numbers. When I take the serial output that I get from all the debug messages, and put it in a spreadsheet, I get the correct numbers. There must be a problem with the line

`````` temp=(5*steps*100)/(4096)) + offset);
``````

For example, here is one loop serial output:

``````622
621
622
621
621
621
621
621
621
621
621
621
621
621
621
621
621
621
630
621
621
621
621
622
9325
2331
Temp: -70.45
``````

When I run 23315100/4096 I get 285 like I’m supposed to, not -70. Where am I going wrong with the code?

2331*5*100 doesn't fit in an int. Use long and an 'L' suffix on your numeric literals too

I see no variable declarations in that function...

2331*5*100 doesn't fit in an int

That's probably it. But I don't understand why. My variable 'temp' is a float. My variable 'steps' is an int. So if I multiply a bunch of ints together, does it blow up even if the thing I'm setting them equal to is long enough to contain the answer?

Would this fix it, if I make 'steps' a type long?

``````temp=(5L*steps*100L)/(4096L)) + offset);
``````

Ok if I just do temp=steps, I get about 3000 as expected. But if I do temp=steps/4096, I always get 0.00. Why don't I get .75 as expected?

Edit: I figured out you can't divide a long by an int and get a float, even if you are setting it equal to a float i.e. there's no magic autotyping.

There IS some auto-typing but not magic.

Since 5, steps, and 100 are all integers it figures you want an integer answer. If the two sides of an operation are of different sizes it will promote the smaller one to match the larger one and the result will be of the larger type. It would probably work if you only changed 5 to 5L OR changes 'steps' to a long.

When you do an assignment it evaluates the right side of the assignment and then converts to the type of the left side. That's why an integer division assigned to a float gives an integer answer. If either side of the division was a float (or cast as a float) you would get a float result:

temp = steps / 4096.0; or temp = (float) steps / 4096;

BetterSense: There must be a problem with the line

`````` temp=(5*steps*100)/(4096)) + offset);
``````

As already pointed out, you can cast this to float to solve your problem - you can also combine all your constants into one float to make your math faster:

`````` temp = ((5*100*steps)/4096)  + offset);
``````

becomes

`````` temp = (500.0/4096.0) * steps  + offset);
``````

becomes

`````` temp=0.1220703125 * steps + offset;
``````

Now one floating point multiply rather than some multiplies and a floating point divide, which is much faster.