For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > integer fail ...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author integer fail ...
stratus

2005-07-27, 10:01 pm

$input_startx=223.0000;
$input_starty=221.0000;

$input_endx=2392.2000;
$input_endy=2484.2000;

$input_step=0.8;

$total=$input_endy-$input_starty;
$track =$total/$input_step;
print "...$track....\n"; ====================> 2829
$track=int $track;
print "...$track....\n"; ====================> 2828

Why ?????? 2829 changed to 2828 ???


Jürgen Exner

2005-07-28, 4:01 am

stratus wrote:
[...]
> $input_step=0.8;

[...]
> Why ?????? 2829 changed to 2828 ???


You ignored the first law of computer numerics: "Thou shalt not use floating
point for precision computations."
See "perldoc -q 9999"

jue


Sisyphus

2005-07-28, 4:01 am


"stratus" <gis86508@cissol1.cis.nctu.edu.tw> wrote in message
news:dc9jqa$2prt$1@news.cis.nctu.edu.tw...
> $input_startx=223.0000;
> $input_starty=221.0000;
>
> $input_endx=2392.2000;
> $input_endy=2484.2000;
>
> $input_step=0.8;
>
> $total=$input_endy-$input_starty;
> $track =$total/$input_step;
> print "...$track....\n"; ====================> 2829
> $track=int $track;
> print "...$track....\n"; ====================> 2828
>
> Why ?????? 2829 changed to 2828 ???
>
>


To (partly) see why:

use warnings;
use Devel::P;

$input_starty=221.0000;

$input_endy=2484.2000;

$input_step=0.8;

$total=$input_endy-$input_starty;
$track =$total/$input_step;

print "############\n";
Dump($track);
print "############\n";

print "...$track....\n";
$track=int $track;

print "############\n";
Dump($track);
print "############\n";

print "...$track....\n";

__END__

Which produces for me:

############
SV = NV(0x8b09bc) at 0x8c07c8
REFCNT = 1
FLAGS = (NOK,pNOK)
NV = 2829
############
....2829....
############
SV = PVNV(0x3f8704) at 0x8c07c8
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 2828
NV = 2829
PV = 0x89304c "2829"\0
CUR = 4
LEN = 35
############
....2828....

The first time $track was printed, the NOK flag was set - so it printed the
value contained in the NV slot.
The second time $track was printed, the IOK flag was set (because the int
function had been called) - so it printed the value contained in the IV
slot.

Of course the above doesn't demonstrate how the IV slot came to contain
2828. Does someone know of a way of providing such a demonstration ?

(We can surmise that the reason the IV slot contains 2828 is that the int()
function received a value something like 2828.99999.... .
But it would be so much more conclusive if there were a way to actually
*demonstrate* that that's what happened.)

Cheers,
Rob


Tad McClellan

2005-07-28, 9:09 am

stratus <gis86508@cissol1.cis.nctu.edu.tw> wrote:
> $input_startx=223.0000;
> $input_starty=221.0000;
>
> $input_endx=2392.2000;
> $input_endy=2484.2000;
>
> $input_step=0.8;
>
> $total=$input_endy-$input_starty;
> $track =$total/$input_step;
> print "...$track....\n"; ====================> 2829
> $track=int $track;
> print "...$track....\n"; ====================> 2828



change your print() statements to:

printf "...%30.20f....\n", $track;

....

> Why ?????? 2829 changed to 2828 ???


.... and you will see why. :-)



Then see the answer to your Frequently Asked Question:

Why am I getting long decimals (eg, 19.9499999999999) instead of the
numbers I should be getting (eg, 19.95)?


And you might also benefit from this FAQ:

Does Perl have a round() function? ...


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com