Thermistor Question

Hello, I have wired a thermistor to be read into an Arduino Uno board on pin A0. I have essentially made a voltage divider between the 5V source and the ground. The setup is:

Ground ------1k Resistor ------- Thermistor ------+5V
I am running this code on the board:

#include <math.h>

int ThermistorPIN = A0;

double Thermistor(int RAWADC){
  double Temp;
  double rinf; 
  double T0 = 25;
  double R0 = 3000;
  double R;
  double B = 3891;
  R = 1024*1000/RAWADC-1000;
  rinf = R0*exp(-B/(T0+273));
  Temp = B/log(R/rinf);
  return Temp;

void setup() {

void loop() {
  float Read_temp;
  Read_temp = Thermistor(analogRead(ThermistorPIN));
  Serial.print("Temperature: ");

So my problem is after I run the R = 10241000/RAWADC-1000; instruction. I keep getting a negative value, but from my reading about the datatypes the value should be well within the range of a double variable. I have tried tracing the problem and noticed that when I do 10241000, the result is always -24576. Any ideas about the problem here?

In AVR-GCC used on the Arduino, "int" is a 16-bit number whose value can be -32768...32767. You are encountering an integer overflow. Since you are looking for a floating point result, you might as well do the calculation using floats. Something like this:

R = 1024.0 * 1000.0 / (float) RAWADC - 1000.0

This will not overflow and should give you the result you're expecting.

double R;

Also keep in mind that AVR-GCC doesn't implement double. Anything declared as a double/long float is just treated as a float. This won't really cause any problems, unless you have a habit of testing code on a real computer before running it on Arduino in which case you may have some precision surprises. Best practice is to stick with float.