scaled integers - trying to figure out what size bit value a function returns

Hi,

I'm reading some code that returns a 14 bit temperature value. Here is the function.

/**
 * Reads the current temperature in degrees Celsius
 */
float readTemperatureC()
{
  int val;                // Raw value returned from sensor
  float temperature;      // Temperature derived from raw value

  // Conversion coefficients from SHT15 datasheet
  const float D1 = -40.0;  // for 14 Bit @ 5V
  const float D2 =   0.01; // for 14 Bit DEGC

  // Fetch raw value
  val = readTemperatureRaw();

  // Convert raw value to degrees Celsius
  temperature = (_val * D2) + D1;

  return (temperature);
}

Here the function has been modified to return an int instead of a float. The reason is because the 'float library is very large'. I'm working with an arduino and another non-arduino platform. The conversion coefficients have been scaled by 10,000 to integers. i.e. 0.01 became 100.

I wondering now what bit value would the function return? It is no longer 14 bit, correct?

//Reads the current temperature in degrees Celsius
int readTemperatureC()
{
  int val;                // Raw value returned from sensor
  int temperature;      // Temperature derived from raw value

  // Conversion coefficients from SHT15 datasheet
  const int D1 = -400000;  
  const int D2 =   100; 

  // Fetch raw value
  val = readTemperatureRaw();

  // Convert raw value to degrees Celsius
  temperature = (val * D2) + D1;

  return (temperature);
}

Assuming that 100 C is the max, 10,000 * 100 is 1,000,000 which won't fit in 16 bits. You need a 32 bit long int.

Hi Keith, thanks the non-arduino target is 32 bit therefore I guess the code will work on it.

The value could be (100*10000) -400000 = 600000. So does it return a 20 bit number now instead of a 14 bit number.

I think you would be fine making .01 to be 1 - You don't have that much accuracy anyway.