Handling floating point number input in Perl CGI scripts 
Author Message
 Handling floating point number input in Perl CGI scripts

Perl has direct support for integers such as the int() function, but
I couldn't find any such built-in function for floating point numbers..

In my cgi scripts, I have needed to do two functions:

1) untaint user input (accept only floating point numbers

$value = float($in{'key'});

if the string value entered is "1.234" then I get the proper value
back, but if an invalid string is entered then it returns an empty string.
The following function works for me.

sub float {

 /^\s*([\d\.]+)/;
 $1;

Quote:
}

It doesn't handle 1.5e+3 and the like, but I only expect small numbers anyway.

2) truncate-up a floating point number to an integer (3.0 -> 3, 3.3 -> 4)

  $i = &ceil(($stoprec - $startrec + 1) / $maxrecs);
  print "plot $i of $n\n";

sub ceil {

 local($ix) = int($x);
 return $ix if $ix == $x;
 if ($x < 0) { return $ix - 1}
 else { return $ix + 1 }

Quote:
}

The floor function to truncate-down is just int($value)

Has anybody used a similar or better function?
Otherwise, these examples are posted for all to use.

Jason Mathews
--
------------------------------------------------------------------------------
Jason Mathews, Code 633.2       |National Space Science Data Center

Greenbelt, MD 20771-0001  USA   | http://www.*-*-*.com/



Mon, 08 Dec 1997 03:00:00 GMT  
 Handling floating point number input in Perl CGI scripts

Quote:
(Jason Mathews) writes:
>Perl has direct support for integers such as the int() function, but
>I couldn't find any such built-in function for floating point numbers..

        Thats because perl represents ALL scalars as double-precision
        floating point values in the machines native format. (pg. 65)

Quote:
>2) truncate-up a floating point number to an integer (3.0 -> 3, 3.3 -> 4)

>sub ceil {

> local($ix) = int($x);
> return $ix if $ix == $x;
> if ($x < 0) { return $ix - 1}
> else { return $ix + 1 }
>}

        BZZZZZZZ! The ceiling of -3.01 is -3, not -4. You should
        always return $ix + 1 if $ix != $x.

Quote:
>Has anybody used a similar or better function?

        I don't do much math-intensive programming, so these
        conversions are foreign to me. Now that I'm *forced*
        to do CGI in C again, I'm having a hard time defining
        all my variables before I use them, and not switching
        between data types.

*******************************************************************************
Andrew Mueller                                    MultiMedia Madness Consulting
HyperMedia Programmer                                   Specializing in CGI and
Nationwide Life Insurance Co.                             Web Site development

http://www.nationwide.com/>             http://www.cis.ohio-state.edu/~amueller
*******************************************************************************



Tue, 09 Dec 1997 03:00:00 GMT  
 Handling floating point number input in Perl CGI scripts
comp.lang.perl is going away.  Note redirection to comp.lang.perl.misc

   (Jason Mathews) writes:
   >2) truncate-up a floating point number to an integer (3.0 -> 3, 3.3 -> 4)

   >Has anybody used a similar or better function?

Perl5 has access to the POSIX ceil and floor functions:

# perl
use POSIX qw(ceil);
print ceil(-3.01);
^D
-3

For more information on ceil and floor, see the FAQ section 4 question 13.

Steve
--
Stephen P Potter                                Pencom Systems Administration




Tue, 09 Dec 1997 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Handling floating point number input in Perl CGI scripts

2. Detecting floating point number in input

3. floating point numbers on C vs. Perl

4. rounding floating point numbers

5. Floating Points and Large Numbers...

6. unpacking IEEE 437 Floating point numbers

7. TOY: explore the representation of binary floating point numbers

8. floating point number format...

9. Q: getting the decimal portion of a floating point number

10. Floating Point Number With Time Function

11. comparing floating point numbers

12. floating point number precision?

 

 
Powered by phpBB® Forum Software