Comment On IsInteger() ... really?

Oy. What was Chad thinking, going and referencing "/global/lib/numericFunctions.php"? Sure, it may *seem* like there's some helpful methods in there, epsecially with names like "RoundUp()", "ExtractNumber()" and so on. And yes, it seems logical enough to commonly used methods in some sort of library. But what happens when a simple sounding function, like IsInteger(), doesn't actually do what it says? [expand full text]
« PrevPage 1 | Page 2Next »

Re: IsInteger() ... really?

2005-02-11 14:01 • by

Alex, you have the best titles for these.


 


They always crack me up.

Re: IsInteger() ... really?

2005-02-11 14:06 • by

I can only assume that the developer of that function didn't look at the manual too hard, otherwise they would have spotted the built-in is_numeric() function in PHP.


That, or this codebase was developed for PHP version 3, which didn't have that function. But who uses PHP 3 these days?

Re: IsInteger() ... really?

2005-02-11 14:15 • by
Obviously, the function name we were looking for here was IsBetweenZeroAndNine().

Re: IsInteger() ... really?

2005-02-11 14:20 • by
29496 in reply to 29494
Technically, they should use is_integer() instead of is_numeric().  There is probably an equally bad IsNumeric() somewhere that returns false for 5.4735e+001

Re: IsInteger() ... really?

2005-02-11 14:47 • by Theologian

Most technically, the name should probably be:


function is_$Len=strlen($String);for($i=0;$i<$Len;$i++){if(($String[$i]>'9')||($String[$i]<'0')){return(false);}}return(true);_($String)


[H]

Re: IsInteger() ... really?

2005-02-11 15:00 • by
29503 in reply to 29502

I'm so avoiding-work-right-now that I actually tested this function. I can't figure out what it tells you. The param $String is presumably, well, a string, but then he references it as an array - $String[$i].


It always returns false unless you pass it a null or empty string, which of course returns true.

Re: IsInteger() ... really?

2005-02-11 15:04 • by Stan Rogers
29505 in reply to 29502
I'd prefer
is_safely_convertible_to_integer_without_truncation_unless_it_has_to_convert_to_float_due_to_overflow_oh_well_it_can_be_made_some_kind_of_non_fractional_number_anyway()

Re: IsInteger() ... really?

2005-02-11 15:24 • by
29510 in reply to 29503
$String[$i] is the old way of saying $String{$i}. Zend developers don't like overloading operators (they get confused easily), so it was changed sometime in PHP4. Presumably it was removed (or at least turned off by default) in 5.

Re: IsInteger() ... really?

2005-02-11 15:53 • by KraGiE
Ok.  this is intense. 

Re: IsInteger() ... really?

2005-02-11 15:59 • by Raymond Chen
And IsInteger("") returns true.

Re: IsInteger() ... really?

2005-02-11 16:04 • by Stan Rogers
29517 in reply to 29516
So it does -- I'll have to adjust the name to account for that.

Re: IsInteger() ... really?

2005-02-11 17:06 • by
I don't know anything about PHP, so could someone explain the WTF to an outsider?



With my lack of PHP-knowledge, it looks like it checks if a string is an integer (*)
or the empty string..? And this is what one would expect from the name
of the function (except maybe the empty-string thing - depends on how
you define an "integer-string" (or whatever you wanna call it)).



What am i not getting?



(*) ofcause i know a string is not an integer, but you guys should know what i mean

Re: IsInteger() ... really?

2005-02-11 17:18 • by
(I'm assuming we're talking unsigned integer-strings ofcause - it it's supposed to work for signed, i get it)

Re: IsInteger() ... really?

2005-02-11 17:20 • by Blue
29523 in reply to 29519
I believe that the WTF here, as mentioned above in pieces, is that the
intended purpose of the function was to walk through every character in
the string input, and ensure it is a numeric digit (0-9).  The function
name is "isInteger".  Integers can be signed (ie, be positive or
negative), so if you call the function with "-2", you will get a false
return value even though -2 is a valid integer.




Also, an empty string will cause the for loop to be bypassed, since the
string length is zero, leading directly to the return true statement,
which in effect is saying that an empty string is an integer also.



in pseudocode:



a. Get a string containing 0 or more characters.

b. loop through each of the characters in the string, and return false if any aren't between 0-9

c. return true otherwise.



Hope that helps!



Re: IsInteger() ... really?

2005-02-11 17:21 • by Blue
29524 in reply to 29523
Wish we could edit posts.  What I meant was, the intended purpose
of the function, given its' name, is to determine if the string input
represents an integer.  What it in fact does is attempt to
validate that all characters in the string are digits, and does not
correctly handle the negative sign or the empty string case.



Re: IsInteger() ... really?

2005-02-11 17:33 • by
Ah, ok - so it's a function-naming thing (or a really lame code error made by a tired/horny coder :)). Ok, thanx.

Re: IsInteger() ... really?

2005-02-11 17:35 • by
And should '00' return true or false?  Leading zeroes are OK I suppose, but still...

Re: IsInteger() ... really?

2005-02-11 18:25 • by

"IsWholeNumber()" would pretty much capture it succinctly. Oh, except for that empty string thingy. “IsWholeNumberOrEmptyString()”. Or Null. “IsWholeNumberOrEmptyStringOrNull().” Okay. Still could have its uses.


Wait, 1.435e6 is a whole number.


“IsWholeNumberOrEmptyStringOrNullOrNotExpressedInScientificNotation()”


There.


--Rank Amateur


I’m not even a developer, but this is funniest site on the web for me.

Re: IsInteger() ... really?

2005-02-11 19:01 • by
29530 in reply to 29529
Hmm... yeah... But for those who really likes descriptive function-names, maybe these names are better:



isTheParameterGivenToThisFunctionAnIntegerThatIsNotNegativeButMaybeEmpty ()

isTheParameterThatIsGivenToThisFunctionAString_WhereEachCharIsBetween0And9IncludingBoth0And9_WhereTheIntergerThatTheseCharsRepresentsAreNotNegative_ByTheWayTheStringCouldBeEmpty
Aswell() [:)]



but other might argue that the name is too verbose. Maybe "isUInt()" or "a()" would be better?



Re: IsInteger() ... really?

2005-02-11 20:12 • by KraGiE

honestly, it disturbs me to see an integer wrapped in ' delimiters.  I'm still at a lost for words with this one, and I'm fairly glad I didn't have to learn php.   


I'm still trying to convince my friend to give me a copy of the if statement that went like


if (true)
{
   // Complete Action
   /* insert other code here */
}
else
{
   // There was an error.
   /* there was actually code here. */
}

Re: IsInteger() ... really?

2005-02-11 20:35 • by

Oh, what about 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999?


This function would return true, but I can't see any language (I don't know about PHP) supporting such an integer.

Re: IsInteger() ... really?

2005-02-12 00:31 • by Blue
29537 in reply to 29532
Anonymous: Regarding the insanely long sequence of 9's.



Congrats on being the first to catch that!  Totally didn't occur to me (or anyone else, it would seem.)



Re: IsInteger() ... really?

2005-02-12 01:40 • by init6
The funny part is that the discoverer was not calling this function. He was re-using a class (in production for a year and a half) written by someone else which called the above function He noticed a member function of the class did not work with negative numbers so he had to trace it back to the library function.

Next he looked like he might explode into a rant about the competency of his co-workers but instead he told someone else about it. His co-worker said "which one of them did this?" He replied with "I, um, it could be a couple of people." I think he started to cry at that point.

Re: IsInteger() ... really?

2005-02-12 02:47 • by
29551 in reply to 29548

I feel your pain, I work with someone that does some interesting things sometimes, and has some interesting ideas.


This latest one was interesting...


C# has this cute little class called FileInfo, you create it by passing a filename or path to a file in its constructor, and it can tell you all sorts of neat little things about a file. It also has some useful methods to manipulate files. One of these methods is called CopyTo, which you guessed it, copies the file described by the FileInfo to the location you specify.


So, one of the things he has to implement is a way to send a file from the server to a client (see where this is going?). So he tells me what he wants to do, which is create a FileInfo class of the file on the server, send that class to the client, then call CopyTo from the client to send the file to the client. I told him it wouldn't work, but he didn't believe me. So I told him to go ahead and implement and show me. I'm still waiting to see it =)

Re: IsInteger() ... really?

2005-02-12 06:01 • by AndrewVos
this person can be lucky im not their boss, would have to throw them out a high window. anyway does php not have some sort of integer.parse function?????!??!??!?! wtf!

Re: IsInteger() ... really?

2005-02-12 11:27 • by
29561 in reply to 29532
Oh, what about 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999?

This function would return true, but I can't see any language (I don't know about PHP) supporting such an integer.




There are a few, python is one - it has native support for unbounded
integers. I'm pretty certain matlab does and functional languages will.


But that doesn't make this function any less moronic.



Re: IsInteger() ... really?

2005-02-12 14:48 • by
29565 in reply to 29548

init6:
"which one of them did this?" He replied with "I, um, it could be a couple of people."


So go to your Source Code Control system, review the history of the file, determine who's the responsible party and take the corrective action of <your code goes here>.

Re: IsInteger() ... really?

2005-02-12 18:18 • by
everyone forgets about "+2"...

Re: IsInteger() ... really?

2005-02-12 21:09 • by
29571 in reply to 29532
:

Oh, what about 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999?


This function would return true, but I can't see any language (I don't know about PHP) supporting such an integer.





Really?  I can't see see any proper language not supporting
something like that.  In fact, transparent bignum support is one
of my (several) criteria for judging whether or not a language is worth
using.



Re: IsInteger() ... really?

2005-02-12 23:29 • by Blue
29577 in reply to 29556
Andrew, in php the integer parse function is intval()



Re: IsInteger() ... really?

2005-02-12 23:35 • by Blue
29578 in reply to 29571
Re: "transparent bignum support"



I would disagree with that - If I'm using the return value of this
function to determine if I can store into a variable declared as a
"regular" integer, I do not want that variable promoted automagically
to a bigint (or whatever you want to call it).   If code
using that result tried to insert the value of my promoted variable
into a database (none of which I am aware of can promote ints
automagically), an exception would be thrown.



But, what the heck to I know? [:D]



And, to quote someone's sig that I see on here regularly, I've had a
few beers before posting this, so excuse me if I'm way off base.



Re: IsInteger() ... really?

2005-02-12 23:36 • by Blue
29579 in reply to 29578
Cool, I created one of the microscopic font posts.   Which I
could remember how I managed to do that, so I could write a "proper bug
report"[:P]

Re: IsInteger() ... really?

2005-02-13 00:51 • by

This is the "cowardly" anonymous poster who refuses to register (for fear of being "clowned"):


Microscopic font huh? Now who's the clown? Ha!


+1 for all the anonymous posters who need support groups!

Re: IsInteger() ... really?

2005-02-13 02:54 • by Blue
29588 in reply to 29586
While others may have, I for one have not accused anonymous posters of
being cowardly.  Again, it's just to maintain a coherent reply chain
due to lack of threading.





Exactly how does the fact that the forum software occasionally causes posts to appear in tiny fonts cause me to be a clown?





-1 for not making any sense whatsoever.



This whole conversation is beginning to have the stench of trolling,
so, as I stated in the other post, I'm done.   Enjoy your
anonymousness.   My point has obviously been missed.









Re: IsInteger() ... really?

2005-02-13 10:04 • by

"Oh, what about 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999?



This function would return true, but I can't see any language (I don't know about PHP) supporting such an integer."


Python 2.4 (#1, Jan 12 2005, 11:15:47)

[GCC 3.4.3 20050104 (Red Hat 3.4.3-13)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> i = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

>>> print i

99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

>>> print i+1

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000








Re: IsInteger() ... really?

2005-02-13 10:05 • by
python can support such a long sequence of 9s and there are surely others.

Re: IsInteger() ... really?

2005-02-13 13:57 • by
Anonymous: the 99999999999999999... integer



There are many languages which support this. Python  is one of the more popular one.



$ python

....

>>> i = 999999999999999999999999999999999999999

>>> print i

999999999999999999999999999999999999999

>>>








Re: IsInteger() ... really?

2005-02-13 15:19 • by CPound
29610 in reply to 29586
:
This is the "cowardly" anonymous poster who refuses to register (for fear of being "clowned"):

Microscopic font huh? Now who's the clown? Ha!


+1 for all the anonymous posters who need support groups!


Blue is absolutely right when he says -1 for you not making sense. As a matter of fact I think it's pretty funny...because you were so fearful of being "clowned" and yet you completely clowned yourself with your last comment.


Enjoy the clownage! [;)]

Re: IsInteger() ... really?

2005-02-13 18:07 • by bat
In the interests of being a fug smucker, I shall post my version of
this function (let's forget that the is_xxx functions exist, for
didactic purposes):


// optional sign and any number of digits
// note: doesn't check for overflow


function is_integer($val)

{

  return preg_match('/^[-+]?\d+$/',$val);
}



PHP's regexp handling is lifted straight from Perl, and is therefore
some of the best-optimised string-handling code ever written.  You
can't in all honesty expect to write anything faster.



Oh... and the correct name for the original function would have been
"is_cardinal" - a cardinal number is basically a non-negative
integer.  The only problem then is people passing the names of
Catholic clergy to the function and being surprised at the answer...

Re: IsInteger() ... really?

2005-02-13 20:42 • by kentcb
29621 in reply to 29532
:

Oh, what about 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999?


This function would return true, but I can't see any language (I don't know about PHP) supporting such an integer.



So? The function isn't called Is32BitInteger(), it's called IsInteger().

Re: IsInteger() ... really?

2005-02-14 01:29 • by Drak

Hmm, I wonder.. Does PHP require return values to be encased in parentheses? Or was the coder just rying to be sure the non-existant ands and ors would be resolved in the proper order?


Drak

Re: IsInteger() ... really?

2005-02-14 02:59 • by V.

Actually 99999999999999999999999999999999999999999... (without the ...). Is a valid mathematical integer although your compiler is not able to accept this in this way. [;)]


new functionname: TryToDeterminIfIsPositiveInteger_WithBugs($String);[^o)]


 

Re: IsInteger() ... really?

2005-02-14 04:30 • by
29629 in reply to 29617

bat:
Oh... and the correct name for the original function would have been "is_cardinal" - a cardinal number is basically a non-negative integer.  The only problem then is people passing the names of Catholic clergy to the function and being surprised at the answer...


Nice one bat, made me laugh out loud. Could just call the function PopeJeanPaulII() and then it's something that is sort of like a cardinal but likely to keel over at any moment.

Re: IsInteger() ... really?

2005-02-14 04:41 • by
29630 in reply to 29617

bat:

Oh... and the correct name for the original function would have been "is_cardinal" - a cardinal number is basically a non-negative integer.  The only problem then is people passing the names of Catholic clergy to the function and being surprised at the answer...


Not the only problem - "is_cardinal" would also have to recognise a songbird of the finch family and Arizona football players.

Re: IsInteger() ... really?

2005-02-14 05:23 • by nsimeonov

I don't think it's a big deal. Setting aside that he is re-inventing a function, which seems to be very common for newbies, who don't know s**t about the capabilities of the language and the libs they could use.


The quick solution would be to just search the entire project and replace the name of the function with IsPositiveInteger then create another IsInteger and it's ok - or start using the built in functions but this is just another search & replace - basically every text editor for developers can do a search and replace on an entire project...... and everyone managed to screw up royally at least once in his/her lifetime a project using this feature.

Re: IsInteger() ... really?

2005-02-14 09:34 • by SurfaceTension
29637 in reply to 29633
The thing is that he reinvented a function in place already, and did so incorrectly.



Relying on search and replace will lead you down the dark path. Once it
consumes you, forever will it control your destiny, or something like
that. Especially in a real-time interpreted language like PHP.

Re: IsInteger() ... really?

2005-02-14 09:54 • by

Um, asides from the fact that it may puke on negative numbers and the like, did y'all (other than the originator of the thread) not notice that the function return value is the opposite of what the function name seems to indicate?


IOW - it returns a a "TRUE" if the string is NOT an integer!


So it should be named IsNotInteger()


 


 

Re: IsInteger() ... really?

2005-02-14 10:33 • by Mike R
29640 in reply to 29638
:

Um, asides from the fact that it may puke on negative numbers and the like, did y'all (other than the originator of the thread) not notice that the function return value is the opposite of what the function name seems to indicate?


IOW - it returns a a "TRUE" if the string is NOT an integer!


So it should be named IsNotInteger()



Umm....


// is a variable an integer?
    function IsInteger($String)
    {
        $Len = strlen($String);
        for ( $i=0; $i< $Len; $i++ ) {
            if ( ($String[$i] > '9') || ($String[$i] < '0') ) {
                return (false);
            }
        }
        return(true);
    }


I have taken the liberty of highlighting a few key things in this function. Let see, the function returns false of the value of $string[$i] is less than 0 or greater than 9. Think about that for a few seconds. If after reading the entire string in, none of these conditions are met, and they will not be if it's a string of digits, then it returns true.


I certainly hope you are not a programmer or if not, you do not enter into the field of programming, if you cannot follow simple boolean logic.


You've been cl0wned. [:P]

Re: IsInteger() ... really?

2005-02-14 11:33 • by
29641 in reply to 29640
Well, when booleans consist of true, false, or maybe...it gets kinda confusing.

Re: IsInteger() ... really?

2005-02-14 11:56 • by
29642 in reply to 29532
Oh, what about 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999?
This function would return true, but I can't see any language (I don't know about PHP) supporting such an integer.
Let
me introduce you to a language called Python.  It's similar to a
lot of other programming languages, except that it's good.  Here's
an cut-and-paste from the interactive environment:
>>> i = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
>>> i
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999L
>>> i + 1
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
>>> int(i)
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999L

« PrevPage 1 | Page 2Next »

Add Comment