perl - sprintf : fixed point, big numbers and precision loss -
i need read numbers in database , write them text file using perl.
in table numbers, data format defined numeric (25,5)
(it reads 25 digits, including 5 decimals).
i format numbers in file sprintf "%.5f", $myvalue
force 5 decimals , noticed greats values, there precision loss numbers more 17 digits :
db = 123.12345 file = 123.12345 (ok) db = 12345678901234891.12345 file = 12345678901234892.00000 (seems rounded upper integer) db = 12345678901234567890.12345 file = 12345678901234567000.00000 (truncation ?)
what perl's greatest precision fixed decimal numbers?
i aware of concepts , limitations of floating point arithmetic in general, not perl monk , not know internals of perl don't know if normal (or if related @ floating point). not sure either if internal limitation of perl, or problem related sprintf
processing.
is there workaround or dedicated module problem?
some notable points :
- this additional feature of system uses perl, using tool not option
- the data being crunched financial need keep every cent , cannot cope +/- 10 000 units precision :^s
once again, finding solution right after asking so. putting solution here, future visitor :
replace
$myout = sprintf "%.5f", $myvalue;
by
use math::bigfloat; $myout = math::bigfloat->new($myvalue)->ffround( -5 )->bstr;
Comments
Post a Comment