0

I'm generating a random integer (between 0 & 99), converting it into a string and successfully transferring it over bluetooth to a second machine, which then converts it back to an integer for further processing. However any subsequent calculations result in an answer of 0. I'm using: -

str_int = strtol(buf, NULL, 10);

But when I then try to: -

height = (str_int / 100);

The answer is always 0. What am I missing? I need to eventually scale this random number between -0.5 and 0.5 for OpenGL processing.

Disposer
  • 6,201
  • 4
  • 31
  • 38
ranger3
  • 23
  • 3

1 Answers1

2

Assuming height is a float or double:

height = (str_int / 100.0);

Integer division in C always truncates to an integer before assigning it to your variable. Putting a .0 at the end of the number informs the compiler that you want that number to be interpreted as a double and hence the compiler will interpret the expression as a floating point division.

user3386109
  • 34,287
  • 7
  • 49
  • 68
JohnH
  • 2,713
  • 12
  • 21