in reply to Getting different results with $var++ and $var += 1

Could you post some code that people can actually run and get the same results? The code you post doesn't show where the values come from.
  • Comment on Re: Getting different results with $var++ and $var += 1

Replies are listed 'Best First'.
Re^2: Getting different results with $var++ and $var += 1
by splicer (Novice) on Dec 03, 2008 at 20:20 UTC
    Well, I'm skeptical that it would be a good idea for me to upload my Order and Tour object libraries, and I don't expect anyone to sift through all my code and debug it for me. What I'm looking for is a good direction in which to direct my diagnostic efforts. Under what conditions can I take a string "110.98" and not be able to use it in numeric context? I thought the answer was "under no conditions ever for any reason under any version of Perl" but clearly I'm wrong. That said, I'll chop out the code that spits out the values and isolate them and try to get a manageable test case.

      Under what conditions can I take a string "110.98" and not be able to use it in numeric context?

      None, unless it's not a string or not that string, as oshalla very eloquently puts. That's why I asked to see what the value really is. I'm still waiting for an answer.

      I didn't suggest in anyway you post an enormous program here.

      What I suggest is that you reduce your code to a minimal program such that it 1) runs, and 2) shows your problem.

      Under what conditions can I take a string "110.98" and not be able to use it in numeric context? I thought the answer was "under no conditions ever for any reason under any version of Perl" but clearly I'm wrong.
      Oh, no, you are right. If you have a string "110.98", you will be able to use it in numeric context. The problem however is that you may very well have some else than a string.

      But since we don't know where the thing you have is coming from, we don't know, do we? And I don't like the game of "I've problem. I only half describe it. Now guess what's going on". If I want to play situation puzzles, I'll go to rec.puzzles.

        It's cheating because I've already got the answer (in part because you identified the problem) but this had nothing at all to do with the values being generated in the other objects. That I used Math::BigInt in the module (and didn't need it except for in one place) was the problem. So having arrived at the problem, the test case is pretty easy:

        #!/usr/bin/perl use Math::BigInt ':constant'; $discount = 15; $price = "109.98"; print $discount * $price; exit();

        Results NaN unless the use statement is commented out. Then it results in 1649.7;

        I'm guessing the problem is that Math::BigInt is converting all numeric constants to Math::BigInt objects, without checking to see that the numeric constant is an integer or not. Since "109.98" is not an integer, Math::BigInt claims it isn't a number. I'm too ignorant to know whether that is by design or even whether that's really what's happening, but it is my guess.