[prog] c types confusion

Alice Moomlyn alice_moomlyn at yahoo.com
Wed Mar 24 09:12:26 EST 2004



<old>
>    Firstly, I would have expected signed int
>    to wrap at 2^15, rather than 2^31.

Huh, why? Is that a typo/braino, or did you really
mean
2^15?
The size of signed int is 4 bytes, or 32 bits, 
and 2^32 / 2 == 2^31.
</old>

<new>
Whoops!
Silly me

<stupid grin />

2^15 + 2^15 = 2*2^15 = 2^16

2^31 + 2^31 = 2*2^31 = 2^32

I guess its only in newbie-land that 2^15 + 2^15 =
2^32 :-P
</new>

<old>
You have to tell printf that the integer you want to
output
is unsigned, of course! :-)

printf("%u", MyUnsignedInt);

Again, see printf(3).
</old>

<new>
Ahhh.... man *3* printf

I was doing a plain old:

man printf 

and getting:

man 1 printf 

(which doesn't tell you anything useful about %x
format strings)
</new>

<old>
>    Thirdly I would have expected float to start
>    losing precision well before 2^31.
>    But this didn't happen!

I don't understand the question. Can you elaborate on
why you
think so?
</old>

<new>
I think that the truth of the matter is that I don't
really understand how floating point numbers are
represented in a computer. But I always imagined it
was something like this:

You have 8*sizeof(float) bits to work with 
(on my computer that would be 32)

Those 32 bits now have to be split between the base (I
don't think that's quite the right word) and the
exponent (mantissa?).

So if you have: 2.456e14 then you'd have x bits to
store the 2456 and y bits to store the 14 and you are
constrained by x + y = 32.

Now, 2^32 = 4294967296 = 4.294967296e9, and its going
to take all your 32 bits to store the: 4.294967296,
leaving nothing to store the: 9, so won't it get
approximated to: 4.294967e9, or something?
</new>

=====
"the only way to learn how to be clever is to say stupid things" - anon

__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html


More information about the Programming mailing list