Stroke of integer genius

Started by
22 comments, last by Sik_the_hedgehog 11 years, 1 month ago

There I was, all alone - in debugging hell.

Why was our GUI not able to display the required disk space that was needed? Why was it showing 0?!

And why did our unit tests of the FileManager all shine of vibrant green?!

I started deep in the mounts of what our prophets label the business layer -- it was correctly calculating the bytes based on the file sizes.

I continued upward towards the swamp-like territory of the GUI-people.

There I stood. Face to face, with...


int gb = (int)fileManager.RequiredDiskSpace / 1024 / 1024 / 1024;
Advertisement

I'm not sure what's worse here. The fact that "gb" can only take two different values (zero or one) on 32-bit systems, or the lack of rounding causing huge accuracy problems (0.99GB anyone?)

Probably should make those floats or doubles anyway. That way you can't screw up! (but you still will)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Sadly reminiscent of a certain company's installers which - last time I checked - still hadn't been updated for that magical moment when drives > 2gb became not only possible, but also commonplace. Shudder.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

To be politically correct, that should say:


int GiB = (int)fileManager.RequiredDiskSpace / 1024 / 1024 / 1024;
 

Beginner in Game Development?  Read here. And read here.

 

I should never have digged through this part of the code base.

In a function (EstimateTime), a background thread with a while(true) loop (terminated using Abort() -- yeah, Abort), at the end I found this comment:


Thread.Sleep(500); // Estimate time every 5 seconds.

Yeah, that's not how milliseconds work.

And why not use the built in C# class Timer?

I don't know. I give up.

I should never have digged through this part of the code base.

In a function (EstimateTime), a background thread with a while(true) loop (terminated using Abort() -- yeah, Abort), at the end I found this comment:


Thread.Sleep(500); // Estimate time every 5 seconds.

Yeah, that's not how milliseconds work.

And why not use the built in C# class Timer?

I don't know. I give up.

Thanks for these examples. I use them as "warm up" exercises for the C# programming class I teach. My 16-17 year old students wondered why anybody in their right mind would have used an int for that disk space operation if you were working with numbers that should easily be 2+ billion. One of my female students chimed in and wondered why they would do that given that integer division will result in truncated remainders.. she thought it would dramatically skew the calculation. I wonder how many of them would have caught it if it was a debugging exercise as part of a larger project example though.

Granted this question is dependent on when that code was written, but int is normally 2^(number of bits in the CPU). So on a modern processor (64 bit) an unsigned int or even int shouldn't be a problem.

Though to be clear, I know a long would be the better and safer choice.

Beginner in Game Development?  Read here. And read here.

 

Actually, int remains 32-bits for most languages and compilers out there.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Actually, int remains 32-bits for most languages and compilers out there.

Really? I thought it was based on the CPU and you had to target the compiler to 32-bit or whatever XX-bit.

Beginner in Game Development?  Read here. And read here.

 

Actually, int remains 32-bits for most languages and compilers out there.

Really? I thought it was based on the CPU and you had to target the compiler to 32-bit or whatever XX-bit.

Aye, on practically all compilers I know of (other than for more obscure chips), int is a 32-bit value. GCC and Clang treat long as a 64-bit value on 64-bit systems, but MSVC requires you do use long long. In C++, at least. Languages like C#, int is 32-bit, long is 64-bit.

This topic is closed to new replies.

Advertisement