Something I have never seen before [message #169460] |
Tue, 14 September 2010 00:07 |
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 |
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 #169463 is a reply to message #169460] |
Tue, 14 September 2010 02:42 |
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 |
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 |
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 |
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 |
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 |
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 |
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)
|
|
|