Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

identify real floating point number 1

Status
Not open for further replies.

PIRob

Technical User
Nov 9, 2007
3
GB
I have the following code to identify a decimal number, however if the number is 10. or 10.0 format ie no trailing numbers after decimal point or a single trailing zero my function fails can ay one help?

sub is_float {
#Warning this does not return is_float true when the number ends like:-
# 10. or 10.0 not sure why the decimal point and trailing zero are dropped!
# which then gives variable $_[0] an integer value!!!!

my ($val) = $_[0];

print "Number: $val \n";

return $val=~/\.{1}/;
}

$x = 10.0;


if (is_float($x)){
print "Is A floating point Number\n"}
else {
print "Is not a floating point Number\n"
}
 
if you put quotes around it, it works ... 10.0 ==10 as far as perl is concerned, but "10.0" is not the same as "10." or "10"

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Paul's answered your direct question.

The subroutine you're using isn't particularly effective though. It will return a true value for any string that contains a dot character, including:
Code:
print is_float( '1.2.3.4.5' ), "\n"; # prints 1
print is_float( 'john.smith@example.com' ), "\n"; # also prints 1
That presumably isn't the result you expect.
 
Hi Thanks for the input so far althouh not answered yet! a little more explanation may be required...
I am reading values from a file so do not have the opportunity to wrap with quotes, because each time I read a "value" into a variable with the format i am stuck with I loose the decimal point and/or trailing zero:{ also my check for decimal number has been cut down to simplfy the example therefore my "real" check accounts for a well structured decimal / floating point number.
 
When you assign to a variable a value that is a number, perl will transform it into a number and represent it in compact form (e.g. stripping away the decimal separator and any trailing zeroes after it). Can't see a solution to your problem at the moment, if you really need to assign a variable with each value read from the file. If you can read a full line first, and you split it, then the resulting list should be intact at that moment.
Must see examples of real data and the part of your code that does the reading and the check to say more.

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Code:
while (<FH>) {
  chomp;
  $_="\"".$_."\"";
  print is_float($_);
}

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Hi Thanks for your support in this I have now resolved my problem with your help and the following is my solution
Cheers
Phil.R

#content of test_file.dat
#ALPHA 3 4 5 6 7.0 8 9 4

open (FH, "<test_file.dat") or die "cant open file $!";

while (<FH>) {
chop($_[0]);
$i=0;
@elements=unpack("A8 A8 A8 A8 A8 A8 A8 A8 A8",$_);#get line of data in 8char fields
shift(@elements);#remove first element not required!!
foreach (@elements) {
$elements[$i]=~s/ //g;#remove spaces
$elements[$i]=~s/([0-9.])([+-][0-9])/\1E\2/g;#ensure number!
$i++; #increment count
}
}
print "@elements\n";

#Test for float among integer data
foreach (@elements){
if (is_float($_)){
print "is A floating point number\n";
}
else {
print "is NOT a floating point number\n";
}
}

#RESULTS

echo of numeric fields: 3 4 5 6 7.0 8 9 4
#Results of tests on data:
Number: 3 is NOT a floating point number
Number: 4 is NOT a floating point number
Number: 5 is NOT a floating point number
Number: 6 is NOT a floating point number
Number: 7.0 is A floating point number
Number: 8 is NOT a floating point number
Number: 9 is NOT a floating point number
Number: 4 is NOT a floating point number
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top