Skip to content

multiMap doesn't check for proper size value #13

Description

@noiasca

In https://forum.arduino.cc/t/hat-multimap-eine-begrenzung/1432289/ a user reported a problem with arrays of 331 values.

The constructor of multiMap accepts only uint8_t for the array size.
The code breaks after 255 values.

Either check for >=255 or make the code working for higher values.

Proposal:
changing the variable size to a size_t solves the issue (for the parameter and the local variable pos)

template<typename T>
T multiMap(T value, T* _in, T* _out, size_t size)
//T multiMap(T value, T* _in, T* _out, uint8_t size)   // breaks
{
  //  output is constrained to out array
  if (value <= _in[0]) return _out[0];
  if (value >= _in[size-1]) return _out[size-1];

  //  search right interval
  //uint8_t pos = 1;  // _in[0] already tested         // breaks
  size_t pos = 1;  // _in[0] already tested     
  while(value > _in[pos]) pos++;

  //  this will handle all exact "points" in the _in array
  if (value == _in[pos]) return _out[pos];

  //  interpolate in the right segment for the rest
  return (value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
}

other functions might be also effected.

(added syntax highlight)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdocumentationImprovements or additions to documentationenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions