This is a Journal entry by IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system
Programming Quiz
IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system Started conversation Dec 10, 2006
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:
the language is PHP
for those not used to it, '==' is comparison ("$amount is equal to 0.00") and '=' is assignment ("set $amount to 0.00")
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!
Programming Quiz
IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system Posted Dec 10, 2006
Nope, weirder than those...
Programming Quiz
SEF Posted Jan 28, 2007
-ve zero causing problems? I remember some floating point issues like that from the early '90s.
Programming Quiz
IMSoP - Safely transferred to the 5th (or 6th?) h2g2 login system Posted Jan 28, 2007
That's the one!
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 - 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
Programming Quiz
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."