Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtraction of Decimals in Perl

Status
Not open for further replies.

Awksome

Programmer
Jul 17, 2009
11
US
Hi All,

I have a simple perl script which subtracts two variables holding decimal numbers and the results is not as expected.

$a = 417747646.853143
$b = 417747646.853142

$c = $a-$b

expected result = $0.000001
actual result = -9.5367431640625e-07

any idea on why I am getting wrong results?
 
You are close (or beyond) the precision available for standard numbers (about 15 decimal figures), so you cannot expect any precision of result in subtraction.
Consider the module Math::BigFloat if you really need that result.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
If you don't like its appearance, try formatting it like this:

$c=sprintf('%15.6f', $a-$b)

print "c='$c'\n";

c=' 0.000001'

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top