in reply to Re: Floating Point Looping In Perl
in thread Floating Point Looping In Perl
Something like
The trick is selecting a delta that works. For instance if $i was 2E40 and $j was 1E40, selecting a delta of 1E-40 probably wouldn't work - it's going to depend on how many significant figures your platform's floating point numbers can represent (the example above would require 80 significant figures, if I'm remembering all this right: 40 in front of the decimal point and 40 after it). You should carefully check out your platform's floating point representation and precision before embarking on adventures in floating point computations.while ($i - $j < $delta) { ... }
This is why many financial calculations in which it was necessary to be precise about exact cents used to be (and probably are still) done in cents rather than in fractional dollars - you can precisely add and subtract integers as long as you have enough digits to represent the amounts you care about.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Floating Point Looping In Perl
by aquarium (Curate) on Aug 16, 2010 at 03:29 UTC |