CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Subtraction

 



jonny5509
Novice

Sep 13, 2010, 2:56 AM

Post #1 of 4 (1207 views)
Subtraction Can't Post

Hello



I'm completely baffled with this. It seems simple enough - all I am trying to do is subtract one number from another in a perl script, and its giving me wacky results.

$q = "2605.2397";

$w = "2605.2197";

$r = ($q - $w);



Even I can see that $r should be 0.02. Why does Perl give me 0.0199999999999818? Or have I gone mental?

I dont really want to truncate or round the numbers - I have a huge list of depth points in a file and am trying to see if it has a consistent sample rate.

Many thanks for any help!



Jon


BillKSmith
Veteran

Sep 13, 2010, 4:02 AM

Post #2 of 4 (1202 views)
Re: [jonny5509] Subtraction [In reply to] Can't Post

See the FAQ: perldoc -q Why am I getting long decimals
Good Luck,
Bill


jonny5509
Novice

Sep 13, 2010, 4:32 AM

Post #3 of 4 (1199 views)
Re: [BillKSmith] Subtraction [In reply to] Can't Post

Ah I see, thanks for that Bill.

Is that true for all programming languages then? It must cause problems - I have files with thousands of depth points (from oil well logging), and need to know that each depth point has a standard increment (eg each progressive depth point might be 0.1524m deeper than the previous one). If just one depth point in the file is 0.15240001m deeper, the file has an irregular sample rate and is then difficult to deal with. If every depth point increases by exactly 0.1524, then its regularly sampled.

If I was to printf to four decimal places, I wouldnt know that the above depth point of 0.15240001m was irregular, but if I dont printf, Perl wont give me the true depth increment. Surely this has been dealt with by now, it must appply to a million things!

Cheers



Jon


shawnhcorey
Enthusiast


Sep 13, 2010, 5:52 AM

Post #4 of 4 (1193 views)
Re: [jonny5509] Subtraction [In reply to] Can't Post

There's a whole field of mathematics devoted to this problem. It's called Numerical Analysis.

If you need precision to 8 places, then set up printf to so you can see 8 places. Also, what good is such precision if it's greater than your error in measurement?

You can also set up Perl for even more precision. See `perldoc bigrat` and `perldoc Math::BigRat`.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives