From: "Ford, Mike" on
On 03 February 2006 13:14, Andrei wrote:

> Welcome,
> Please note that using bc function variables will be of type string.
> So a code working with numeric values like:
>
> $a = 1;
> if( $a )
> {
> ...
> }
>
> it's ok but with bc functions:
>
> $a = 14.5;
> $b = -14.5;
> $c = bcadd( $a, $b );
> if( $c )
> {
> ...
> }
>
> will not work as expected ( if( "0" ) is interpreted like a string
> that is not null and so condition is interpreted as true.

Er - nope. The string "0" is explicitly equivalent to FALSE -- see http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford(a)leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
From: Andrei on
Er - yes.
If you do conversion type you are correct. but in the example I showed I
didn't...
If you run the code bellow you will see...

bcscale( 10 );
$a = 14.5;
$b = -14.5;
$c = bcadd( $a, $b );
if( $c )
{
echo "true: c = ".$c."<br>";
} else
{
echo "false: c = ".$c."<br>";
}

var_dump((bool) "0");

It's a common use to do if( $var ) and not if( (bool)$var )...

Andy

Ford, Mike wrote:
> On 03 February 2006 13:14, Andrei wrote:
>
>> Welcome,
>> Please note that using bc function variables will be of type string.
>> So a code working with numeric values like:
>>
>> $a = 1;
>> if( $a )
>> {
>> ...
>> }
>>
>> it's ok but with bc functions:
>>
>> $a = 14.5;
>> $b = -14.5;
>> $c = bcadd( $a, $b );
>> if( $c )
>> {
>> ...
>> }
>>
>> will not work as expected ( if( "0" ) is interpreted like a string
>> that is not null and so condition is interpreted as true.
>
> Er - nope. The string "0" is explicitly equivalent to FALSE -- see http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
>
> Cheers!
>
> Mike
>
> ---------------------------------------------------------------------
> Mike Ford, Electronic Information Services Adviser,
> Learning Support Services, Learning & Information Services,
> JG125, James Graham Building, Leeds Metropolitan University,
> Headingley Campus, LEEDS, LS6 3QS, United Kingdom
> Email: m.ford(a)leedsmet.ac.uk
> Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
>
>
> To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
>
From: Andrei on

Actually we'r talking about diffrent things... What I wanted to
highlight is that results that should be normally seen as 0 or false for
logic comparations when using bc functions are not processed as 0 or
false actually and you should take care when using constructs like

if( $var )
{
...
}

when working with variables that are a result of bc functions.

Andy

Ford, Mike wrote:
> On 03 February 2006 13:14, Andrei wrote:
>
>> Welcome,
>> Please note that using bc function variables will be of type string.
>> So a code working with numeric values like:
>>
>> $a = 1;
>> if( $a )
>> {
>> ...
>> }
>>
>> it's ok but with bc functions:
>>
>> $a = 14.5;
>> $b = -14.5;
>> $c = bcadd( $a, $b );
>> if( $c )
>> {
>> ...
>> }
>>
>> will not work as expected ( if( "0" ) is interpreted like a string
>> that is not null and so condition is interpreted as true.
>
> Er - nope. The string "0" is explicitly equivalent to FALSE -- see http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
>
> Cheers!
>
> Mike
>
> ---------------------------------------------------------------------
> Mike Ford, Electronic Information Services Adviser,
> Learning Support Services, Learning & Information Services,
> JG125, James Graham Building, Leeds Metropolitan University,
> Headingley Campus, LEEDS, LS6 3QS, United Kingdom
> Email: m.ford(a)leedsmet.ac.uk
> Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
>
>
> To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
>
From: "Richard Lynch" on


It's possible that MySQL has larger range of floats than PHP.

Then when your numbers come in, PHP ends up making them be things like:
'NaN' (not a number) or '6.02e+23' or ...

There are a LOT of ways to represent floats in such a way that you can
confuse the heck out of PHP and MySQL between the two of them.

You really need to figure out where/how your calculcations should be
made to get the sort of range and precision you need.

On Fri, February 3, 2006 6:32 am, Barry wrote:
> Andrei wrote:
>> When working with floats with php/mysql I had problems too...
>> When
>> summing amounts I had errors so my solution was using decimal (20,
>> 10)
>> type into mysql for storing amounts and when using sums in php I
>> used bc
>> functions. This way u get exact calculations (depending on bcscale()
>> parameter.
>
>
> Thanks Andy sounds great.
> Changed DB to decimal 10.2
>
> Tried it, but using bcadd i get a value of 0.00.
>
> If you have som sample code lying around somewhere that would be
> great,
> or do you know what happened?
>
> Barry
> --
> Smileys rule (cX.x)C --o(^_^o)
> Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Like Music?
http://l-i-e.com/artists.htm
From: Barry on
Hi!

>I forget the rules of float contagion in PHP, but it "should" work, I
>would expect...

That's what i also did, though.

>I don't suppose that by sheer chance the decimal portions add up to an
>even integer?...
>0.3333 + 0.6666
>might, for example, turn into: 1
>(Or might not, depending on a low-level float implementation)

It prints out 0.9999

>Some other options:
>Cast them in MySQl and let MySQL add them up.

Works fine, just needs 3 seconds for the query.
3 seconds for the table + 3 seconds for the summary.
This is taking a bit to long ;)

>I forget MySQL's typecast operator/function, but it's in the manual:
>http://dev.mysql.com
>Something like:
>select sum(cast(floatval, 'decimal')) from somewhere
>
>You may also need to use coalesce to change NULL into 0.
>
>You'd have to tell use what the actual string values ARE for us to do
>any more than guess, though.

Normal prices like:
345.76
234.09
324.54

And so on

Greets
Barry

--
Smileys rule (cX.x)C --o(^_^o)
Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)