Closed josipk closed 1 year ago
This is expected behaviour. The int type is 16 bit, and if you print it as signed with the %d you get the first bit interpreted as -32768. You might want to use the %u formatter in the printf to get an unsigned result.
OK, so incorrect printf format. Thank You.
One more question, does the sqrt function work correctly with unsigned int?
int main(void) {
unsigned int n = 32760;
int i;
for(i=0; i<10; i++) {
n++;
int l=(int) sqrt(n);
printf("%u,%d\n",n,l);
}
return 0;
}
output:
32761,181 32762,181 32763,181 32764,181 32765,181 32766,181 32767,181 32768,0 32769,0 32770,0
It appears unsiged int to float does not work. But you can use Dijkstras integer square root instead: `// Integer square root based on Dijkstras algorithm
unsigned tsqrt(unsigned n) { unsigned p, q, r, h;
p = 0;
r = n;
{
h = p | q;
p >>= 1;
if (r >= h)
{
p |= q;
r -= h;
}
}
return p;
} `
There is also a version in <gfx/bitmaph.>
// Fast unsigned integer square root unsigned bm_usqrt(unsigned n);
Thank You, btw. the compiler is awesome :)
Unsigned Int is behaving like Singed Int. (Linux build environment)
include
include
include <c64/types.h>
int main(void) {
}
output: 32761 32762 32763 32764 32765 32766 32767 -32768 -32767 -32766