So I am trying to convert a number from a 0 to 270 range to a 0 to 180 range and it always outputs 0. I could be stupid but I am unsure what I'm doing wrong.
Strictly saying, not always.
Your function will return 0 for position values in range 0-269, but 180 for position = 270.
It is because the result of integer division x/y where x < y will truncated down to nearest integer, so always returns zero
Whatever this does or does not, there is nothing to do with 48600, or 270 * 180.
I ran this program
# include <stdio.h>
long a, b, c;
int main()
{
printf("Hello World\n");
a = 12345678 / 270 * 180;
b = 12345678 / (270 * 180);
c = 12345678 * 180 / 270;
printf("a = %ld b = %ld c = %ld\n", a, b, c);
return 0;
}
and got this reuslt:
main.cpp: In function βint main()β:
main.cpp:13:18: warning: integer overflow in expression of type βintβ results in β-2072745256β [-Woverflow]
13 | c = 12345678 * 180 / 270;
| ~~~~~~~~~^~~~~
Hello World
a = 8230320 b = 254 c = -7676834
...Program finished with exit code 0
Press ENTER to exit console.
The rearranged calculation of c overflows. The other calculataions are what you would expect.
why didn't you try using the value of 90 that the OP uses?
output
Hello World
a = 0 b = 0 c = 60
# include <stdio.h>
long a, b, c;
int main()
{
printf("Hello World\n");
#define X 90
a = X / 270 * 180;
b = X / (270 * 180);
c = X * 180 / 270;
printf("a = %ld b = %ld c = %ld\n", a, b, c);
return 0;
}