Map function looks incorrect

I have been given that the map function should give this return;
value= (x-inmin)x(outmax-outmin)/(inmax-inmin) +outmin
These are my figures; val=map(73,74,0,0,,90). x=73.
The result using the second line gives 88.78. I am getting 26 using Arduino and those figures.
What is wrong here?

val=map(73,74,0,0,,90). x=73.

Try again. It would help if it were in the context of a functioning sketch, however small.

Review the documentation for the map() function.

a7

What is that? Why are there two commas in a row?

The map() function works with type long, so you'll never get a result like 88.78. It looks like you may also have the arguments in the wrong order. This is the map() function (for an Uno anyway):

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;
}

I'd say simply a typo............
https://docs.arduino.cc/language-reference/en/functions/math/map/

Nothing is wrong with map.
Your usage might be wrong.
Post a full compileable sketch showing your point.

I can't see how you can get the result 26 from an Arduino.

If you have the following line , then the code won't compile:

val=map(73,74,0,0,,90);

You get an error message 'expected primary-expression before ',' token'.

Removing the last comma to give:

val=map(73,74,0,0,90);

Then the result is 1.

To get a result close to 88.78, you need to use:

val=map(73,74,0,90,0);

The result is 89.

Here are the results that I got:

click for code

void setup() {
  Serial.begin(115200);
  
  long val1 = map(73, 74, 0, 0, 90);
  Serial.print(" val1 = ");
  Serial.println(val1);

  long val2 = map(73, 74, 0, 90, 0);
  Serial.print(" val2 = ");
  Serial.println(val2);
}

void loop() {
}

map should return
Xmin + (Ymax - Ymin ) * (x - Xmin) / (Xmax - Xmin)

i get the following

      1     88
      4     85
      7     81
     10     77
     13     74
     16     70
     19     66
     22     63
     25     59
     28     55
     31     52
     34     48
     37     45
     40     41
     43     37
     46     34
     49     30
     52     26
     55     23
     58     19
     61     15
     64     12
     67      8
     70      4
     73      1

char s [90];

void
setup (void)
{
    Serial.begin (9600);
    Serial.println ();

    const int Xmin = 74;
    const int Xmax =  0;
    const int Ymin =  0;
    const int Ymax = 90;

    for (int x = 1; x <= 74; x += 3) {
        int y = map (x, Xmin, Xmax, Ymin, Ymax);
        sprintf (s, " %6d %6d", x, y);
        Serial.println (s);
    }
}

void loop (void) { }

Looks like HTML to markdown struggle.

I think the trouble may be that per post #3, in_min = 74 and in_max =0 cause logical errors. in_min must be less than in_max.

not true
see the code i posted

@petercl14

Maybe useful instead of the map() function
-GitHub - RobTillaart/FastMap: Arduino library for Fast mapping and constraining

works with floats..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.