Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Floating Point Looping In Perl

by pemungkah (Priest)
on Aug 13, 2010 at 22:43 UTC ( [id://855019]=note: print w/replies, xml ) Need Help??


in reply to Re: Floating Point Looping In Perl
in thread Floating Point Looping In Perl

The recommendation from my numerical analysis course was to use a "delta" value when comparing, depending on "how close" the values needed to be for you to decide they were equal.

Something like

while ($i - $j < $delta) { ... }
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.

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
    Most accounting and telecommunications charging tables that I've worked with were to the fourth decimal place. This wasn't in perl but same applied, we ended up using a math library which used BCD or similar, which produces much better results and rounding functions than floating point. But that's probably beside the point. You can also create an "artificial" loop variable that counts in integers instead of decimals..since you know the number of steps.
    the hardest line to type correctly is: stty erase ^H

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://855019]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-19 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found