# Unsigned integer and map function

if a map function returns a negative value and the data type selected is an unsigned integer would the return be a zero value? I am guessing it would be but if not the program might fail. zero is ok with me for my program.

Why not create a small test?

Definitely not.
Every negative value in signed types corresponds to some positive value in unsigned type, and not to zero.

Hello petercl14

store the returned value in a signed type.
afterwards use constrain() to limit it to 0 (or a simple if < 0 then 0)

Yes I might do that. Had hoped for a positive answer if somebody would already know what the result would be.

are you saying if the result is say -7 you would get a +7?

the total sketch is huge. wouldn't want to burden you guys with it. only need a solution to a small part of the sketch

that sounds good to me. i was hoping somebody would know for sure if the return was zero. I did get a "definitely not" from b707 but then he didn't say what the return would be. I think he may mean if the return was -7 you would get +7.
noiasca has a possible fix

As described in the forum how to you can always strip down the question to a MRE (minimal reproducible example).

That makes answering questions so much easier for us, and it even might be that you will get a full working example back. Not just a theory.

Furthermore there is a chance that you spot the answer when you just concentrate on the thing you want to solve.

Most probably not. The exact result will depend on unsigned type.
For uint16 type unsigned value corresponds to -7 is 65529 (0xFFFF - 7)
For long type it will be 4294967289.

You can test any defined value via online calculator:

Why didn't you test it yourself, as suggested in #2 ?

The source of the `map()` function is available in the Arduino documentation:

``````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 don't see anything in it that could convert negative values to zero.

`````` n  10 0x000a, u     10 0x0a
n   7 0x0007, u      7 0x07
n   4 0x0004, u      4 0x04
n   1 0x0001, u      1 0x01
n  -2 0xfffe, u  65534 0xfffe
n  -5 0xfffb, u  65531 0xfffb
n  -8 0xfff8, u  65528 0xfff8
``````
``````
char s [90];

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

unsigned u;

for (int n = 10; n > -10; n -= 3)  {
u = n;
sprintf (s, " n %3d 0x%04x, u %6u 0x%02x", n, n, u, u);
Serial.println (s);
}
}

void loop (void) { }
``````