When is a number not a number?

IMG_5863.jpg

Pop quiz. What would the following C# code do?

float x = 1;
float y = 0;
float result = x / y;
Console.WriteLine(result);
  1. Fail to compile.
  2. Throw an overflow exception.
  3. Print “Infinity”
  4. Cause the universe to explode.

Here’s the thing, the code prints the word “Infinity”. Dividing any floating point variable by zero gives a special result which is encoded into the floating point variable range as “Infinity”. Now what about this?

float y = 0;
float result = y / y;
Console.WriteLine(result);

This time we get the message “NaN”, which means “Not a Number”. Dividing zero by zero does not give a number as a result. There just isn’t a value for this quantity. The value is not as big as as infinity or as small as zero its just, well, not a number. You get the same non-result if you divide infinity by infinity.

The C# runtimes are based on .NET which uses an IEEE (Institute of Electrical Electronics Engineers) standard that contains representations for negative infinity, positive infinity and “not a number”. Calculations in a C# program will produce these results when given dodgy values to work on. This is very important. You might think if you do silly things with numbers in your program you are going to get an exception thrown. Like this would.

int i = 1;
int j = 0;
int Result = i / j;
Console.WriteLine(Result);

Run this code and it throws a “Divide by Zero” exception when it tries to, er, divide by zero. But this does not happen with floating point calculations. This is probably because the range of values for the int type does not include one to represent “infinity”.

The bottom line here is that if you do sums involving floating point values you can’t expect exceptions to be thrown if the calculations go wrong. The good news is that you can use methods provided by the float type to test results of calculations:

if (float.IsNaN(fresult))
    Console.WriteLine("Welcome to the Twilight Zone");

This prints the enigmatic message if the value in fresult is not a number.

Rob on the Road

image

I’m off on my travels again next month. I’m doing sessions about Windows Phone and Kinect at TechDays in Belgium and the Netherlands and then hopping over to SIGCSE in the USA to give some sessions there too.

The plan is to develop and maybe even publish a complete Windows Phone application during one session and then play some rather silly games during the Kinect one. Should be fun.

Crayola ColorStudio Pen

image

Drawing with your fingers on an iPad is fine, but not natural. The Crayola colouring pen gives you something to hold on to as you draw, which is nice. I first thought it was just a pen that works on a touch screen, but there is more to it than that. The pen does something with the connection to the screen (I think it turns it on and off quickly) that enables the software in the iPad to detect when the pen is touching it, and ignore fingers. This means that if you accidently touch the screen with your hand while drawing the program ignores this. If you (or a very small friend) want to get to grips with colouring and free hand drawing the program is worth a look. The pen works fairly reliably, although I had to press quite hard to make it work sometimes.

image

The pen comes with a colouring and drawing application with pictures you can colour in which are quite fun. It has a really nice option you can set. I’ve always wanted something that helps me stay inside the lines..

Lego Star Wars Battle of Hoth

P1030270.jpg

Lego Star Wars Battle of Hoth is a neat little game which they say is for up to four players but works best with just two, light against dark. You take it in turns to move and attack your little Lego snow speeders and AT-ATs in an attempt to wipe out your opponents or storm their defences. It was getting quite strategic until number one son had a run of phenomenal  luck (I suspect the force) and I lost.

Probably not worth the asking price really unless you are a Star Wars fanatic. You could spend around the same amount of money on the Creationary game which has a lot more to it. Having said that, if you have someone little in your family who is a Star Wars nut and you want to get them used to strategy games, perhaps on the way to chess, then it is a good starting point. And being Lego they encourage you to change the rules and board shape to make it more interesting.

Degrees of Fun

p1.jpg

Did my Graduands Marshal thing this morning. As usual I took a picture of the audience and it mostly came out OK, sorry if you are in the blurry part of the audience… There are some more of the other ceremonies on Flickr. Click on the right to find my photostream and take a look. I should have set a faster shutter speed really. I tend to fret about noise (which you get when you make the camera more sensitive to light) whereas I should remember that you can always get rid of noise, but you can’t do anything if the picture is blurred….

Both ceremonies were really good ones, I hope you had a good time if you were there. For me one of the the best bits was finding the chap next to me had a Nokia Lumia 800, and was liking it.

Read Verity Stob

P1030197.jpg

I have a few hero/heroines in my life. One of them is Verity Stob. She has been writing about computers for about as long as I’ve been playing with them. I’ve mentioned her before, and now I’ve discovered to my great joy that she is still writing for The Register. Her piece on exceptions is one of the best I’ve read on the subject. If you have any interest in computers you should read her stuff and treasure it.

Hull Digital for Breakfast

image

Had a chat with Jon Moss today. He’s one of the movers and shakers behind Hull Digital. I’ve been to a few of their events, which are usually (and very sensibly) held at one of my favourite eating places in Hull, Fudge in Princes Ave. If you want to sync up with what’s happening with developers in Hull they are a great way to do this.

Jon was telling me about future events, including a regular programme of Digital Breakfast MeetUps, where  like minded folk get together for “the most important meal of the day” every month. The next one is on Friday this week, which is a shame for me because I’m not able to go. I’m helping out with the Graduation Ceremony on the Hull campus. However, if you want to go along and meet up with digital folk - if you see what I mean- and enjoy some fantastic food (my advice, go for the omelette) then you should pop along.

Jon was also telling me about TEDx at Hull, which brings together a bunch of interesting folk for a day of discussion and debate about the future of, well, everything..

What with that and Platform Expo coming along it looks like it is going to be an interesting few months around here.

Chapter 12 is finished

image

If you’ve wondered where I’ve been for a week. Or why I’ve suddenly just had time to suddenly produce seven blog updates (ahem) it’s because I’ve been writing. Book stuff. Twelve chapters on having fun with your Kinect using the Kinect SDK and your PC. I’ve been squashing pretend bugs with virtual mallets, taking pictures of ghosts and creating all kinds of funky pictures. I even made the computer talk to itself.

The book will be out in late spring. And now I’ve written all the words.

Lightroom Rocks

P1030212.jpg

Last week I sold a MacBook and bought a camera. And got a free copy of Adobe Lightroom. And it is wonderful. Up until now I’ve been muddling through managing my many photographs, keeping them in folders and using Windows explorer to find and look after them. This mostly works, but it is a bit of a pain.

Lightroom provides a really good way to find and then fix your pictures. It works really well with raw files and the noise reduction abilities are awesome. It also has some rather cool picture styles built in. And it will upload directly to Facebook, Flickr and SmugMug, among other places. It also takes care of the importing and has very good tagging and metadata management.

If you are serious about your photography, then I think you should take a good look at it. The trial download runs for 30 days or so, by which time you should be hooked.

Nokia Blue Case

P1030234.jpg

I think my Lumia 800 is turning me into a fashion victim. I got the Nokia blue case today. Looks pretty good too, as you can see. Special prize for naming the person in the back of the picture. I love the way that the case doesn’t look like a case, it just looks like I have a cool, blue phone. Which I have.

If you want to be cool too, Amazon have them in stock in a range of colours, look for the magic stock number Nokia CC-1031.

Escape Characters in XNA fonts

DSCF2130.jpg

I’m making a sister application for the PlayMusic application. The PlayMusic program doesn’t do much, it just starts playing a random playlist when you run it. Not a lot of functionality, but it does mean you can start music playing by issuing a spoken command. I’ve had nearly a thousand downloads up to now, so a few people must find it useful.

The PlayMeMore application will do something similar. When you run it the program will find the album that the currently playing song is part of, and then play that album. I want this because frequently on random play I hear a track that I’ve not heard for a while, and fancy hearing the album it came from. I thought it would be nice to be able to say “Start PlayMeMore” and have it do this for you.

I’ve written the program in XNA because I like XNA and I might add a visualisation or two later. The program displays the name of the album when it starts it. By a wonderful quick of fate the first album it tried to play was Bête Noire by Bryan Ferry (darned good album too). And the program crashed. Any idea why? Took me a few minutes to work it out. It has to do with SpriteFonts.

When you use a font in XNA it builds a raster with the character designs on it. The font requirements are given in an little XML file that specifies the TrueType font to be used for the character designs. This font also specifies which character codes are rendered. And the default character codes don’t include the character ê. The fix is simple enough, open the file and make the end value a bit bigger.

    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#255;</End>
      </CharacterRegion>
    </CharacterRegions>

Normally the end value is 126, but this does not include accented characters like ê. Making the value 255 seems to fix the problem. And I’ve put a try-catch around my DrawString calls in case I hit any music info that has any weird characters in it. Like the artist formally known as Prince….

Get your apps out there….

DSCF2158.jpg

I had one of those happy/sad moments today. Someone showed me a stunning little game they’d written for Windows Phone. Snappy graphics and fun to play. “Is it in the Marketplace?” I asked. “No” was the reply.

Some people hold back until their game is “finished”. This is a bad idea. There is no such thing as finished as far as a developer is concerned. You can always add bits, make it better, tidy up the class design, make all the curly brackets line up. But if you keep on like that you never get anything out there. I’m not saying that you should push out broken programs, I’m saying that things like on-line high score tables, multi-player, extra levels, a level editor etc can always be added later.

The faster you can get something out there the faster you can start getting feedback and recognition. So go for it.

Hash Tag Revision

Chimneys

I said that anyone revising the Software Development exam could tweet their question with the tag #08220revison and I’d answer as best I could. Here are some of the answers. Good luck in the exam folks.

Multiplicity: A library can have lots of books. The multiplicity is one library to 1 to many books.

When to pick the programming language: Analysis should be all about the problem, so the language should be picked in the Design stage.

Waterfall vs. Rapid Development: Waterfall: do each stage, move on and don't go back. Rapid: Keep iterating and adding functionality.

Coupling and Cohesion: Coupling I ask you to do things. If you change what you do, I'm in trouble. Cohesion I can do my thing without asking you anything

Stakeholder: anyone who has an interest in the system(put up the money, wrote it, uses it, gets fired if it fails etc)

TV Gone Backwards

Hornsea Gulls

I’m always impressed by how clever modern things are. But I’m also perplexed how some people take all this cleverness and make things that aren’t very useful. Take my telly. I’ve got a Sony PS3 plugged into a Sony amplifier which is connected to a Sony TV. When I try to watch a Bu-ray disk on the PS3 it takes ages to connect to the TV and I sometimes get a bright green (or purple – it varies) screen. And often the PS3 has forgotten all about any previous audio settings that I’ve laboriously made so that I have to go and do them all again. And when I watch the Blu-Ray (also from Sony Columbia Studios) it shows me a warning that because the disc contains “Advanced Interactive Content” (which I have no interest in) it might take several minutes to get started. And then when it has loaded the disk it insists on showing me lots of trailers and other stuff about how much Blu-Ray is better than DVDs before I get to the main feature that I have paid money to see. Say what you like about my old record player, but I could guarantee that within 10 seconds of arriving home with a new album  I could be listening to it.

Wah. Did nobody at Sony actually try to use this stuff? Apple are rumoured to be moving into TV soon. I bet their system won’t have 30 second pauses while nothing useful happens. Here’s hoping that their presence will force manufacturers to get their act together.