Monday, March 16, 2009

unsigned long long long long long int

Congratulations! You've encountered one of the more hair-brained stupid schemes of the past century. There is no way in ANSI C/C++ to say, "I want X bits of storage for this integer."

Your initial thought might be, "Wait a minute, that can't be right...right?" Let us look at the various data types available to an ANSI C/C++ programmer:

char
int
float
double
struct/class/template/union
pointer

A 'char' lets us declare a single byte. A byte is 8 bits...or is it? CHAR_BIT (in limits.h) is typically defined as 8 bits (and the Standard requires it to be defined as a _minimum_ of 8). However, it could be defined as 9 and there has been some hardware where it is defined as 32. Additionally, it is up to each compiler to decide whether just saying 'char' is signed or unsigned.

Thankfully 'float' and 'double' use the IEEE floating point standard. Edit: Due to some response, I should clarify: Every compiler I have ever used (and I've used quite a few) relies on IEEE floating point for float and double. However, the ANSI C/C++ Standard does not require IEEE floating point to be used (it just happens to be convienently used basically everywhere).

The struct/class/template/union are not technically 'data types' but more of a grouping mechanism to group logical chunks of data types (and data) together.

Pointers can point at any of the other data types.

Now that I've worked through all the other types, we are left with 'int'. People first get their first taste of this problem when they discuss sizeof(int) on the various forums out there. They learn quickly about 'short', 'long', 'signed' and 'unsigned'. Below is a chart of what the ANSI C/C++ Standard says is the _minimum_ number of bits for each type of 'int':

short int - 16
signed short int - 16
unsigned short int - 16
int - 16
signed int - 16
unsigned int - 16
long int - 32
signed long int - 32
unsigned long int - 32
long long int - 64
signed long long int - 64
unsigned long long int - 64

Those are the minimum number of bits. Each compiler author chooses what various forms of sizeof(int) are in terms of bits. So, some compilers are 16. Some are 32. Some are 64. And there are even a couple compilers that define sizeof(int) as 24 bits. It varies so widely.

The 'long long' type was added so that 64-bit hardware could be programmed for in C/C++. And, to accommodate this hacky extension of C/C++, you use the printf()-family format specifier %lld, %lli, or %llu to display the integer.

So, now here is a question to ponder: What happens when native 256-bit data types show up in hardware? Are we going to create more hacky accommodations and sit all day writing 'unsigned long long long long int/%llllu'? What happens if some popular compiler vendor decides that 'unsigned long long long long int' is 384 bits? Or are we going to start mixing 'short's and 'long's - 'unsigned long long short long int'? How much longer will this "minimum number of bits" foolishness continue? The lack of being able to declare how many bits/bytes we need for integer representation is silly and only going to get sillier as int sizes get larger.

Someone will point out bitfields. I hereby point any such someones at the numerous issues surrounding bitfields. Bitfields are typically restricted to the size of an 'unsigned int', have poorer performance (compared to doing the same things they do yourself), are only available
inside 'struct's, etc. They are a modest solution, but hardly workable in the long run.

'int' (implicitly 'signed') and 'unsigned int' should be all we need and should mean, "I don't care how many bits there are for this integer and trust the compiler to generate enough bits for the target platform for most common operations." That is - 32 bits for 32-bit platforms, 64
bits for 64-bit platforms, etc. For specific declarations where we need exact bit sizes and refined control, I'd like to see something like:

int@64 x, y;

To declare a couple of 64-bit signed integers. The '@' is illegal C/C++ right now (i.e. won't compile if you try it) but would be a pretty good candidate.

For printf() and similar routines, %@64i seems like it could work well enough. The '@' symbol also avoids existing printf() "extensions" such as multilingual parameter support. This suggested approach would make things more readable and this approach also allows us to get rid of the near-worthless 'short' and 'long' keywords from the language.

I'm definitely agreeable to restrictions on such an addition to the language such as "implementations may choose to place a limit on the value after the '@'". And such limits could be "multiple of 8" or "power of 2" or "between 1 and 256 inclusive" (or a combination). For where a compiler has restrictions and does not necessarily provide native support, it should be possible to do something like this:

#if !native(int@512)
typedef __Int512Handler int@512;
#endif

int@512 x, y;

The 'native' preprocessor keyword would mean, "If the compiler supports this type natively" (the above example means, 'If the compiler does NOT support this type natively'). The above example would allow printf() to assume that the value passed in will be of the specific class if the @value falls out of range of native values. The class would provide the necessary logic to handle all normal integer operations.

Even with the above, the compiler author should still be able to put restrictions on what can be declared. Let's say the compiler author only wants to support "integers that are multiples of 8 and powers of 2 up to 512 bits" and the hardware only supports everything in that range up to 64-bits natively, then the author only has to define 128, 256, and 512 bit support. As each platform comes into existence, native support can be added and, in the event older native elements disappear (e.g. 16-bit), support can be relegated to a class. Old code would just simply need to be recompiled for the new hardware.

Sadly, though, the ANSI C/C++ committees aren't likely to ever see this suggestion. So, we will likely end up writing 'unsigned long long long long short long long long short short long int' in the not too distant future.


Edit: Someone pointed out stdint.h to me. I feel slightly embarrassed for not knowing about this BUT implementations are only required to provide definitions for types that the hardware and compiler supports. My approach does something similar EXCEPT it allows us to get rid of a potential plague before it becomes quite troublesome: 'short' and 'long'. The whole point of this post is that we are using 'short' and 'long' for something they are NOT intended to be used for - to define integers of specific bit-lengths and if we keep on going like this, it will come back around to haunt us later. stdint.h is definitely a good starting point but we need to get rid of 'short' and 'long' in favor of a more generic approach.

"Hair-brained stupid schemes" is a great phrase.

Saturday, March 14, 2009

Into the Deep Fryer

I've ranted before about the poor quality of today's constructed components. I have another rant today that isn't really computer related but still interesting nonetheless.

Back in the day when IBM reigned supreme, they came up with a piece of "big iron" that still runs much of the financial world. Most people will recognize the following instantly: AS/400.

What crossed your mind when I said that? "Old" and "Ancient" come to the forefront of my mind. But are immediately followed by the words and phrases, "Reliable", "Solid", "Well-built", "Well-engineered".

Now think about today's software and hardware. Do you think the same things about reliability, being solid, well-built, and well-engineered? Probably not. This is a plague in our world and it boils down one simple truth: People don't care deeply about their jobs today. And that lack of caring results in slipshod, poor quality design and construction. Worse, people don't take responsibility for their lack of care when something really bad happens. Even worse, we have laws in some cases where people who fail at life can simply point at the law and say, "Well, the law says we have to do that, so we did........and therefore I'm not at fault."

And this brings me to deep fryers. "Haha" jokes aside (yes, I realize I could do a number of humorous bits here), a deep fryer is a pretty dangerous piece of equipment. You have to be responsible to own and operate one. You've got a heating element that can melt and/or set fire to virtually anything, really hot oil flying everywhere capable of inflicting 3rd degree (or worse) burns and/or starting grease fires, and typically some (usually flimsy) electrical cable that gets feedback from the heating element and gets rather hot too (electrical fire anyone?)

I'm looking for a deep fryer at the moment and, while I'm not too terribly shocked because I know no one cares about their day job to actually make a quality deep fryer, I am a bit shocked at what I am finding. I decided to go on to Amazon.com to look at deep fryers. I am a bit old-school I guess because all the deep fryers I've worked with in the past reach a nice toasty 375 degrees Fahrenheit. Anything less than that will cause whatever you put into the fryer to soak up the oil. Now here's something you probably didn't know: Fried foods can actually be healthier than baked foods, but only if the oil is hot enough and recovers quickly after putting the food in. The reason why it is healthier is because it won't absorb the oil and the oil will burn off the fat during the cooking process leaving you with a nice, lean dish that is moist and tastes great. 375F is the recommended temperature, but 400F is the ideal initial temperature because the temperature will drop rapidly upon putting the food in (about 50 degrees in a matter of seconds). Anything lower than 375 will cause the food to absorb the oil and keep the fat intact, which is worse (healthwise) than if you baked whatever you were trying to fry. Additionally, vegetable oil is healthier for you than any other type of oil (e.g. peanut oil is really bad for you).

It took me looking through about 20 different fryers to finally find one without a 1-star review stating "the unit doesn't get above 325F and takes an hour to get even that high". I've never owned a fryer that didn't get up to 400F. We can blame the stupid "safety" laws for this dumb and, ironically, unsafe engineering decision. Many of the "modern" deep fryers I found on Amazon.com apparently have to be plugged in for an hour just to reach the low temperature of 325F. Most people will leave the fryer unattended during that time - great for small, curious children and the nearby hospital burn center!

Some of the fryers made now have "magnetic cords". I guess some engineer saw the Apple laptop cable and thought, "Hmm...I can do that for our new deep fryer. It will be a cool feature." The reviews on such fryers reveals that it is not only a bad design decision, it is a dangerous design decision. People end up burning themselves trying to keep the cable plugged into the unit or use electrical tape to keep it in place, which has a fairly low melting temperature - ah, I love the smell of burnt electrical tape in the morning! Great fire hazard for the whole family! Hospital burn units love this "feature" too.

Now here's a doozy: T-Fal FR7008002 Ultimate EZ Clean Pro-Fryer - check out the 1-star reviews on that one. Apparently the automatic oil filtering/cleaner feature on that unit is flawed in a serious way. Hot oil is bad enough inside any deep fryer. This deep fryer apparently leaks hot oil everywhere even during cooking. 325F oil. And this unit apparently only reaches 325F despite saying 375F. It makes a mess and melts your kitchen (I feel sorry for the one poor soul who had this shoddy device destroy their perfectly good kitchen on first use). Additionally, this amazing, disaster creating device can be yours for only $117.49. That's right folks. For only $120, you can turn your beautiful kitchen into a disaster! And likely set it on fire in the process.

Surprisingly, the cable on the deep fryer (you know, that thing connecting the fryer to the wall) has not changed in the past two decades. What is the whole point of an electrical ground on sockets if the engineer isn't going to design for it? Deep fryer electrical cords get hot enough to melt themselves. For only $49.95, you too can have an electrical fire!

The only "innovations" deep fryer technology has made in the past four decades is the grease splatter "window" (tiny glass cover), which gets so covered in grease you can't tell if it is done without pulling up the lid. The other "innovation" is the charcoal filter, which traps most of the odor produced by the frying oil. That is the good part. The downside is the filters have to be replaced and are hard to find and sometimes expensive. It might make more sense (and be a lot cheaper) to use a grease trap (wire mesh screen) and stick some regular charcoal on top of that. Just saying.


Okay, now that I've covered all the bad issues with today's "modern" deep fryer. The main issue boils down to what I mentioned before: Shoddy construction due to people not caring deeply for their job. So, let me, a software developer, tell you, Mr. Deep Fryer Engineer, how to do your job.

First, scrap your existing design because it more than likely sucks. See Amazon.com 1-star and 2-star reviews for why your design sucks.

Let's start with the base. It needs to be solid, stable, and capable of supporting 15 pounds of metal. The heating element should be at least a good three inches off the ground. The base should be made of a material capable of absorbing, drawing away, and dissipating the heat so it doesn't melt countertops. Take a serious look at computer processor and graphics processor cooling technology. They've got to keep super hot processors from frying themselves. A similar approach could be used to keep the base super cool while keeping the main reservoir hot.

Based on what I've read, the heating element needs to be capable of handling and drawing 1800W of power. Don't skimp here and use quality components. Some places are using cheap-o heating elements and then creating lame excuses in the documentation later with stuff like, "Don't use shortening with this deep fryer." Make this full-on awesome. Test the heck out of this thing to make sure it reaches temperatures in excess of 400F easily (450F would be great) and does so as quickly as possible. It should be capable of withstanding its own heat for a solid year (i.e. you leave this plugged in for a solid year and doesn't burn itself out before even considering going to market with it).

The heating element should be tied to a controllable thermostat. Dials are time-tested and proven. Digital is not. Less things to go wrong = more awesome (and less likely of a fire breaking out). The thermostat should be accurate to within a degree and stay that way over thousands of uses.

A thermometer capable of taking the heat. None of this "when the light goes out so that you can no longer tell that the unit is on in the first place" or "when the unit beeps at you through digital technology" junk. Let the user see for themselves that the temperature is correct.

The cord should basically be one of those bright orange power cords you find in the hardware store. You know the kind: Grounded, three prong cable that is a good solid 1/4" thick and can take a licking and keep on ticking. Made of material capable of withstanding the temperatures and not melting and shorting itself out...and if it does, it is grounded so you don't start an electrical fire.

The main reservoir should be made of roughly 15 pounds of steel (let's assume for frying up to 1.5 pounds at a time). If your deep fryer doesn't have 15 pounds of steel, you are skimping. 15 pounds will keep the oil hot and have quicker recovery and it has the added bonus of not going anywhere very fast (e.g. small children with the insatiable desire to land themselves in a hospital burn unit). Okay, maybe 15 pounds is a bit much and maybe not steel (Cast iron is harder to keep clean and tends to rust too easily though), but you get the idea. Heavy enough it isn't going anywhere when a small child tugs at the cable and holds enough heat to recover the oil temperature very quickly when the hot food is put in.

Grease trap flip cover. I don't have the foggiest idea why engineers use glass covers. And plastic and heat never mix (plus melted plastic in your food probably causes cancer). Metal mesh grease traps are, IMO, much more reliable and you can still see the food. This grease trap should extend and arch down instead of just barely covering the top (as most engineers who design one are prone to do). There should be a handle. Rubber would be okay but that might melt given the heat conductivity of metal and the high temperatures involved. Maybe a way to quickly snap-on attach and detach the handle (push button on the handle to lock/unlock?), but you deep fryer engineers are likely to screw that up, so just forget I mentioned it. Rely on oven mitts or really fast fingers, I guess.

Test all of this stuff for a solid year before going to market. No part should break, bend, or die on the prototype model. It should be left turned on 24/7/365 @ 375F with fresh oil several times daily. If you can pass that test, you are ready for the consumer market. Consumers are tired of stuff that breaks.

Now for a few features I want to see that you'll likely screw up.

Rapid cooling system. A spiral around the outer edge that acts like a hose (open at both ends). You put the deep fryer in one half of a sink and insert one end of a metal tube into the upper part of the spiral and hold a funnel-like end under cold running water in the other half of the sink. The two ends can be connected by some sort of flexible heat-resistant plastic. No water touches the oil and yet the container cools very quickly. The talented individual may use a large spoon to stir the oil to help it cool more evenly. Maybe create an attachment for the funnel end so that the user doesn't have to hold it but it stays under the faucet and above the upper end of the spiral (otherwise you'll be fighting gravity). The $.02 hack is a couple twist ties to tie the funnel end to the running faucet. I'm sure you can come up with something far more professional. This, however, is probably a feature that will make your brain explode trying to understand it. So, don't bother implementing it because you are likely to screw it up.

Easy oil emptying and filtration system. Some engineer already screwed this feature up (see the T-Fal link above). You can simply avoid their costly mistake by following this simple idea instead: A siphon. That's right! I'm referring to the ancient (several thousand years old), time-proven, stable concept that works! Except for one little problem: As a deep fryer engineer with numerous past mistakes and doesn't test your products, you are likely to screw this up too. The easiest solution is to provide the user with TWO of the metal, flexible plastic, funnel devices except this one will have the funnel facing the tube, have a filter, be sealed, and possibly be a lot skinnier. So, if you perfect the first feature above, you have a decent chance of getting this one right too. Cooling comes before emptying out the oil anyway, so this is a logical expansion. Don't skimp on this and do something funky with the first tube combination (e.g. a reversible funnel). Deliver two complete sets of tubes and accessories to the customer.


Once you have completed all of the above, hammered out all the bugs, and have zero complaints on Amazon.com, then and ONLY then should you consider adding more features beyond what I've listed.

Rhetorical: Why do I keep having to come up with what should be blatantly obvious? Why the heck am I designing your deep fryer for you? Isn't this your job? Then do your job already and I won't have to tell the world how lousy you do your job.

Deep Fryer without any 1-star complaints

Which is the best thing I've found so far. It took me a lot of digging around to find that one particular device. I looked at over 20 different deep fryers with 4+ star ratings on Amazon.com before finding that one. This tells me that Deep Fryer engineers don't care about their jobs and users don't bother taking the time to measure oil temperature and then wonder why everything comes out soggy (but the same people were giving it 4/5 star ratings anyway, ruining Amazon.com's rating system from the high-level perspective). It comes the closest to what I described and, according to an Internet post I found elsewhere, reaches 375F easily, thanks to the 1800W heating element. According to Amazon.com, and I'm not surprised, the digital components falter (digital = some embedded software/hardware engineers were involved = shame on you for making junky software/hardware). For hardware like this, analog is still where it is at folks.

And now, my dear programmer types who are reading this, make sure your software is quality. It has been said, "code like the next person who will maintain it is a serial killer who knows where you live." "Eat your own dog food." Use your own Deep Fryers.

Perhaps we should deep fry the Deep Fryer engineers? Into the Deep Fryer you go!
(More jokes could ensue, but I figure that is plenty).