Archive for February 20th, 2007

Sometimes 7 and 8 are equal.

Back in college I had a professor who would dock off points every time he saw the word float in our code. He never really told us why, but from then on we all started using double any time we needed a decimal.

It’s been a few years since college, but I think I’m finally closer to understanding what he meant. Take a look at the following PHP code. What do you think it does?


echo (int) ((.1 + .7) * 10);

It’s a basic PHP math operation. It should add .1 and .7 to get .8, then multiply that by 10 to yield 8 right?

Not so fast, what’s that (int) doing?

For those of you unfamiliar with PHP, it’s a dynamicaly typed language. That means unlike c++, you don’t have to declare a type when you declare a variable. PHP will look at how you’re using it and decide what you meant.

The (int) is a cast. It tells PHP to take whatever that result is and make sure it’s an integer. It’s very seldom used, but there are a few examples I can think of where it’s a good idea to cast your variables. I’ll save that for a later column though.

Anyway, run the above code. What happens? Do you see 8? No! It outputs 7!!

Take the (int) cast away though, and you’ll see your expected result.

So what’s going on here? The simple answer is that it has something to do with the way PHP handles floating point numbers. The long answer is, it’s the reason why the data from one of my applications never added up correctly.

February 20th, 2007

Gmail Tracking Numbers

I think one of the coolest features of Gmail is its ability to identify tracking numbers in emails and provide me with direct links to track my package. Here’s an example:

Amazon sent me an email with the following text:

If you notice, Amazon wants me to click the link, and login to their page to track my package. They don’t even tell me what shipper is sending it, so the tracking number is pretty useless to me.

Fortunately, Google picked up on it right away. Have a look:

This is just one of the really cool features that makes Gmail my main email client.

As a side note to E-Commerce sites. It’s important to nurture the customer relationship after the sale. Marketing may think it’s a good idea to draw them back to your website, but it’d be much more useful in this case if Amazon provided me with a direct link to track my package instead of forcing me to go through their interface. It may cut down page views, but I’ll be willing to bet it would increase brand loyalty.

February 20th, 2007


About Ryan Jones

Name: Ryan Jones
Alias: HockeyGod
Location: Michigan
Company: Team Detroit
Title: Sr. Search Strategist
AIM: TheHockeyGod
Pets: Who Dey

Twitter & Klout



My Websites

Internet Slang Dictionary
Fail Pictures
FeedButton
Translate British
TextSendr
URL Shortener
Bad Words
WoW Slang
Free Softball Stats

Buy My Book

Recent dotCULT Posts

Calendar

February 2007
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728  

Posts by Month

Posts by Category

Subscribe To RSS Feed

Link Me





ypblogs.com