Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Q: NaN again
by ikegami (Patriarch) on Oct 18, 2006 at 12:45 UTC

    NaN is a feature of the IEEE format Perl (and C and VB and ...) use to represent floating point numbers. It's supported by the CPU/FPU. It's impossible to remove support for it from Perl, since an XS module could return NaN and Perl wouldn't know how to handle it. ( Of course, it's possible to change Perl to handle NaN differently. But really, not the way to go. )

    If you don't want your script's users to specify NaN, check to make sure they didn't specify NaN.

Re: Q: NaN again
by davorg (Chancellor) on Oct 18, 2006 at 12:17 UTC

    Please explain what problem you're trying to solve. We can probably help you find an easier way to solve it.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Problem in filtering of user input.
      For example. User try to make bid at auction, and enter NaN in "price" field. All standart methods like: $price = sprintf("%.2f", $price); return me NaN But it not so bad. Really bad is:
      if ($price > $user_money) { return "You have not enough money". }

      Will fail for NaN. I found about 30 "nan holes" in my code after 5.8.4, and I suppose, I found not all of it.

      P.S. Sorry for my bad english.

        Then, as you say, your problem is with filtering user input. When you recieve input from a user, you need to check that it is a valid number and reject (or ignore) it if it isn't.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        P.S. Sorry for my bad english

        That's ok ... no need to apologise for that. But it would really help if you could provide a complete (and simple) perl script that demonstrates one of the problems you are having.

        Support for NaN's does not exist in perl for all architectures/platforms. It would help us to know which architecture and platform you're running.

        Cheers,
        Rob
Re: Q: NaN again
by swampyankee (Parson) on Oct 18, 2006 at 19:24 UTC

    Usually, NaN shows up because of errors such as 0/0, so disabling it seems rather pointless (and philosophically related to the discussion here).

    It would be nice if you told us why you're worried about NaN. In my experience NaN usually means somebody (usually the programmer) forgot to initialize a variable or forget to test for something akin to 0/0 or atan2(0,0). In other words, NaN is indicative of a problem that should be dealt with in application code, not papered over by turning off floating point exception handling.

    Now, if you really, really, really need to disable NaN, I'd suggest writing a module to overload the arithmetic operators, as was done to handle arbitrarily long floats in Math::BigFloat, which you'll have to build with appropriate compiler options so that you, the programmer, can trap and deal with NaN.

    Don't munge up Perl; write a custom arithmetic module which does NaN-less arithmetic.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: Q: NaN again
by ysth (Canon) on Oct 19, 2006 at 00:19 UTC
    Perl doesn't do very much that's special for NaNs. The main thing is not warning if you use a NaN string in numeric context. Pretty much all the hard work is done by strtod and sprintf/gcvt/etc. and there's not good way to turn that off.
Re: Q: NaN again
by kai (Initiate) on Oct 19, 2006 at 12:58 UTC
    Example of "NaN script":
    #!/usr/bin/perl use CGI; my $q = new CGI; my $summ = 100; my $peritem = 1; print "Content-type: text/html\n\n"; unless ($q->param('buyit')) { print "<HTML>You have $summ coins.<BR>All items cost $peritem coin +.<FORM>Enter number of items to buy<INPUT type=text name=items><input + type=SUBMIT name=buyit value=go></FORM>"; } else { my $num = int($q->param('items')); my $cost = sprintf("%.2f", $num * $peritem); print "<HTML>You try to buy $num items, for $cost coins.<BR>"; if ($cost > $summ) { print "You have not enough money<BR>"; } elsif ($cost < 0) { print "Invalid number<BR>"; } else { $summ-=$cost; print "You buy $num items for $cost coins. You have $summ coin +s left<BR>"; } }
    Results:
    ** 23 coins: You try to buy 23 items, for 23.00 coins. You buy 23 items for 23.00 coins. You have 77 coins left ** 102 coins: You try to buy 102 items, for 102.00 coins. You have not enough money ** -3 coins: You try to buy -3 items, for -3.00 coins. Invalid number ** nan coins: You try to buy nan items, for nan coins. You buy nan items for nan coins. You have nan coins left