# mapping floating point numbers

Hello,
I urgently require to map the analog readings from my pot, in range 0-1023 , to a floating point range like: 0.001,0.002,......,15.99,16.00. For this the ordinary map function doesn't serve the purpose, i tried using fscale , but it says that "fscale was not declared in this scope". This is my code:

void setup() {
Serial.begin(38400);
}

void loop() {

float valp = fscale(0,1023,0,16,analogRead(A0),0);

}

Map the integer to within the limits of the float time a power of ten. Divide the result by said power of ten.

Say for example, you need a value between 0.00 and 16.00. Example:

``````int value = analogRead(5);
int newval = map(value, 0, 1023, 0, 1600);
float result = newval/100;
``````

Hey,
No luck, now I am getting values from 0.00 ,1.00,......15.00,16.00. Not the correct values.

Try casting newval to a float and making the literal a float too.

``````int value = analogRead(5);
int newval = map(value, 0, 1023, 0, 1600);
float result = (float)newval/100.0;
``````

Thank you, that worked..

dxw00d:
Try casting newval to a float and making the literal a float too.

``````int value = analogRead(5);
``````

int newval = map(value, 0, 1023, 0, 1600);
float result = (float)newval/100.0;

Whoops, forgot the cast can be done in one line as the mapping contains the point (0,0);

try this

``````// 0.015640274 == 16.0 / 1023.0

#define FACTOR (0.015640274)

void setup()
{
Serial.begin(38400);
}

void loop()
{
float result = analogRead(A0) * FACTOR ;  // * 16.0 / 1023.0
Serial.print(result, 3);
Serial.print("\t");

float result = analogRead(A1) * FACTOR;
Serial.print(result, 3);
Serial.print("\t");

float result = analogRead(A2) * FACTOR;
Serial.print(result, 3);
Serial.println();
}
``````

using CTRL-T in the IDE cleans up the layout of your code, and code tags makes it better readable