new map function?

For a bit of my code, I need a function that could display 2 decimal places in the serial monitor and uses a potentiometer.
I have thought to do it with the map function, but that wouldn't work.
That would only increase with 1.00 and not like 0.05 what I want.

What I have done is this:
Use the map function to constrain it between 0 and 300 and then divide it by a 100, so I could get the value between 0.00 and 3.00.

here is the code that I used.
I'm working with a esp-32.

#define potentiometer 34 
#define TOLERANCE 5 
int oldVal;
float devVal;
void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
pinMode(potentiometer, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:

  int val = analogRead(potentiometer);
  val = map(val, 0, 4095, 0, 300);

  delay(15);

  int diff = abs(val - oldVal);

  if (diff > TOLERANCE)
  {
    // lcd.backlight();
    oldVal = val;// only save if the val has changed enough to avoid slowly drifting
    devVal = val / 100; 

  }
  //Serial.print(val);
 // Serial.print(" ");
 // Serial.print(oldVal);
 // Serial.print(" ");
 // Serial.println(devVal);
}

instead of using integer maths for the map() function (that's how it's defined)

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

just write a new mapFloat() function where all the parameters are floating points. Then you won't get the issue with integer maths

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
  val = map(val, 0, 4095, 0, 300);

Range translation where the basis is zero, is not an appropriate use of map(). It's far simpler and clearer to simply do the math:

val = val * 300.0 / 4096.0;

Thank you for your reply and help!

J-M-L:
just write a new mapFloat() function where all the parameters are floating points. Then you won't get the issue with integer maths

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)

{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

that's exactly what I needed.

aarg:
Range translation where the basis is zero, is not an appropriate use of map(). It's far simpler and clearer to simply do the math:

i have tried this but the function only will return: 0.00

sebasdt1:
i have tried this but the function only will return: 0.00

What function? You asked for a floating point solution, then you complain when it doesn't work with integer math...

Sheesh, then

float result = val * 300.0 / 4096.0;

that's exactly what I needed.

No, when in_min and out_min are 0, then just doing the math as aarg explains is faster (it's the same formula with the null members not being listed)

fmap?

TheMemberFormerlyKnownAsAWOL:
fmap?

yes

aarg:
What function?

sorry I have misread your post.

aarg:

val = val * 300.0 / 4096.0;

the mapFloat the function here above function just work fine

TheMemberFormerlyKnownAsAWOL:
fmap?

when I put this in the ide it doesn't turn orange. is this function not implemented in the ide?

just change the name mapFloat() to fmap() which is what it is usually called