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

Hi,
I'm running the simple code above:

if ($col3=~'D'){ $result=-1*CALCUL_DIV($col1,$col2); } else {$result = CALCUL_DIV($col1,$col2);}
where CALCUL_DIV is a subroutine doing the operation: $col1/10^($col2) ( I put It in a subroutine 'cause the operation is very often done) the result is printed in a file, and here's my problem:

It occurs when $col3="D", when $col1="0.0" , CALCUL_DIV returns normally "0" but cause of the "D" , I have "-0" printed in the file, when edited with VI, (UNIX)

I edited the file with VIM on windows, and I don't have "-0", but the right "0" printed !
I must be missing something, but I don't see what.
Has anybody already noticed something like that ?

20031030 Edit by Corion: Added formatting

Replies are listed 'Best First'.
Re: calcul returns "-0"
by Abigail-II (Bishop) on Oct 30, 2003 at 10:07 UTC
    Your description is vague. You don't tell us the value of $col2. You don't give us CALCUL_DIV. You don't tell us how you print the results. You don't tell us which version of Perl, or which size integers/reals it's using.

    It's most likely a rounding issue, with CALCUL_DIV returning a very small value - which if it gets multiplied by -1 will be printed as "-0". But without more details, it's hard to pinpoint where it happens, and how to prevent it.

    Could you post a self contained program that prints "-0" where you expect "0"?

    Abigail