Re: [PHP] Round help needed

2012-09-28 Thread Jay Blanchard

On 9/27/2012 10:05 PM, Chris Payne wrote:

Hi everyone,

I'm having one of those nights where nothing is working, please help

What I have is this:

$rounded_number = round($test, -3);

Here's the problem i'm having, I need it to increment to the nearest 1000
but it seems to only work if the number is over 500.  For example:


123666  WILL round to 124000 BUT if I put 123085 (As an example) it doesn't
round it, it just stays at 123085 - I know it's probably something totally
ridiculously simple but i'm having a mental block tonight.

Any help would really be appreciated.

Chris
http://www.youtube.com/user/zoolook3264


I just tried it here - http://ideone.com/0icSG and it seems to work fine.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round help needed

2012-09-27 Thread Daniel Brown
On Thu, Sep 27, 2012 at 11:05 PM, Chris Payne oxygene...@gmail.com wrote:
 Hi everyone,

 I'm having one of those nights where nothing is working, please help

 What I have is this:

 $rounded_number = round($test, -3);

 Here's the problem i'm having, I need it to increment to the nearest 1000
 but it seems to only work if the number is over 500.  For example:


 123666  WILL round to 124000 BUT if I put 123085 (As an example) it doesn't
 round it, it just stays at 123085 - I know it's probably something totally
 ridiculously simple but i'm having a mental block tonight.

 Any help would really be appreciated.

 Sounds like one of those should be obvious, but isn't issues.
This gives the desired result:

?php echo round(123085,-3); ?

So I wonder if it's your variable (perhaps even type-casting) or
some other portion of your code.  Can you elaborate and share some of
your bytes with the class, Mr. Payne?

-- 
/Daniel P. Brown
Network Infrastructure Manager
http://www.php.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: Re: [PHP] Round with money_format

2011-09-17 Thread Bill Guion
On Sep 17, 2011, at 3:46 AM, Cyril Lopez wrote:

 From: Cyril Lopez cy...@nethik.fr
 Date: September 16, 2011 10:58:28 AM EDT
 To: php-general@lists.php.net
 Subject: Round with money_format
 
 Hi,
 
 Can someone help me understand how money_format() rounds numbers ?
 
 ?php
 setlocale(LC_ALL, 'fr_FR.UTF-8');
 $price = 12.665;
 echo money_format('%i',$price);
 // 12.66 EUR, 12.67 EUR expected
 
 $price2 = 12.666;
 echo money_format('%i',$price2);
 // 12.67 EUR, ok
 
 echo round($price,2);
 // 12.67, ok
 echo round($price2,2);
 // 12.67, ok
 ?
 
 Misconfiguration ? Bug ?
 Thanks !
 
 Cyril
 
 Config :
 Debian Lenny, PHP 5.3.8

As someone else pointed out, rounding rules vary by locale, but I was taught 
40+ years ago in graduate school programming class, 4 rounds down, 6 rounds up, 
and 5 rounds to the even number. This means 65 rounds to 6, while 75 rounds to 
8. Your example seems to follow that rule.

 -= Bill =-
-- 

Virtuous action will never go unpunished.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with money_format

2011-09-17 Thread Tedd Sperling
On Sep 17, 2011, at 10:56 AM, Bill Guion wrote:
 On Sep 17, 2011, at 3:46 AM, Cyril Lopez wrote:
 
 Can someone help me understand how money_format() rounds numbers ?
 
 As someone else pointed out, rounding rules vary by locale, but I was taught 
 40+ years ago in graduate school programming class, 4 rounds down, 6 rounds 
 up, and 5 rounds to the even number. This means 65 rounds to 6, while 75 
 rounds to 8. Your example seems to follow that rule.

Bill:

For many years (40+) I've claimed the same solution. I even wrote a routine to 
prove that algorithm is better than the one used in round().

The only problem is that it requires millions of instances to show any 
significant difference. IOW, it's simply not worth the effort.

Cheers,

tedd


_
t...@sperling.com
http://sperling.com


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: Re: [PHP] Round with money_format

2011-09-17 Thread Richard Quadling
On 17 September 2011 15:56, Bill Guion bgu...@comcast.net wrote:
 On Sep 17, 2011, at 3:46 AM, Cyril Lopez wrote:

 From: Cyril Lopez cy...@nethik.fr
 Date: September 16, 2011 10:58:28 AM EDT
 To: php-general@lists.php.net
 Subject: Round with money_format

 Hi,

 Can someone help me understand how money_format() rounds numbers ?

 ?php
 setlocale(LC_ALL, 'fr_FR.UTF-8');
 $price = 12.665;
 echo money_format('%i',$price);
 // 12.66 EUR, 12.67 EUR expected

 $price2 = 12.666;
 echo money_format('%i',$price2);
 // 12.67 EUR, ok

 echo round($price,2);
 // 12.67, ok
 echo round($price2,2);
 // 12.67, ok
 ?

 Misconfiguration ? Bug ?
 Thanks !

 Cyril

 Config :
 Debian Lenny, PHP 5.3.8

 As someone else pointed out, rounding rules vary by locale, but I was taught 
 40+ years ago in graduate school programming class, 4 rounds down, 6 rounds 
 up, and 5 rounds to the even number. This means 65 rounds to 6, while 75 
 rounds to 8. Your example seems to follow that rule.


I've been looking to see if I can find the locale's rounding rules - I can't.

Would be useful if anyone knows where they are defined.

As far as I can find (pages similar to
http://www.cl.cam.ac.uk/cgi-bin/manpage?7+locale), the locale data
doesn't seem to include the rounding rules. Sure, the number of DP,
grouping, etc. Yes. But no rounding.

So, does this just truncate?


-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Round with money_format

2011-09-16 Thread Cyril Lopez
Hi,

Can someone help me understand how money_format() rounds numbers ?

?php
  setlocale(LC_ALL, 'fr_FR.UTF-8');
  $price = 12.665;
  echo money_format('%i',$price);
  // 12.66 EUR, 12.67 EUR expected

  $price2 = 12.666;
  echo money_format('%i',$price2);
  // 12.67 EUR, ok

  echo round($price,2);
  // 12.67, ok
  echo round($price2,2);
  // 12.67, ok
?

Misconfiguration ? Bug ?
Thanks !

Cyril

Config :
Debian Lenny, PHP 5.3.8

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with money_format

2011-09-16 Thread Richard Quadling
On 16 September 2011 15:58, Cyril Lopez cy...@nethik.fr wrote:
 Hi,

 Can someone help me understand how money_format() rounds numbers ?

 ?php
  setlocale(LC_ALL, 'fr_FR.UTF-8');
  $price = 12.665;
  echo money_format('%i',$price);
  // 12.66 EUR, 12.67 EUR expected

  $price2 = 12.666;
  echo money_format('%i',$price2);
  // 12.67 EUR, ok

  echo round($price,2);
  // 12.67, ok
  echo round($price2,2);
  // 12.67, ok
 ?

 Misconfiguration ? Bug ?
 Thanks !

 Cyril

Each locale can define its own rules regarding the number of digits to
show, but I can't see anything that covers the rounding.

OOI.

What do you get for 12.66999?

I'm on windows and there's no money_format function (due to a lack of
strfmon() function),


-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with money_format

2011-09-16 Thread Stuart Dallas
On 16 Sep 2011, at 15:58, Cyril Lopez wrote:

 Can someone help me understand how money_format() rounds numbers ?
 
 ?php
  setlocale(LC_ALL, 'fr_FR.UTF-8');
  $price = 12.665;
  echo money_format('%i',$price);
  // 12.66 EUR, 12.67 EUR expected
 
  $price2 = 12.666;
  echo money_format('%i',$price2);
  // 12.67 EUR, ok
 
  echo round($price,2);
  // 12.67, ok
  echo round($price2,2);
  // 12.67, ok
 ?
 
 Misconfiguration ? Bug ?

It's a direct mapping to the libc function, strfmon [1], and an equivalent C 
script gave the same result so it's not a bug in PHP. Locales can't define 
rounding rules, so it's not related to that.

Googling for strfmon rounding errors doesn't get any useful results, so either 
this is by design or it's never caused anyone a problem.

Solution... pass the number through round before going in to money_format.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with money_format

2011-09-16 Thread Stuart Dallas
Oops...

On 16 Sep 2011, at 16:36, Stuart Dallas wrote:

 On 16 Sep 2011, at 15:58, Cyril Lopez wrote:
 
 Can someone help me understand how money_format() rounds numbers ?
 
 ?php
 setlocale(LC_ALL, 'fr_FR.UTF-8');
 $price = 12.665;
 echo money_format('%i',$price);
 // 12.66 EUR, 12.67 EUR expected
 
 $price2 = 12.666;
 echo money_format('%i',$price2);
 // 12.67 EUR, ok
 
 echo round($price,2);
 // 12.67, ok
 echo round($price2,2);
 // 12.67, ok
 ?
 
 Misconfiguration ? Bug ?
 
 It's a direct mapping to the libc function, strfmon [1], and an equivalent C 
 script gave the same result so it's not a bug in PHP. Locales can't define 
 rounding rules, so it's not related to that.
 
 Googling for strfmon rounding errors doesn't get any useful results, so 
 either this is by design or it's never caused anyone a problem.
 
 Solution... pass the number through round before going in to money_format.
 
 -Stuart

[1] http://lxr.php.net/opengrok/xref/PHP_5_3/ext/standard/string.c#5156

 -- 
 Stuart Dallas
 3ft9 Ltd
 http://3ft9.com/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



SV: SV: [PHP] Round to a given value?

2008-12-26 Thread Anders Norrbring
 Anders Norrbring wrote:
  On Wed, 2008-12-24 at 20:38 +0100, Anders Norrbring wrote:
  Rounding an integer to the closest divisor by ten is easy with
  round() and a
  negative precision, but I turn out to be lost if I want to round
  to
  a
  given
  number..
 
  Example, round to the closest 5000, or closest 400? Any great
  ideas?
  Thanks, and merry x-mas!
  Anders.
 
 
 
  http://www.alixaxel.com/wordpress/wp-content/2008/05/Round.php
 
  Someone was way ahead of you and found the answer and posted on
 the
  PHP
  manual pages back in May. The manual pages are always useful, even
  if
  only for the user-written functions!
 
 
  Ash
  www.ashleysheridan.co.uk
 
  Thanks Ash.
  I browse the pages frequently for ideas, and also did it
 yesterday..
  However, I must have missed that post, maybe because it was so
 small
  in
  the
  list.. ;-)
 
  Thanks for the tip!
 
  Anders.
 
  I cheered too fast.. That function rounds to the closest 500,
  literally..
  So, 1100 is rounded to 1500, not to 1000 which would be the correct
  value.
 
  Any more ideas from anyone? My mind isn't working right at the
 moment,
  so a
  ready-to-plug-in function would be really great!
 
 
  Dear Lord.. It seems like it was so much simpler than I would even
 guess..
  Maybe that's why I didn't see it?
 
  function roundto($number, $to) {
  return round($number / $to) * $to;
  }
 
  Anders.
 
 
 
 Isn't what you wrote the same as the following?
 
 function Multiple($number, $multiple)
 {
  return round($number / $multiple) * $multiple;
 }
 
 The above was on the page that Ash linked to...
 
 Named differently true, but it was right there in front of you...


Absolutely true. But when I saw it on the php.net site at first, I didn't
see it as the one I was looking for. I guess when looking to hard, you miss
the obvious. ;-)



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



SV: [PHP] Round to a given value?

2008-12-25 Thread Anders Norrbring
  On Wed, 2008-12-24 at 20:38 +0100, Anders Norrbring wrote:
   Rounding an integer to the closest divisor by ten is easy with
  round() and a
   negative precision, but I turn out to be lost if I want to round to
 a
  given
   number..
  
   Example, round to the closest 5000, or closest 400? Any great
 ideas?
  
   Thanks, and merry x-mas!
   Anders.
  
  
  
  http://www.alixaxel.com/wordpress/wp-content/2008/05/Round.php
 
  Someone was way ahead of you and found the answer and posted on the
 PHP
  manual pages back in May. The manual pages are always useful, even if
  only for the user-written functions!
 
 
  Ash
  www.ashleysheridan.co.uk
 
 
 Thanks Ash.
 I browse the pages frequently for ideas, and also did it yesterday..
 However, I must have missed that post, maybe because it was so small in
 the
 list.. ;-)
 
 Thanks for the tip!
 
 Anders.


I cheered too fast.. That function rounds to the closest 500, literally..
So, 1100 is rounded to 1500, not to 1000 which would be the correct value.

Any more ideas from anyone? My mind isn't working right at the moment, so a
ready-to-plug-in function would be really great!

Thanks,
Anders.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



SV: [PHP] Round to a given value?

2008-12-25 Thread Anders Norrbring
   On Wed, 2008-12-24 at 20:38 +0100, Anders Norrbring wrote:
Rounding an integer to the closest divisor by ten is easy with
   round() and a
negative precision, but I turn out to be lost if I want to round
 to
  a
   given
number..
   
Example, round to the closest 5000, or closest 400? Any great
  ideas?
   
Thanks, and merry x-mas!
Anders.
   
   
   
   http://www.alixaxel.com/wordpress/wp-content/2008/05/Round.php
  
   Someone was way ahead of you and found the answer and posted on the
  PHP
   manual pages back in May. The manual pages are always useful, even
 if
   only for the user-written functions!
  
  
   Ash
   www.ashleysheridan.co.uk
 
 
  Thanks Ash.
  I browse the pages frequently for ideas, and also did it yesterday..
  However, I must have missed that post, maybe because it was so small
 in
  the
  list.. ;-)
 
  Thanks for the tip!
 
  Anders.
 
 
 I cheered too fast.. That function rounds to the closest 500,
 literally..
 So, 1100 is rounded to 1500, not to 1000 which would be the correct
 value.
 
 Any more ideas from anyone? My mind isn't working right at the moment,
 so a
 ready-to-plug-in function would be really great!


Dear Lord.. It seems like it was so much simpler than I would even guess..
Maybe that's why I didn't see it?

function roundto($number, $to) {
return round($number / $to) * $to;
}

Anders.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: SV: [PHP] Round to a given value?

2008-12-25 Thread Jim Lucas

Anders Norrbring wrote:

On Wed, 2008-12-24 at 20:38 +0100, Anders Norrbring wrote:

Rounding an integer to the closest divisor by ten is easy with

round() and a

negative precision, but I turn out to be lost if I want to round

to

a

given

number..

Example, round to the closest 5000, or closest 400? Any great

ideas?

Thanks, and merry x-mas!
Anders.




http://www.alixaxel.com/wordpress/wp-content/2008/05/Round.php

Someone was way ahead of you and found the answer and posted on the

PHP

manual pages back in May. The manual pages are always useful, even

if

only for the user-written functions!


Ash
www.ashleysheridan.co.uk


Thanks Ash.
I browse the pages frequently for ideas, and also did it yesterday..
However, I must have missed that post, maybe because it was so small

in

the
list.. ;-)

Thanks for the tip!

Anders.


I cheered too fast.. That function rounds to the closest 500,
literally..
So, 1100 is rounded to 1500, not to 1000 which would be the correct
value.

Any more ideas from anyone? My mind isn't working right at the moment,
so a
ready-to-plug-in function would be really great!



Dear Lord.. It seems like it was so much simpler than I would even guess..
Maybe that's why I didn't see it?

function roundto($number, $to) {
return round($number / $to) * $to;
}

Anders.




Isn't what you wrote the same as the following?

function Multiple($number, $multiple)
{
return round($number / $multiple) * $multiple;
}

The above was on the page that Ash linked to...

Named differently true, but it was right there in front of you...

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Round to a given value?

2008-12-24 Thread Anders Norrbring
Rounding an integer to the closest divisor by ten is easy with round() and a
negative precision, but I turn out to be lost if I want to round to a given
number..

Example, round to the closest 5000, or closest 400? Any great ideas?

Thanks, and merry x-mas!
Anders.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round to a given value?

2008-12-24 Thread Ashley Sheridan
On Wed, 2008-12-24 at 20:38 +0100, Anders Norrbring wrote:
 Rounding an integer to the closest divisor by ten is easy with round() and a
 negative precision, but I turn out to be lost if I want to round to a given
 number..
 
 Example, round to the closest 5000, or closest 400? Any great ideas?
 
 Thanks, and merry x-mas!
 Anders.
 
 
 
http://www.alixaxel.com/wordpress/wp-content/2008/05/Round.php

Someone was way ahead of you and found the answer and posted on the PHP
manual pages back in May. The manual pages are always useful, even if
only for the user-written functions!


Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



SV: [PHP] Round to a given value?

2008-12-24 Thread Anders Norrbring
 On Wed, 2008-12-24 at 20:38 +0100, Anders Norrbring wrote:
  Rounding an integer to the closest divisor by ten is easy with
 round() and a
  negative precision, but I turn out to be lost if I want to round to a
 given
  number..
 
  Example, round to the closest 5000, or closest 400? Any great ideas?
 
  Thanks, and merry x-mas!
  Anders.
 
 
 
 http://www.alixaxel.com/wordpress/wp-content/2008/05/Round.php
 
 Someone was way ahead of you and found the answer and posted on the PHP
 manual pages back in May. The manual pages are always useful, even if
 only for the user-written functions!
 
 
 Ash
 www.ashleysheridan.co.uk


Thanks Ash.
I browse the pages frequently for ideas, and also did it yesterday..
However, I must have missed that post, maybe because it was so small in the
list.. ;-)

Thanks for the tip!

Anders.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: RE: [PHP] round()

2007-10-12 Thread Jürgen Wind



tedd wrote:
 
 At 7:30 PM -0700 10/11/07, Instruct ICC wrote:


Now I see why BCMath was mentioned.
 
 Yes, but precision is not the issue.
 
 It doesn't make any difference if you are rounding.
 
 (a) 1.489123451985765
 
 or
 
 (b) 148912345198576.5
 
 You still have to make a decision as to if the above (a) rounds to:
 
 (a) 1.48912345198577
 
 or
 
 (a) 1.48912345198576
 
 or the above (b) rounds to:
 
 (b)148912345198577
 
 or
 
 (b) 148912345198576
 
 It's a question of rounding, not precision.
 
 Cheers,
 
 tedd
 
 -- 
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 

It's a question of what you expect from a rounding function. 
If you work with reals on a computer you always have a bit of fuzzynes 
due to the internal conversions from float to binary and resulting
truncations.
If you need reproducable results which fit your expectations and if speed 
doesn't matter just use your own routine depending on string comparison.

YMMV
-- 
View this message in context: 
http://www.nabble.com/round%28%29-tf4602528.html#a13177072
Sent from the PHP - General mailing list archive at Nabble.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round()

2007-10-12 Thread tedd

At 10:22 AM -0400 10/11/07, Nathan Nobbe wrote:

On 10/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED] wrote:

At 4:18 PM -0600 10/10/07, 
mailto:[EMAIL PROTECTED][EMAIL PROTECTED] wrote:

I disagree. I will need to see an example where the round() is inaccurate.



You may disagree if you wish, but the php function round() is
inaccurate by definition -- all *rounding* algorithms are inaccurate.


thats why i suggested the bcmath based algorithm, as bcmath supports arbitrary
precision.  i built a quick test script using the method from the 
top comment of the
bcmath page, and round().  it allows for quick comparisons of the 2 
algorithms and
tells you if the results are the same.  i messed around w/ it a 
little bit and the results

were the same every time.


Well, if the results were the same, then both exhibit the same 
accuracy bias in rounding.


However, precision is not the issue. There's a difference between 
precision and accuracy and I am addressing accuracy.


For example, if the answer is 4.5, then 4.4 is more accurate than 
4.399, but the later is more precise.


Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] round()

2007-10-12 Thread Instruct ICC

  Yes, but precision is not the issue.
  
  It doesn't make any difference if you are rounding.
  You still have to make a decision


Uuhm, what was $t on the platform before the round please (the += 
.000..1 post)?  Then that will confirm that precision is not the 
problem.



Also, making a decision is setting the optional precision value in the round 
function.
_
Climb to the top of the charts!  Play Star Shuffle:  the word scramble 
challenge with star power.
http://club.live.com/star_shuffle.aspx?icid=starshuffle_wlmailtextlink_oct

RE: [PHP] round()

2007-10-12 Thread Instruct ICC

   Yes, but precision is not the issue.
   
php -r '$t=123.45; echo $t . \n; $t+=0.001; echo $t . \n;'
123.45
123.451
php -r '$t=123.45; echo $t . \n; $t+=0.0001; echo $t . \n;'
123.45
123.45

php -r '$t=123.45678901234567; echo $t . \n;'
123.45678901235

Geee garbage in, garbage out.
round would have been given garbage to process depending upon the precision 
desired.

Anyway, you can still believe what you want.  I see others agree.

_
Help yourself to FREE treats served up daily at the Messenger Café. Stop by 
today.
http://www.cafemessenger.com/info/info_sweetstuff2.html?ocid=TXT_TAGLM_OctWLtagline

RE: RE: [PHP] round()

2007-10-12 Thread tedd

At 11:08 AM -0700 10/12/07, =?UTF-8?Q?J=C3=BCrgen_Wind?= wrote:

It's a question of what you expect from a rounding function.
If you work with reals on a computer you always have a bit of fuzzynes
due to the internal conversions from float to binary and resulting
truncations.


It is only reals that use a rounding function.


If you need reproducable results which fit your expectations and if speed
doesn't matter just use your own routine depending on string comparison.


Yes, I can round my own -- one that is more accurate than round(). 
But, I was wondering if others had observed that round() was not as 
accurate as other methods -- and that was the point of my initial 
post.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] round()

2007-10-12 Thread tedd

At 12:55 PM -0700 10/12/07, Instruct ICC wrote:

Yes, but precision is not the issue.

  

php -r '$t=123.45; echo $t . \n; $t+=0.001; echo $t . \n;'
123.45
123.451
php -r '$t=123.45; echo $t . \n; $t+=0.0001; echo $t . \n;'
123.45
123.45

php -r '$t=123.45678901234567; echo $t . \n;'
123.45678901235

Geee garbage in, garbage out.
round would have been given garbage to process depending upon the 
precision desired.


Anyway, you can still believe what you want.  I see others agree.



Never mind.

Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round()

2007-10-11 Thread Nathan Nobbe
On 10/11/07, tedd [EMAIL PROTECTED] wrote:

 At 4:18 PM -0600 10/10/07, [EMAIL PROTECTED] wrote:
 I disagree. I will need to see an example where the round() is
 inaccurate.


 You may disagree if you wish, but the php function round() is
 inaccurate by definition -- all *rounding* algorithms are inaccurate.


thats why i suggested the bcmath based algorithm, as bcmath supports
arbitrary
precision.  i built a quick test script using the method from the top
comment of the
bcmath page, and round().  it allows for quick comparisons of the 2
algorithms and
tells you if the results are the same.  i messed around w/ it a little bit
and the results
were the same every time.

[EMAIL PROTECTED] ~/working/www $ vim bcRound.php
[EMAIL PROTECTED] ~/working/www $ ./bcRound.php 5.58635634564356 6
round result: 5.586356
roundbc result: 5.586356
the results are different
[EMAIL PROTECTED] ~/working/www $ vim bcRound.php
[EMAIL PROTECTED] ~/working/www $ ./bcRound.php 5.58635634564356 6
round result: 5.586356
roundbc result: 5.586356
the results are the same
[EMAIL PROTECTED] ~/working/www $ ./bcRound.php 5.58635634564356 8
round result: 5.58635635
roundbc result: 5.58635635
the results are the same
[EMAIL PROTECTED] ~/working/www $ ./bcRound.php 5.58635634564356 3
round result: 5.586
roundbc result: 5.586
the results are the same


i dont know what the point of the toFixed() function is (posted earlier).
round takes a second parameter that specifies the desired precision.

#!/usr/bin/php
?php
if($argc != 3) {
die('Usage: rounder.php float precision' . PHP_EOL);
}

$float = $argv[1];
$precision = $argv[2];

$roundResult = round($float, $precision);
$roundbcResult = roundbc($float, $precision);

echo round result: $roundResult .  PHP_EOL;
echo roundbc result: $roundbcResult . PHP_EOL;

if($roundResult !== $roundbcResult) {
echo 'the results are the same' . PHP_EOL;
} else {
echo 'the results are different' . PHP_EOL;
}

/// borrowed from post on php.net
function roundbc($x, $p) {

$x = trim($x);
$data = explode(.,$x);

if(substr($data[1],$p,1) = 5) {

   //generate the add string.
   $i=0;
   $addString = 5;
while($i  $p) {
$addString = 0 . $addString;
$i++;
}//end while.

   $addString = . . $addString;

   //now add the addString to the original fraction.
   $sum = bcadd($data[0] . . . $data   [1],$addString,$p+1);

   //explode the result.
   $sumData = explode(.,$sum);

   //now, return the correct precision on the rounded number.
   return $sumData[0] . . . substr($sumData[1],0,$p);

 } else {
   //don't round the value and return the orignal to the desired
   //precision or less.
   return $data[0] . . . substr($data[1],0,$p);

  }//end if/else.

}//end roundbc.
?


-nathan


RE: [PHP] round()

2007-10-11 Thread tedd

At 4:18 PM -0600 10/10/07, [EMAIL PROTECTED] wrote:

I disagree. I will need to see an example where the round() is inaccurate.



You may disagree if you wish, but the php function round() is 
inaccurate by definition -- all *rounding* algorithms are inaccurate.


My claim is the there are more accurate ways to round.

Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] round()

2007-10-10 Thread tedd

Hi gang:

While we're entertaining algorithms, has anyone else noticed that 
php's round() isn't the most accurate algorithm to round?


Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round()

2007-10-10 Thread David Giragosian
On 10/10/07, tedd [EMAIL PROTECTED] wrote:

 Hi gang:

 While we're entertaining algorithms, has anyone else noticed that
 php's round() isn't the most accurate algorithm to round?

 Cheers,

 tedd

 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com


Yes. sprintf seems to give more expectable results.

David


Re: [PHP] round()

2007-10-10 Thread Nathan Nobbe
On 10/10/07, tedd [EMAIL PROTECTED] wrote:

 Hi gang:

 While we're entertaining algorithms, has anyone else noticed that
 php's round() isn't the most accurate algorithm to round?


there is a function that uses the bc math library on the bc math
pagehttp://www.php.net/manual/en/ref.bc.php
;
first comment in the list; maybe it works better, but i havent tried it.

-nathan


RE: [PHP] round()

2007-10-10 Thread Instruct ICC

 While we're entertaining algorithms, has anyone else noticed that
 php's round() isn't the most accurate algorithm to round?

For those that replied that there is a problem, can you provide examples?

precision

The optional number of decimal digits to round to, defaults to 0


_
Peek-a-boo FREE Tricks  Treats for You!
http://www.reallivemoms.com?ocid=TXT_TAGHMloc=us
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] round()

2007-10-10 Thread admin
 While we're entertaining algorithms, has anyone else noticed that 

 php's round() isn't the most accurate algorithm to round?

 

If you will refer to chafy's reply  on 28-Feb-2007 06:13
http://us2.php.net/manual/en/function.round.php#73537 

The function round numbers to a given precision.

function toFixed($number, $round=2)
{ 

$tempd = $number*pow(10,$round);
$tempd1 = round($tempd);
$number = $tempd1/pow(10,$round);
return $number;
 
}
echo toFixed(5.555,2);  //return 5.56 

 

If your rounding issue is passed 2 decimal places. I found this function
covenant and problem solving. As for the accuracy  of the algorithm 

I disagree. I will need to see an example where the round() is inaccurate.

 



Re: [PHP] round()

2007-10-10 Thread Bob Chatman
Why arent you just using sprintf?

$var = 5.555;

printf(%.2f, $var); # output : 5.56
printf(%.1f, $var); # output : 5.6
printf(%.0f, $var); # output : 6

On 10/10/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

  While we're entertaining algorithms, has anyone else noticed that

  php's round() isn't the most accurate algorithm to round?



 If you will refer to chafy's reply  on 28-Feb-2007 06:13
 http://us2.php.net/manual/en/function.round.php#73537

 The function round numbers to a given precision.

 function toFixed($number, $round=2)
 {

 $tempd = $number*pow(10,$round);
 $tempd1 = round($tempd);
 $number = $tempd1/pow(10,$round);
 return $number;

 }
 echo toFixed(5.555,2);  //return 5.56



 If your rounding issue is passed 2 decimal places. I found this function
 covenant and problem solving. As for the accuracy  of the algorithm

 I disagree. I will need to see an example where the round() is inaccurate.






[PHP] Round

2007-08-29 Thread Koen van den Boogaart
Is it expected behaviour that

?php var_dump( round(-0.26) ); ?

outputs

float(-0) 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Richard Heyes

Koen van den Boogaart wrote:

Is it expected behaviour that

?php var_dump( round(-0.26) ); ?

outputs

float(-0) 



Yes. You (probably) want:

?php
var_dump(round(-0.26, 1));
?

http://uk.php.net/manual/en/function.round.php

--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Koen van den Boogaart
No, I want it to go to float(0), so minus the minus. -0 doesn't exist in 
math, as far as I know. Probably a precision thing.

Richard Heyes [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 Koen van den Boogaart wrote:
 Is it expected behaviour that

 ?php var_dump( round(-0.26) ); ?

 outputs

 float(-0)


 Yes. You (probably) want:

 ?php
 var_dump(round(-0.26, 1));
 ?

 http://uk.php.net/manual/en/function.round.php

 -- 
 Richard Heyes
 +44 (0)844 801 1072
 http://www.websupportsolutions.co.uk

 Knowledge Base and HelpDesk software
 that can cut the cost of online support 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Jim Lucas

Koen van den Boogaart wrote:

Is it expected behaviour that

?php var_dump( round(-0.26) ); ?

outputs

float(-0) 


What version of PHP are you running.

I'm running 5.1.6 and I get float(0)

--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Richard Heyes

Koen van den Boogaart wrote:
No, I want it to go to float(0), so minus the minus. -0 doesn't exist in 
math, as far as I know. Probably a precision thing.


Ok, then try abs() first then.

--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Daniel Brown
On 8/29/07, Richard Heyes [EMAIL PROTECTED] wrote:
 Koen van den Boogaart wrote:
  No, I want it to go to float(0), so minus the minus. -0 doesn't exist in
  math, as far as I know. Probably a precision thing.

 Ok, then try abs() first then.

 --
 Richard Heyes
 +44 (0)844 801 1072
 http://www.websupportsolutions.co.uk

 Knowledge Base and HelpDesk software
 that can cut the cost of online support

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



Richard (Heyes) is right.

?php echo var_dump(abs(round(-0.26))).\n; ?

Tested on 5.0.4 and 5.2.3.

-- 
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

Hey, PHP-General list
50% off for life on web hosting plans $10/mo. or more at
http://www.pilotpig.net/.
Use the coupon code phpgeneralaug07
Register domains for about $0.01 more than what it costs me at
http://domains.pilotpig.net/.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Jim Lucas

Daniel Brown wrote:

On 8/29/07, Richard Heyes [EMAIL PROTECTED] wrote:

Koen van den Boogaart wrote:

No, I want it to go to float(0), so minus the minus. -0 doesn't exist in
math, as far as I know. Probably a precision thing.

Ok, then try abs() first then.

--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Richard (Heyes) is right.

?php echo var_dump(abs(round(-0.26))).\n; ?

Tested on 5.0.4 and 5.2.3.


Think this through before you respond...

Try this

?php
var_dump( round(-0.26) );
var_dump( abs( round(-0.26) ) );
var_dump( round(-1.26) );
var_dump( abs( round(-1.26) ) );
?

does this give you the desired results?

What if I expected -1 for the last answer?


--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Richard Heyes

What if I expected -1 for the last answer?


Then only use abs() if the result from round() is zero;

--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Zoltán Németh
2007. 08. 29, szerda keltezéssel 09.29-kor Jim Lucas ezt írta:
 Daniel Brown wrote:
  On 8/29/07, Richard Heyes [EMAIL PROTECTED] wrote:
  Koen van den Boogaart wrote:
  No, I want it to go to float(0), so minus the minus. -0 doesn't exist in
  math, as far as I know. Probably a precision thing.
  Ok, then try abs() first then.
 
  --
  Richard Heyes
  +44 (0)844 801 1072
  http://www.websupportsolutions.co.uk
 
  Knowledge Base and HelpDesk software
  that can cut the cost of online support
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 
 
  
  Richard (Heyes) is right.
  
  ?php echo var_dump(abs(round(-0.26))).\n; ?
  
  Tested on 5.0.4 and 5.2.3.
  
 Think this through before you respond...
 
 Try this
 
 ?php
 var_dump( round(-0.26) );
 var_dump( abs( round(-0.26) ) );
 var_dump( round(-1.26) );
 var_dump( abs( round(-1.26) ) );
 ?
 
 does this give you the desired results?
 
 What if I expected -1 for the last answer?

why would you expect -1 for the last one?
abs(-1) should return 1 AFAIK

that's what the above code produces too.

greets
Zoltán Németh

 
 
 -- 
 Jim Lucas
 
 Some men are born to greatness, some achieve greatness,
 and some have greatness thrust upon them.
 
 Twelfth Night, Act II, Scene V
  by William Shakespeare
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Daniel Brown
On 8/29/07, Zoltán Németh [EMAIL PROTECTED] wrote:
  Think this through before you respond...
 
  Try this
 
  ?php
  var_dump( round(-0.26) );
  var_dump( abs( round(-0.26) ) );
  var_dump( round(-1.26) );
  var_dump( abs( round(-1.26) ) );
  ?
 
  does this give you the desired results?
 
  What if I expected -1 for the last answer?

It didn't take much thinking this time if you were expecting
-1 for the last answer, you'd be wrong.  ;-P

The very nature of abs() is to return an absolute number, which is
never a negative.

-- 
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

Hey, PHP-General list
50% off for life on web hosting plans $10/mo. or more at
http://www.pilotpig.net/.
Use the coupon code phpgeneralaug07
Register domains for about $0.01 more than what it costs me at
http://domains.pilotpig.net/.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Jim Lucas

Daniel Brown wrote:

On 8/29/07, Zoltán Németh [EMAIL PROTECTED] wrote:

Think this through before you respond...

Try this

?php
var_dump( round(-0.26) );
var_dump( abs( round(-0.26) ) );
var_dump( round(-1.26) );
var_dump( abs( round(-1.26) ) );
?

does this give you the desired results?

What if I expected -1 for the last answer?


It didn't take much thinking this time if you were expecting
-1 for the last answer, you'd be wrong.  ;-P

The very nature of abs() is to return an absolute number, which is
never a negative.



Exactly my point, abs() is not the answer

if he had any negative number that did not round to zero, say it would round to -2, then having the 
abs() in the calculations would return 2 instead of -2, which would be wrong.


From what I read from the OP, I don't think this is what he was looking for.

the op was asking why he got -0 instead of 0.  not for a solution to fix it.

ok, better example.

plaintext?php

$list[] =  1;   # I expect to get  1 and I get 1
$list[] = -1;   # I expect to get -1 but I get 1
$list[] = -1.2; # I expect to get -1 but I get 1
$list[] = -0.23;# I expect to get  0 and I get 0
$list[] = -0.75;# I expect to get -1 and I get 1

foreach ($list AS $value) {
var_dump( abs( round($value) ) );
}

?

But, from what the OP says, he would get -0 instead of 0 for the 4th entry.  Am 
I correct with this?

if so, you could try casting it as an int like so

var_dump( (int)round(-0.26) );

That might fix the problem

--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Koen van den Boogaart
I'm running 5.2.3. Reaction on above discussion: of course this problem can 
be solved with all sorts of solutions, but my question is: is it good that a 
function returns a number that doesn't exist? I once posted a bug report 
about a wrong round()-result, though it wasn't a bug, but a consequence of 
the limited precision of PHP when using floats. So I'm just checking, before 
shouting out loud that there's something wrong.

So, again: my question is not how to solve this, but whether or not this is 
expected behaviour.

Jim Lucas [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 Koen van den Boogaart wrote:
 Is it expected behaviour that

 ?php var_dump( round(-0.26) ); ?

 outputs

 float(-0)
 What version of PHP are you running.

 I'm running 5.1.6 and I get float(0)

 -- 
 Jim Lucas

Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them.

 Twelfth Night, Act II, Scene V
 by William Shakespeare 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Round

2007-08-29 Thread PHP-Gen
I believe there is some confusion on what ABS actually does.  Ignoring all
the rounding that you are trying to do ABS is a very simple function.

ABS definition: Returns the absolute value of number.

What that means is.

Abs(1) = 1
Abs(2) = 2
Abs(3) = 3
Abs(0) = 0
Abs(-1) = 1
Abs(-2) = 2
Abs(-3) = 3

Simply put, returns the positive value of the number given.  Thus if you put
it on a negative number it will always return positive.

This most likely doesn't help what your trying to do, but I wanted to
clarify this as you keep stating you are expecting a -1 when you use the ABS
function.  Unless someone has some trick that I don't know about, ABS will
NEVER return a negative number.


-Original Message-
From: Jim Lucas [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 29, 2007 1:43 PM
To: Daniel Brown
Cc: Zoltán Németh; php-general@lists.php.net
Subject: Re: [PHP] Round

Daniel Brown wrote:
 On 8/29/07, Zoltán Németh [EMAIL PROTECTED] wrote:
 Think this through before you respond...

 Try this

 ?php
 var_dump( round(-0.26) );
 var_dump( abs( round(-0.26) ) );
 var_dump( round(-1.26) );
 var_dump( abs( round(-1.26) ) );
 ?

 does this give you the desired results?

 What if I expected -1 for the last answer?
 
 It didn't take much thinking this time if you were expecting
 -1 for the last answer, you'd be wrong.  ;-P
 
 The very nature of abs() is to return an absolute number, which is
 never a negative.
 

Exactly my point, abs() is not the answer

if he had any negative number that did not round to zero, say it would round
to -2, then having the 
abs() in the calculations would return 2 instead of -2, which would be
wrong.

 From what I read from the OP, I don't think this is what he was looking
for.

the op was asking why he got -0 instead of 0.  not for a solution to fix it.

ok, better example.

plaintext?php

$list[] =  1;   # I expect to get  1 and I get 1
$list[] = -1;   # I expect to get -1 but I get 1
$list[] = -1.2; # I expect to get -1 but I get 1
$list[] = -0.23;# I expect to get  0 and I get 0
$list[] = -0.75;# I expect to get -1 and I get 1

foreach ($list AS $value) {
var_dump( abs( round($value) ) );
}

?

But, from what the OP says, he would get -0 instead of 0 for the 4th entry.
Am I correct with this?

if so, you could try casting it as an int like so

var_dump( (int)round(-0.26) );

That might fix the problem

-- 
Jim Lucas

Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
 by William Shakespeare

-- 
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] Round

2007-08-29 Thread Jim Lucas

PHP-Gen wrote:

I believe there is some confusion on what ABS actually does.  Ignoring all
the rounding that you are trying to do ABS is a very simple function.

ABS definition: Returns the absolute value of number.

What that means is.

Abs(1) = 1
Abs(2) = 2
Abs(3) = 3
Abs(0) = 0
Abs(-1) = 1
Abs(-2) = 2
Abs(-3) = 3

Simply put, returns the positive value of the number given.  Thus if you put
it on a negative number it will always return positive.

This most likely doesn't help what your trying to do, but I wanted to
clarify this as you keep stating you are expecting a -1 when you use the ABS
function.  Unless someone has some trick that I don't know about, ABS will
NEVER return a negative number.


Well, just to clarify, it was not I who suggested abs() as the fix.  You can look back to see who 
first suggested it your self.


First off, I completely understand what abs() does, I did passed my basic 
math class...

Secondly, I have been the one trying to point out how this is the wrong 
approach.

Finally, from what I can tell, the OP wasn't looking for a solution.  He was looking for an answer 
as to why it returned -0 and not 0.


--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Richard Lynch
On Wed, August 29, 2007 11:38 am, Zoltán Németh wrote:
 2007. 08. 29, szerda keltezéssel 09.29-kor Jim Lucas ezt írta:
 Daniel Brown wrote:
  On 8/29/07, Richard Heyes [EMAIL PROTECTED] wrote:
  Koen van den Boogaart wrote:
  No, I want it to go to float(0), so minus the minus. -0
 doesn't exist in
  math, as far as I know. Probably a precision thing.

I missed the start of this thread...

Technically, -0 exists and is a tautology to +0.

That said, PHP should not be outputting -0 for a round() operation,
nor abs() for that matter.

Check http://bugs.php.net with your PHP version for a similar problem.

-- 
Please vote for this great band:
http://acl.mp3.com/feature/soundandjury/?band=COMPANY-OF-THIEVES

Requires email confirmation.
One vote per day per email limit.
Obvious ballot-stuffing will be revoked.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round

2007-08-29 Thread Koen van den Boogaart
Some information from Wikipedia: http://en.wikipedia.org/wiki/Negative_zero. 
Says in math a negative zero doesn't exist, in computing it does. Strange 
thing is that PHP4 and 5 until at least 5.1.6 (see Jim Lucas) round() gives 
a float(0) and then 5.2.3 gives float(-0). I'll report a bug.

Richard Lynch [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On Wed, August 29, 2007 11:38 am, Zoltán Németh wrote:
 2007. 08. 29, szerda keltezéssel 09.29-kor Jim Lucas ezt írta:
 Daniel Brown wrote:
  On 8/29/07, Richard Heyes [EMAIL PROTECTED] wrote:
  Koen van den Boogaart wrote:
  No, I want it to go to float(0), so minus the minus. -0
 doesn't exist in
  math, as far as I know. Probably a precision thing.

 I missed the start of this thread...

 Technically, -0 exists and is a tautology to +0.

 That said, PHP should not be outputting -0 for a round() operation,
 nor abs() for that matter.

 Check http://bugs.php.net with your PHP version for a similar problem.

 -- 
 Please vote for this great band:
 http://acl.mp3.com/feature/soundandjury/?band=COMPANY-OF-THIEVES

 Requires email confirmation.
 One vote per day per email limit.
 Obvious ballot-stuffing will be revoked. 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Rounding -- was [PHP] round to nearest 500?

2007-02-14 Thread tedd

At 8:40 PM +0100 2/13/07, Satyam wrote:

- Original Message - From: Jon Anderson [EMAIL PROTECTED]

The reason is simple:
0: No rounding. It's already there. (8.0 doesn't need to be rounded 
to 8 - it already *is* 8.)

1-4: You round down - 4 of 9 times you round down.
5-9: You round up - 5 of 9 times you round up.


That is not quite correct, there is no such 4 ninths agains 5 
ninths.  You round down in the interval from 0 to0.4 and you 
round up from 0.5 to 0.999.  If you substract the .5 from 
0.9, you get 0.499 so it is about the same interval for both.


If there is any difference, and actually there is, is because any 
number is, in reality, truncated, and not rounded, at some point. 
Depending on the number of bits of the mantissa, it might be a long 
way off, but eventually, it will happen, and that one is truncation, 
nor rounding, and if you repeat any calculation involving fractional 
numbers, it will eventually add up to something noticeable.


Actually, there is the further problem that computers actually use 
binary, not decimal, so they cannot represent decimal numbers 
exactly.  To offer an example of what I'm talking about, 1/3, which 
results in a never ending 0.33 is exactly 0.1 in base 3 
arithmetic!  Conversely, many 'round' numbers in our decimal 
notation are not 'round' in binary and viceversa.  So it is all the 
piling of lots of rounding errors in real-life number 
representations that produce the error, not the mathematics of 
rounding, which actually work in an abstract world of infinitely 
precise number representations (in other words, infinite bits to 
represent any numbers).


Satyam


Satyam and Jon:

Actually, both of you are correct.

The bias in rounding comes from the concept of rounding down for 
numbers 0-4 and rounding up for number 5-9. Please, let's take a 
critical look at that premise.


For zero, as Jon claims, there is no rounding at all -- you don't do 
anything. You can't include zero as one of the conditions that claims 
to do something when it doesn't do anything. It doesn't do anything 
to the data at all! So, to include zero in a rounding scheme is 
fundamentally flawed. Satyam, put your objections on hold for a 
moment and consider.


For values one to four, you round down. So, you have four conditions 
where you actually do something to the data, you round down.


For values five to nine, you round up. So, you have five conditions 
where you actually do something to the data, you round up.


If you do anything four times one way and five times the other, 
you're going to introduce a bias.


So, how do you get around this?

One way is to use Statistical (Stochastic) rounding. It singles out 
five as being the culprit for this bias. It uses the even/odd rule of 
the preceding digit to determine which direction to round 5. If the 
value is even, then it rounds up -- whereas if the value is odd, then 
it rounds down. (or reverse the rounding, it doesn't matter as long 
as you are consistent).


For all other values (1-4 and 6-9, note four each direction) the 
typical rounding is observed.


Examples of it at work:

0.5 - 1  (zero is treated as even -- see below *)
1.5 - 1
2.5  - 3
3.5 - 3
4.5 - 5
5.5 -5
6.5 - 7
7.5 - 7
8.5 - 9
9.5 - 9

From this distribution (actually all you'll ever need to prove this), 
you can see we have just as many cases that round up as we do that 
round down -- thus, the aforementioned rounding bias has been 
reduced. The negative range is just a mirror image.


Now, one can try to pump this algorithm through a PHP loop to see the 
difference (as I've tried) but you'll find that there are rounding 
errors (due to what Satyam said about computers representing decimal 
numbers exactly) that will prohibit making the algorithm work as 
described.


For example, the built-in functions, such as intval(), do not work 
the way you might expect them to work. Try using this statement:


$t =intval(($number - intval($number)) * 10);

It works fine if the number is 89441560.5  -- it returns 5.

However, if the number is 139690837.6 -- returns 5 instead of 6.

And the list goes on of examples where the result isn't quite what's expected.

If you look deeper into this, you'll find the rounding problem 
doesn't really have a solution, but rather it's a compromise. I'm 
sure that Rasmus Lendorf could shed some light on this -- remember 
the When is z != z? argument I started many moons ago? I think this 
is along the same lines.


Perhaps the round() function takes all this into account, but I don't 
know and therein lies my suspicion.


Cheers,

tedd

*By definition, any number that can be divided by two and leaves no 
remainder is even. Some say that the value is undefined, but for sake 
of this argument, it's considered even. Besides, symmetry in nature 
is more desirable.

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To 

Re: [PHP] round to nearest 500?

2007-02-13 Thread tedd

At 7:57 PM +0100 2/12/07, Marc Weber wrote:

On Mon, 12 Feb 2007 18:02:41 +0100, [EMAIL PROTECTED] wrote:


Is there an easy way in php to round to the nearest 500?

Yeah
$rouned = round($val/500) * 500;


I've always questioned the round() function.

I believe it has a downward bias, am I wrong?

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-13 Thread tg-php
For downward rounding, you'd always want to use floor() and use ceil() for 
rounding up.  round() rounds up on a 5, down on a 4 and below.

Example:
echo round(141.074, 2);  // 141.07
echo round(141.065, 2);  // 141.07


I thought round() (or maybe it was a rounding function in another language or 
something like Excel) did the supposed accounting trick of rounding up and down 
depending on the next significant digit or whatever you'd call it.

For example, 141.075 might round to 141.08 (because 7 is odd) and 141.065 might 
round to 141.06 (because 6 is even).  Or vice versa.  Supposedly this is an 
accounting trick that ultimatley works out in the end for proper rounding of 
money values.  Guess our PHP 4.3.4 here doesn't do that though.

-TG




= = = Original message = = =

At 7:57 PM +0100 2/12/07, Marc Weber wrote:
On Mon, 12 Feb 2007 18:02:41 +0100, [EMAIL PROTECTED] wrote:

Is there an easy way in php to round to the nearest 500?
Yeah
$rouned = round($val/500) * 500;

I've always questioned the round() function.

I believe it has a downward bias, am I wrong?

tedd
-- 
---
http://sperling.com  http://ancientstones.com  http://earthstones.com


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] round to nearest 500?

2007-02-13 Thread Tim
snip
  Supposedly this is an accounting trick that 
 ultimatley works out in the end for proper rounding of money 
 values.  

Yeah works out for who? Bet it doesn't for the guy paying :P

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] round to nearest 500?

2007-02-13 Thread tg-php
hah yeah, always worth a little skepticism, but it seemed to make some kind of 
sense.   If you always round up or always round down, that's obviously not 
right and you end up losing potentially a lot of money or over-estimating the 
money involved.

Founding up for 5 through 9 and down for 0 through 4 seems like it makes some 
kind of sense, but apparently it doesn't work out that way.

I'm sure someone out there knows what I'm talking about (it might be the first 
time, but I know I'm not making this up hah), but rounding 0.75 up to 0.8 and 
0.65 down to 0.6 (or vice versa) is supposed to be more accurate or at least 
leads to fewer anomalies.

Someone feel like writing a quick script that generates random numbers and does 
the rounding based on these two ideas (doing it the 'hard way') and see how 
much variation there is after like 10,000 iterations?  If I have time later, 
I'll do it.  Now I'm even more curious.

-TG

= = = Original message = = =

snip
  Supposedly this is an accounting trick that 
 ultimatley works out in the end for proper rounding of money 
 values.  

Yeah works out for who? Bet it doesn't for the guy paying :P


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-13 Thread Jon Anderson

[EMAIL PROTECTED] wrote:

hah yeah, always worth a little skepticism, but it seemed to make some kind of 
sense.   If you always round up or always round down, that's obviously not 
right and you end up losing potentially a lot of money or over-estimating the 
money involved.

Founding up for 5 through 9 and down for 0 through 4 seems like it makes some 
kind of sense, but apparently it doesn't work out that way.

I'm sure someone out there knows what I'm talking about (it might be the first 
time, but I know I'm not making this up hah), but rounding 0.75 up to 0.8 and 
0.65 down to 0.6 (or vice versa) is supposed to be more accurate or at least 
leads to fewer anomalies.

Someone feel like writing a quick script that generates random numbers and does 
the rounding based on these two ideas (doing it the 'hard way') and see how 
much variation there is after like 10,000 iterations?  If I have time later, 
I'll do it.  Now I'm even more curious.
I wasn't aware of the accounting trick before today, but I believe I can 
explain it: If your numbers are statistically random, then the above 
solution will lead to an even distribution of rounding up and rounding down.


The reason is simple:
0: No rounding. It's already there. (8.0 doesn't need to be rounded to 8 
- it already *is* 8.)

1-4: You round down - 4 of 9 times you round down.
5-9: You round up - 5 of 9 times you round up.

So you round up 11.1% more often than you round down. As a result, if 
you round up when it's odd, and down when it's even, you eliminate the 
11.1% difference in when you'd round up then round down.


That said, if someone were aware of the above rounding trick, it 
wouldn't take someone very much effort to come up with things like fee 
structures or pricing structures that could take advantage of that 
scheme to force rounding errors to remain permanently in the person's favor.


I certainly hope that PHP continues to use the standard technique, and 
not the accounting trick above. :-)


jon

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] round to nearest 500?

2007-02-13 Thread tg-php
Ok, screw work.. it's snowing out anyway (not that that makes a real difference 
to doing PHP work inside), curiosity got the better of me.

btw.. the banker rounding code here was pulled from the round() manual page.  
It's not what I read before, but it's the same concept:

function bankers_round ($moneyfloat = null)
{
   $money_str = sprintf(%01.3f, round($moneyfloat, 3)); // convert to rounded 
(to the nearest thousandth) string
   $thous_pos = strlen($money_str)-1;// Thousandth string 
position
   $hundt_pos = strlen($money_str)-2;// Hundredth string 
position
   if ($money_str[$thous_pos] === 5) $money_str[$thous_pos] = 
((int)$money_str[$hundt_pos]  1) ? 9 : 0; // use a bitwise test, its 
faster than modulus
   // The above statement assists round() by setting the thousandth position to 
9 or zero if the hundredth is even or odd (respectively)
   return round($money_str, 2); // Return rounded value
}


for ($i = 0; $i  1; $i++) {
  $rand = rand() / 1;
  $round += round($rand, 2);
  $banker += bankers_round($rand);
  $total += $rand;
}

echo Total: $totalbr\n;
echo round() rounding: $roundbr\n;
echo Banker Rounding: $bankerbr\n;


Results for one run:
Total: 1082648588.0678
round() rounding: 1082648588.52
Banker Rounding: 1082648588.21

Banker is closer in this case.  Total should round to around ..88.07

Next example...

Total: 1087871577.5462
round() rounding: 1087871578.04
Banker Rounding: 1087871577.83

Banker again closer..   should be around  ..77.55


Let's do 100,000 iterations now:

Total: 10769106454.867
round() rounding: 10769106456.21
Banker Rounding: 10769106456.15

Hmm....54.xx is a bit different than ..56.xx.   Technically the banker one 
is still closer, but you can see how after a while the numbers start to drift 
apart if you don't (can't?) maintain the precision to keep exact values.

I definitely don't want to be an accountant when I grow up.

-TG








= = = Original message = = =

hah yeah, always worth a little skepticism, but it seemed to make some kind of 
sense.   If you always round up or always round down, that's obviously not 
right and you end up losing potentially a lot of money or over-estimating the 
money involved.

Founding up for 5 through 9 and down for 0 through 4 seems like it makes some 
kind of sense, but apparently it doesn't work out that way.

I'm sure someone out there knows what I'm talking about (it might be the first 
time, but I know I'm not making this up hah), but rounding 0.75 up to 0.8 and 
0.65 down to 0.6 (or vice versa) is supposed to be more accurate or at least 
leads to fewer anomalies.

Someone feel like writing a quick script that generates random numbers and does 
the rounding based on these two ideas (doing it the 'hard way') and see how 
much variation there is after like 10,000 iterations?  If I have time later, 
I'll do it.  Now I'm even more curious.

-TG

= = = Original message = = =

snip
  Supposedly this is an accounting trick that 
 ultimatley works out in the end for proper rounding of money 
 values.  

Yeah works out for who? Bet it doesn't for the guy paying :P



___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-13 Thread tg-php
Ahh.. good call.

http://en.wikipedia.org/wiki/Rounding


Apprently it's called banker's rounding or statistician's rounding and is a 
little more complicated than just looking at the odd/even of the digit being 
arounded.

This is starting to get into some heavy math theory and scary stuff that I'll 
leave up to people much smarter than me as far as statistical analysis goes.

I'm still suprised nobody's mentioned Superman 3/Office Space in all this.

-TG

= = = Original message = = =

I think there's a fairly good article on it on wikipedia... but it's  
been a while since I read it and I don't have a link for you... sorry.



On Feb 13, 2007, at 12:32 PM, [EMAIL PROTECTED] tg- 
[EMAIL PROTECTED] wrote:

 hah yeah, always worth a little skepticism, but it seemed to make  
 some kind of sense.   If you always round up or always round down,  
 that's obviously not right and you end up losing potentially a lot  
 of money or over-estimating the money involved.

 Founding up for 5 through 9 and down for 0 through 4 seems like it  
 makes some kind of sense, but apparently it doesn't work out that way.

 I'm sure someone out there knows what I'm talking about (it might  
 be the first time, but I know I'm not making this up hah), but  
 rounding 0.75 up to 0.8 and 0.65 down to 0.6 (or vice versa) is  
 supposed to be more accurate or at least leads to fewer anomalies.

 Someone feel like writing a quick script that generates random  
 numbers and does the rounding based on these two ideas (doing it  
 the 'hard way') and see how much variation there is after like  
 10,000 iterations?  If I have time later, I'll do it.  Now I'm even  
 more curious.

 -TG

 = = = Original message = = =

 snip
  Supposedly this is an accounting trick that
 ultimatley works out in the end for proper rounding of money
 values.

 Yeah works out for who? Bet it doesn't for the guy paying :P


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-13 Thread Satyam
- Original Message - 
From: Jon Anderson [EMAIL PROTECTED]




[EMAIL PROTECTED] wrote:
hah yeah, always worth a little skepticism, but it seemed to make some 
kind of sense.   If you always round up or always round down, that's 
obviously not right and you end up losing potentially a lot of money or 
over-estimating the money involved.


Founding up for 5 through 9 and down for 0 through 4 seems like it makes 
some kind of sense, but apparently it doesn't work out that way.


I'm sure someone out there knows what I'm talking about (it might be the 
first time, but I know I'm not making this up hah), but rounding 0.75 up 
to 0.8 and 0.65 down to 0.6 (or vice versa) is supposed to be more 
accurate or at least leads to fewer anomalies.


Someone feel like writing a quick script that generates random numbers 
and does the rounding based on these two ideas (doing it the 'hard way') 
and see how much variation there is after like 10,000 iterations?  If I 
have time later, I'll do it.  Now I'm even more curious.
I wasn't aware of the accounting trick before today, but I believe I can 
explain it: If your numbers are statistically random, then the above 
solution will lead to an even distribution of rounding up and rounding 
down.


The reason is simple:
0: No rounding. It's already there. (8.0 doesn't need to be rounded to 8 - 
it already *is* 8.)

1-4: You round down - 4 of 9 times you round down.
5-9: You round up - 5 of 9 times you round up.



That is not quite correct, there is no such 4 ninths agains 5 ninths.  You 
round down in the interval from 0 to0.4 and you round up from 0.5 to 
0.999.  If you substract the .5 from 0.9, you get 0.499 so it is 
about the same interval for both.


If there is any difference, and actually there is, is because any number is, 
in reality, truncated, and not rounded, at some point.   Depending on the 
number of bits of the mantissa, it might be a long way off, but eventually, 
it will happen, and that one is truncation, nor rounding, and if you repeat 
any calculation involving fractional numbers, it will eventually add up to 
something noticeable.


Actually, there is the further problem that computers actually use binary, 
not decimal, so they cannot represent decimal numbers exactly.  To offer an 
example of what I'm talking about, 1/3, which results in a never ending 
0.33 is exactly 0.1 in base 3 arithmetic!  Conversely, many 'round' 
numbers in our decimal notation are not 'round' in binary and viceversa.  So 
it is all the piling of lots of rounding errors in real-life number 
representations that produce the error, not the mathematics of rounding, 
which actually work in an abstract world of infinitely precise number 
representations (in other words, infinite bits to represent any numbers).


Satyam


So you round up 11.1% more often than you round down. As a result, if you 
round up when it's odd, and down when it's even, you eliminate the 11.1% 
difference in when you'd round up then round down.


That said, if someone were aware of the above rounding trick, it wouldn't 
take someone very much effort to come up with things like fee structures 
or pricing structures that could take advantage of that scheme to force 
rounding errors to remain permanently in the person's favor.


I certainly hope that PHP continues to use the standard technique, and not 
the accounting trick above. :-)


jon

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.17.37/682 - Release Date: 
12/02/2007 13:23





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-13 Thread tg-php
I don't buy zero doesn't count.  But again, this is getting into serious 
math.  It should be good enough to say 0-4 = 0, 5-9 = 10, but if you don't keep 
strict high precision throughout the whole process and round at every step, 
things are going to be off no matter what.  It's just a matter of being off as 
little as possible (especially when it comes to money.. people are a little 
touchy about that).

Don't malign zero though..  :)

See also:
Zero: The Biography of a Dangerous Idea (Paperback) by Charles Seife

-TG

= = = Original message = = =

I wasn't aware of the accounting trick before today, but I believe I can 
explain it: If your numbers are statistically random, then the above 
solution will lead to an even distribution of rounding up and rounding down.

The reason is simple:
0: No rounding. It's already there. (8.0 doesn't need to be rounded to 8 
- it already *is* 8.)
1-4: You round down - 4 of 9 times you round down.
5-9: You round up - 5 of 9 times you round up.

So you round up 11.1% more often than you round down. As a result, if 
you round up when it's odd, and down when it's even, you eliminate the 
11.1% difference in when you'd round up then round down.

That said, if someone were aware of the above rounding trick, it 
wouldn't take someone very much effort to come up with things like fee 
structures or pricing structures that could take advantage of that 
scheme to force rounding errors to remain permanently in the person's favor.

I certainly hope that PHP continues to use the standard technique, and 
not the accounting trick above. :-)

jon


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] round to nearest 500?

2007-02-12 Thread blackwater dev

Is there an easy way in php to round to the nearest 500?

So if I have 600, I 500 and if I have 800 I want 1000?

Thanks!


Re: [PHP] round to nearest 500?

2007-02-12 Thread Jon Anderson

blackwater dev wrote:

Is there an easy way in php to round to the nearest 500?

So if I have 600, I 500 and if I have 800 I want 1000? 
Multiply by 2, round to 1000, divide by 2. Maybe there's an easier way, 
but that's what I use.


600*2 = 1200, round(1200,-3) = 1000, 1000/2 = 500
800*2 = 1600, round(1600,-3) = 2000, 2000/2 = 1000

jon

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-12 Thread Robert Cummings
On Mon, 2007-02-12 at 11:52 -0500, blackwater dev wrote:
 Is there an easy way in php to round to the nearest 500?
 
 So if I have 600, I 500 and if I have 800 I want 1000?

This should work:

?php

$rounded = round( $someValue / 500 ) * 500;

?

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-12 Thread tg-php

$num = 749;
$rounded = round($num * 2, -3) / 2;
echo $rounded;

-TG

= = = Original message = = =

Is there an easy way in php to round to the nearest 500?

So if I have 600, I 500 and if I have 800 I want 1000?

Thanks!


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-12 Thread Robert Cummings
On Mon, 2007-02-12 at 11:59 -0500, Jon Anderson wrote:
 blackwater dev wrote:
  Is there an easy way in php to round to the nearest 500?
 
  So if I have 600, I 500 and if I have 800 I want 1000? 
 Multiply by 2, round to 1000, divide by 2. Maybe there's an easier way, 
 but that's what I use.
 
 600*2 = 1200, round(1200,-3) = 1000, 1000/2 = 500
 800*2 = 1600, round(1600,-3) = 2000, 2000/2 = 1000

That's cool, I was too lazy to read the docs so I didn't notice you
could do negative precision :)

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] round to nearest 500?

2007-02-12 Thread Tim


 -Message d'origine-
 De : Robert Cummings [mailto:[EMAIL PROTECTED] 
 Envoyé : lundi 12 février 2007 18:00
 À : blackwater dev
 Cc : php-general@lists.php.net
 Objet : Re: [PHP] round to nearest 500?
 
 On Mon, 2007-02-12 at 11:52 -0500, blackwater dev wrote:
  Is there an easy way in php to round to the nearest 500?
  
  So if I have 600, I 500 and if I have 800 I want 1000?
 
 This should work:
 
 ?php
 
 $rounded = round( $someValue / 500 ) * 500;
 
 ?
 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round to nearest 500?

2007-02-12 Thread Marc Weber

On Mon, 12 Feb 2007 18:02:41 +0100, [EMAIL PROTECTED] wrote:


Is there an easy way in php to round to the nearest 500?

Yeah
$rouned = round($val/500) * 500;

Marc

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round behavior changed

2006-05-20 Thread tedd

At 3:17 PM -0500 5/19/06, Richard Lynch wrote:

On Fri, May 19, 2006 1:27 pm, Sébastien Guay wrote:

 I searched the archives but did not found anything.

 It seem that the round function behavior has changed in php 4.4.x (I
 don't
 have 4.4.1 handy for testing).

 In 4.3.11, the output of round(23.005,2) is 23.01 which is
 mathematically
 correct.

 In 4.4.2, the output is 23 which is not correct.  The output of
 round(23.0051,2) is correct though (23.01).

 Is there a workaround/patch for this?


The correctness of rounding kinda depends on which
mathematical/historical model one subscribes to.

There is at least one statistician's model of 'round' wherein a
trailing '5' goes 'up' for preceding even numbers and 'down' for
preceding odd numbers, to provide more symmetry in statistical
analysis of rounding.

Or, at least, that's how I recall it.



Your recall is correct.

The reasoning is that for a trailing 1-4, you 
round down; for 6-9, you round up; and for 0 you 
don't do anything, but for five it becomes a 
statistical problem as to which way you round.


If you always round up, then the bias is up. If 
you always round down (which I believe php does), 
then the bias is down.


However, if you use the statistician's model (up 
for even and down for odd whole numbers), then 
rounding errors are reduced as compared to other 
methods, but they are not eliminated.


tedd


--

http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] round behavior changed

2006-05-19 Thread Sébastien Guay
Hi,

I searched the archives but did not found anything.

It seem that the round function behavior has changed in php 4.4.x (I don't
have 4.4.1 handy for testing).

In 4.3.11, the output of round(23.005,2) is 23.01 which is mathematically
correct.

In 4.4.2, the output is 23 which is not correct.  The output of
round(23.0051,2) is correct though (23.01).

Is there a workaround/patch for this?

Thanks in advance

Sébas

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round behavior changed

2006-05-19 Thread tg-php
I seem to remember something about rounding functions and accounting.. that 
there's a rule in accounting that says if the trailing number is even you round 
down, odd you round up.. or something like that.  I know that's different than 
standard 'math' but in accounting it apparently helps deal with the fractions 
of a penny that can go missing if you always round the same direction.

Someone correct me if I'm wrong, but I suspect what you're seeing has something 
to do with that.. or could.

With that in mind, I took a look at the round function documentation at:


This note may shed some light on what you're seeing:
http://www.php.net/manual/en/function.round.php

 twain47 at gmail dot com
10-May-2005 07:25
The bankers' rounding behaviour mentioned in the notes below seems to have been 
changed to normaly rounding. 

From the comments in http://bugs.php.net/bug.php?id=24142 it was changed in 
4.3.0 but various bugs seem to have been introduced and it wasn't stable again 
until 4.3.5 (http://bugs.php.net/bug.php?id=25694)


That may not explain it though.. Banker's rounding 
(http://en.wikipedia.org/wiki/Significance_arithmetic#The_round-to-even_rounding_rule)
 seems to have to do with the whole number.

I dunno..  some links to read up on though.  Good luck!

-TG

= = = Original message = = =

Hi,

I searched the archives but did not found anything.

It seem that the round function behavior has changed in php 4.4.x (I don't
have 4.4.1 handy for testing).

In 4.3.11, the output of round(23.005,2) is 23.01 which is mathematically
correct.

In 4.4.2, the output is 23 which is not correct.  The output of
round(23.0051,2) is correct though (23.01).

Is there a workaround/patch for this?

Thanks in advance

S~bas


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] round behavior changed

2006-05-19 Thread Richard Lynch
On Fri, May 19, 2006 1:27 pm, Sébastien Guay wrote:
 I searched the archives but did not found anything.

 It seem that the round function behavior has changed in php 4.4.x (I
 don't
 have 4.4.1 handy for testing).

 In 4.3.11, the output of round(23.005,2) is 23.01 which is
 mathematically
 correct.

 In 4.4.2, the output is 23 which is not correct.  The output of
 round(23.0051,2) is correct though (23.01).

 Is there a workaround/patch for this?

The correctness of rounding kinda depends on which
mathematical/historical model one subscribes to.

There is at least one statistician's model of 'round' wherein a
trailing '5' goes 'up' for preceding even numbers and 'down' for
preceding odd numbers, to provide more symmetry in statistical
analysis of rounding.

Or, at least, that's how I recall it.

If the rounding off is absolutely crucial to your application, you
probably should switch to a dedicated mathematics extension such as
BC_MATH or that new-fangled one whose name I never remember.

You are only going to be disappointed time and again as PHP's
general mathematics routines are altered for the masses who don't
really care that much about the minutiae of a single point in the
spectrum.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Round with ONE-decimal... always...

2005-09-12 Thread Gustav Wiberg

Hi there!

I want to adjust the round() -function a little...

If I put round(6,0) there will be an output of 6
If I put round(6,32) there will be an output of 6,3

I want it to have 6,0 instead of just 6... I guess there is an easy solution 
to this? Suggestions?


/G
http://www.varupiraten.se/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with ONE-decimal... always...

2005-09-12 Thread Gustav Wiberg

Thanx!

/G

- Original Message - 
From: George Pitcher [EMAIL PROTECTED]

To: Gustav Wiberg [EMAIL PROTECTED]
Sent: Monday, September 12, 2005 10:55 AM
Subject: RE: [PHP] Round with ONE-decimal... always...



Look at sprint_f() in the manual.

George


-Original Message-
From: Gustav Wiberg [mailto:[EMAIL PROTECTED]
Sent: 12 September 2005 9:52 am
To: PHP General
Subject: [PHP] Round with ONE-decimal... always...


Hi there!

I want to adjust the round() -function a little...

If I put round(6,0) there will be an output of 6
If I put round(6,32) there will be an output of 6,3

I want it to have 6,0 instead of just 6... I guess there is an
easy solution
to this? Suggestions?

/G
http://www.varupiraten.se/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.10.21/96 - Release Date: 2005-09-10




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with ONE-decimal... always...

2005-09-12 Thread Jordan Miller

besides sprintf, number_format will also do it:

number_format(6, 1, ',', '');// Outputs '6,0'

Jordan



On Sep 12, 2005, at 3:52 AM, Gustav Wiberg wrote:



Hi there!

I want to adjust the round() -function a little...

If I put round(6,0) there will be an output of 6
If I put round(6,32) there will be an output of 6,3

I want it to have 6,0 instead of just 6... I guess there is an easy  
solution to this? Suggestions?


/G
http://www.varupiraten.se/

--
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] Round to the nearest X - SOLVED

2005-04-29 Thread Chris Boget
Thank you for everyone's suggestions.  Using them
(and a few other things I've found elsewhere), I've
come up with the following function:

function roundToNearest( $number, $toNearest = 5 ) {

  $retval = 0;

  $mod = $number % $toNearest;

  if( $mod = 0 ) {
$retval = ( $mod  ( $toNearest / 2 )) ? $number + ( $toNearest - $mod )
: $number - $mod;

  } else {
$retval = ( $mod  ( -$toNearest / 2 )) ? $number - $mod : $number +
( -$toNearest - $mod );

  }
  return $retval;

} // end function round();

that works with positive and negative numbers and seems
to work with every test I threw at it.

Thanks again for everyone's help!

thnx,
Chris

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Round to the nearest X

2005-04-28 Thread Chris Boget
I realize this is a dumb question but I just can't come
up with an equation that will do this and I don't see an
internal PHP function (for version 4.3) that will do this...
Basically I need to come up with an equation/algorithm
that will round any number up/down to the nearest X.
I tried using

y +/- (y mod x)

but after I started putting in some test numbers, it didn't
always work.  For example, 

37678 + ( 37678 % 500 )

did not round up to the nearest 500.  

Any ideas?

thnx,
Chris

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round to the nearest X

2005-04-28 Thread Greg Donald
On 4/28/05, Chris Boget [EMAIL PROTECTED] wrote:
 I realize this is a dumb question but I just can't come
 up with an equation that will do this and I don't see an
 internal PHP function (for version 4.3) that will do this...
 Basically I need to come up with an equation/algorithm
 that will round any number up/down to the nearest X.

#!/usr/bin/php
?php

$num = isset( $_SERVER[ 'argv' ][ 1 ] )
? $_SERVER[ 'argv' ][ 1 ]
: 0;

do {
$num++;
} while( $num % 500 );

echo $num;

?

 ./round.php 499
500

 ./round.php 501
1000


-- 
Greg Donald
Zend Certified Engineer
http://destiney.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round to the nearest X

2005-04-28 Thread Brent Baisley
I think the formula you are looking for is something like this:
round( y/x, 0) * x
With y being your number and x being the nearest increment number to 
round to.

On Apr 28, 2005, at 4:10 PM, Chris Boget wrote:
I realize this is a dumb question but I just can't come
up with an equation that will do this and I don't see an
internal PHP function (for version 4.3) that will do this...
Basically I need to come up with an equation/algorithm
that will round any number up/down to the nearest X.
I tried using
y +/- (y mod x)
but after I started putting in some test numbers, it didn't
always work.  For example,
37678 + ( 37678 % 500 )
did not round up to the nearest 500.
Any ideas?
thnx,
Chris
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search  Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Round to the nearest X

2005-04-28 Thread Greg Donald
On 4/28/05, Greg Donald [EMAIL PROTECTED] wrote:
 do {
 $num++;
 } while( $num % 500 );

Actually that fails for the base number 500.  This works for everything:

#!/usr/bin/php
?php

$num = isset( $_SERVER[ 'argv' ][ 1 ] )
? $_SERVER[ 'argv' ][ 1 ]
: 0;

if( $num % 500 )
{
do {
$num++;
} while( $num % 500 );
}

echo $num;

?

 ./round.php
0

 ./round.php 499
500

 ./round.php 500
500

 ./round.php 501
1000


-- 
Greg Donald
Zend Certified Engineer
http://destiney.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Round to the nearest X

2005-04-28 Thread Jared Williams

 
 I realize this is a dumb question but I just can't come up 
 with an equation that will do this and I don't see an 
 internal PHP function (for version 4.3) that will do this...
 Basically I need to come up with an equation/algorithm that 
 will round any number up/down to the nearest X.
 I tried using
 
 y +/- (y mod x)
 
 but after I started putting in some test numbers, it didn't 
 always work.  For example, 
 
 37678 + ( 37678 % 500 )
 
 did not round up to the nearest 500.  
 

37678 + 500 -  (37678 % 500)



Jared

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] round?

2005-04-16 Thread Chris Knipe
Hi,
I'm not sure if round() is what I am after.  I want to round whole numbers 
to the closest 10 - thus, not decimals, etc.

For example.
11.23 = 10
14.76 = 10
15.23 = 20
18.53 = 20
Thanks,
--
Chris.
I love deadlines. I especially love the whooshing sound they make as they 
fly by... - Douglas Adams, 'Hitchhiker's Guide to the Galaxy' 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] round?

2005-04-16 Thread trlists
On 16 Apr 2005 Chris Knipe wrote:

 I'm not sure if round() is what I am after.  I want to round whole numbers 
 to the closest 10 - thus, not decimals, etc.

Just use a precision of -1.  For example round(125, -1) will return 
130.

--
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] round

2005-01-26 Thread blackwater dev
Hello,

I have these values:

8.26456
9.7654
3.
5.2689

and I want them rounded to two decimal places so I use the round :
round($value,2) which gives me, for example:
8.26

But, it also gives me 3 instead of 3.00.  How can I round but retain the 0's?

Thanks!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] round

2005-01-26 Thread Gerard Petersen
You can use the number_format($number, decimal_places) function.

-Original Message-
From: blackwater dev [mailto:[EMAIL PROTECTED] 
Sent: 26 January 2005 01:33 PM
To: php-general@lists.php.net
Subject: [PHP] round

Hello,

I have these values:

8.26456
9.7654
3.
5.2689

and I want them rounded to two decimal places so I use the round :
round($value,2) which gives me, for example:
8.26

But, it also gives me 3 instead of 3.00.  How can I round but retain the
0's?

Thanks!

-- 
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] round

2005-01-26 Thread Sergio Gorelyshev
On Wed, 26 Jan 2005 06:32:32 -0500
blackwater dev [EMAIL PROTECTED] wrote:

 Hello,
 
 I have these values:
 
 8.26456
 9.7654
 3.
 5.2689
 
 and I want them rounded to two decimal places so I use the round :
 round($value,2) which gives me, for example:
 8.26

You can try the sprintf() function

 But, it also gives me 3 instead of 3.00.  How can I round but retain the 0's?
 
 Thanks!
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


-- 
RE5PECT
Sergio Gorelyshev

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Round() behaviour issue

2003-12-16 Thread Scott McDaid
Hi there. I've been looking at the behaviour of the round functionality in 
PHP. We're currently still using v4.2.3, (but the documentation seems to 
suggest it's the same for versions after this).

Doing the following rounds always rounds *up* to the nearest whole number. 
i.e.

0.5 = 1
1.5 = 2
2.5 = 3
3.5 = 4

In earlier PHP docs, it stated that halves would be rounded to the nearest 
even number.
i.e. 

3.5 = 4
4.5 = 4

When did this change. Or is this *actually* the case in a version of PHP 
later than 4.2.3. I've had a look at the PHP changelog and the bug lists 
and found nothing. :(

I know I can work around this by adding a fuzz factor, but I'd rather not. 
I'm just confused as to why something so fundamental to math would change 
across PHP versions.

Kind Regards

Scott

-- 
Scott McDaid
lightershade
t: +44 131 514 4000
d: +44 131 514 4009

-- 

This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the sender. Any
offers or quotation of service are subject to formal specification.
Errors and omissions excepted.  Please note that any views or opinions
presented in this email are solely those of the author and do not
necessarily represent those of edNET or lightershade ltd. Finally, the
recipient should check this email and any attachments for the presence of
viruses.  edNET and lightershade ltd accepts no liability for any damage
caused by any virus transmitted by this email.

-- 
-- 
Virus scanned by edNET.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round() behaviour issue

2003-12-16 Thread Eugene Lee
On Tue, Dec 16, 2003 at 11:13:25AM +, Scott McDaid wrote:
: 
: Hi there. I've been looking at the behaviour of the round functionality in 
: PHP. We're currently still using v4.2.3, (but the documentation seems to 
: suggest it's the same for versions after this).
: 
: Doing the following rounds always rounds *up* to the nearest whole number. 
: i.e.
: 
: 0.5 = 1
: 1.5 = 2
: 2.5 = 3
: 3.5 = 4

Any non-zero fractional component that gets rounded to the next whole
number is done with ceil().

: In earlier PHP docs, it stated that halves would be rounded to the nearest 
: even number.
: i.e. 
: 
: 3.5 = 4
: 4.5 = 4

I recall seeing those docs and getting very confused, especially with
your listed example.  Personally, I think the current behavior for
round() is mathematically correct; i.e. any number whose fractional
component is greater-than or equal-to 1/2 is rounded up to the next
whole number, otherwise it's rounded down.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Round() behaviour issue

2003-12-16 Thread Ford, Mike [LSS]
On 16 December 2003 11:13, Scott McDaid wrote:

 Hi there. I've been looking at the behaviour of the round
 functionality in PHP. We're currently still using v4.2.3, (but the
 documentation seems to
 suggest it's the same for versions after this).
 
 Doing the following rounds always rounds *up* to the nearest
 whole number.
 i.e.
 
 0.5 = 1
 1.5 = 2
 2.5 = 3
 3.5 = 4
 
 In earlier PHP docs, it stated that halves would be rounded
 to the nearest
 even number.
 i.e.
 
 3.5 = 4
 4.5 = 4
 
 When did this change. Or is this *actually* the case in a
 version of PHP
 later than 4.2.3. I've had a look at the PHP changelog and
 the bug lists
 and found nothing. :(

From a quick peruse of the source in CVS, round() seems to have been
intended to round positive halves up and negative halves down since at least
PHP 4.0.0 -- although a bug-fix in 4.3.3 seems to suggest this might not
always have been happening in reality.  It seems likely that the reference
to rounding halves to the nearest even number was an error in the
documentation which has been fixed by removing it!

(http://bugs.php.net/bug.php?id=25694, which looks like it might be
relevant, is actually fixed by changing the behaviour of number_format()!)

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, 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



[PHP] Round a number

2003-10-03 Thread Shaun
Hi,

I have a query that returns a number from  culculation in my table. It
returns say 4.00, 8.75, 0.00, 12.50 etc. How format the number so that the
trailing zeros aer removed i.e. 4, 8.75, 0, 12.5 etc?

Thanks for your help

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round a number

2003-10-03 Thread Marek Kilimajer
echo (iny)$number;

Shaun wrote:

Hi,

I have a query that returns a number from  culculation in my table. It
returns say 4.00, 8.75, 0.00, 12.50 etc. How format the number so that the
trailing zeros aer removed i.e. 4, 8.75, 0, 12.5 etc?
Thanks for your help

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Round Off $ prices

2003-06-05 Thread Joci
Hi!

ceil -- Round fractions up
round -- Rounds a float
floor -- Round fractions down

Joe
- Original Message - 
From: Ralph [EMAIL PROTECTED]
To: PHP General Mailing List [EMAIL PROTECTED]
Sent: Wednesday, June 04, 2003 2:51 PM
Subject: [PHP] Round Off $ prices


 How do I round off prices to the  like rounding off $24.80 to $25?
 
 
 
 -- 
 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



[PHP] Round Off $ prices

2003-06-04 Thread Ralph
How do I round off prices to the  like rounding off $24.80 to $25?



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Round robin script revisited..

2002-12-30 Thread Chad Day
I was trying to find a script to do round robin pairings for a sports
schedule generator..  I was able to come across one in Perl, and am in the
process of trying to convert it to PHP, but since my perl knowledge is a bit
(well, a lot) lacking, I was hoping someone could help..

Perl script:

http://www.perlmonks.org/index.pl?parent=90132title=Round%20Robin%20Schedul
inglastnode_id=90132displaytype=displaytype=superdocnode=Comment%20on


The part I'm having trouble with:

for(1..($size-$even))
{
my @this_week;
# push the weeks worth of games onto @games
foreach my $sched_ref (@stripes)
{
push (@this_week, [$teams[$sched_ref-[0]], $teams[$sched_ref-
+[1]]]);
}
push(@games, \@this_week);


I'm not sure how to rewrite that in PHP and be pushing the correct array
elements into the games array .. If anyone can help me out or point me in
the right direction, I'd appreciate it.

Thanks,
Chad


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Round robin script revisited..

2002-12-30 Thread Chad Day
Ok, after some more fiddling, I'm really close to getting this to work.

The script seems to correctly generate a schedule if an odd number of teams
are specified:

Given an array of 5 teams, A through E, the following pairs are generated:

Round 1 - A/E, B/D
Round 2 - D/E, A/C
Round 3 - C/D, B/E
Round 4 - B/C, A/D
Round 5 - A/B, C/E

Looks good.. each team gets a bye, each team plays each other.  With an even
number of teams however, some pairings are repeated, and I'm sure this is a
bug I created when translating the code from perl to PHP.  The PHP code I
have is below, the link to the perl source is
(http://www.perlmonks.org/index.pl?parent=90132title=Round%20Robin%20Schedu
linglastnode_id=90132displaytype=displaytype=superdocnode=Comment%20on),
and the set of pairings I get with 6 teams, A through F is below.

6 teams, A/F

Round 1 - A/E, B/D, C/F
Round 2 - F/D, A/C, B/E
Round 3 - E/C, F/B, A/D
Round 4 - D/B, E/A, F/C (dupe of round 1)
Round 5 - C/A, D/F, E/B (dupe of round 2)

PHP code:

?

$teamarray = array('A', 'B', 'C', 'D', 'E', 'F');

$num_teams = sizeof($teamarray);

$pivot = undef;
$even = 0;
$end_point_a = 0;
$end_point_b = $num_teams-1;

if($num_teams % 2 == 0) {
# $pivot = array_pop($teamarray);
#$pivot = $#teams;
#$pivot = $num_teams-;
$pivot = ($num_teams - 1);
$even = 1;
$end_point_b--;
}


# Create a list of the stripes of the polygon, the stripes will be
# pairs of indices into the array

$stripes = array();

# Assume that each element in the array is a vertex of the polygon
# and the vertices are listed in order, stripes are created

for ($x = 1; $x  (sizeof($teamarray) / 2); $x++) {
# for (1..int(($size-1)/2)) {
array_push($stripes, array($end_point_a, $end_point_b));
$end_point_a++;
$end_point_b--;
}

if ($even) {
array_push($stripes, array($end_point_a,  $pivot));
}

$games = array();
for ($x = 0; $x  ($num_teams - $even); $x++) {
$this_week = array();
for ($y = 0; $y  sizeof($stripes); $y++) {
$num1 = $stripes[$y][0];
$num2 = $stripes[$y][1];
array_push($this_week, $teamarray[$num1], $teamarray[$num2]);
}
array_push($games, $this_week);

# Now rotate the @teams array
# Save the last team as the pivot for the polygon
$last_team = array_pop($teamarray);
array_unshift($teamarray, $last_team);
//  echo pArray Order: ;
//  for ($z = 0; $z  sizeof($teamarray); $z++) {
//  echo $teamarray[$z];
//  }
/*  if ($even) {
array_push($teamarray, $last_team);
}
*/
}

// echo 'brbrbr';
print_r($games);

?

-Original Message-
From: Chad Day [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 30, 2002 10:57 AM
To: php general
Subject: [PHP] Round robin script revisited..


I was trying to find a script to do round robin pairings for a sports
schedule generator..  I was able to come across one in Perl, and am in the
process of trying to convert it to PHP, but since my perl knowledge is a bit
(well, a lot) lacking, I was hoping someone could help..

Perl script:

http://www.perlmonks.org/index.pl?parent=90132title=Round%20Robin%20Schedul
inglastnode_id=90132displaytype=displaytype=superdocnode=Comment%20on


The part I'm having trouble with:

for(1..($size-$even))
{
my @this_week;
# push the weeks worth of games onto @games
foreach my $sched_ref (@stripes)
{
push (@this_week, [$teams[$sched_ref-[0]], $teams[$sched_ref-
+[1]]]);
}
push(@games, \@this_week);


I'm not sure how to rewrite that in PHP and be pushing the correct array
elements into the games array .. If anyone can help me out or point me in
the right direction, I'd appreciate it.

Thanks,
Chad


--
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] Round robin script revisited..

2002-12-30 Thread Chad Day
Nevermind, I found the bug.

End of the code should be:

# Now rotate the @teams array
# Save the last team as the pivot for the polygon
if ($even) {
$last_team_save = array_pop($teamarray);
}
$last_team = array_pop($teamarray);
array_unshift($teamarray, $last_team);
if ($even) {
array_push($teamarray, $last_team_save);
}


Hope this helps anyone else who needs it.

Chad

-Original Message-
From: Chad Day [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 30, 2002 2:03 PM
To: php general
Subject: RE: [PHP] Round robin script revisited..


Ok, after some more fiddling, I'm really close to getting this to work.

The script seems to correctly generate a schedule if an odd number of teams
are specified:

Given an array of 5 teams, A through E, the following pairs are generated:

Round 1 - A/E, B/D
Round 2 - D/E, A/C
Round 3 - C/D, B/E
Round 4 - B/C, A/D
Round 5 - A/B, C/E

Looks good.. each team gets a bye, each team plays each other.  With an even
number of teams however, some pairings are repeated, and I'm sure this is a
bug I created when translating the code from perl to PHP.  The PHP code I
have is below, the link to the perl source is
(http://www.perlmonks.org/index.pl?parent=90132title=Round%20Robin%20Schedu
linglastnode_id=90132displaytype=displaytype=superdocnode=Comment%20on),
and the set of pairings I get with 6 teams, A through F is below.

6 teams, A/F

Round 1 - A/E, B/D, C/F
Round 2 - F/D, A/C, B/E
Round 3 - E/C, F/B, A/D
Round 4 - D/B, E/A, F/C (dupe of round 1)
Round 5 - C/A, D/F, E/B (dupe of round 2)

PHP code:

?

$teamarray = array('A', 'B', 'C', 'D', 'E', 'F');

$num_teams = sizeof($teamarray);

$pivot = undef;
$even = 0;
$end_point_a = 0;
$end_point_b = $num_teams-1;

if($num_teams % 2 == 0) {
# $pivot = array_pop($teamarray);
#$pivot = $#teams;
#$pivot = $num_teams-;
$pivot = ($num_teams - 1);
$even = 1;
$end_point_b--;
}


# Create a list of the stripes of the polygon, the stripes will be
# pairs of indices into the array

$stripes = array();

# Assume that each element in the array is a vertex of the polygon
# and the vertices are listed in order, stripes are created

for ($x = 1; $x  (sizeof($teamarray) / 2); $x++) {
# for (1..int(($size-1)/2)) {
array_push($stripes, array($end_point_a, $end_point_b));
$end_point_a++;
$end_point_b--;
}

if ($even) {
array_push($stripes, array($end_point_a,  $pivot));
}

$games = array();
for ($x = 0; $x  ($num_teams - $even); $x++) {
$this_week = array();
for ($y = 0; $y  sizeof($stripes); $y++) {
$num1 = $stripes[$y][0];
$num2 = $stripes[$y][1];
array_push($this_week, $teamarray[$num1], $teamarray[$num2]);
}
array_push($games, $this_week);

# Now rotate the @teams array
# Save the last team as the pivot for the polygon
$last_team = array_pop($teamarray);
array_unshift($teamarray, $last_team);
//  echo pArray Order: ;
//  for ($z = 0; $z  sizeof($teamarray); $z++) {
//  echo $teamarray[$z];
//  }
/*  if ($even) {
array_push($teamarray, $last_team);
}
*/
}

// echo 'brbrbr';
print_r($games);

?

-Original Message-
From: Chad Day [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 30, 2002 10:57 AM
To: php general
Subject: [PHP] Round robin script revisited..


I was trying to find a script to do round robin pairings for a sports
schedule generator..  I was able to come across one in Perl, and am in the
process of trying to convert it to PHP, but since my perl knowledge is a bit
(well, a lot) lacking, I was hoping someone could help..

Perl script:

http://www.perlmonks.org/index.pl?parent=90132title=Round%20Robin%20Schedul
inglastnode_id=90132displaytype=displaytype=superdocnode=Comment%20on


The part I'm having trouble with:

for(1..($size-$even))
{
my @this_week;
# push the weeks worth of games onto @games
foreach my $sched_ref (@stripes)
{
push (@this_week, [$teams[$sched_ref-[0]], $teams[$sched_ref-
+[1]]]);
}
push(@games, \@this_week);


I'm not sure how to rewrite that in PHP and be pushing the correct array
elements into the games array .. If anyone can help me out or point me in
the right direction, I'd appreciate it.

Thanks,
Chad


--
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



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] ROUND inconsistency

2001-04-13 Thread Yasuo Ohgaki

Correction, MySQL is not returning floor, since it returns 2 for round(1.5). - I
didn't see it.
MySQL should not return 2 for round(2.5), but it should return 3. I think it's
MySQL bug.

How about ask in MySQL mailing list?

--
Yasuo Ohgaki


""Yasuo Ohgaki"" [EMAIL PROTECTED] wrote in message
9b60qv$v9b$[EMAIL PROTECTED]">news:9b60qv$v9b$[EMAIL PROTECTED]...
 MySQL is returning floor. (I'm not a MySQL heavy user, though :)

 PHP's result is correct for its function name, I think.
 If MySQL returns floor for round(), how about use floor() in PHP?

 Regards,
 --
 Yasuo Ohgaki


 "Lee Howard" [EMAIL PROTECTED] wrote in message
 [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
  Using MySQL 3.23.32 on RedHat Linux 7.0...
 
  MySQL's ROUND function rounds 5 up when the preceding digit is odd and down
  when the preceding digit is even.
 
  mysql select round(1.5);
  ++
  | round(1.5) |
  ++
  |  2 |
  ++
  1 row in set (0.00 sec)
 
  mysql select round(2.5);
  ++
  | round(2.5) |
  ++
  |  2 |
  ++
  1 row in set (0.00 sec)
 
  I think that this is technically the correct behavior, scientifically,
  anyway.  However, this is not the common "lay-man's" method of rounding,
  which is to always round 5 up, as exhibited by PHP-4.0.4pl1...
 
  ? echo round(1.5); ?
  br
  ? echo round(2.5); ?
 
  Apache 1.3.14 output for this is:
 
  2
  3
 
  This discrepancy causes a difficulty in programming PHP and MySQL together,
  for example, because all of the rounding must be done in either PHP or
  MySQL but not both partially unless you want conflicting data.
 
  I would like to see MySQL ROUND() syntax expand to be ROUND(X,D,M) where
  optional M value indicates the method of rounding, the default being the
  current method.
 
  I would also like to see PHP round() syntax expand to be
  double round (double val [, int precision] [, char method])
  where the optional method value indicates the method of rounding, the
  default being the current method.
 
  Thanks.
 
  Lee Howard
 
 
  --
  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 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]




Re: [PHP] ROUND inconsistency

2001-04-13 Thread Philip Hallstrom

In article 9b6c4v$m41$[EMAIL PROTECTED] you write:
Correction, MySQL is not returning floor, since it returns 2 for round(1.5). - I
didn't see it.
MySQL should not return 2 for round(2.5), but it should return 3. I think it's
MySQL bug.

How about ask in MySQL mailing list?

I don't think it's just a MySQL thing... it occurs in PostgreSQL as
well...

test= select version();
version 

 PostgreSQL 7.0.3 on i386-unknown-freebsdelf4.1, compiled by gcc 2.95.2
(1 row)

test= select round(1.5);
 round 
---
 2
(1 row)

test= select round(2.5);
 round 
---
 2
(1 row)


However, you could always do floor(number + .5) to consistent
behaviour...

-philip

-- 
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] ROUND inconsistency

2001-04-12 Thread Lee Howard

Using MySQL 3.23.32 on RedHat Linux 7.0...

MySQL's ROUND function rounds 5 up when the preceding digit is odd and down
when the preceding digit is even.

mysql select round(1.5);
++
| round(1.5) |
++
|  2 |
++
1 row in set (0.00 sec)

mysql select round(2.5);
++
| round(2.5) |
++
|  2 |
++
1 row in set (0.00 sec)

I think that this is technically the correct behavior, scientifically,
anyway.  However, this is not the common "lay-man's" method of rounding,
which is to always round 5 up, as exhibited by PHP-4.0.4pl1...

? echo round(1.5); ?
br
? echo round(2.5); ?

Apache 1.3.14 output for this is:

2
3

This discrepancy causes a difficulty in programming PHP and MySQL together,
for example, because all of the rounding must be done in either PHP or
MySQL but not both partially unless you want conflicting data.

I would like to see MySQL ROUND() syntax expand to be ROUND(X,D,M) where
optional M value indicates the method of rounding, the default being the
current method.

I would also like to see PHP round() syntax expand to be 
double round (double val [, int precision] [, char method])
where the optional method value indicates the method of rounding, the
default being the current method.

Thanks.

Lee Howard


-- 
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]




Re: [PHP] ROUND inconsistency

2001-04-12 Thread Yasuo Ohgaki

MySQL is returning floor. (I'm not a MySQL heavy user, though :)

PHP's result is correct for its function name, I think.
If MySQL returns floor for round(), how about use floor() in PHP?

Regards,
--
Yasuo Ohgaki


"Lee Howard" [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 Using MySQL 3.23.32 on RedHat Linux 7.0...

 MySQL's ROUND function rounds 5 up when the preceding digit is odd and down
 when the preceding digit is even.

 mysql select round(1.5);
 ++
 | round(1.5) |
 ++
 |  2 |
 ++
 1 row in set (0.00 sec)

 mysql select round(2.5);
 ++
 | round(2.5) |
 ++
 |  2 |
 ++
 1 row in set (0.00 sec)

 I think that this is technically the correct behavior, scientifically,
 anyway.  However, this is not the common "lay-man's" method of rounding,
 which is to always round 5 up, as exhibited by PHP-4.0.4pl1...

 ? echo round(1.5); ?
 br
 ? echo round(2.5); ?

 Apache 1.3.14 output for this is:

 2
 3

 This discrepancy causes a difficulty in programming PHP and MySQL together,
 for example, because all of the rounding must be done in either PHP or
 MySQL but not both partially unless you want conflicting data.

 I would like to see MySQL ROUND() syntax expand to be ROUND(X,D,M) where
 optional M value indicates the method of rounding, the default being the
 current method.

 I would also like to see PHP round() syntax expand to be
 double round (double val [, int precision] [, char method])
 where the optional method value indicates the method of rounding, the
 default being the current method.

 Thanks.

 Lee Howard


 --
 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] round up number

2001-03-14 Thread Jacky

How do I round up a lot of decimal number like 10.232656898 to be 10.23 ? Should I use 
this?
$num = 10.26564787;
$Rnum = round($num);
What would be the function I am looking for and how?
Jack
[EMAIL PROTECTED]
"There is nothing more rewarding than reaching the goal you set for yourself"



SV: [PHP] round up number

2001-03-14 Thread Johan Holst Nielsen

How do I round up a lot of decimal number like 10.232656898 to be 10.23 ?
Should I use this?
$num = 10.26564787;
$Rnum = round($num);
What would be the function I am looking for and how?


Try:
$num = 10.26564787;
$Rnum = round($num, 2);

/Johan


-- 
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] round

2001-01-31 Thread AJDIN BRANDIC

Hi

Lets say $foo=20.12345

In php4 you can use : $foo=$round($foo,2);  to get $foo=20.12

What can I use in php3 to get the same result.  $foo=$round($foo); gives 
me $foo=20 Not what I realy want???

Thanks 

Ajdin


-- 
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]