Unexpected negative values

Hey Arduinoers,
in the given function:


while x starts as 0 and increases by 20 each time , id expect to get only positive increasing v values. but as you can see in the Serial monitor screenshot, i get
negative values and it's bouncing between minus 10 to plus 10 repetitively.

Thanks guys

Hello hadasdv

Welcome to the worldbest Arduino forum ever.

I assume that you have written the programme by yourself, then it is quite easy to find the error.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code to see the values of variables, especially ones that control the motors, and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

Welcome to the forum.

Please start by reading this post: How to get the best out of this forum

In particular: Posting code and common code problems - this will tell you how to properly post code - not as an image!

Unfortunately, that expectation is incorrect:

If you only want positive values then, as the name suggests, you should use unsigned int

A plains int is signed; ie, it can contain both positive and negative values.

If you keep increasing a (signed) int, it will eventually overflow - and that overflow will be seen as a "roll-over" to negative numbers.

If you keep increasing an unsigned int, it will also eventually overflow - this time, the "roll-over" will go back to zero.

Integer overflow - Wikipedia.

But that's exactly what @hadasdv has done - the question is why that's giving unexpected (to the OP) results.

1 Like

(x * 255)
if x = 140, then the result is 35,700, which doesn't fit in an int, so it will overflow negative.

Try
bright = (x * 255.) / (fadetime * 1000.);
Leo..

Thank you for the information, here's the code:


#define base 3
float bright;


int brightness (int x) {
  int v;
  bright = (x * 255) / (fadeTime * 1000);
  v = (int)bright;
  Serial.print("v value is");
  Serial.print(v);
  Serial.print('\n');
  Serial.print("x value is");
  Serial.print(x);
  Serial.print('\n');
  Serial.print("bright value is");
  Serial.print(bright);
  Serial.print('\n');
  return v;
}

Unfortunately, that expectation is incorrect:

If you only want positive values then, as the name suggests, you should use unsigned int

A plains int is signed; ie, it can contain both positive and negative values.

the problem is in the calculation of the "btright" value. when x equals to 140, for example, bright should be 11.9 but instead i get -9

Thanks man, what does the dots do?

it worked!!
thanks Leo you're amazing!!

The dots will be seen as decimal points, so the calculations will be done as floating point.

That's a grossly inefficient way to do it! :astonished:

Sometimes, efficiency is important, clarity not so much. In this case, clarity wins, IMHO.

can't be that clear - as it had to be explained! :stuck_out_tongue_winking_eye:

Adding a UL (maybe just L?) suffix would probably also do it ?

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