isInteger, isFloat functions?

D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 19 Sep 2004 19:10:21, seen in D.
Alvarado said:
Hello,
Does anyone have handy functions for testing whether a text field value is

a) a valid integer
b) a valid floating point number?

Additionally :

You specify a text field, which is a string. Any response article which
does not start with a string has missed the point.

12345 is an integer string
12345.6 is a fixed-point string
12345.0 is a fixed-point string with an integer value
123.4e5 is a floating-point string with an integer value.

Before trying to get the answer right, it is necessary to get the
question right.
 
P

Philip Ronan

Really.
Division by zero is no more a mathematical operation than is cracking an egg.
Any numeric value assigned to it is simply a convenient fiction.

So what? For the purposes of Javascript, 1/0=infinity. I only gave that as
an example. If you want a better one, then how about a string of 500 "9"s.

We are discussing JAVASCRIPT here, right?
 
F

Fred Oz

Philip Ronan wrote:

We are discussing JAVASCRIPT here, right?

Yes. And like all good programmers, if there is *any* chance
that your algorithm may result in division by zero, you
absolutely must test for it and avoid it.

As Andrew pointed out, 3 browsers gave 3 different (likely fully
ECMA-262 compliant) results. So if you don't test for division
by zero in the first place, you had better have done it afterward
because now you must deal with a bigger problem - infinity!

Fred.
 
L

Lasse Reichstein Nielsen

Fred Oz said:
1/0 is not infinity, is it "undefined"- as is the result
of any division by zero.

"1/0" is "Infinity" when "1" and "0" and "/" are as defined by the
IEEE-754 standard. And "Infinity" is not the same as "undefined" when
thay are as defined by the ECMA-262 standard (ECMAScript).

/L 'and 1/-0 == -Infinity, even though 0 == -0 ... just don't think about it:)'
 
L

Lasse Reichstein Nielsen

Evertjan. said:
I don't think infinity is an integer but I wouldn't mind if it is.

It is a number in Javascript. Whether it is an integer, I won't
begin to think about. Hmm. Math.floor(Infinity) == Infinity
Nah.
(+t+1)!=+t ?

You don't need infinity for that in IEEE-754 floating point numbers.
As soon as the numbers becomes so large, that the precission is less
than 1, then t+1 cannot be represented and the result is the same as
t.

Example:
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 20 sep 2004 in comp.lang.javascript:
var t = Math.pow(2,53);
if (t+1 == t) { alert("surprice"); }

I cannot resist bettering you on that, Lasse:

alert("surprise ;-}")
 
P

Philip Ronan

Philip Ronan wrote:



Yes. And like all good programmers, if there is *any* chance
that your algorithm may result in division by zero, you
absolutely must test for it and avoid it.

As Andrew pointed out, 3 browsers gave 3 different (likely fully
ECMA-262 compliant) results. So if you don't test for division
by zero in the first place, you had better have done it afterward
because now you must deal with a bigger problem - infinity!

Fred.

Sorry, I shouldn't have mentioned division by zero in the first place.

What I meant to refer to is the Javascript (ECMA-262) entity called
"Infinity". Any number greater than (roughly?) 10^308 equates to "Infinity"
in Javascript.

Let's get back to the original question:

If I entered this into a text box:
"9999999---99999.9" (replace "---" with 300 "9"s)
then it would fail this test for integers:
alert(!/\./.test(+t) && +t==t)

OK now?
 
F

FredOz

Sorry, I shouldn't have mentioned division by zero in the first place.

Don't be sorry, the discussion was very useful (to me, anyway).
What I meant to refer to is the Javascript (ECMA-262) entity called
"Infinity". Any number greater than (roughly?) 10^308 equates to "Infinity"
in Javascript.

Spot on.
Let's get back to the original question:

If I entered this into a text box:
"9999999---99999.9" (replace "---" with 300 "9"s)
then it would fail this test for integers:
alert(!/\./.test(+t) && +t==t)

OK now?

Yes. Incidentally, the OP has not followed up ... hopefully something
here was of use?
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 20 Sep 2004
22:42:30, seen in Lasse Reichstein Nielsen
It is a number in Javascript. Whether it is an integer, I won't
begin to think about. Hmm. Math.floor(Infinity) == Infinity
Nah.


You don't need infinity for that in IEEE-754 floating point numbers.
As soon as the numbers becomes so large, that the precission is less
than 1, then t+1 cannot be represented and the result is the same as
t.

Example:

But try it with var t = - Math.pow(2,53)
var t = - Math.pow(2,53) - 66666

***

The OP needs to be aware that, in current javascript, all Numbers are
floating-point binary IEEE-754 doubles.
 
G

Grant Wagner

Fred said:
Yes. And like all good programmers, if there is *any* chance
that your algorithm may result in division by zero, you
absolutely must test for it and avoid it.
Agreed.

As Andrew pointed out, 3 browsers gave 3 different (likely fully
ECMA-262 compliant) results. So if you don't test for division
by zero in the first place, you had better have done it afterward
because now you must deal with a bigger problem - infinity!

Andrew pointed out that 3 browsers gave consistent results. His
original message contained:

---

All three of IE 6.0026, Moz. 1.7 and Opera 6.54 (editor: probably
meant 7.54, but I tested 6.05 as well and it was the same) give
consistent results for me..

javascript:alert(1/0) -> 'Infinity'
javascript:alert(0/0) -> 'NaN'
javascript:alert(-1/0) -> '-Infinity'

---

I have to disagree that "Infinity" is a bigger problem. The behaviour
of Number.POSITIVE_INFINITY (and NEGATIVE_INFINITY) are clearly
documented and if the ECMAScript is following the implementation, the
results will be the same regardless of the environment:

<url:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/number.html#1193380
/>


So you can catch division by zero with:

var result = (nominator + Number.MIN_VALUE) / denominator;
if (Math.abs(result) == Number.POSITIVE_INFINITY) { ....


Number.MIN_VALUE should not affect the result too terribly much, and
it prevents 0/0 == NaN, 0/0 now becomes Infinity as well. Of course,
identifying and dealing with demoninator == 0 would be better, but
allowing division by zero in JavaScript is no where near as fatal as
it can be in other languages.
 
M

Michael Winter

On Mon, 20 Sep 2004 14:05:10 GMT, Michael Winter

[snip]
/^(0|[1-9])\d*$/

This should have actually been

/^(0|[1-9]\d*)$/

Where this same pattern, (0|[1-9])\d*, occurs in other expressions (almost
all), it should be replaced with (0|[1-9]\d*).

[snip]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top