The error nobody spotted

March 8th, 2007

… or maybe they were just too polite to say.

I’m currently working on the draft of a new book about PHP, and decided to create a form to demonstrate the effect of the various arithmetic operators. I used the same set of examples that I’ve stuck with ever since Foundation Dreamweaver MX 2004. The example for modulo division is $x % $z, where $x is 20 and $z is 4.5. Since modulo gives you the remainder of a division, the result is obviously 2.

Well, at least that’s what I thought. It’s mathematically correct, so nobody ever questioned it. Imagine my surprise, then, when I fed this example into my form and the result came back as 0. Had I stumbled across a PHP bug?

I hunted around on the official PHP site, and couldn’t find anything. So I did some more tests. When I played with integers, modulo worked exactly as I expected. It’s just with decimal fractions that it behaved oddly.

Finally, I consulted Programming PHP by Kevin Tatroe and Rasmus Lerdorf, which reveals that, in PHP, the modulo operator converts both numbers to integers before performing the calculation. As a result, 4.5 was being rounded up to 5. Hence my mistake.

It’s nice to discover an error in one of your own books before others point it out, but it’s a bit embarrassing to have made the same mistake in four books. :( Strange that it’s not mentioned in the PHP manual, either.

Entry Filed under: Books, Dreamweaver, PHP

6 Comments

  • 1. Caroline Schnapp  |  March 9th, 2007 at 10:30 pm

    It’s a minor mistake. I am sure that a lot of people saw the bug, because a lot of people actually use the examples in your books (they don’t just buy your books for their distinctive ‘narrative’ quality and pretty cover), and they surely realized what was going on (that floats were converted into integers…) and they were too polite to tell you OR too lazy to fill the erratum form. I will always make sure that whenever I see something wrong in your books I will report in on Friends Of Ed. So that you will not feel that embarrassment again (over that minor error going in print 4 times). I hate that you would feel embarrassed. This being said, I must say that I am very impressed with the work of your ‘editors’ (whoever is reviewing your books, these extra ‘pair of prying eyes’), it’s darn hard to spot innacuracies.

  • 2. Ross W.  |  April 1st, 2007 at 7:14 am

    This gave me a lot of trouble when trying to calculate a check digit (using ISBN 10) for which the MOD function makes it easier to calculate or confirm. I think from memory i ended up using fmod instead.

  • 3. Kevon K. Hayes  |  April 4th, 2007 at 2:16 pm

    I purchased the foundations PHP for Dreamweaver 8.

    I used the following:

    and I know what my password is but the login also asks for a username (my password workings in the mysql monitor).

    Is there a default username because I don’t remember entering in a username at all?

  • 4. Kevon K. Hayes  |  April 4th, 2007 at 2:22 pm

    Nevermind found it out, it’s root! Once I used the no password method it shows the “user” as being root.

    David, by the way, I am enjoying your book 1/2 way through and it’s great.

  • 5. Matti  |  May 7th, 2007 at 4:59 pm

    I just bought your book PHP Solutions and it’s great. Thank you. Even I as a novice can understand most of it.

    However, when I try to install My SQL with the ‘MySQL Server Instance Configuration Wizard’ it tells me at the very end that it ‘Cannot create Windows service for MySQL.Error:0′ The ‘Prepare configuration’ and ‘Write configuration file (c:\Program Files\MySQL\MySQL Server 5.0\my.ini)’ both show a blue check mark, but the ‘Start service’ box shows a red cross.

    I went through the Wizard several times just to make sure that I didn’t do anything different from what you discribe in your book, but then I get to Execute the result is always the same. If I go to ‘Control Panel/Administrative Tools/Services and My SQL Properties it shows the fololowing path (which I think is incorrect): c:/mysql/bin/mysqld-nt.exe

    Should I change this path to c:\Program Files\MySQL\MySQL Server 5.0\bin/mysqld-nt.exe

    And if so, how do I go about doing so?

    Thank you in advance and sorry for posting this here, but I really don’t know what else to do.

  • 6. David  |  May 14th, 2007 at 9:18 am

    Matti, the error probably means that you have installed MySQL on a computer that had an older installation of MySQL. In fact, the path shown in the service properties is almost 100% proof that you had a version of MySQL prior to 4.1.5, because that’s where old versions were installed.

    You should be able to change its properties by opening the Services panel, highlighting MySQL, right-clicking and selecting Properties.

    Click Stop if Service status reads “Started”.

    Enter this in the Path to executable field (you need to include the quotes where indicated):

    “C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt” -defaults-file=”C:\Program Files\MySQL\MySQL Server 5.0\my.ini” MySQL

    If that doesn’t work, change Startup type to “Disabled”. Then run the MySQL Server Configuration wizard again. Choose a different name for the MySQL service. There’s a drop-down menu with some suggested names. MySQL5 is a good choice.

Trackback this post


Calendar

March 2007
M T W T F S S
« Jan   Apr »
 1234
567891011
12131415161718
19202122232425
262728293031  

Most Recent Posts