By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,397 Members | 1,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,397 IT Pros & Developers. It's quick & easy.

isInteger, isFloat functions?

P: n/a
Hello,
Does anyone have handy functions for testing whether a text field value is

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

Thanks for any assistance, - Dave
Jul 23 '05 #1
Share this Question
Share on Google+
35 Replies


P: n/a
D. Alvarado wrote:
Hello,
Does anyone have handy functions for testing whether a text field value is

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


Try the FAQ:

http://www.jibbering.com/faq/#FAQ4_12

Follow the links for parseInt and parseFloat - choose your
- poison MS or Netscape. I think the Netscape reference is
better on this one. You may find isNaN useful too.

Cheers, Rob.
Jul 23 '05 #2

P: n/a
Fox


RobG wrote:
D. Alvarado wrote:
Hello,
Does anyone have handy functions for testing whether a text field
value is
a) a valid integer
b) a valid floating point number?

Try the FAQ:

http://www.jibbering.com/faq/#FAQ4_12

Follow the links for parseInt and parseFloat - choose your
- poison MS or Netscape. I think the Netscape reference is
better on this one. You may find isNaN useful too.

Cheers, Rob.


parseInt and parseFloat will both accept non-numeric characters in a string and
return the digits used up to the first non-digit character -- e.g.:

parseInt("123abc") will return 123 [a "true"]
parseFloat("123.2abc") will return 123.2 [also a "true"]

technically, you cannot pass either of these to an "isInteger" and "isFloat" and
receive a true -- they should both be false.
Jul 23 '05 #3

P: n/a
Fox


D. Alvarado wrote:
Hello,
Does anyone have handy functions for testing whether a text field value is

a) a valid integer
function
isInteger(s)
{
var n = trim(s);
return n.length > 0 && !(/[^0-9]/).test(n);

}
b) a valid floating point number?


function
isFloat(s)
{
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);
}
isFloat "qualifies" and differentiates from an integer by testing for a decimal
point and at least one digit after it. Therefore, 123. will not qualify as a
floating point number [nor an integer in isInteger] -- it is an invalid input.
If you require a digit *before* the decimal point for a float, you'll have to
adjust the regex.

These routines merely test for *any* non-digit(/non-numeric) character
(therefore the 'g' selector in the regex is not required).
[note: empty strings will return a "false positive" - that's why testing for
string length]

To trim away any spaces before or after the string:

function
trim(s)
{
return s.replace(/^\s+|\s+$/g, "");
}
You can test for Integer OR Float with:

function
isNumber(s)
{
var n = trim(s);
return n.length>0 && +n == n;
}

or you can use !isNaN(numstr) on a non-empty string. [!isNaN("") returns true
and isNaN apparently trims strings of whitespace before testing]
Jul 23 '05 #4

P: n/a
D. Alvarado wrote on 20 sep 2004 in comp.lang.javascript:
Does anyone have handy functions for testing whether a text field
value is

a) a valid integer
t = " -12.345"
alert(!/\./.test(t) && +t==t)
t = " -12345"
alert(!/\./.test(t) && +t==t)
b) a valid floating point number?


t = " -12.34.5"
alert(+t==t)
t = " -12.345"
alert(+t==t)

Shoot!
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #5

P: n/a
On 20/9/04 10:54 am, Evertjan. wrote:
D. Alvarado wrote on 20 sep 2004 in comp.lang.javascript:
Does anyone have handy functions for testing whether a text field
value is

a) a valid integer


t = " -12.345"
alert(!/\./.test(t) && +t==t)
t = " -12345"
alert(!/\./.test(t) && +t==t)
b) a valid floating point number?


t = " -12.34.5"
alert(+t==t)
t = " -12.345"
alert(+t==t)

Shoot!


<pedant>
what about t = "1.2e+03"?
</pedant>

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #6

P: n/a
Philip Ronan wrote on 20 sep 2004 in comp.lang.javascript:
On 20/9/04 10:54 am, Evertjan. wrote:
a) a valid integer
t = " -12.345"
alert(!/\./.test(t) && +t==t)
t = " -12345"
alert(!/\./.test(t) && +t==t)

[..]
<pedant>
what about t = "1.2e+03"?
</pedant>


t = "1.2e+03"
alert(!/\./.test(+t) && +t==t)
t = "5.0000"
alert(!/\./.test(+t) && +t==t)

will take care of those.

However, it will miserably fail here:

t = "1.2e+33"
alert(!/\./.test(+t) && +t==t)

Though technically incorrect [John?],
are such big/small numbers important to the OP ?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #7

P: n/a
On 20/9/04 11:15 am, Evertjan. wrote:
t = "1.2e+03"
alert(!/\./.test(+t) && +t==t)
t = "5.0000"
alert(!/\./.test(+t) && +t==t)

will take care of those.

However, it will miserably fail here:

t = "1.2e+33"
alert(!/\./.test(+t) && +t==t)


It will also fail when t = infinity (e.g., "t=1/0")

How about:

alert(!/\./.test(+t) && +t==t && (t+1)!=t)

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #8

P: n/a
Fox wrote:

technically, you cannot pass either of these to an "isInteger" and
"isFloat" and receive a true -- they should both be false.


I think Rob is on the right track (but could have offered some
code...)

Testing strings using regular expressions to see if they have
decimal places (and how many), surplus characters, scientific
notation, etc. requires exhaustive testing... let JS do
the work.

How about:

var x = someValue;
if (x == parseInt(x) && x == parseFloat(x)) {
alert(x + ' is a int');
} else if (x == parseFloat(x)) {
alert(x + ' is a float');
} else {
alert(x + ' is not a number I like...');
}

You can even use scientific notation if you like, so 1.2e+03 is
recognised as a float quite happily without complex pattern
matching.

Cheers, Fred
Jul 23 '05 #9

P: n/a
Philip Ronan wrote on 20 sep 2004 in comp.lang.javascript:
It will also fail when t = infinity (e.g., "t=1/0")

How about:

alert(!/\./.test(+t) && +t==t && (t+1)!=t)


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

(+t+1)!=+t ?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #10

P: n/a
Philip Ronan wrote:

It will also fail when t = infinity (e.g., "t=1/0")


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

Cheers, Fred.
Jul 23 '05 #11

P: n/a
On 20/9/04 12:44 pm, Fred Oz wrote:
Philip Ronan wrote:

It will also fail when t = infinity (e.g., "t=1/0")


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

Cheers, Fred.


Really?
javascript:alert(1/0) returns "Inf" in IE.

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #12

P: n/a
Philip Ronan wrote:

Really?
javascript:alert(1/0) returns "Inf" in IE.


And now IE is an authority on mathematics? Heaven forbid!

Please read here:

http://home.ubalt.edu/ntsbarsh/zero/...TM#rsimpalgcom

Have a good one, Fred.
Jul 23 '05 #13

P: n/a
On Mon, 20 Sep 2004 13:12:39 +0100, Philip Ronan wrote:
On 20/9/04 12:44 pm, Fred Oz wrote:
Philip Ronan wrote:
It will also fail when t = infinity (e.g., "t=1/0")
... 1/0 is not infinity, is it "undefined"- as is the result
of any division by zero.
... javascript:alert(1/0) returns "Inf" in IE.


All three of IE 6.0026, Moz. 1.7 and Opera 6.54
give consistent results for me..

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

Makes perfect sense to me.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
Jul 23 '05 #14

P: n/a
Andrew Thompson wrote on 20 sep 2004 in comp.lang.javascript:
All three of IE 6.0026, Moz. 1.7 and Opera 6.54
give consistent results for me..

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

Makes perfect sense to me.


And which of them are integers ?
;-}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #15

P: n/a
On 20/9/04 2:00 pm, Fred Oz wrote:
Philip Ronan wrote:

Really?
javascript:alert(1/0) returns "Inf" in IE.


And now IE is an authority on mathematics? Heaven forbid!


OK then, how about the ECMA-262 specification:
Division of a non-zero finite value by a zero results in a signed
infinity.

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #16

P: n/a
On 20 Sep 2004 13:23:12 GMT, Evertjan. wrote:
javascript:alert(1/0) -> 'Infinity'
javascript:alert(0/0) -> 'NaN'
javascript:alert(-1/0) -> '-Infinity'

Makes perfect sense to me.
.... And which of them are integers ?


<please insert 'mathematician' now>
.....
</please insert 'mathematician' now>
Jul 23 '05 #17

P: n/a
On Mon, 20 Sep 2004 21:12:54 +1000, Fred Oz <oz****@iinet.net.auau> wrote:

[snip]
Testing strings using regular expressions to see if they have
decimal places (and how many), surplus characters, scientific
notation, etc. requires exhaustive testing [...]
Not at all:

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

ensures that the string contains an integer with no leading zeros.

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

allows for signed integers.

To check for basic real numbers, it can be modified to:

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

which allows integer or real number inputs. To force decimal places and to
limit their number again, only simple modification is necessary:

/^(0|[1-9])\d*\.\d+$/ // must be real
/^(0|[1-9])\d*\.\d\d$/ // must be real with two decimal places
/^(0|[1-9])\d*\.\d{1,5}$/ // must be real with at most five d.p.

Scientific notation:

/^(0|[1-9])\d*\.\d+e(-|+)?\d+$/i

Optional scientific notation:

/^(0|[1-9])\d*\.\d+(e(-|+)?\d+)?$/i

Optional scientific notation with positive and negative numbers:

/^(-|+)?(0|[1-9])\d*\.\d+(e(-|+)?\d+)?$/i

Mandatory canonical scientific notation:

/^(-|+)?[1-9]\.\d+e(-|+)?[1-9]\d*$/i

Hexadecimal:

/^0x[0-9a-f]+$/i

[Note: these are untested]

The problem with using parseInt and parseFloat is that parsing stops at an
unrecognised character. That doesn't really tell you if the value's format
is correct or not, just that part of it converts to a number. If you want
check an input format, regular expressions are the best approach.

As you can see from the above, it doesn't take much effort once you've
established some basic patterns. After that point, it's simply a matter of
adding the components in the appropriate places.
How about:

var x = someValue;
if (x == parseInt(x) && x == parseFloat(x)) {


Calling parseInt without a radix argument can be unpredictable. Don't do
it.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #18

P: n/a
JRS: In article <ec**************************@posting.google.com >,
dated Sun, 19 Sep 2004 19:10:21, seen in news:comp.lang.javascript, D.
Alvarado <la***********@gmail.com> posted :
Does anyone have handy functions for testing whether a text field value is

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


At least for the first case, the test might or might not need to
include/exclude a sign and/or zero itself.

Floating-point numbers are of the form +12.345E-6 ; you may instead
want to accept only fixed-point <digits> optional: <point><digits> .

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

Normally, one can limit the number of digits in an integer of fixed-
point number.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #19

P: n/a
JRS: In article <ci**********@news.datasync.com>, dated Mon, 20 Sep
2004 01:05:33, seen in news:comp.lang.javascript, Fox
<fo*@fxmahoney.com> posted :
D. Alvarado wrote:
Does anyone have handy functions for testing whether a text field value is

a) a valid integer


function
isInteger(s)
{
var n = trim(s);
return n.length > 0 && !(/[^0-9]/).test(n);


return /^\d+$/.test(n) // seems simpler
return /^[+-]?\d+$/.test(n) // seems better
}
b) a valid floating point number?
function
isFloat(s)
{
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);


return /^[+-]?\d+(\.\d+)?$/.test(n) // seems better
// but the function deals with fixed-, not floating-, point.
}
isFloat "qualifies" and differentiates from an integer by testing for a decimal
point and at least one digit after it. Therefore, 123. will not qualify as a
floating point number [nor an integer in isInteger] -- it is an invalid input.
If you require a digit *before* the decimal point for a float, you'll have to
adjust the regex.


That should be done, if circumstances permit; international
recommendations deprecate a decimal point that it not surrounded by
digits.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #20

P: n/a
Lee
Philip Ronan said:

On 20/9/04 12:44 pm, Fred Oz wrote:
Philip Ronan wrote:

It will also fail when t = infinity (e.g., "t=1/0")


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

Cheers, Fred.


Really?
javascript:alert(1/0) returns "Inf" in IE.


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.

Jul 23 '05 #21

P: n/a
On 20/9/04 7:07 pm, Lee wrote:
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?

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #22

P: n/a
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.
Jul 23 '05 #23

P: n/a
Fred Oz <oz****@iinet.net.auau> writes:
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:)'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #24

P: n/a
"Evertjan." <ex**************@interxnl.net> writes:
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:
---
var t = Math.pow(2,53);
if (t+1 == t) { alert("surprice"); }
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #25

P: n/a
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 ;-}")

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #26

P: n/a
"Evertjan." <ex**************@interxnl.net> writes:
I cannot resist bettering you on that, Lasse:

alert("surprise ;-}")


I'll learn it eventually. Maybe. :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #27

P: n/a
JRS: In article <Xn********************@194.109.133.29>, dated Mon, 20
Sep 2004 10:15:21, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
t = "1.2e+33"
alert(!/\./.test(+t) && +t==t)

Though technically incorrect [John?],
are such big/small numbers important to the OP ?


I think all such tests ate liable to fail with long numbers.
Long != big.
Also 125.1 fails, 125.0 passes.
IMHO, the method is basically unpredictable.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #28

P: n/a
JRS: In article <ec**************************@posting.google.com >,
dated Sun, 19 Sep 2004 19:10:21, seen in news:comp.lang.javascript, D.
Alvarado <la***********@gmail.com> posted :
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
Jul 23 '05 #29

P: n/a
On 20/9/04 9:29 pm, Fred Oz wrote:
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.


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?

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #30

P: n/a
In article <BD**************************@virgin.net>,
ph***********@virgin.net says...
On 20/9/04 9:29 pm, Fred Oz wrote:
Philip Ronan wrote:
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?
Jul 23 '05 #31

P: n/a
On Mon, 20 Sep 2004 22:39:22 +0200, Lasse Reichstein Nielsen wrote:
/L 'and 1/-0 == -Infinity, even though 0 == -0 ... just don't think about it:)'


What I was about to add in my 1st post was that
the 'simple'situations are obvious (to me), but
the others make my head hurt, so I add checks to
avoid them! ;-)

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
Jul 23 '05 #32

P: n/a
Fred Oz wrote:
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.


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/...r.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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #33

P: n/a
On Tue, 21 Sep 2004 13:34:05 GMT, Grant Wagner wrote:

(Browser tesing - versions)
Opera 6.54 (editor: probably
meant 7.54, ..


Correct, thank you. ( And I *was* doing so
well in being precise in that answer.. ;-)
Jul 23 '05 #34

P: n/a
JRS: In article <is**********@hotpop.com>, dated Mon, 20 Sep 2004
22:42:30, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lr*@hotpop.com> posted :
"Evertjan." <ex**************@interxnl.net> writes:
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:
---
var t = Math.pow(2,53);
if (t+1 == t) { alert("surprice"); }
---


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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
some Astro stuff via astro.htm, gravity0.htm; quotes.htm; pascal.htm; &c, &c.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 23 '05 #35

P: n/a
On Mon, 20 Sep 2004 14:05:10 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #36

This discussion thread is closed

Replies have been disabled for this discussion.