RE: [PHP] Simple math failing - PHP Bug?
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm On 05 November 2004 10:42, Olaf van der Spek wrote: > Mike Ford wrote: > > for. (Some early business-oriented computers, and some calculators > > (especially financial ones) did use a system called binary-coded > > decimal -- BCD -- to calculate "accurately" in the sense you mean, > > but the fact that they were never widely used and have died out > > almost totally should tell you something about their usefulness.) > > Wasn't that just a way to store 11 as 0x11 and 56 as 0x56? > x86 has BCD instructions too. Effectively, yes -- although it wasn't very usual to talk in hex back in the days when I learnt about BCD! But it also meant that a number such as 564.29897 was stored as 0x564.29897 (if you see what I mean!), so there were none of these nasty binary<=>decimal rounding approximations and you always had an exact answer (to the limit of the number of significant figures supported, anyway). But it makes the calculations more complex as you effectively have to do everything byte-by-byte (since neither 0x8+0x8 nor 0x4*0x4 is 0x16!), and hence both more expensive and slower (sometimes *much* slower), even when done in hardware. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
Mike Ford wrote: for. (Some early business-oriented computers, and some calculators (especially financial ones) did use a system called binary-coded decimal -- BCD -- to calculate "accurately" in the sense you mean, but the fact that they were never widely used and have died out almost totally should tell you something about their usefulness.) Wasn't that just a way to store 11 as 0x11 and 56 as 0x56? x86 has BCD instructions too. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Simple math failing - PHP Bug?
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm On 01 November 2004 06:31, Brian T. Allen wrote: [] > When adding dollar amounts using only whole cents (and excluding the > limitation we've been discussing) you should always have a credit >= > .01, a balance >= .01, or no balance / credit due, which > would equal 0 > or 0.00, but NOT 0.00034322. > > Yes, there are ways to program around this, but it shouldn't be > necessary. The example above shouldn't be questionable or > complicated, in fact it is about as basic as it comes. It's 2nd > Grade math. Possibly -- but the explanation of the discrepancy is probably something nearer high school level (or GCSE Maths, in my vernacular!). Very briefly, computers work in binary -- and the binary system can represent very few decimal fractions exactly. Take, for example, your 503.54: in binary, this converts to 10111.1000101000010111..., or, slightly more readably, in hexadecimal it's 1f7.8a3d70a3d70a3d70 You can see how this has a repeating fractional part, and so cannot be represented exactly in any finite number of bits -- so there is no way of storing exactly 503.54 in any computer working in the binary system. > You just should have to program around that. I would > MUCH rather > have PHP > run a little slower Unfortunately, it is very unlikely that "fixing" it the way you'd like would be only a *little* slower -- more likely, it would be massively slower. Given this, it's more efficient and effective to have the computer calculate the answer as accurately as it can, and let the programmer decide the level of accuracy it should be tested for. (Some early business-oriented computers, and some calculators (especially financial ones) did use a system called binary-coded decimal -- BCD -- to calculate "accurately" in the sense you mean, but the fact that they were never widely used and have died out almost totally should tell you something about their usefulness.) Having said all that, here are a couple of golden rules: (i) If your numbers can be represented exactly to a given number of decimals, consider using integers for your calculations and dividing/multiplying by the appropriate power of 10 on output/input (this, of course, won't work if the scaled numbers exceed the integer range). (ii) NEVER EVER compare floats for equality -- always build in an appropriate amount of fuzz (e.g. if you have to use floats for money amounts, a check for equality to 4 decimal places is probably sufficient: if (abs($a-100)<0.0001) echo "close enough to $100.00"; (iii) Always format your numbers to an appropriate number of decimal places on output (using number_format() or printf(), for example). Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
From: "Chris Shiflett" <[EMAIL PROTECTED]> To: "Jason Wong" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Monday, November 01, 2004 12:22 AM Subject: Re: [PHP] Simple math failing - PHP Bug? --- Jason Wong <[EMAIL PROTECTED]> wrote: Most computer languages handling floating point calculations just as poorly. If accuracy is important use the BCMath functions. Or use Fortran and double precision. :-) Wouldn't help. Exact same problem. The root cause it trying to express the number range of 0.0-1.0 in a finite set of bits. The more bits you use, the smaller the gaps between the numbers exactly represented, but there are always an infinite number of "numbers" in every gap. It works well enough as an approximation to be useful; the Fortran programmers use format clauses just like any other programmer to force rounding to a known precision. Mark C., who's worked on all the Fortrans up to 95. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
On Sun, 31 Oct 2004 23:30:53 -0700, Brian T. Allen <[EMAIL PROTECTED]> wrote: No worries, this isn't personal. I just want to explore this limitation and it's implications for my benefit and the benefit of any that read this thread later when they are searching for answers to a similar problem. I just think the following should work, and outside of this limitation of computers is sound programming: $subtotal = 0; $subtotal += 503.54; $subtotal += 303.55; $subtotal -= 503.54; $subtotal -= 303.55; if($subtotal > 0){ // A balance is due } But if $subtotal is 0.00034322 then it shows a balance is due when one isn't. When adding dollar amounts using only whole cents (and excluding the limitation we've been discussing) you should always have a credit >= .01, a balance >= .01, or no balance / credit due, which would equal 0 or 0.00, but NOT 0.00034322. Yes, there are ways to program around this, but it shouldn't be necessary. The example above shouldn't be questionable or complicated, in fact it is about as basic as it comes. It's 2nd Grade math. You just should have to program around that. I would MUCH rather have PHP run a little slower and take up more memory and get the answer right then have every PHP programmer have to program around this limitation for even the most basic math. Thanks, Brian It would be more than a minor slowdown, as php would have to inplement something that is so slow when done at a far lower level that there used to be a seperate processor just to handle it. Also, 1/3 is certainly basic math, but it eventually gets impossible to store accuratly, as there is not exact decimal value. You could store the fractional value of it, but what happens when you have something like log(sqrt(1/3))? The only way to have an exact value would be to store every single operation done to the number, and rerun them every time the number is refered to, but you still have rounding between every step of that. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ http://www.smempire.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
Brian T. Allen wrote: Chris Shiflett wrote: --- "Brian T. Allen" <[EMAIL PROTECTED]> wrote: Well, in fairness, it's one of the worst ideas you've ever heard because you know of this limitation. Of course. That wasn't a dig at you or anything - just a comment. You're free to heed or ignore it. In reality, using a calculator or something that doesn't share this limitation, 0 = 0 and this wouldn't be a problem. Well, 0 == 0 in PHP, too. Calculators have the same limitations that any other computing device does. Some modern calculators have some pretty impressive precision, but it's not hard to exceed it with a few seemingly harmless operations. A couple of numbers like 12.34567 and 23.45678 can get pretty nasty with a few operations. Regardless of this, I'm not in favor of depending on anything that's the least bit questionable or complicated. I don't even like evaluating integers as boolean unless they are strictly 0 or 1, and even then there can be problems (you accidentally decrement a variable twice before evaluating it again, so that it goes from 1 to -1 and continues to evaluate as true forever). In short, complexity breeds problems, regardless of how smart you are. Chris = Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly HTTP Developer's Handbook - Sams Coming January 2005 http://httphandbook.org/ No worries, this isn't personal. I just want to explore this limitation and it's implications for my benefit and the benefit of any that read this thread later when they are searching for answers to a similar problem. I just think the following should work, and outside of this limitation of computers is sound programming: $subtotal = 0; $subtotal += 503.54; $subtotal += 303.55; $subtotal -= 503.54; $subtotal -= 303.55; if($subtotal > 0){ // A balance is due } But if $subtotal is 0.00034322 then it shows a balance is due when one isn't. When adding dollar amounts using only whole cents (and excluding the limitation we've been discussing) you should always have a credit >= .01, a balance >= .01, or no balance / credit due, which would equal 0 or 0.00, but NOT 0.00034322. Yes, there are ways to program around this, but it shouldn't be necessary. The example above shouldn't be questionable or complicated, in fact it is about as basic as it comes. It's 2nd Grade math. You just should have to program around that. I would MUCH rather have PHP run a little slower and take up more memory and get the answer right then have every PHP programmer have to program around this limitation for even the most basic math. Thanks, Brian have you ever thought about working with all those numbers in cents, as integers, and applying the math to that? That'll work *just fine* -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
Chris Shiflett wrote: --- "Brian T. Allen" <[EMAIL PROTECTED]> wrote: Well, in fairness, it's one of the worst ideas you've ever heard because you know of this limitation. Of course. That wasn't a dig at you or anything - just a comment. You're free to heed or ignore it. In reality, using a calculator or something that doesn't share this limitation, 0 = 0 and this wouldn't be a problem. Well, 0 == 0 in PHP, too. Calculators have the same limitations that any other computing device does. Some modern calculators have some pretty impressive precision, but it's not hard to exceed it with a few seemingly harmless operations. A couple of numbers like 12.34567 and 23.45678 can get pretty nasty with a few operations. Regardless of this, I'm not in favor of depending on anything that's the least bit questionable or complicated. I don't even like evaluating integers as boolean unless they are strictly 0 or 1, and even then there can be problems (you accidentally decrement a variable twice before evaluating it again, so that it goes from 1 to -1 and continues to evaluate as true forever). In short, complexity breeds problems, regardless of how smart you are. Chris = Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly HTTP Developer's Handbook - Sams Coming January 2005 http://httphandbook.org/ No worries, this isn't personal. I just want to explore this limitation and it's implications for my benefit and the benefit of any that read this thread later when they are searching for answers to a similar problem. I just think the following should work, and outside of this limitation of computers is sound programming: $subtotal = 0; $subtotal += 503.54; $subtotal += 303.55; $subtotal -= 503.54; $subtotal -= 303.55; if($subtotal > 0){ // A balance is due } But if $subtotal is 0.00034322 then it shows a balance is due when one isn't. When adding dollar amounts using only whole cents (and excluding the limitation we've been discussing) you should always have a credit >= .01, a balance >= .01, or no balance / credit due, which would equal 0 or 0.00, but NOT 0.00034322. Yes, there are ways to program around this, but it shouldn't be necessary. The example above shouldn't be questionable or complicated, in fact it is about as basic as it comes. It's 2nd Grade math. You just should have to program around that. I would MUCH rather have PHP run a little slower and take up more memory and get the answer right then have every PHP programmer have to program around this limitation for even the most basic math. Thanks, Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
--- "Brian T. Allen" <[EMAIL PROTECTED]> wrote: > Well, in fairness, it's one of the worst ideas you've ever heard > because you know of this limitation. Of course. That wasn't a dig at you or anything - just a comment. You're free to heed or ignore it. > In reality, using a calculator or something that doesn't share > this limitation, 0 = 0 and this wouldn't be a problem. Well, 0 == 0 in PHP, too. Calculators have the same limitations that any other computing device does. Some modern calculators have some pretty impressive precision, but it's not hard to exceed it with a few seemingly harmless operations. A couple of numbers like 12.34567 and 23.45678 can get pretty nasty with a few operations. Regardless of this, I'm not in favor of depending on anything that's the least bit questionable or complicated. I don't even like evaluating integers as boolean unless they are strictly 0 or 1, and even then there can be problems (you accidentally decrement a variable twice before evaluating it again, so that it goes from 1 to -1 and continues to evaluate as true forever). In short, complexity breeds problems, regardless of how smart you are. Chris = Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly HTTP Developer's Handbook - Sams Coming January 2005 http://httphandbook.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
Chris Shiflett wrote: --- Jason Wong <[EMAIL PROTECTED]> wrote: Most computer languages handling floating point calculations just as poorly. If accuracy is important use the BCMath functions. Or use Fortran and double precision. :-) Still, testing a floating point number as a boolean is one of the worst ideas I've ever heard... Chris = Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly HTTP Developer's Handbook - Sams Coming January 2005 http://httphandbook.org/ Well, in fairness, it's one of the worst ideas you've ever heard because you know of this limitation. In reality, using a calculator or something that doesn't share this limitation, 0 = 0 and this wouldn't be a problem. It's only when 0 = 0.1342344 that this becomes a problem. Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
Thomas Goyne wrote: On Sun, 31 Oct 2004 21:52:32 -0700, Brian T. Allen <[EMAIL PROTECTED]> wrote: Hi, OK, I'm totally stumped by this. This should be the simplest math imaginable (addition and subtraction), but PHP is coming up with the wrong answer! [snip] Is this legitimately bad math on the part of PHP? Thanks, Brian This is a flaw in how computers handle floating point numbers, not php. As the memory needed to store exact floats to any useful precision would quickly get out of control, floats are handled inprecisely. As a result, you should never test for equality with a float, but rather check if the float is within a certain range of the desired number. Wow, this is pretty dissappointing. I can understand if I was doing math on 12 or 13 digit numbers, but just adding and subtracting back out a couple of 2 digit decimals I would expect to end up back at zero. I understand it isn't PHP's fault. Thanks for the quick reply, Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
--- Jason Wong <[EMAIL PROTECTED]> wrote: > Most computer languages handling floating point calculations > just as poorly. If accuracy is important use the BCMath > functions. Or use Fortran and double precision. :-) Still, testing a floating point number as a boolean is one of the worst ideas I've ever heard... Chris = Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly HTTP Developer's Handbook - Sams Coming January 2005 http://httphandbook.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
On Monday 01 November 2004 04:52, Brian T. Allen wrote: > OK, I'm totally stumped by this. This should be the simplest math > imaginable (addition and subtraction), but PHP is coming up with the > wrong answer! > > I've checked on 3 different machines (all linux) running both PHP 4 and > PHP 5. > > Here is the code to duplicate the problem: [snip] > Now I'm the first to admit that 0.284217094304 and 0 are > really very close, but when you are testing a variable to see if it > equals 0, it is the difference between true and false. > > This seems very, very basic to me, but I can't find the flaw in my code. > > Is this legitimately bad math on the part of PHP? Most computer languages handling floating point calculations just as poorly. If accuracy is important use the BCMath functions. -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* BOFH Excuse #332: suboptimal routing experience */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple math failing - PHP Bug?
On Sun, 31 Oct 2004 21:52:32 -0700, Brian T. Allen <[EMAIL PROTECTED]> wrote: Hi, OK, I'm totally stumped by this. This should be the simplest math imaginable (addition and subtraction), but PHP is coming up with the wrong answer! [snip] Is this legitimately bad math on the part of PHP? Thanks, Brian This is a flaw in how computers handle floating point numbers, not php. As the memory needed to store exact floats to any useful precision would quickly get out of control, floats are handled inprecisely. As a result, you should never test for equality with a float, but rather check if the float is within a certain range of the desired number. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ http://www.smempire.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Simple math failing - PHP Bug?
Hi, OK, I'm totally stumped by this. This should be the simplest math imaginable (addition and subtraction), but PHP is coming up with the wrong answer! I've checked on 3 different machines (all linux) running both PHP 4 and PHP 5. Here is the code to duplicate the problem: --- $a = 2503.54; $b = 303.55; $c = 202.13; $total = 0; $total += $a; $total += $b; $total += $c; $added_total = $total; echo "$a + $b + $c = $total"; echo "Actual: $total"; $total -= $a; $total -= $b; $total -= $c; echo "$added_total - $a - $b - $c = 0"; echo "Actual: $total"; ?> --- This ~should~ output this: 2503.54 + 303.55 + 202.13 = 3009.22 Actual: 3009.22 3009.22 - 2503.54 - 303.55 - 202.13 = 0 Actual: 0 But instead it outputs this: 2503.54 + 303.55 + 202.13 = 3009.22 Actual: 3009.22 3009.22 - 2503.54 - 303.55 - 202.13 = 0 Actual: 2.84217094304E-13 Now I'm the first to admit that 0.284217094304 and 0 are really very close, but when you are testing a variable to see if it equals 0, it is the difference between true and false. This seems very, very basic to me, but I can't find the flaw in my code. Is this legitimately bad math on the part of PHP? Thanks, Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple math computation..
i found the culprit. i remembered, that i use to pass the value with.. number_format($totalCost, 2, ".", ",") so the values being received by my computation has a "," comma 4,000 so that is why i always get wrong values. thanks again! On Mon, 04 Oct 2004 00:02:32 -0700, Matthew Fonda <[EMAIL PROTECTED]> wrote: > Howdy. > > It seems to work fine for me, perhaps you have a typo along the lines > some where. > > $totalCost = 4000; > $shippingestimate = $totalCost * .20; > echo $shippingestimate; > ?> > > echoes 800 > > -- > Regards, > Matthew Fonda > > > > On Sun, 2004-10-03 at 23:26, Louie Miranda wrote: > > the percent of 20% is = .20 right? > > how can i compute the correct value for this? > > > > my $totalCost is $4,000 and when i compute it to .20 using this method.. > > > > $shippingestimate = $totalCost * .20; > > > > i get the value for the shippingestimate = $0.8 which is wrong.. it should $800 > > what seems to be wrong? > > > > -- > > Louie Miranda > > http://www.axishift.com > > -- Louie Miranda http://www.axishift.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple math computation..
Howdy. It seems to work fine for me, perhaps you have a typo along the lines some where. echoes 800 -- Regards, Matthew Fonda On Sun, 2004-10-03 at 23:26, Louie Miranda wrote: > the percent of 20% is = .20 right? > how can i compute the correct value for this? > > my $totalCost is $4,000 and when i compute it to .20 using this method.. > > $shippingestimate = $totalCost * .20; > > i get the value for the shippingestimate = $0.8 which is wrong.. it should $800 > what seems to be wrong? > > -- > Louie Miranda > http://www.axishift.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple math computation..
On Monday 04 October 2004 15:26, Louie Miranda wrote: > the percent of 20% is = .20 right? 'don't know what's "the pecent of 20% is" ;) but in decimal form, yes, it's right. Or, just ".2" or "0.2". > how can i compute the correct value for this? > > my $totalCost is $4,000 and when i compute it to .20 using > this method.. > > $shippingestimate = $totalCost * .20; > > i get the value for the shippingestimate = $0.8 which is > wrong.. it should $800 what seems to be wrong? Formatting problem, most likely. Try this: * I changed the "E" on $shippingEstimate on purpose :) -- - E - copperwalls was here ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] simple math computation..
the percent of 20% is = .20 right? how can i compute the correct value for this? my $totalCost is $4,000 and when i compute it to .20 using this method.. $shippingestimate = $totalCost * .20; i get the value for the shippingestimate = $0.8 which is wrong.. it should $800 what seems to be wrong? -- Louie Miranda http://www.axishift.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple Math Calculation Problem....
* Thus wrote Scott Fletcher ([EMAIL PROTECTED]): > Hi! > > I'm a little stuck with this simple math problem. I'm trying to get a > number of days in integer, not float. > > --snip-- >$input_date = "12/16/2002"; >$UserInput = explode("/", $input_date); > >$UserInputMonth = $UserInput[0]; >$UserInputDay = $UserInput[1]; >$UserInputYear = $UserInput[2]; > >$clockDate = mktime(0,0,0,date("m"),date("d"),date("Y")); >$inputDate = mktime(0,0,0,$UserInputMonth,$UserInputDay,$UserInputYear); I think strtotime might be more efficient.. $clockDate = strtotime(date("m/d/Y")); $inputDate = strtotime("12/16/2002"); > >$Days = (($clockDate - $inputDate) / (24 * 3600)); You can cast it: $Days = (integer)(($clockDate - $inputDate) / (24 * 3600)); >//$Days = round(Days); > --snip-- HTH, Curt -- "I used to think I was indecisive, but now I'm not so sure." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple Math Calculation Problem....
* Thus wrote Scott Fletcher ([EMAIL PROTECTED]): > The int() function is for C/C++ programming only. This Int() function is > not supported in PHP. In PHP it would be Intval(). For float, Floatval(). > For double, DoubleVal(). Etc. But beware of the Octual numbers. You can cast like you can in C/C++ $Days = (int) $value; // C compatible $Days = (integer) $value; // php method Curt -- "I used to think I was indecisive, but now I'm not so sure." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple Math Calculation Problem....
> The int() function is for C/C++ programming only. This Int() function is > not supported in PHP. In PHP it would be Intval(). For float, Floatval(). > For double, DoubleVal(). Etc. But beware of the Octual numbers. > sorry, confusing my flash action script again :( -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple Math Calculation Problem....
Gee! I forgot the '$'. It's been right there in my face. Thanks! I hope the round() function work without a problem because I saw in some posting that some people have this problem where if 3.51 is rounded, it return a 3. "Seth - Kate Buntin" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] I used your snip you just posted and changed the $Days = round(Days) to $Days = round($Days) and got 228. -Original Message- From: Scott Fletcher [mailto:[EMAIL PROTECTED] Sent: Friday, August 01, 2003 9:18 AM To: [EMAIL PROTECTED] Subject: [PHP] Simple Math Calculation Problem Hi! I'm a little stuck with this simple math problem. I'm trying to get a number of days in integer, not float. --snip-- $input_date = "12/16/2002"; $UserInput = explode("/", $input_date); $UserInputMonth = $UserInput[0]; $UserInputDay = $UserInput[1]; $UserInputYear = $UserInput[2]; $clockDate = mktime(0,0,0,date("m"),date("d"),date("Y")); $inputDate = mktime(0,0,0,$UserInputMonth,$UserInputDay,$UserInputYear); $Days = (($clockDate - $inputDate) / (24 * 3600)); //$Days = round(Days); --snip-- But then I get the idea of doing the round function but it only return a '0' value. Anyone know? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple Math Calculation Problem....
The int() function is for C/C++ programming only. This Int() function is not supported in PHP. In PHP it would be Intval(). For float, Floatval(). For double, DoubleVal(). Etc. But beware of the Octual numbers. "Skate" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > >$Days = (($clockDate - $inputDate) / (24 * 3600)); > > $Days = int(($clockDate - $inputDate) / (24 * 3600)); > > you tried that? > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Simple Math Calculation Problem....
I used your snip you just posted and changed the $Days = round(Days) to $Days = round($Days) and got 228. -Original Message- From: Scott Fletcher [mailto:[EMAIL PROTECTED] Sent: Friday, August 01, 2003 9:18 AM To: [EMAIL PROTECTED] Subject: [PHP] Simple Math Calculation Problem Hi! I'm a little stuck with this simple math problem. I'm trying to get a number of days in integer, not float. --snip-- $input_date = "12/16/2002"; $UserInput = explode("/", $input_date); $UserInputMonth = $UserInput[0]; $UserInputDay = $UserInput[1]; $UserInputYear = $UserInput[2]; $clockDate = mktime(0,0,0,date("m"),date("d"),date("Y")); $inputDate = mktime(0,0,0,$UserInputMonth,$UserInputDay,$UserInputYear); $Days = (($clockDate - $inputDate) / (24 * 3600)); //$Days = round(Days); --snip-- But then I get the idea of doing the round function but it only return a '0' value. Anyone know? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple Math Calculation Problem....
>$Days = (($clockDate - $inputDate) / (24 * 3600)); $Days = int(($clockDate - $inputDate) / (24 * 3600)); you tried that? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Simple Math Calculation Problem....
Hi! I'm a little stuck with this simple math problem. I'm trying to get a number of days in integer, not float. --snip-- $input_date = "12/16/2002"; $UserInput = explode("/", $input_date); $UserInputMonth = $UserInput[0]; $UserInputDay = $UserInput[1]; $UserInputYear = $UserInput[2]; $clockDate = mktime(0,0,0,date("m"),date("d"),date("Y")); $inputDate = mktime(0,0,0,$UserInputMonth,$UserInputDay,$UserInputYear); $Days = (($clockDate - $inputDate) / (24 * 3600)); //$Days = round(Days); --snip-- But then I get the idea of doing the round function but it only return a '0' value. Anyone know? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple math
11 % 3 On Sat, 20 Jan 2001 [EMAIL PROTECTED] wrote: > Lets say I have 11 / 3 > > answer is 2.667 > > what if I want to get the remainder of it > so 11 / 3 would be 2 > > is there a built in function for this in PHP? > kinda like MOD in Visual Basic? > > - Thanks > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] simple math
Lets say I have 11 / 3 answer is 2.667 what if I want to get the remainder of it so 11 / 3 would be 2 is there a built in function for this in PHP? kinda like MOD in Visual Basic? - Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]