Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Is a value a decimal number or not?

Reply
Thread Tools

Is a value a decimal number or not?

 
 
Tore Aursand
Guest
Posts: n/a
 
      11-14-2003
Hi!

Not having written many tests myself, I started yesterday looking av some
of the Test modules. Boring stuff, but an excellent way of keeping your
code clean from those annoying bugs.

However. In one of the modules, there was a 'is_decimal' function, and it
failed some of my tests. Not surprisingly, though. But. Is there a way
to check if a number is a decimal number even when you declare the value
as a "plain number"?

Example:

my $nr = 5.00;

if ( is_decimal($nr) ) {
print "is_decimal\n";
}

sub is_decimal {
my $nr = shift;
return ( defined $nr && $nr =~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;
}

This always fails, of course, as Perl seems to evaluate _the number_ 5.00
as the number 5 as soon as the variable $nr gets set. Setting $nr to a
string - ie. '5.00' - works fine.

Is there a way round this "problem"? No big deal, really, but just out of
curiousity.


--
Tore Aursand <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Josef Möllers
Guest
Posts: n/a
 
      11-14-2003
Tore Aursand wrote:
>
> Hi!
>
> Not having written many tests myself, I started yesterday looking av some
> of the Test modules. Boring stuff, but an excellent way of keeping your
> code clean from those annoying bugs.
>
> However. In one of the modules, there was a 'is_decimal' function, andit
> failed some of my tests. Not surprisingly, though. But. Is there a way
> to check if a number is a decimal number even when you declare the value
> as a "plain number"?
>
> Example:
>
> my $nr = 5.00;
>
> if ( is_decimal($nr) ) {
> print "is_decimal\n";
> }
>
> sub is_decimal {
> my $nr = shift;
> return ( defined $nr && $nr =~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;
> }
>
> This always fails, of course, as Perl seems to evaluate _the number_ 5.00
> as the number 5 as soon as the variable $nr gets set. Setting $nr to a
> string - ie. '5.00' - works fine.
>
> Is there a way round this "problem"? No big deal, really, but just outof
> curiousity.


It's not really a "problem", but you give the solution yourself: you're
mixing apples with oranges.
You assign a number to a variable, but do a string test on it. Perl does
a lot of stringification for you (as I have learned only yesterday), but
when it sees "$nr = 5.00", it first parses the string "5.00" itself and
converts it to its internal representation of a number. As this number
has no decimals, stringification of it will return "5", as this is the
simplest representation of 5.00.


[X] I don't know Perl
... but then ... who does?
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      11-14-2003
Tore Aursand <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi!
>
> Not having written many tests myself, I started yesterday looking av some
> of the Test modules. Boring stuff, but an excellent way of keeping your
> code clean from those annoying bugs.
>
> However. In one of the modules, there was a 'is_decimal' function, and it
> failed some of my tests. Not surprisingly, though. But. Is there a way
> to check if a number is a decimal number even when you declare the value
> as a "plain number"?


What do you consider a decimal number?

Numbers as such aren't decimal or binary or anything, they're numbers.
Various systems can be used to represent numbers *as strings*, and that
is what properties like "hex", "binary" and "decimal" are talking about.
So the distinction is about strings, not the numbers they represent.

> Example:
>
> my $nr = 5.00;
>
> if ( is_decimal($nr) ) {
> print "is_decimal\n";
> }
>
> sub is_decimal {
> my $nr = shift;
> return ( defined $nr && $nr =~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;


Why the "?:" operator? It doesn't change the boolean result.

> }


It seems you require a period in a string for it to count as a "decimal
number". That is not the usual definition, though I know one participant
on clpm who would agree with you. In the usual definition a string is
a decimal number if it consists of an optional sign, one or more digits
and at most one period among the digits. I suppose that's what is_decimal()
checks, though I haven't looked.

> This always fails, of course, as Perl seems to evaluate _the number_ 5.00
> as the number 5 as soon as the variable $nr gets set. Setting $nr to a
> string - ie. '5.00' - works fine.
>
> Is there a way round this "problem"? No big deal, really, but just out of
> curiousity.


There is no problem. A number is a number, and the question whether it
is decimal makes no sense. You can write the same number in different
ways, and with these routines like is_decimal() give reasonable answers.

Anno
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      11-14-2003
On Fri, 14 Nov 2003 10:51:38 +0000, Anno Siegel wrote:
>> Is there a way to check if a number is a decimal number even when you
>> declare the value as a "plain number"?


> What do you consider a decimal number?


As <http://www.shodor.org/interactivate/dictionary/d.html> states:

"A fraction where the denominator is a power of ten and is therefore
expressed using a decimal point. For example: 0.37 is the decimal
equivalent of 37/100."

>> sub is_decimal {
>> my $nr = shift;
>> return ( defined $nr && $nr =~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;
>> }


> Why the "?:" operator? It doesn't change the boolean result.


Hmm. Don't know; I didn't write the code. Personally, I would've skipped
it;

sub is_decimal {
my $nr = shift;
return ( defined $nr && $nr =~ /.../ );
}

That's not the point, though.

> It seems you require a period in a string for it to count as a "decimal
> number". That is not the usual definition, though I know one
> participant on clpm who would agree with you.


Only _one_ who agrees with me!? Yes, I would like to think of a
decimal number as something express with a decimal point (period), as
mentioned in the dictionary definition.

> In the usual definition a string is a decimal number if it consists of
> an optional sign, one or more digits and at most one period among the
> digits.


Optional sign, optional digits, at most one period and one or more digits.

Anyway; This topic was really a digression. I just got curious if it was
possible to re-evaluate (or whatever) '$i = 5.00' and state that it _is_ a
decimal number (or whatever you prefer to call it).


--
Tore Aursand <(E-Mail Removed)>
 
Reply With Quote
 
Josef Möllers
Guest
Posts: n/a
 
      11-14-2003
Tore Aursand wrote:

> Only _one_ who agrees with me!? Yes, I would like to think of a
> decimal number as something express with a decimal point (period), as
> mentioned in the dictionary definition.


A "decimal number" is a representation of a number with a base 10 (in
contrast to hexadecimal, octal and binary numbers).

A number with a decimal point, however, is said to "have decimals", it
is a "number with decimals", i.e. there are digits after the decimal
point.

That's the difference, but then ... I'm not a native english speaker.

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-14-2003
Tore Aursand <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> On Fri, 14 Nov 2003 10:51:38 +0000, Anno Siegel wrote:
> >> Is there a way to check if a number is a decimal number even when you
> >> declare the value as a "plain number"?


> Anyway; This topic was really a digression. I just got curious if it was
> possible to re-evaluate (or whatever) '$i = 5.00' and state that it _is_ a
> decimal number (or whatever you prefer to call it).


No. "$x = 5.00" and "$x = 5" are indistinguishable after the fact.
That is *why* it makes little sense to exclude whole numbers from the
set of decimal numbers.

Anno
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      11-14-2003
Also sprach Anno Siegel:

> Tore Aursand <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> On Fri, 14 Nov 2003 10:51:38 +0000, Anno Siegel wrote:
>> >> Is there a way to check if a number is a decimal number even when you
>> >> declare the value as a "plain number"?

>
>> Anyway; This topic was really a digression. I just got curious if it was
>> possible to re-evaluate (or whatever) '$i = 5.00' and state that it _is_ a
>> decimal number (or whatever you prefer to call it).

>
> No. "$x = 5.00" and "$x = 5" are indistinguishable after the fact.
> That is *why* it makes little sense to exclude whole numbers from the
> set of decimal numbers.


In fact, no, they shouldn't be indistinguishable:

ethan@ethan:~$ perl -MDevel:eek
Dump(5);
Dump(5.0);
__END__
SV = IV(0x812b05 at 0x811e408
REFCNT = 1
FLAGS = (IOK,READONLY,pIOK)
IV = 5
SV = NV(0x812dfb0) at 0x8128ab0
REFCNT = 1
FLAGS = (NOK,READONLY,pNOK)
NV = 5

Stress is on "shouldn't". I am not sure whether there is a way to call
the various macros (SvNOK, SvIOK etc.) from pure-Perl. Maybe there's
some module around that exposes them. At least it isn't in the latest
Scalar::Util where they'd probably belong.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-14-2003
Tassilo v. Parseval <(E-Mail Removed)-aachen.de> wrote in comp.lang.perl.misc:
> Also sprach Anno Siegel:
>
> > Tore Aursand <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >> On Fri, 14 Nov 2003 10:51:38 +0000, Anno Siegel wrote:
> >> >> Is there a way to check if a number is a decimal number even when you
> >> >> declare the value as a "plain number"?

> >
> >> Anyway; This topic was really a digression. I just got curious if it was
> >> possible to re-evaluate (or whatever) '$i = 5.00' and state that it _is_ a
> >> decimal number (or whatever you prefer to call it).

> >
> > No. "$x = 5.00" and "$x = 5" are indistinguishable after the fact.
> > That is *why* it makes little sense to exclude whole numbers from the
> > set of decimal numbers.

>
> In fact, no, they shouldn't be indistinguishable:
>
> ethan@ethan:~$ perl -MDevel:eek


No fair, peeking!

> Dump(5);
> Dump(5.0);
> __END__
> SV = IV(0x812b05 at 0x811e408
> REFCNT = 1
> FLAGS = (IOK,READONLY,pIOK)
> IV = 5
> SV = NV(0x812dfb0) at 0x8128ab0
> REFCNT = 1
> FLAGS = (NOK,READONLY,pNOK)
> NV = 5
>
> Stress is on "shouldn't". I am not sure whether there is a way to call
> the various macros (SvNOK, SvIOK etc.) from pure-Perl. Maybe there's
> some module around that exposes them. At least it isn't in the latest
> Scalar::Util where they'd probably belong.


I don't think the difference between IOK and NOK is anywhere visible
in pure Perl. The problem is that, since Perl lacks a formal specification,
nobody knows what "pure Perl" is.

In any case, the difference is in the internal representation of the
numbers -- a subject I had hoped to keep out of this discussion of
"decimals". Conceptually, there is no difference between 1 and 1.0.

Anno

Anno
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-14-2003

http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote:
> In any case, the difference is in the internal representation of the
> numbers -- a subject I had hoped to keep out of this discussion of
> "decimals". Conceptually, there is no difference between 1 and 1.0.


There can be. There is no difference between 1-as-a-member-of-the-
set-of-reals and 1.0-as-a-member-of-the-set-of-reals. But 1-as-a-
member-of-the-set-of-integers is a quite different beast. For
instance, there is no such thing as the '1.0'th element of an array,
for that would imply that there was also a '1.1'th element, which is
clearly nonsense.

Sorry, as a mathematician I tend to be a little picky about such
things .

Ben

--
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John Stanley
Guest
Posts: n/a
 
      11-14-2003
In article <bp33fu$dka$(E-Mail Removed)>,
Ben Morrow <(E-Mail Removed)> wrote:
>(E-Mail Removed)-berlin.de (Anno Siegel) wrote:
>> "decimals". Conceptually, there is no difference between 1 and 1.0.

>
> ... For
>instance, there is no such thing as the '1.0'th element of an array,
>for that would imply that there was also a '1.1'th element, which is
>clearly nonsense.


Furthermore, there is a serious difference in the physical sciences
between 1 and 1.0, as the former says that the measurement was done
to only one significant figure and the latter has two. For the former,
the true value is somewhere between .5 and 1.49+, while for the latter,
the real value is between .95 and 1.049+. The difference is an order
of magnitude.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Value of type Decimal cannot be converted to 1 dimensional array of Decimal Neal Software 0 03-20-2011 03:08 AM
format a float number with specific number of decimal point Oscar Lok Ruby 1 11-19-2006 05:59 PM
Conversion from Hexadecimal number into Decimal number dharmdeep@gmail.com C Programming 14 11-16-2006 07:51 PM
convert decimal number in a hexadecimal number ? muss C Programming 13 03-27-2006 08:14 AM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments