[PHP] Weird results of "=="
Hello I have the following code: ($b + $c)) { // do the second thing } elseif ($a < ($b + $c)) { // do the third thing } ?> Each of the three variables is derived by some earlier calculation, but for testing purposes I have made sure that they end up being $a = $b and $c = 0. I have tested for three different values (of $a) and gotten all three results. That is, once the first block has executed, once the second block and once the third block. Am I missing something really obvious here? How do I execute only the first block if indeed $a == ($b + $c) ? Thank you for your help. Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Weird results of "=="
That did the trick. Thanks a million. Brian "Jasper Bryant-Greene" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Brian P. O'Donnell wrote: > > > > > $a = 252.73; > > $b = 252.73; > > $c = 0; > > > > if ($a == ($b + $c)) > > { > > // do the first thing > > } > > elseif ($a > ($b + $c)) > > { > > // do the second thing > > } > > elseif ($a < ($b + $c)) > > { > > // do the third thing > > } > > > > ?> > > > > Each of the three variables is derived by some earlier calculation, but for > > testing purposes I have made sure that they end up being $a = $b and $c = 0. > > I have tested for three different values (of $a) and gotten all three > > results. That is, once the first block has executed, once the second block > > and once the third block. > > > > Am I missing something really obvious here? > > This is an inherent problem with floating-point operations, especially > comparison, and is not unique to PHP. Often numbers will be off by some > miniscule amount, just enough to make them not equal. > > What I would do in this situation is create a function float_equals(), > after deciding what delta is acceptable for your situation, like this: > > define('MAX_FLOAT_DELTA', 0.001); // Or whatever is acceptable for you > > function float_equals($a, $b) { > return (abs($a - $b) <= MAX_FLOAT_DELTA); > } > > Then use float_equals($x, $y) instead of $x == $y. > > Jasper -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP]
It's bad enough when somebody posts a blank email to the list, but when people start posting blank replies, it gets really frustrating. Please cease & desist. Thanks Brian "Andy Pieters" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Saturdays and Sundays
""Shaun"" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > > Is it possible to get the number of saturdays and sundays for a given month > / year? > > Thanks for your help. Here's another way to do it. Each function will return either 4 or 5. If you need both Saturdays and Sundays, just call both functions: 3) { $sat++; } break; case 2: if ((date("L", $first_day) == 1) && (date("w", $first_day) > 5)) { $sat++; } break; case 4: case 6: case 9: case 11: if (date("w", $first_day) > 4) { $sat++; } break; } return($sat); } function get_sundays($month, $year) { $sun = 4; // time stamp of noon on the last day of the month $last_day = mktime(12, 0, 0, $month, date("t", $first_day), $year); switch ($month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (date("w", $last_day) < 3) { $sat++; } break; case 2: if ((date("L", $last_day) == 1) && (date("w", $last_day) < 1)) { $sat++; } break; case 4: case 6: case 9: case 11: if (date("w", $last_day) < 2) { $sat++; } break; } return($sun); } ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Saturdays and Sundays
Sorry, I made a mistake. See below: ""Brian P. O'Donnell"" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > ""Shaun"" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Hi, > > > > Is it possible to get the number of saturdays and sundays for a given > month > > / year? > > > > Thanks for your help. > > Here's another way to do it. Each function will return either 4 or 5. If you > need both Saturdays and Sundays, just call both functions: > > > function get_saturdays($month, $year) { > > $sat = 4; > > // time stamp of noon on the first day of the month > $first_day = mktime(12, 0, 0, $month, 1, $year); > > switch ($month) { > case 1: > case 3: > case 5: > case 7: > case 8: > case 10: > case 12: > if (date("w", $first_day) > 3) { > $sat++; > } > break; > case 2: > if ((date("L", $first_day) == 1) && (date("w", $first_day) > 5)) { > $sat++; > } > break; > case 4: > case 6: > case 9: > case 11: > if (date("w", $first_day) > 4) { > $sat++; > } > break; > } > > return($sat); > > } > > function get_sundays($month, $year) { > > $sun = 4; > > // time stamp of noon on the last day of the month The following line referenced a variable in the other function. DUH! It should be: $last_day = mktime(12, 0, 0, $month, date("t", mktime(12, 0, 0, $month, 1, $year)), $year); > > switch ($month) { > case 1: > case 3: > case 5: > case 7: > case 8: > case 10: > case 12: > if (date("w", $last_day) < 3) { > $sat++; > } > break; > case 2: > if ((date("L", $last_day) == 1) && (date("w", $last_day) < 1)) { > $sat++; > } > break; > case 4: > case 6: > case 9: > case 11: > if (date("w", $last_day) < 2) { > $sat++; > } > break; > } > > return($sun); > > } > > ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Integer - boundary?
""Gustav Wiberg"" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > - Original Message - > From: "Shaw, Chris - Accenture" <[EMAIL PROTECTED]> > To: "Gustav Wiberg" <[EMAIL PROTECTED]>; "PHP General" > > Sent: Wednesday, September 07, 2005 5:03 PM > Subject: RE: [PHP] Integer - boundary? > > > > > -Original Message- > > From: Gustav Wiberg [mailto:[EMAIL PROTECTED] > > > > > Hi there! > > > > > What is the boundary for an integer? > > > > > seems to be a easy question, but I can't find it... > > > > > > /G > > http://www.varupiraten.se/ > > > > > According to the manual, > > > "The size of an integer is platform-dependent, although a maximum value of > about two billion is the usual value (that's 32 bits signed). PHP does not > support unsigned integers." > > > > > > This message has been delivered to the Internet by the Revenue Internet > e-mail service > > * > > > -- > No virus found in this incoming message. > Checked by AVG Anti-Virus. > Version: 7.0.344 / Virus Database: 267.10.18/91 - Release Date: 2005-09-06 > > Hi there! > > Thanx! > > Hm... I would rephrase my question.. Is it possible to get the maximum > integer-value that can be used? > Hi Try this code to see what it is on your platform: Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Integer - boundary?
""M. Sokolewicz"" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > or, easier, simply make this script: > echo PHP_INT_MAX; > ?> > > and you're done :P The maximum integer value is stored in PHP_INT_MAX as > of 4.4.0 and PHP 5.0.5 > > before that, you had to use a way as the one Brian posted Ahhh, I see. I am running 4.3.9 Thanks; I'll know that when I upgrade. Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: mktime
"Dan Brow" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > A little confused with mktime, I'm trying to get how many days are in a > year. > > $year = "2006"; > $epoch = mktime(0, 0, 0, 1, 0, $year); // I have to have 1 for month or > I get which day it is now. Which sucks. > $date = date("z Y", $epoch); > print($date); // here is the problem, I get 365 days, but the year is > 2005, not 2006, same goes for any year I put in. > > What am I doing wrong? January 0th is December 31st last year Brian > > Thanks, > Dan. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mktime
"Jasper Bryant-Greene" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Dan Brow wrote: > > A little confused with mktime, I'm trying to get how many days are in a > > year. > > How about doing it differently. I'd tackle this problem like this: > > $year = '2005'; > $time = strtotime("1 January $year"); > $isLeapYear = date('L', $time); > > if($isLeapYear == '1') { > $days = 366; > } else { > $days = 365; > } > ?> I'd shorten it up even more: Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php