FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Something I have never seen before
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Something I have never seen before [message #169460] Tue, 14 September 2010 00:07 Go to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
In all my years of programming here is something I have never seen
before (that actually happened in a program:

$b = array('5', '3');
$a = (count($b) + 1)/2;
print 'count = ' . count($b) . '<br>';
print 'a = ' . $a;


Output:
=======
count = 2
a = 1.5

I just don't understand how this is possible, that a is not one.

--
Shelly
Re: Something I have never seen before [message #169461 is a reply to message #169460] Tue, 14 September 2010 00:21 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/13/2010 8:07 PM, sheldonlg wrote:
> In all my years of programming here is something I have never seen
> before (that actually happened in a program:
>
> $b = array('5', '3');
> $a = (count($b) + 1)/2;
> print 'count = ' . count($b) . '<br>';
> print 'a = ' . $a;
>
>
> Output:
> =======
> count = 2
> a = 1.5
>
> I just don't understand how this is possible, that a is not one.
>

(2 + 1) / 2 = 1.5.

How could it be anything else?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Something I have never seen before [message #169462 is a reply to message #169461] Tue, 14 September 2010 02:04 Go to previous messageGo to next message
Marious Barrier is currently offline  Marious Barrier
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
On 09/13/2010 08:21 PM, Jerry Stuckle wrote:
> (2 + 1) / 2 = 1.5.
>
> How could it be anything else?

Whatever... but please just don’t divide by zero. I still have a life to
live.
Re: Something I have never seen before [message #169463 is a reply to message #169460] Tue, 14 September 2010 02:42 Go to previous messageGo to next message
Sherm Pendley is currently offline  Sherm Pendley
Messages: 33
Registered: September 2010
Karma: 0
Member
sheldonlg <sheldonlg> writes:

> In all my years of programming here is something I have never seen
> before (that actually happened in a program:
>
> $b = array('5', '3');
> $a = (count($b) + 1)/2;
> print 'count = ' . count($b) . '<br>';
> print 'a = ' . $a;
>
>
> Output:
> =======
> count = 2
> a = 1.5
>
> I just don't understand how this is possible, that a is not one.

How on earth *could* a be one? (2+1)/2 == 1.5.

sherm--

--
Sherm Pendley
<http://camelbones.sourceforge.net>
Cocoa Developer
Re: Something I have never seen before [message #169464 is a reply to message #169461] Tue, 14 September 2010 06:54 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 9/14/2010 2:21 AM, Jerry Stuckle wrote:
> On 9/13/2010 8:07 PM, sheldonlg wrote:
>> In all my years of programming here is something I have never seen
>> before (that actually happened in a program:
>>
>> $b = array('5', '3');
>> $a = (count($b) + 1)/2;
>> print 'count = ' . count($b) . '<br>';
>> print 'a = ' . $a;
>>
>>
>> Output:
>> =======
>> count = 2
>> a = 1.5
>>
>> I just don't understand how this is possible, that a is not one.
>>
>
> (2 + 1) / 2 = 1.5.
>
> How could it be anything else?
>

Indeed. Sheldonlg must have a bad day.
I know how that feels. ;-)

My advise: Take a day or two to relax, then code on.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Re: Something I have never seen before [message #169466 is a reply to message #169460] Tue, 14 September 2010 09:24 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On 14 Sep, 01:07, sheldonlg <sheldonlg> wrote:
> In all my years of programming here is something I have never seen
> before (that actually happened in a program:
>
> $b = array('5', '3');
> $a = (count($b) + 1)/2;
> print 'count = ' . count($b) . '<br>';
> print 'a = ' . $a;
>
> Output:
> =======
> count = 2
> a = 1.5
>
> I just don't understand how this is possible, that a is not one.
>
> --
> Shelly

I agree it is pretty amazing that you have found some programming
lines that actually give the correct output!

I have seen it before, once or twice ;-)
Re: Something I have never seen before [message #169467 is a reply to message #169460] Tue, 14 September 2010 12:21 Go to previous messageGo to next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 13, 8:07 pm, sheldonlg <sheldonlg> wrote:
> In all my years of programming here is something I have never seen
> before (that actually happened in a program:
>
> $b = array('5', '3');
> $a = (count($b) + 1)/2;
> print 'count = ' . count($b) . '<br>';
> print 'a = ' . $a;
>
> Output:
> =======
> count = 2
> a = 1.5
>
> I just don't understand how this is possible, that a is not one.

Are you expecting a to be one because you are thinking you're dividing
2/2, or because you realize you're dividing 3/2 and expecting integer
math?
Re: Something I have never seen before [message #169468 is a reply to message #169461] Tue, 14 September 2010 15:59 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 9/13/2010 8:21 PM, Jerry Stuckle wrote:
> On 9/13/2010 8:07 PM, sheldonlg wrote:
>> In all my years of programming here is something I have never seen
>> before (that actually happened in a program:
>>
>> $b = array('5', '3');
>> $a = (count($b) + 1)/2;
>> print 'count = ' . count($b) . '<br>';
>> print 'a = ' . $a;
>>
>>
>> Output:
>> =======
>> count = 2
>> a = 1.5
>>
>> I just don't understand how this is possible, that a is not one.
>>
>
> (2 + 1) / 2 = 1.5.
>
> How could it be anything else?
>

Wow. I guess in all the years of PHP programming I had never before had
a need for integer arithmetic. In Java, C, Fortran, etc. an integer
slash integer yields an integer. In PHP (I just looked it up) it can be
a float. So, I just assumed that it was the same in PHP. I guess that
this is because PHP is an untyped language.

Yes, I do feel silly now.

--
Shelly
Re: Something I have never seen before [message #169469 is a reply to message #169468] Tue, 14 September 2010 17:09 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
sheldonlg wrote:
> On 9/13/2010 8:21 PM, Jerry Stuckle wrote:
>> On 9/13/2010 8:07 PM, sheldonlg wrote:
>>> In all my years of programming here is something I have never seen
>>> before (that actually happened in a program:
>>>
>>> $b = array('5', '3');
>>> $a = (count($b) + 1)/2;
>>> print 'count = ' . count($b) . '<br>';
>>> print 'a = ' . $a;
>>>
>>>
>>> Output:
>>> =======
>>> count = 2
>>> a = 1.5
>>>
>>> I just don't understand how this is possible, that a is not one.
>>>
>>
>> (2 + 1) / 2 = 1.5.
>>
>> How could it be anything else?
>>
>
> Wow. I guess in all the years of PHP programming I had never before had
> a need for integer arithmetic. In Java, C, Fortran, etc. an integer
> slash integer yields an integer. In PHP (I just looked it up) it can be
> a float. So, I just assumed that it was the same in PHP. I guess that
> this is because PHP is an untyped language.
>
> Yes, I do feel silly now.
>
no, you have to cast your mind back to BASIC to find a language designed
to be 'easy to use' that completely masks any possible knowledge of what
exact type of variable is in play at any given point.

And whose specification is not unambiguous on that point. Or was that
JavaShite (TM).

it's less untyped, than randomly casting types about like pearls before
swine.

whether '3' is in integer, a character, a string or a floating point
number, is entirely down to context. And will change with it, too.


Its known as programming for dummies, and its very fashionable.
Re: Something I have never seen before [message #169470 is a reply to message #169469] Tue, 14 September 2010 17:34 Go to previous message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 14, 1:09 pm, The Natural Philosopher <t...@invalid.invalid>
wrote:
> sheldonlg wrote:
>> On 9/13/2010 8:21 PM, Jerry Stuckle wrote:
>>> On 9/13/2010 8:07 PM, sheldonlg wrote:
>>>> In all my years of programming here is something I have never seen
>>>> before (that actually happened in a program:
>
>>>> $b = array('5', '3');
>>>> $a = (count($b) + 1)/2;
>>>> print 'count = ' . count($b) . '<br>';
>>>> print 'a = ' . $a;
>
>>>> Output:
>>>> =======
>>>> count = 2
>>>> a = 1.5
>
>>>> I just don't understand how this is possible, that a is not one.
>
>>> (2 + 1) / 2 = 1.5.
>
>>> How could it be anything else?
>
>> Wow.  I guess in all the years of PHP programming I had never before had
>> a need for integer arithmetic.  In Java, C, Fortran, etc. an integer
>> slash integer yields an integer.  In PHP (I just looked it up) it can be
>> a float.  So, I just assumed that it was the same in PHP.  I guess that
>> this is because PHP is an untyped language.
>
>> Yes, I do feel silly now.
>
> no, you have to cast your mind back to BASIC to find a language designed
> to be 'easy to use' that completely masks any possible knowledge of what
> exact type of variable is in play at any given point.
>
> And whose specification is not unambiguous on that point. Or was that
> JavaShite (TM).
>
> it's less untyped, than randomly casting types about like pearls before
> swine.
>
> whether '3' is in integer, a character, a string or a floating point
> number, is entirely down to context. And will change with it, too.
>
> Its known as programming for dummies, and its very fashionable.

And just to confuse the matter even further:

$a = 3;
$b = 2;
var_dump(is_int($a));
var_dump($a);
var_dump(is_int($b));
var_dump($b);
var_dump($a/$b);

bool(true)
int(3)
bool(true)
int(2)
float(1.5)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Function that returns name of itself?
Next Topic: Last element in an array?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 03:17:23 GMT 2024

Total time taken to generate the page: 0.02173 seconds