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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1+1 Nearly Equals 2 1

Status
Not open for further replies.

Ffungo

Technical User
Dec 4, 2007
2
US
I'm having a weird problem, and I'm hoping that somebody can put me on the path to fixing it.

I have a number of scripts where I perform a subtraction operation and get a weird result. I get things like 6 - 0.2 = 5.8000000001 or 5.79999999999 instead of just good ol' vanilla 5.8. I'm not sure why it's happening, but I've been writing some really inelegant rounding routines to try to fix it. Any ideas?
 
There should be no problem with "6 - 0.2", post your code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I checked on my perl 5.8.4 and got the expected results.

Code:
#!/usr/bin/perl

$a = 6;
$b = 0.2;
$c = ($a - $b);
print $c;
printed 5.8.

This prodcuded the same result;

Code:
#!/usr/bin/perl

$c = ($ARGV[0] - $ARGV[1]);
print $c;

 
I searched around a bit on my own, and I found that I may be suffering from the way Perl (and other languages, apparently) represent floating point numbers in binary form.


I didn't post my code because it didn't happen every time - I'd have a loop, and most of them would work fine while one or two would barf. It looks like I may just have to code around this "feature".
 
Kudos on finding the solution to your own problem. I would have just posted the link to the same resource.

Unfortunately, I can't give you any advice on how to deal with this "feature". I'm fortunate in that the machines that I run do not elicit this behavior.

- Miller
 
This is a "feature" of every computer language. Just as some innocent-looking fractions have infinite decimal expansions (1/3 = 0.33333333..) the same is true in binary and, indeed, every number base, although precisely which fractions terminate and which don't varies with the chosen base.

For most business programming, quantities are ultimately required as integers or as currency and printf/sprintf is often adequate to present the desired results.

For scientific programming, there will be error tolerances in the input data and the algorithm will usually increase the magnitude of the error. It is possible (and desireable) to calculate the cumulative tolerance and the results are usually presented with only enough decimal digits to show the "accurate" data. If you are measuring lengths in metres with millimetre accuracy, for example, you would normally round to 3 dp as any following digits are meaningless artifacts of the representation, rather than valid data.

Again, printf/sprintf is usually all that is required, although sometimes you need to round rather than merely truncate. This is normally done by adding .5x10^n for some choice of n.

Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life was going to be spent in finding mistakes in my own programs.["]
--Maurice Wilkes, 1949
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top