Re: [PHP] Math Weirdness

2008-07-16 Thread Daniel Brown
On Tue, Jul 15, 2008 at 3:50 PM, tedd <[EMAIL PROTECTED]> wrote:
>
> Well, someone's client took infinity and turned it into an integral.
>
> tdd

Yeah, but they stole your 'e' to make up for it.

-- 

Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

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



RE: [PHP] Math Weirdness

2008-07-16 Thread Jay Blanchard
[snip]
And then put it in Jay's pocket since we know he's skimming! If you're  
lucky maybe he'll cut you in for not telling the boss :P
[/snip]

"Allegedly". 

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



Re: [PHP] Math Weirdness

2008-07-16 Thread Jason Pruim


On Jul 16, 2008, at 11:39 AM, Shawn McKenzie wrote:


Jochem Maas wrote:

Robert Cummings schreef:

On Tue, 2008-07-15 at 12:37 -0400, tedd wrote:

At 12:31 PM -0400 7/15/08, Robert Cummings wrote:

Umm... here it is to unlimited precision: ¼

Cheers,
Rob.

Yeah and here's ƒ

Like or not, that's all there is to it.


Weird... you're client bastar-dized my beautiful pi symbol.

no it's the worst round error ever. how exactly do you go
from 3.1 to .25 :-P


Easy, subtract 2.85


And then put it in Jay's pocket since we know he's skimming! If you're  
lucky maybe he'll cut you in for not telling the boss :P



--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
11287 James St
Holland, MI 49424
www.raoset.com
[EMAIL PROTECTED]





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



Re: [PHP] Math Weirdness

2008-07-16 Thread Shawn McKenzie

Jochem Maas wrote:

Robert Cummings schreef:

On Tue, 2008-07-15 at 12:37 -0400, tedd wrote:

At 12:31 PM -0400 7/15/08, Robert Cummings wrote:

Umm... here it is to unlimited precision: ¼

Cheers,
Rob.

Yeah and here's ƒ

Like or not, that's all there is to it.


Weird... you're client bastar-dized my beautiful pi symbol.


no it's the worst round error ever. how exactly do you go
from 3.1 to .25 :-P


Easy, subtract 2.85

-Shawn





Cheers,
Rob.




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



Re: [PHP] Math Weirdness

2008-07-15 Thread tedd

At 8:21 PM +0200 7/15/08, Jochem Maas wrote:

Robert Cummings schreef:

On Tue, 2008-07-15 at 12:37 -0400, tedd wrote:

At 12:31 PM -0400 7/15/08, Robert Cummings wrote:

Umm... here it is to unlimited precision: 1Ž4

Cheers,
Rob.

Yeah and here's Ÿ

Like or not, that's all there is to it.


Weird... you're client bastar-dized my beautiful pi symbol.


no it's the worst round error ever. how exactly do you go
from 3.1 to .25 :-P


Well, someone's client took infinity and turned it into an integral.

tdd



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

2008-07-15 Thread Jay Blanchard
[snip]
Ask Jay, he's the skimming master!  >:)
[/snip]

Bow down before mebut not too far lest ye be skimmed as well. 

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



Re: [PHP] Math Weirdness

2008-07-15 Thread Robert Cummings
On Tue, 2008-07-15 at 20:21 +0200, Jochem Maas wrote:
> Robert Cummings schreef:
> > On Tue, 2008-07-15 at 12:37 -0400, tedd wrote:
> >> At 12:31 PM -0400 7/15/08, Robert Cummings wrote:
> >>> Umm... here it is to unlimited precision: ¼
> >>>
> >>> Cheers,
> >>> Rob.
> >> Yeah and here's ƒ
> >>
> >> Like or not, that's all there is to it.
> > 
> > Weird... you're client bastar-dized my beautiful pi symbol.
> 
> no it's the worst round error ever. how exactly do you go
> from 3.1 to .25 :-P

Ask Jay, he's the skimming master!  >:)

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Math Weirdness

2008-07-15 Thread Robert Cummings
On Tue, 2008-07-15 at 20:17 +0200, Jochem Maas wrote:
> Robin Vickery schreef:
> > 2008/7/15 tedd <[EMAIL PROTECTED]>:
> >> I said:
> >>
> >> "Round-off errors normally don't enter into things unless your doing
> >> multiplication and division operations."
> >>
> >> And that is not "Bull" -- it's true. You can add and subtract all the
> >> floating point numbers (the one's we are talking about here) you want
> >> without any rounding errors whatsoever.
> > 
> > $ php -r 'echo 0.7 - 0.2 == 0.5 ? "true\n" : "false\n";'
> > false
> > 
> > The operation you do isn't important. You just can't represent certain
> > numbers exactly in binary, just as you can't represent 1/3 exactly in
> > decimal. They have to be rounded internally.
> 
> which makes php casting so much fun:
> 
> $ php -r 'echo (string)(0.7 - 0.2) == 0.5 ? "true\n" : "false\n";'
> true

Just make a simple function...



Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Math Weirdness

2008-07-15 Thread Jochem Maas

Robert Cummings schreef:

On Tue, 2008-07-15 at 12:37 -0400, tedd wrote:

At 12:31 PM -0400 7/15/08, Robert Cummings wrote:

Umm... here it is to unlimited precision: ¼

Cheers,
Rob.

Yeah and here's ƒ

Like or not, that's all there is to it.


Weird... you're client bastar-dized my beautiful pi symbol.


no it's the worst round error ever. how exactly do you go
from 3.1 to .25 :-P



Cheers,
Rob.



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



Re: [PHP] Math Weirdness

2008-07-15 Thread Jochem Maas

Robin Vickery schreef:

2008/7/15 tedd <[EMAIL PROTECTED]>:

I said:

"Round-off errors normally don't enter into things unless your doing
multiplication and division operations."

And that is not "Bull" -- it's true. You can add and subtract all the
floating point numbers (the one's we are talking about here) you want
without any rounding errors whatsoever.


$ php -r 'echo 0.7 - 0.2 == 0.5 ? "true\n" : "false\n";'
false

The operation you do isn't important. You just can't represent certain
numbers exactly in binary, just as you can't represent 1/3 exactly in
decimal. They have to be rounded internally.


which makes php casting so much fun:

$ php -r 'echo (string)(0.7 - 0.2) == 0.5 ? "true\n" : "false\n";'
true





-robin




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



Re: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-15 Thread Jochem Maas

tedd schreef:

At 4:45 PM -0400 7/14/08, Daniel Brown wrote:

That's as far OT as I'm going, but it's worth noting how
regulation doesn't do shit if it's not enforced.


The only thing that's regulated AND enforced is what you owe the 
government.


actually your under no obligation to declare any income tax in your
country, look into it




Don't get me started.  :-)

Cheers,

tedd




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



RE: [PHP] Math Weirdness

2008-07-15 Thread Robert Cummings
On Tue, 2008-07-15 at 12:37 -0400, tedd wrote:
> At 12:31 PM -0400 7/15/08, Robert Cummings wrote:
> >
> >Umm... here it is to unlimited precision: ¼
> >
> >Cheers,
> >Rob.
> 
> Yeah and here's ƒ
> 
> Like or not, that's all there is to it.

Weird... you're client bastar-dized my beautiful pi symbol.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness

2008-07-15 Thread tedd

At 12:31 PM -0400 7/15/08, Robert Cummings wrote:


Umm... here it is to unlimited precision: ¼

Cheers,
Rob.


Yeah and here's ƒ

Like or not, that's all there is to it.

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

2008-07-15 Thread Robert Cummings
On Tue, 2008-07-15 at 12:26 -0400, tedd wrote:
> At 4:52 PM +0100 7/15/08, Ford, Mike wrote:
> >On 15 July 2008 14:33, tedd advised:
> >  > Mike:
> >>
> >>  No reason to be rude.
> >>
> >>  I said:
> >>
> >>  "Round-off errors normally don't enter into things unless your doing
> >>  multiplication and division operations."
> >>
> >>  And that is not "Bull" -- it's true. You can add and subtract all the
> >>  floating point numbers (the one's we are talking about here) you want
> >>  without any rounding errors whatsoever.
> >
> >Sorry, I do apologise if I came over too strongly -- there was no
> >intention to offend.
> >
> >However, you really can't dismiss the effects of round-off errors on
> >addition and subtraction as lightly as that.  It's simply not true that
> >approximations only occur at the point of doing multiplication and
> >division -- there *are* approximations involved in addition and
> >subtraction, and it is necessary to be aware that this is the case -- as
> >Jay proved, 0.1+0.2 is hardly ever exactly 0.3. In this sort of case, it
> >may well be that an appropriate degree of suspicion is simply to round
> >to 2 decimal places at every stage, or compare the absolute difference
> >to .001, but nonetheless one has to *know* that this is necessary.
> >
> >Ummm -- sorry, better , now!!!
> 
> No problem about the rudeness -- email is a terrible form of 
> communication. Sometimes we don't realize how we are being perceived.
> 
> I know full well about what you speak and why there are problems 
> dealing with numbers.
> 
> My only point here was that the OP was talking about his 
> balance-sheet not balancing at the end of the day.
> 
> I said that if all he was doing was adding and subtracting, then he 
> wouldn't have any problems -- those operations are not subject to the 
> rounding errors that division and multiplication induce. And 
> experience has shown me that my claim is true. I can add and subtract 
> dollars and cents all day without an error whatsoever.
> 
> Now, if you get into more complicated math, such as 
> multiplication/division then of course rounding errors come into play 
> much more noticeably. It is true that not every number can be 
> represented in binary because of the limits of the processor. Take 
> for example pi, no computer in the world has capabilities to 
> represent that number in it's totality (i.e.,  unlimited precision).

Umm... here it is to unlimited precision: π

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness

2008-07-15 Thread tedd

At 4:52 PM +0100 7/15/08, Ford, Mike wrote:

On 15 July 2008 14:33, tedd advised:
 > Mike:


 No reason to be rude.

 I said:

 "Round-off errors normally don't enter into things unless your doing
 multiplication and division operations."

 And that is not "Bull" -- it's true. You can add and subtract all the
 floating point numbers (the one's we are talking about here) you want
 without any rounding errors whatsoever.


Sorry, I do apologise if I came over too strongly -- there was no
intention to offend.

However, you really can't dismiss the effects of round-off errors on
addition and subtraction as lightly as that.  It's simply not true that
approximations only occur at the point of doing multiplication and
division -- there *are* approximations involved in addition and
subtraction, and it is necessary to be aware that this is the case -- as
Jay proved, 0.1+0.2 is hardly ever exactly 0.3. In this sort of case, it
may well be that an appropriate degree of suspicion is simply to round
to 2 decimal places at every stage, or compare the absolute difference
to .001, but nonetheless one has to *know* that this is necessary.

Ummm -- sorry, better , now!!!


No problem about the rudeness -- email is a terrible form of 
communication. Sometimes we don't realize how we are being perceived.


I know full well about what you speak and why there are problems 
dealing with numbers.


My only point here was that the OP was talking about his 
balance-sheet not balancing at the end of the day.


I said that if all he was doing was adding and subtracting, then he 
wouldn't have any problems -- those operations are not subject to the 
rounding errors that division and multiplication induce. And 
experience has shown me that my claim is true. I can add and subtract 
dollars and cents all day without an error whatsoever.


Now, if you get into more complicated math, such as 
multiplication/division then of course rounding errors come into play 
much more noticeably. It is true that not every number can be 
represented in binary because of the limits of the processor. Take 
for example pi, no computer in the world has capabilities to 
represent that number in it's totality (i.e.,  unlimited precision). 
However, if one defines the precision, then that's a different 
matter. As with the OP's problem, the precision he was dealing with 
was simply 0.00 and addition and subtraction operations don't come 
into play at the precision level (i.e., 0.10 + 0.20 will equal 0.30 
no matter how many times you add it).


In addition, the algorithm that used to round the number also comes 
into play. For example php always round down and that introduces 
bias, as proved here:


http://webbytedd.com/bbb/rounding/

So, all in all, I agree with your statements, but I just don't think 
you fully appreciated what I was trying to communicate to the OP.


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

2008-07-15 Thread Jay Blanchard
[snip]
So what you're saying is that you've been skimming for a while and the
fruits of your mislabour are adding up?
[/snip]

Not as far as you know. ;) 

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



RE: [PHP] Math Weirdness

2008-07-15 Thread Robert Cummings
On Tue, 2008-07-15 at 10:16 -0500, Jay Blanchard wrote:
> [snip]
> for most of what the majority of php/script apps deal with, it's
> probably
> safe to assume that addition/subtraction will be safe, but i can think
> of
> numerous examples where this isn't true...
> [/snip]
> 
> As demonstrated by the example I provided, just checking the difference
> (or lack thereof) between two floats brought the issue to light.
> 
> I think that this falls into the same category that cleaning input data
> does in the fact that it should always be done. While there is a joke
> about "skimming" the diffs it just demonstrates that the mathematical
> constructs we use based on a digital platform leaves room for
> error...and the errors add up really quickly because the platforms are
> so darn much quicker than we are.

So what you're saying is that you've been skimming for a while and the
fruits of your mislabour are adding up?

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness

2008-07-15 Thread Robert Cummings
On Tue, 2008-07-15 at 11:32 +0100, Ford, Mike wrote:
> On 14 July 2008 21:44, Jay Blanchard advised:
> 
> > [snip]
> > So does that mean your problem is solved?
> > [/snip]
> > 
> > It would appear so. I just need to remember to check for absolute
> values
> > when working with floats.
> 
> Yeah, that's Computer Science 101 stuff.  (Well, it was when I did my
> degree 30+ years ago, so it's probably nearer primary school level by
> now ;)

You didn't get the alumni memo did you? Supposedly they teach less now
than 30 years ago ;) Well... less theory I've heard.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness

2008-07-15 Thread Ford, Mike
On 15 July 2008 16:07, bruce advised:

> anyone remember the intel debacle in the 1990's.. when they
> ate a couple
> hundred million when they had a math err in one of their coprocessors!

Oh, yes!

And then, back in the day, there was the DEC arithmetic processor which
hung if you happened to divide the most negative possible integer by
zero (instead of returning a divide-by-zero error as it did for any
other dividend!).

Cheers!

Mike

 --
Mike Ford,  Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730



To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



RE: [PHP] Math Weirdness

2008-07-15 Thread Ford, Mike
On 15 July 2008 14:33, tedd advised:

> At 11:24 AM +0100 7/15/08, Ford, Mike wrote:
>> On 14 July 2008 20:47, tedd advised:
>> 
>>  > Round-off errors normally don't enter into things unless your
doing
>>  > multiplication and division operations. At that point,
> what you get
>>>  back from the operation is an approximation and not the actual
>>>  number.
>> 
>> Bull! Nearly all computer floating point numbers are approximations
>> because of being held in binary rather than decimal. Any number with
a
>> (decimal) fractional part that doesn't end with the digit 5 is
>> necessarily an approximation, and that's only half the story. So as
soon
>> as you involve numbers like 0.1 or 0.2, you've already got minor
>> inaccuracies which will propagate through any kind of arithmetic --
it's
>> just worse with multiplication or division because these tend to
result
>> in inaccuracy in more significant digits!
>> 
>> Bottom line: a floating point value should *always* be treated with
an
>> appropriate degree of suspicion.
>> 
>> Cheers!
>> 
>> Mike
> 
> Mike:
> 
> No reason to be rude.
> 
> I said:
> 
> "Round-off errors normally don't enter into things unless your doing
> multiplication and division operations."
> 
> And that is not "Bull" -- it's true. You can add and subtract all the
> floating point numbers (the one's we are talking about here) you want
> without any rounding errors whatsoever.

Sorry, I do apologise if I came over too strongly -- there was no
intention to offend.

However, you really can't dismiss the effects of round-off errors on
addition and subtraction as lightly as that.  It's simply not true that
approximations only occur at the point of doing multiplication and
division -- there *are* approximations involved in addition and
subtraction, and it is necessary to be aware that this is the case -- as
Jay proved, 0.1+0.2 is hardly ever exactly 0.3. In this sort of case, it
may well be that an appropriate degree of suspicion is simply to round
to 2 decimal places at every stage, or compare the absolute difference
to .001, but nonetheless one has to *know* that this is necessary.

Ummm -- sorry, better , now!!!

Cheers!

Mike

 --
Mike Ford,  Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] Math Weirdness

2008-07-15 Thread Daniel Brown
On Tue, Jul 15, 2008 at 11:16 AM, Jay Blanchard <[EMAIL PROTECTED]> wrote:
>
> the errors add up really quickly because the platforms are
> so darn much quicker than we are.

All I need to prove you wrong is an abacus and an old man with Parkinson's.





;-P

-- 

Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

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



RE: [PHP] Math Weirdness

2008-07-15 Thread Jay Blanchard
[snip]
for most of what the majority of php/script apps deal with, it's
probably
safe to assume that addition/subtraction will be safe, but i can think
of
numerous examples where this isn't true...
[/snip]

As demonstrated by the example I provided, just checking the difference
(or lack thereof) between two floats brought the issue to light.

I think that this falls into the same category that cleaning input data
does in the fact that it should always be done. While there is a joke
about "skimming" the diffs it just demonstrates that the mathematical
constructs we use based on a digital platform leaves room for
error...and the errors add up really quickly because the platforms are
so darn much quicker than we are.

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



RE: [PHP] Math Weirdness

2008-07-15 Thread bruce
hi..


in all seriousness, when you talk about mathmetical operations, you need to
really clearly define what the parameters of the conversation are. are you
just restricting the conversation to ints, or floats as well.. you should
also restrict the range of the number you're discussing.

for most of what the majority of php/script apps deal with, it's probably
safe to assume that addition/subtraction will be safe, but i can think of
numerous examples where this isn't true...

the internal representation of a number/value is really complex when you get
into the guts of the cpu/fpu... and it can be hairy as hell to get it right,
depending on what you're doing... that said, the vast majority of php will
never think about this, and the world will still keep spinning!!

anyone remember the intel debacle in the 1990's.. when they ate a couple
hundred million when they had a math err in one of their coprocessors!

peace..


-Original Message-
From: tedd [mailto:[EMAIL PROTECTED]
Sent: Tuesday, July 15, 2008 6:33 AM
To: php php
Subject: RE: [PHP] Math Weirdness


At 11:24 AM +0100 7/15/08, Ford, Mike wrote:
>On 14 July 2008 20:47, tedd advised:
>
>  > Round-off errors normally don't enter into things unless your doing
>  > multiplication and division operations. At that point, what you get
>>  back from the operation is an approximation and not the actual
>>  number.
>
>Bull! Nearly all computer floating point numbers are approximations
>because of being held in binary rather than decimal. Any number with a
>(decimal) fractional part that doesn't end with the digit 5 is
>necessarily an approximation, and that's only half the story. So as soon
>as you involve numbers like 0.1 or 0.2, you've already got minor
>inaccuracies which will propagate through any kind of arithmetic -- it's
>just worse with multiplication or division because these tend to result
>in inaccuracy in more significant digits!
>
>Bottom line: a floating point value should *always* be treated with an
>appropriate degree of suspicion.
>
>Cheers!
>
>Mike

Mike:

No reason to be rude.

I said:

"Round-off errors normally don't enter into things unless your doing
multiplication and division operations."

And that is not "Bull" -- it's true. You can add and subtract all the
floating point numbers (the one's we are talking about here) you want
without any rounding errors whatsoever.

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 General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Math Weirdness

2008-07-15 Thread Robin Vickery
2008/7/15 tedd <[EMAIL PROTECTED]>:
>
> I said:
>
> "Round-off errors normally don't enter into things unless your doing
> multiplication and division operations."
>
> And that is not "Bull" -- it's true. You can add and subtract all the
> floating point numbers (the one's we are talking about here) you want
> without any rounding errors whatsoever.

$ php -r 'echo 0.7 - 0.2 == 0.5 ? "true\n" : "false\n";'
false

The operation you do isn't important. You just can't represent certain
numbers exactly in binary, just as you can't represent 1/3 exactly in
decimal. They have to be rounded internally.

-robin

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



RE: [PHP] Math Weirdness

2008-07-15 Thread tedd

At 11:24 AM +0100 7/15/08, Ford, Mike wrote:

On 14 July 2008 20:47, tedd advised:

 > Round-off errors normally don't enter into things unless your doing
 > multiplication and division operations. At that point, what you get

 back from the operation is an approximation and not the actual
 number.


Bull! Nearly all computer floating point numbers are approximations
because of being held in binary rather than decimal. Any number with a
(decimal) fractional part that doesn't end with the digit 5 is
necessarily an approximation, and that's only half the story. So as soon
as you involve numbers like 0.1 or 0.2, you've already got minor
inaccuracies which will propagate through any kind of arithmetic -- it's
just worse with multiplication or division because these tend to result
in inaccuracy in more significant digits!

Bottom line: a floating point value should *always* be treated with an
appropriate degree of suspicion.

Cheers!

Mike


Mike:

No reason to be rude.

I said:

"Round-off errors normally don't enter into things unless your doing 
multiplication and division operations."


And that is not "Bull" -- it's true. You can add and subtract all the 
floating point numbers (the one's we are talking about here) you want 
without any rounding errors whatsoever.


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

2008-07-15 Thread Jay Blanchard
[snip]
> It would appear so. I just need to remember to check for absolute
values
> when working with floats.

Yeah, that's Computer Science 101 stuff.  (Well, it was when I did my
degree 30+ years ago, so it's probably nearer primary school level by
now ;)
[/snip]

This is what happens when you spend a lot of time managing projects and
then have a need to step in and write some quick little application. The
rust has frozen all of the things you should remember to do! :)

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



RE: [PHP] Math Weirdness

2008-07-15 Thread Ford, Mike
On 14 July 2008 21:44, Jay Blanchard advised:

> [snip]
> So does that mean your problem is solved?
> [/snip]
> 
> It would appear so. I just need to remember to check for absolute
values
> when working with floats.

Yeah, that's Computer Science 101 stuff.  (Well, it was when I did my
degree 30+ years ago, so it's probably nearer primary school level by
now ;)

Cheers!

Mike

 --
Mike Ford,  Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



RE: [PHP] Math Weirdness

2008-07-15 Thread Ford, Mike
On 14 July 2008 20:47, tedd advised:

> Round-off errors normally don't enter into things unless your doing
> multiplication and division operations. At that point, what you get
> back from the operation is an approximation and not the actual
> number.

Bull! Nearly all computer floating point numbers are approximations
because of being held in binary rather than decimal. Any number with a
(decimal) fractional part that doesn't end with the digit 5 is
necessarily an approximation, and that's only half the story. So as soon
as you involve numbers like 0.1 or 0.2, you've already got minor
inaccuracies which will propagate through any kind of arithmetic -- it's
just worse with multiplication or division because these tend to result
in inaccuracy in more significant digits!

Bottom line: a floating point value should *always* be treated with an
appropriate degree of suspicion.

Cheers!

Mike

 --
Mike Ford,  Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-15 Thread Waynn Lue
There's an algorithm known as banker's rounding that I thought all US
banks used. I'm on blackberry right now so I don't have a pointer, but
essentially you round to the nearest even cent. 1.5 goes to 2, as does
2.5.

Waynn



On 7/15/08, tedd <[EMAIL PROTECTED]> wrote:
> At 4:15 PM -0400 7/14/08, Robert Cummings wrote:
>>
>>
>>Nope, banks can't round like that when calculating your daily
>>interest :)
>>
>>Cheers,
>>Rob.
>
> I do know that when it comes to interest you pay them, they round up.
> When it comes to interest they pay you, they round down -- and why
> not? A decision has to be made -- you can't pay someone less than a
> penny.
>
> 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 General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-14 Thread tedd

At 4:45 PM -0400 7/14/08, Daniel Brown wrote:

That's as far OT as I'm going, but it's worth noting how
regulation doesn't do shit if it's not enforced.


The only thing that's regulated AND enforced is what you owe the government.

Don't get me started.  :-)

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] Math Weirdness - Can it be done with integers alone??

2008-07-14 Thread tedd

At 4:15 PM -0400 7/14/08, Robert Cummings wrote:



Nope, banks can't round like that when calculating your daily
interest :)

Cheers,
Rob.


I do know that when it comes to interest you pay them, they round up. 
When it comes to interest they pay you, they round down -- and why 
not? A decision has to be made -- you can't pay someone less than a 
penny.


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

2008-07-14 Thread Jay Blanchard
[snip]
Yeah, I prefer to work with pennies too when doing that stuff. But
floats work much better when skimming... harder to track ;) Remind me
not to buy weed from Jay.
[/snip]

Hehehehehehe ;) 

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



Re: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-14 Thread Daniel Brown
On Mon, Jul 14, 2008 at 4:28 PM, Robert Cummings <[EMAIL PROTECTED]> wrote:
>
> I'm not completely sure about the U.S. but banks in Canada are
> regulated... fortunately :)

They are here, too, to a certain extent.  The problem is, they can
create their own rules that cost the customer a fortune, and still
somehow complain that they need to have rates cut, raised, or
otherwise modified so that they can make more of a profit.

I have to empathize with them, though.  I don't know how I'd
survive if I was someone like former Wachovia CEO Ken Thompson, who
only got a $7 Million bonus in 2004 (in addition to $11.1 Million in
salary, stock, et cetera).  And speaking of Wachovia, using them by
example, the audacity isn't even masked:

"Shares of Wachovia dropped as much as 11 percent to their lowest
level since 1991 after the bank said it might lose as much as $2.8
billion in the second quarter. It declined to offer specifics on how
it plans to reduce balance sheet risk or whether it needs more capital
or might again cut the dividend.

"Wachovia said it approached [Robert] Steel for the [position of
CEO], offering him a package that could total $38.1 million in salary,
bonus and other awards in his first year."


I'm no genius (despite what MENSA may say  ;-P), but if I was so
horribly worried about how to begin cutting expenses, the first thing
I'd do is STOP RAISING @$#%ING SALARIES.  If I even got a cost of
living increase, I'd be happy but if I got an exorbitant cost of
living increase in the millions, you wouldn't hear me complain.

And I'm sure many of you remember when Exxon Mobil CEO Lee Raymond
retired in December of 2006.  Despite rising gas and oil prices, he
"was compensated more than $686 million from 1993 to 2005, according
to an analysis done for the New York Times by Brian Foley, an
independent compensation consultant. That is $144,573 for each day he
spent leading Exxon's "God pod," as the executive suite at the
company's headquarters in Irving, Texas, is known.

"Despite the company's performance, some shareholders, academics,
corporate governance experts and consumer groups were taken aback when
they learned for the first time this week the details of Raymond's
total compensation package, including the more than $400 million he
received in his final year at the company."


That's as far OT as I'm going, but it's worth noting how
regulation doesn't do shit if it's not enforced.

-- 

Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

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



RE: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-14 Thread Alex Chamberlain
> On Mon, 2008-07-14 at 16:25 -0400, Daniel Brown wrote:
> > On Mon, Jul 14, 2008 at 4:15 PM, Robert Cummings
> <[EMAIL PROTECTED]> wrote:
> > >
> > > Nope, banks can't round like that when calculating your daily
> > > interest :)
> >
> > If it works in their favor, you'd be surprised what they do.
> > That's why extending TILA to banks in the US is such a big deal to
> me,
> > yet gets no attention.
> 
> I'm not completely sure about the U.S. but banks in Canada are
> regulated... fortunately :)
> 
> Cheers,
> Rob.

Ok, I may have been wrong about the banks thing - don't quote me on it!!
However, there must be some degree of accuracy?? Which can then be reflected
in your integers.

Alex

No virus found in this outgoing message. Scanned by AVG Free 8.0
Checked by AVG - http://www.avg.com 
Version: 8.0.138 / Virus Database: 270.4.10/1550 - Release Date: 13/07/2008
17:58


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



RE: [PHP] Math Weirdness

2008-07-14 Thread Jay Blanchard
[snip]
So does that mean your problem is solved?
[/snip]

It would appear so. I just need to remember to check for absolute values
when working with floats. 

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



Re: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-14 Thread Robert Cummings
On Mon, 2008-07-14 at 16:25 -0400, Daniel Brown wrote:
> On Mon, Jul 14, 2008 at 4:15 PM, Robert Cummings <[EMAIL PROTECTED]> wrote:
> >
> > Nope, banks can't round like that when calculating your daily
> > interest :)
> 
> If it works in their favor, you'd be surprised what they do.
> That's why extending TILA to banks in the US is such a big deal to me,
> yet gets no attention.

I'm not completely sure about the U.S. but banks in Canada are
regulated... fortunately :)

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-14 Thread Daniel Brown
On Mon, Jul 14, 2008 at 4:15 PM, Robert Cummings <[EMAIL PROTECTED]> wrote:
>
> Nope, banks can't round like that when calculating your daily
> interest :)

If it works in their favor, you'd be surprised what they do.
That's why extending TILA to banks in the US is such a big deal to me,
yet gets no attention.

-- 

Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

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



RE: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-14 Thread Robert Cummings
On Mon, 2008-07-14 at 21:09 +0100, Alex Chamberlain wrote:
> [quote]
> I don't quite understand your problem, but I use integers for any monetary
> workings as you can guarantee it is accurate (obviously, you work in pence
> or cents rather than GBP or USD).
> 
>  
> 
> Alex
>   
> 
> Hello Alex,
> 
> I was reading through this thread, and I was curious about what methods you
> use to handle fractions of a dollar and/or fractions of a penny if you are
> always using integers.  Do you only use a decimal for printing?  do you
> adjust all interest rates, etc...then as well?  How about when interest
> calculations result in fractions of pennies, how do you handle it then? 
> Basically, I can't see how it could be done with just integers alone?  
> 
> thanks
> 
> dank
> [/quote]
> 
> To be honest, I haven't used it for any complicated use - shopping carts are
> additive contraptions. However, I don't see the problem.
> 
> 1) I use Smarty a lot - I use a plugin to format the integer to a price (ie
> divide by 100 and display in 2 decimal places). So yes, I only put the
> decimal place there to print.
> 
> 2) Why do interest rates need to be changed?? Take £123.45 @ 5%. 123.45 *
> 1.05 = (12345 [pennies] / 100) [pounds] * 1.05 = (12345 * 1.05) / 100
>Same calculation in my mind!! (Tell me if I have missed the point!!)
> 
> 3) If you have a fraction of a penny - round it!! Surely, that's what the
> banks do!! It's a penny!!

Nope, banks can't round like that when calculating your daily
interest :)

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Math Weirdness

2008-07-14 Thread Robert Cummings
On Mon, 2008-07-14 at 15:00 -0500, Daniel Kolbo wrote:
> >
> > I don't quite understand your problem, but I use integers for any monetary
> > workings as you can guarantee it is accurate (obviously, you work in pence
> > or cents rather than GBP or USD).
> >
> >  
> >
> > Alex
> >   
> 
> Hello Alex,
> 
> I was reading through this thread, and I was curious about what methods 
> you use to handle fractions of a dollar and/or fractions of a penny if 
> you are always using integers.  Do you only use a decimal for printing?  
> do you adjust all interest rates, etc...then as well?  How about when 
> interest calculations result in fractions of pennies, how do you handle 
> it then?  Basically, I can't see how it could be done with just integers 
> alone?

Depends on what you're working with. I don't think banks can get away
with using pennies. But a storefront can usually do so since they don't
charge fractions of pennies and don't offer interest :)

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness - Can it be done with integers alone??

2008-07-14 Thread Alex Chamberlain
[quote]
I don't quite understand your problem, but I use integers for any monetary
workings as you can guarantee it is accurate (obviously, you work in pence
or cents rather than GBP or USD).

 

Alex
  

Hello Alex,

I was reading through this thread, and I was curious about what methods you
use to handle fractions of a dollar and/or fractions of a penny if you are
always using integers.  Do you only use a decimal for printing?  do you
adjust all interest rates, etc...then as well?  How about when interest
calculations result in fractions of pennies, how do you handle it then? 
Basically, I can't see how it could be done with just integers alone?  

thanks

dank
[/quote]

To be honest, I haven't used it for any complicated use - shopping carts are
additive contraptions. However, I don't see the problem.

1) I use Smarty a lot - I use a plugin to format the integer to a price (ie
divide by 100 and display in 2 decimal places). So yes, I only put the
decimal place there to print.

2) Why do interest rates need to be changed?? Take £123.45 @ 5%. 123.45 *
1.05 = (12345 [pennies] / 100) [pounds] * 1.05 = (12345 * 1.05) / 100
   Same calculation in my mind!! (Tell me if I have missed the point!!)

3) If you have a fraction of a penny - round it!! Surely, that's what the
banks do!! It's a penny!!

I think it can be done with integers alone - let me know if you disagree!!

Alex

No virus found in this outgoing message. Scanned by AVG Free 8.0
Checked by AVG - http://www.avg.com 
Version: 8.0.138 / Virus Database: 270.4.10/1550 - Release Date: 13/07/2008
17:58


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



Re: [PHP] Math Weirdness

2008-07-14 Thread Daniel Brown
On Mon, Jul 14, 2008 at 4:00 PM, Alex Chamberlain
<[EMAIL PROTECTED]> wrote:
>
> Surely, for difference,
>  $totChargeDiff = abs($endingBal - $totalCharges);

Yes, in live code.  I gave my example to illustrate my point, not
to be used in code.  I believe Jay would understand that.

-- 

Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

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



Re: [PHP] Math Weirdness

2008-07-14 Thread Daniel Kolbo




I don't quite understand your problem, but I use integers for any monetary
workings as you can guarantee it is accurate (obviously, you work in pence
or cents rather than GBP or USD).

 


Alex
  


Hello Alex,

I was reading through this thread, and I was curious about what methods 
you use to handle fractions of a dollar and/or fractions of a penny if 
you are always using integers.  Do you only use a decimal for printing?  
do you adjust all interest rates, etc...then as well?  How about when 
interest calculations result in fractions of pennies, how do you handle 
it then?  Basically, I can't see how it could be done with just integers 
alone? 


thanks

dank


RE: [PHP] Math Weirdness

2008-07-14 Thread Alex Chamberlain
> On Mon, Jul 14, 2008 at 3:51 PM, Daniel Brown <[EMAIL PROTECTED]>
> wrote:
> >
> >You're performing math to get the result for $totChargeDiff by
> > subtracting $totalCharges from $endingBal.  That will give you -0.10
> > in this case, because 0.10 - 0.20 = -0.10.  You want the difference,
> > not the result.
> 
> Oh, and the code, by the way:
> 
>  //  your code to this point
> 
> /* what is the difference between the ending balance and the charges?
> */
> $totChargeDiff = ($totalCharges > $endingBal) ?
> ($endingBal + $totalCharges) : ($endingBal - $totalCharges);
> echo number_format($totChargeDiff, 2, '.', '')."\t";
> 
> //  and continue with your code here
> ?>

Surely, for difference,
 $totChargeDiff = abs($endingBal - $totalCharges);

Alex

No virus found in this outgoing message. Scanned by AVG Free 8.0
Checked by AVG - http://www.avg.com 
Version: 8.0.138 / Virus Database: 270.4.10/1550 - Release Date: 13/07/2008
17:58


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



Re: [PHP] Math Weirdness

2008-07-14 Thread Daniel Brown
On Mon, Jul 14, 2008 at 3:54 PM, Daniel Brown <[EMAIL PROTECTED]> wrote:
>
>Oh, and the code, by the way:
>
>  //  your code to this point
>
> /* what is the difference between the ending balance and the charges? */
>$totChargeDiff = ($totalCharges > $endingBal) ?
> ($endingBal + $totalCharges) : ($endingBal - $totalCharges);
>echo number_format($totChargeDiff, 2, '.', '')."\t";
>
> //  and continue with your code here
> ?>

Sorry, I screwed up.  Because in that example, if $endingBal was
negative, it wouldn't work correctly.  Update it to this:

 $endingBal) && $endingBal >= 0 ?
($endingBal + $totalCharges) : ($endingBal - $totalCharges);
?>

-- 

Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

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



Re: [PHP] Math Weirdness

2008-07-14 Thread Daniel Brown
On Mon, Jul 14, 2008 at 3:51 PM, Daniel Brown <[EMAIL PROTECTED]> wrote:
>
>You're performing math to get the result for $totChargeDiff by
> subtracting $totalCharges from $endingBal.  That will give you -0.10
> in this case, because 0.10 - 0.20 = -0.10.  You want the difference,
> not the result.

Oh, and the code, by the way:

 $endingBal) ?
($endingBal + $totalCharges) : ($endingBal - $totalCharges);
echo number_format($totChargeDiff, 2, '.', '')."\t";

//  and continue with your code here
?>


-- 

Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

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



Re: [PHP] Math Weirdness

2008-07-14 Thread Daniel Brown
On Mon, Jul 14, 2008 at 1:39 PM, Jay Blanchard <[EMAIL PROTECTED]> wrote:
> I am totally buffaloed by a set of very simple calculations that I am
> doing;
>
>$totChargeDiff = $endingBal - $totalCharges;
>echo number_format($totChargeDiff, 2, '.', '')."\t";

You're performing math to get the result for $totChargeDiff by
subtracting $totalCharges from $endingBal.  That will give you -0.10
in this case, because 0.10 - 0.20 = -0.10.  You want the difference,
not the result.

-- 

Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

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



Re: [PHP] Math Weirdness

2008-07-14 Thread tedd

At 12:39 PM -0500 7/14/08, Jay Blanchard wrote:

Each number represented by a variable in all of these calculations has
been rounded to 2 decimal points at the point they are generated. For
the most part this works just hunky-dory but I have a handful of calcs
(out of 300k plus records) that look like this

$endingBal  0.10
$totalBalance   0.30
$totalCharges   0.20
$totalChargeDiff-0.10

The balance minus the charges does equal the ending balance as it should
but it is saying that it doesn't and that there a 20 cent swing (-0.10
is 20 cents different than 0.10).

I must be missing something. When I echo out raw data I do not see
negative signs. Does anyone have any insight as to what might be
happening here?


Sounds like rounding errors.

Please note that the php rounding function (like all rounding 
functions) is not accurate. You can write one that is more accurate 
-- like I did -- but, to see any real difference you have to be 
dealing with millions of calculations.


Round-off errors normally don't enter into things unless your doing 
multiplication and division operations. At that point, what you get 
back from the operation is an approximation and not the actual 
number. The difference between the sums of all the approximations the 
the sums of all the calculations you do manually IS going to be 
different. The difference depends upon how accurate the machines' 
operations are and in what order the calculations are performed, 
which is probably the reason for the differences you see here using 
php to do both, but in different order.


My $0.019995

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

2008-07-14 Thread Robert Cummings
On Mon, 2008-07-14 at 14:29 -0500, Jay Blanchard wrote:
> [snip]
> Isn't it possible then that your data is out of synch between the
> current balance and the previous day's ending balance?
> [/snip]
> 
> No, the values are there and correct (it is done with a snapshot). The
> correct numbers are being used in the math (as shown). When I do
> this(as you suggested);
> 
> if(abs($totBalDiff - $endingBal) < .01){
>   echo "OK\t";
>   } else {
>   /* what is the difference between the ending balance and
> the charges? */
>   $totChargeDiff = $endingBal - $totalCharges;
>   echo number_format($totChargeDiff, 2, '.', '')."\t";
>   }
> 
> You can see the abs value difference is extremely small and it causes
> things to work as expected. 0.1 should be equal to 0.1

So does that mean your problem is solved?

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness

2008-07-14 Thread Robert Cummings
On Mon, 2008-07-14 at 15:33 -0400, Wolf wrote:
>  Robert Cummings <[EMAIL PROTECTED]> wrote: 
> > On Mon, 2008-07-14 at 14:12 -0500, Jay Blanchard wrote:
> > > [snip]
> > > So again... from whence do you conjure $endingBal? :)
> > > [/snip]
> > > 
> > > $endingBal is conjured from the database tracking the account balance.
> > > For any 24 hour period the beginning and ending balance for subsequent
> > > days is the same unless adjustments or payments have been made to the
> > > account in that period.
> > > 
> > > 2:35 AM balance becomes the ending balance for the previous day and
> > > becomes the beginning balance for the day we are beginning.
> > 
> > Isn't it possible then that your data is out of synch between the
> > current balance and the previous day's ending balance?
> > 
> > Cheers,
> > Rob. 
> 
> 
> See!!!  I told you he was skimming the profits!  Better check his desk for 
> the "other" set of books! ;)
> 
> I'd suggest the int approach.

Yeah, I prefer to work with pennies too when doing that stuff. But
floats work much better when skimming... harder to track ;) Remind me
not to buy weed from Jay.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness

2008-07-14 Thread Wolf
 Robert Cummings <[EMAIL PROTECTED]> wrote: 
> On Mon, 2008-07-14 at 14:12 -0500, Jay Blanchard wrote:
> > [snip]
> > So again... from whence do you conjure $endingBal? :)
> > [/snip]
> > 
> > $endingBal is conjured from the database tracking the account balance.
> > For any 24 hour period the beginning and ending balance for subsequent
> > days is the same unless adjustments or payments have been made to the
> > account in that period.
> > 
> > 2:35 AM balance becomes the ending balance for the previous day and
> > becomes the beginning balance for the day we are beginning.
> 
> Isn't it possible then that your data is out of synch between the
> current balance and the previous day's ending balance?
> 
> Cheers,
> Rob. 


See!!!  I told you he was skimming the profits!  Better check his desk for the 
"other" set of books! ;)

I'd suggest the int approach.

Wolf

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



RE: [PHP] Math Weirdness

2008-07-14 Thread Jay Blanchard
[snip]
Isn't it possible then that your data is out of synch between the
current balance and the previous day's ending balance?
[/snip]

No, the values are there and correct (it is done with a snapshot). The
correct numbers are being used in the math (as shown). When I do
this(as you suggested);

if(abs($totBalDiff - $endingBal) < .01){
echo "OK\t";
} else {
/* what is the difference between the ending balance and
the charges? */
$totChargeDiff = $endingBal - $totalCharges;
echo number_format($totChargeDiff, 2, '.', '')."\t";
}

You can see the abs value difference is extremely small and it causes
things to work as expected. 0.1 should be equal to 0.1

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



RE: [PHP] Math Weirdness

2008-07-14 Thread Robert Cummings
On Mon, 2008-07-14 at 14:12 -0500, Jay Blanchard wrote:
> [snip]
> So again... from whence do you conjure $endingBal? :)
> [/snip]
> 
> $endingBal is conjured from the database tracking the account balance.
> For any 24 hour period the beginning and ending balance for subsequent
> days is the same unless adjustments or payments have been made to the
> account in that period.
> 
> 2:35 AM balance becomes the ending balance for the previous day and
> becomes the beginning balance for the day we are beginning.

Isn't it possible then that your data is out of synch between the
current balance and the previous day's ending balance?

Cheers,
Rob. 
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness

2008-07-14 Thread Alex Chamberlain
 

 

> I am totally buffaloed by a set of very simple calculations that I am 

> doing;

> 

> /* calculate total balance with payments and adjustments */

> $totalBalance = $acct['BALANCE'] + $adjBalance;

> echo number_format($totalBalance, 2, '.', '')."\t";

> 

> /* calculate total charges */

> $totalCharges = $intlLDCharges + $longDistance + $smsCharges + 

> $daCharges + $totalData + $roaming;

> echo number_format($totalCharges, 2, '.', '')."\t";

> 

> /*

>  * calculate difference between total balance and total charges

>  * if the amount matches the ending balance then all is OK

>  * if not calculate the difference

>  */

> $totBalDiff = $totalBalance - $totalCharges;

> if($totBalDiff === $endingBal){

>   echo "OK\t";

> } else {

>   /* what is the difference between the ending balance and the 

> charges? */

>   $totChargeDiff = $endingBal - $totalCharges;

>   echo number_format($totChargeDiff, 2, '.', '')."\t";

> }

> 

> Each number represented by a variable in all of these calculations has 

> been rounded to 2 decimal points at the point they are generated. For 

> the most part this works just hunky-dory but I have a handful of calcs 

> (out of 300k plus records) that look like this

> 

> $endingBal0.10

> $totalBalance   0.30

> $totalCharges   0.20

> $totalChargeDiff  -0.10

> 

> The balance minus the charges does equal the ending balance as it 

> should but it is saying that it doesn't and that there a 20 cent swing 

> (-0.10 is 20 cents different than 0.10).

> 

> I must be missing something. When I echo out raw data I do not see 

> negative signs. Does anyone have any insight as to what might be 

> happening here?

 

I don't quite understand your problem, but I use integers for any monetary
workings as you can guarantee it is accurate (obviously, you work in pence
or cents rather than GBP or USD).

 

Alex



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



RE: [PHP] Math Weirdness

2008-07-14 Thread Jay Blanchard
[snip]
So again... from whence do you conjure $endingBal? :)
[/snip]

$endingBal is conjured from the database tracking the account balance.
For any 24 hour period the beginning and ending balance for subsequent
days is the same unless adjustments or payments have been made to the
account in that period.

2:35 AM balance becomes the ending balance for the previous day and
becomes the beginning balance for the day we are beginning. 

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



Re: [PHP] Math Weirdness

2008-07-14 Thread Robert Cummings
On Mon, 2008-07-14 at 12:39 -0500, Jay Blanchard wrote:
> I am totally buffaloed by a set of very simple calculations that I am
> doing;
> 
> /* calculate total balance with payments and adjustments */
>   $totalBalance = $acct['BALANCE'] + $adjBalance;
>   echo number_format($totalBalance, 2, '.', '')."\t";
>   
>   /* calculate total charges */
>   $totalCharges = $intlLDCharges + $longDistance + $smsCharges +
> $daCharges + $totalData + $roaming;
>   echo number_format($totalCharges, 2, '.', '')."\t";
>   
>   /* 
>* calculate difference between total balance and total charges
>* if the amount matches the ending balance then all is OK
>* if not calculate the difference
>*/  
>   $totBalDiff = $totalBalance - $totalCharges;
>   if($totBalDiff === $endingBal){
>   echo "OK\t";
>   } else {
>   /* what is the difference between the ending balance and
> the charges? */
>   $totChargeDiff = $endingBal - $totalCharges;
>   echo number_format($totChargeDiff, 2, '.', '')."\t";
>   }
> 
> Each number represented by a variable in all of these calculations has
> been rounded to 2 decimal points at the point they are generated. For
> the most part this works just hunky-dory but I have a handful of calcs
> (out of 300k plus records) that look like this
> 
> $endingBal0.10
> $totalBalance 0.30
> $totalCharges 0.20
> $totalChargeDiff  -0.10
> 
> The balance minus the charges does equal the ending balance as it should
> but it is saying that it doesn't and that there a 20 cent swing (-0.10
> is 20 cents different than 0.10).
> 
> I must be missing something. When I echo out raw data I do not see
> negative signs. Does anyone have any insight as to what might be
> happening here?   

>From whence do you conjure $endingBal?

Most likely this is just a floating point imprecision problem. In
otherwords, you need a small delta of error when doing the following
comparison:

if( $totBalDiff === $endingBal )

Remember, floating point numbers do not always store perfectly. So you
want something like:

if( abs( $totalBalDiff - $endingBal ) > .01 )

Also that last echo:

echo number_format($totChargeDiff, 2, '.', '')

Was calculated with:

$totChargeDiff = $endingBal - $totalCharges;

Whereas the initial conditional checks:

$totBalDiff === $endingBal

And $totBalDiff was calculated as:

$totBalDiff = $totalBalance - $totalCharges;

So again... from whence do you conjure $endingBal? :)

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Math Weirdness

2008-07-14 Thread Jay Blanchard
[snip]
What makes you think the problem is in this code? How about doing the
following right before the block of code you've provided so that we can
actually check the data with what you're using:


[/snip]

$adjBalance: 0
$intlLDCharges: 0.2
$longDistance: 0
$smsCharges: 0
$roaming: 0
$daCharges: 0
$totalData: 0
$endingBal: 0.1

I had done this looking for special characters or negative signs.

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



Re: [PHP] Math Weirdness

2008-07-14 Thread Robert Cummings
On Mon, 2008-07-14 at 12:39 -0500, Jay Blanchard wrote:
> /* calculate total balance with payments and adjustments */
> $totalBalance = $acct['BALANCE'] + $adjBalance;
> echo number_format($totalBalance, 2, '.', '')."\t";
> 
> /* calculate total charges */
> $totalCharges = $intlLDCharges + $longDistance + $smsCharges +
> $daCharges + $totalData + $roaming;
> echo number_format($totalCharges, 2, '.', '')."\t";
> 
> /* 
>  * calculate difference between total balance and total
> charges
>  * if the amount matches the ending balance then all is OK
>  * if not calculate the difference
>  */  
> $totBalDiff = $totalBalance - $totalCharges;
> if($totBalDiff === $endingBal){
> echo "OK\t";
> } else {
> /* what is the difference between the ending balance
> and
> the charges? */
> $totChargeDiff = $endingBal - $totalCharges;
> echo number_format($totChargeDiff, 2, '.', '')."\t";
> }

What makes you think the problem is in this code? How about doing the
following right before the block of code you've provided so that we can
actually check the data with what you're using:



Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



[PHP] Math Weirdness

2008-07-14 Thread Jay Blanchard
I am totally buffaloed by a set of very simple calculations that I am
doing;

/* calculate total balance with payments and adjustments */
$totalBalance = $acct['BALANCE'] + $adjBalance;
echo number_format($totalBalance, 2, '.', '')."\t";

/* calculate total charges */
$totalCharges = $intlLDCharges + $longDistance + $smsCharges +
$daCharges + $totalData + $roaming;
echo number_format($totalCharges, 2, '.', '')."\t";

/* 
 * calculate difference between total balance and total charges
 * if the amount matches the ending balance then all is OK
 * if not calculate the difference
 */  
$totBalDiff = $totalBalance - $totalCharges;
if($totBalDiff === $endingBal){
echo "OK\t";
} else {
/* what is the difference between the ending balance and
the charges? */
$totChargeDiff = $endingBal - $totalCharges;
echo number_format($totChargeDiff, 2, '.', '')."\t";
}

Each number represented by a variable in all of these calculations has
been rounded to 2 decimal points at the point they are generated. For
the most part this works just hunky-dory but I have a handful of calcs
(out of 300k plus records) that look like this

$endingBal  0.10
$totalBalance   0.30
$totalCharges   0.20
$totalChargeDiff-0.10

The balance minus the charges does equal the ending balance as it should
but it is saying that it doesn't and that there a 20 cent swing (-0.10
is 20 cents different than 0.10).

I must be missing something. When I echo out raw data I do not see
negative signs. Does anyone have any insight as to what might be
happening here? 

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



Re: [PHP] Math weirdness with doubles...

2004-02-27 Thread Marek Kilimajer
jon roig wrote:
Ok... It's Friday and maybe my brain is dead, but I'm having a weird
problem with some basic math.
Here's a little snippet of the code I'm working with:

---
echo "Current:$currentAmount:".gettype($currentAmount)." -
Paid:$paidAmount:".gettype($paidAmount)."";
$currentAmount = $currentAmount-$paidAmount;
echo "Final Current: $currentAmount";
-
Straightforward... Yeah?

Here's a sample of the output:
Current:97.6:double - Paid:97.6:double
Final Current: 1.42108547152E-014
Now if both $currentAmount and $paidAmount are doubles, subtracting them
should yield a zero, shouldn't it?
No, because they are doubles. If they were integers it would yield a 
zero. Try to compare the doubles and you find out they are not equal:

if($currentAmount == $paidAmount) echo "equal";
else echo "not equal";
You need to know how are floats and doubles represented. They are in 
fact something like 97.589 or 97.601, so you should never 
compare doubles and floats if they are equal, or you should round them 
before.

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


RE: [PHP] Math weirdness with doubles...

2004-02-27 Thread Daniel Clark
Cool.

There must be a way to display out the full doubles precision huh.



> Thank you so much for the help, folks. That turned out to be weird, but
> correct. Rounding off to 4 decimal point places solved the problem quite
> nicely.
>
>   -- jon
>
>
>
> -Original Message-
> From: Daniel Clark [mailto:[EMAIL PROTECTED]
> Sent: Friday, February 27, 2004 3:16 PM
> To: jon roig
> Cc: [EMAIL PROTECTED]
> Subject: Re: [PHP] Math weirdness with doubles...
>
>
> Looks like the Doubles are not exactly zero.  Must be some precision
> points not displaying.
>
>
>> Ok... It's Friday and maybe my brain is dead, but I'm having a weird
>> problem with some basic math.
>>
>> Here's a little snippet of the code I'm working with:
>>
>> ---
>> echo "Current:$currentAmount:".gettype($currentAmount)." -
>> Paid:$paidAmount:".gettype($paidAmount)."";
>>
>> $currentAmount = $currentAmount-$paidAmount;
>> echo "Final Current: $currentAmount";
>> -
>>
>> Straightforward... Yeah?
>>
>> Here's a sample of the output:
>> Current:97.6:double - Paid:97.6:double
>> Final Current: 1.42108547152E-014
>>
>> Now if both $currentAmount and $paidAmount are doubles, subtracting
>> them should yield a zero, shouldn't it?
>>
>>  -- jon
>>
>> ---
>> jon roig
>> web developer
>> email: [EMAIL PROTECTED]
>> phone: 888.230.7557
>>
>> ---
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.593 / Virus Database: 376 - Release Date: 2/20/2004
>
>
> --
> 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] Math weirdness with doubles...

2004-02-27 Thread jon roig
Thank you so much for the help, folks. That turned out to be weird, but
correct. Rounding off to 4 decimal point places solved the problem quite
nicely.

-- jon



-Original Message-
From: Daniel Clark [mailto:[EMAIL PROTECTED] 
Sent: Friday, February 27, 2004 3:16 PM
To: jon roig
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Math weirdness with doubles...


Looks like the Doubles are not exactly zero.  Must be some precision
points not displaying.


> Ok... It's Friday and maybe my brain is dead, but I'm having a weird 
> problem with some basic math.
>
> Here's a little snippet of the code I'm working with:
>
> ---
> echo "Current:$currentAmount:".gettype($currentAmount)." - 
> Paid:$paidAmount:".gettype($paidAmount)."";
>
> $currentAmount = $currentAmount-$paidAmount;
> echo "Final Current: $currentAmount";
> -
>
> Straightforward... Yeah?
>
> Here's a sample of the output:
> Current:97.6:double - Paid:97.6:double
> Final Current: 1.42108547152E-014
>
> Now if both $currentAmount and $paidAmount are doubles, subtracting 
> them should yield a zero, shouldn't it?
>
>   -- jon
>
> ---
> jon roig
> web developer
> email: [EMAIL PROTECTED]
> phone: 888.230.7557
>
> ---


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.593 / Virus Database: 376 - Release Date: 2/20/2004
 

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



Re: [PHP] Math weirdness with doubles...

2004-02-27 Thread D. Wokan
They are not the same amount.  They each got a different rounding error. 
 Double values only display a small number of decimal places 
(relatively speaking).  If you look at the difference you're getting, 
it's 0.0142108547152 (I may be off by a zero or two), so 
given you're working with a currency, I'd say you can safely round to 
about 4 or 8 decimal places to get a reasonably accurate result.  I 
typically use 4 since most money types I've seen only go out 4 decimal 
places to begin with.
--
D. Wokan
(Sorry for the double reply, Jon.  Didn't realise this list doesn't set 
the reply-to until it was too late.)

jon roig wrote:

Ok... It's Friday and maybe my brain is dead, but I'm having a weird
problem with some basic math.
Here's a little snippet of the code I'm working with:

---
echo "Current:$currentAmount:".gettype($currentAmount)." -
Paid:$paidAmount:".gettype($paidAmount)."";
$currentAmount = $currentAmount-$paidAmount;
echo "Final Current: $currentAmount";
-
Straightforward... Yeah?

Here's a sample of the output:
Current:97.6:double - Paid:97.6:double
Final Current: 1.42108547152E-014
Now if both $currentAmount and $paidAmount are doubles, subtracting them
should yield a zero, shouldn't it?
	-- jon

---
jon roig
web developer
email: [EMAIL PROTECTED]
phone: 888.230.7557
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.593 / Virus Database: 376 - Release Date: 2/20/2004
 

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


Re: [PHP] Math weirdness with doubles...

2004-02-27 Thread Daniel Clark
Looks like the Doubles are not exactly zero.  Must be some precision
points not displaying.


> Ok... It's Friday and maybe my brain is dead, but I'm having a weird
> problem with some basic math.
>
> Here's a little snippet of the code I'm working with:
>
> ---
> echo "Current:$currentAmount:".gettype($currentAmount)." -
> Paid:$paidAmount:".gettype($paidAmount)."";
>
> $currentAmount = $currentAmount-$paidAmount;
> echo "Final Current: $currentAmount";
> -
>
> Straightforward... Yeah?
>
> Here's a sample of the output:
> Current:97.6:double - Paid:97.6:double
> Final Current: 1.42108547152E-014
>
> Now if both $currentAmount and $paidAmount are doubles, subtracting them
> should yield a zero, shouldn't it?
>
>   -- jon
>
> ---
> jon roig
> web developer
> email: [EMAIL PROTECTED]
> phone: 888.230.7557
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.593 / Virus Database: 376 - Release Date: 2/20/2004
>
>
> --
> 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] Math weirdness with doubles...

2004-02-27 Thread jon roig
Ok... It's Friday and maybe my brain is dead, but I'm having a weird
problem with some basic math.

Here's a little snippet of the code I'm working with:

---
echo "Current:$currentAmount:".gettype($currentAmount)." -
Paid:$paidAmount:".gettype($paidAmount)."";

$currentAmount = $currentAmount-$paidAmount;
echo "Final Current: $currentAmount";
-

Straightforward... Yeah?

Here's a sample of the output:
Current:97.6:double - Paid:97.6:double
Final Current: 1.42108547152E-014

Now if both $currentAmount and $paidAmount are doubles, subtracting them
should yield a zero, shouldn't it?

-- jon

---
jon roig
web developer
email: [EMAIL PROTECTED]
phone: 888.230.7557

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.593 / Virus Database: 376 - Release Date: 2/20/2004
 

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