Floating Point Verification

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 00steve

    Floating Point Verification

    Hi,
    I am looking for tips on how to go about creating a function verifies
    floating point input from a text box. For the sake of simplicity I
    would like the function the take 3 parameters: the scale, precision
    and error msg given if the entered value exceeds the max length
    dictated by the scale and precision, or if non-numeric input is
    provided.

    As I have never done anything quite like this with javascript before,
    I was wondering if anyone had any tips on what functions etc. would
    come in handy.

    Thanks for any input!
  • RobG

    #2
    Re: Floating Point Verification

    00steve wrote:[color=blue]
    > Hi,
    > I am looking for tips on how to go about creating a function verifies
    > floating point input from a text box. For the sake of simplicity I
    > would like the function the take 3 parameters: the scale, precision
    > and error msg given if the entered value exceeds the max length
    > dictated by the scale and precision, or if non-numeric input is
    > provided.
    >
    > As I have never done anything quite like this with javascript before,
    > I was wondering if anyone had any tips on what functions etc. would
    > come in handy.
    >
    > Thanks for any input![/color]

    Search for a thread called "isInteger, isFloat functions?" about
    20-sep-04

    From a Mike Winter post:
    [color=blue]
    > /^(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[/color]
    and[color=blue]
    > 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][/color]

    Another, slightly modified method to just test for int or float. It
    depends on the converted number being the same as the input, so it is
    quite simple and flexible:
    [color=blue]
    >
    > var x = someValue;
    > if (x == parseInt(x,10) && 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.[/color]

    Have fun

    --
    Rob

    Comment

    • Michael Winter

      #3
      Re: Floating Point Verification

      On Sun, 12 Dec 2004 18:06:55 +1000, RobG <rgqld@iinet.ne t.auau> wrote:

      [snip]
      [color=blue]
      > Search for a thread called "isInteger, isFloat functions?"
      > about 20-sep-04
      >
      > From a Mike Winter post:[/color]

      I did make a rather late correction (35 days) to that post. Occurances of

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

      should be replaced with

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

      [snip]

      I didn't post that: Fred Oz did.
      [color=blue][color=green]
      > > var x = someValue;
      > > if (x == parseInt(x,10) && 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...');
      > > }[/color][/color]

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Dr John Stockton

        #4
        Re: Floating Point Verification

        JRS: In article <dfedf0ce.04121 10949.784abcd0@ posting.google. com>,
        dated Sat, 11 Dec 2004 09:49:52, seen in news:comp.lang. javascript,
        00steve <298sam@tay.ac. uk> posted :
        [color=blue]
        >I am looking for tips on how to go about creating a function verifies
        >floating point input from a text box. For the sake of simplicity I
        >would like the function the take 3 parameters: the scale, precision
        >and error msg given if the entered value exceeds the max length
        >dictated by the scale and precision, or if non-numeric input is
        >provided.[/color]

        Please proof-read your articles before posting them.

        Your question does not really make sense; presumably you are not a
        mathematician nor a physical scientist. It would be better to explain
        the true requirements.

        There is no relevant maximum length for a floating-point number; any
        number of leading zeroes are allowed in mantissa and exponent, without
        affecting the value.

        For a floating-point quantity, the first thing to do is to remove any
        leading or trailing characters that you wish to allow but that might
        affect the remaining code. Perhaps the only reasonable case would be
        where these characters give units, in which case they will need to be
        parsed and used. Leading/trailing spaces can be removed, but don't need
        to be.

        Let the text box be F.X0 ; then the best move seems to be to do
        x = +F.X0.value first. You will normally have a range of allowed
        values, given by a least L and a greatest G. The following should
        suffice :-
        L = 3 ; G = 9
        x = +F.X0.value
        OK = x >= L && x <= G
        which, if I am not mistaken, correctly handles NaNs and Infs.

        If you insist that the number is given in floating-point notation, then
        test for the presence of the letter E. You might also wish to test for
        the presence of a decimal point, and, if so, that it has a digit on each
        side in accordance with recommended technical practice.

        If you have other needs, please explain them better.

        <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>

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

        Comment

        Working...