This is a Journal entry by IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system

Programming Quiz

Post 1

IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system

This week I added the following snippet of code to one of my company's core apps. Prizes to all those who can work out when and why it makes any difference.

if ( $amount == 0.00 )
{
$amount = 0.00;
}


Some hints:
smiley - geek the language is PHP
smiley - geek for those not used to it, '==' is comparison ("$amount is equal to 0.00") and '=' is assignment ("set $amount to 0.00")
smiley - geek PHP is weakly typed, so $amount could have been something other than a number, but that's not why I added this code - i.e. it's got nothing to do with type-casting

Good luck! smiley - evilgrin


Programming Quiz

Post 2

Zak T Duck

Something to do with null values?


Programming Quiz

Post 3

IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system

Nope, weirder than those...


Programming Quiz

Post 4

SEF

-ve zero causing problems? I remember some floating point issues like that from the early '90s.


Programming Quiz

Post 5

IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system

That's the one! smiley - gift

We were getting some really weird displays because a comparison was coming out to -0.0

Although I can kind of see the logic, the behaviour of negative zero is really smiley - weird - for instance, you can't actually tell whether you've got a -ve zero or positive one (unless there's a magic function you can use somewhere) because -0.0 == 0.0, so $foo == 0.0 and $foo == -0.0 will always give the same answer. Hence "if it's zero, set it to zero". And it really messes with the common sense that ($foo < 0) would be a good test for a negative number...


Key: Complain about this post

More Conversations for IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more