|
Posted by Ben Morrow on February 11, 2008, 9:15 pm
Please log in for more thread options
Quoth dkcombs@panix.com (David Combs):
>
> Am a bit confused about (1) the vocabulary being used
> here and (2) about just what this bug you say you created is.
>
> "Round down" (that was the term used earlier in the thread, yes?)
> I always thought that had to do relative to a .5 or nearby. Clearly,
> I'm wrong, but can someone explain it a bit more fully?
'Round' is a vague term. The only thing it definitely means is 'convert
a float into an int'; the details of the conversion are left
unspecified. Most people unfamiliar with computer numerics want (or
think they want) 'round to nearest, i+0.5 -> i+1 for all integers i', as
they were taught in school. The most useful general-purpose form is
'round to even', that is 1.4->1, 1.5->2, 2.5->2, as with this form
round(a) + round(b) == round(a + b), which is useful.
> Int for positives meant truncation, I thought.
>
> For negatives, hmmm, what you *want* for int(-3.999)?
In C and C-derived languages int means 'round to zero', or truncation,
so int(-3.99) == 3. If people want 'round (strictly) downwards', so
3.5->3 but -3.5->-4, that's POSIX::floor.
> Or would some people *reasonably* want one thing and others the other?
Depending on exactly what you're doing, you may want a different answer;
Perl lets you choose the answer you want.
> And now, for something completely different: Mod on negatives.
>
> I think I recall that some languages and Knuth did it one way,
> and other languages another.
Does $a % $b mean int( int($a) / int($b) ) or does it mean '$r such that
$r is between 0 (inclusive) and $b (exclusive) with $b * $i + $r == $a
for some integer $i'? Perl chooses the latter, so -7 % 2 == 1, not -1.
Ben
|
|
Posted by Jürgen Exner on February 11, 2008, 10:22 pm
Please log in for more thread options
>'Round' is a vague term. The only thing it definitely means is 'convert
>a float into an int';
Actually 'convert a float to a specific number of decimals' which may be
zero decimals, but could just as well be any other number of decimals.
jue
|
|
Posted by Jürgen Exner on January 14, 2008, 9:38 am
Please log in for more thread options >>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,
> There
> is a rogue carriage return (0xd) in the string
> Is there something I can do to deal with this
> situation?
Repair the corrupted file:
perl -p -i -e 'tr/\r//d' bad_file
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
Posted by Ilya Zakharevich on January 14, 2008, 3:23 pm
Please log in for more thread options >>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,
> There
> is a rogue carriage return (0xd) in the string
> Is there something I can do to deal with this
> situation?
Repair the corrupted file:
perl -p -i -e 'tr/\r//d' bad_file
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
Posted by Peter J. Holzer on January 15, 2008, 9:34 am
Please log in for more thread options >>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,
> There
> is a rogue carriage return (0xd) in the string
> Is there something I can do to deal with this
> situation?
Repair the corrupted file:
perl -p -i -e 'tr/\r//d' bad_file
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
| Similar Threads | Posted | | measure time (hopefully in milliseconds) | October 25, 2004, 12:39 am |
| Current Time with 5 digits of milliseconds | May 12, 2008, 3:39 am |
| every(seconds => 4) | February 29, 2008, 1:30 pm |
| FAQ 4.15: How can I take a string and turn it into epoch seconds? | December 25, 2004, 12:03 pm |
| FAQ 4.15 How can I take a string and turn it into epoch seconds? | February 10, 2005, 6:03 am |
| FAQ 4.15 How can I take a string and turn it into epoch seconds? | April 25, 2005, 11:03 pm |
| FAQ 4.15 How can I take a string and turn it into epoch seconds? | July 11, 2005, 4:03 am |
| FAQ 4.15 How can I take a string and turn it into epoch seconds? | August 14, 2005, 10:03 am |
| How to run a perl script after an interval of 10 seconds | October 27, 2005, 7:09 am |
| FAQ 4.15 How can I take a string and turn it into epoch seconds? | December 4, 2005, 11:03 pm |
|