Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: losing precision reading numbers from file

by pg (Canon)
on Nov 12, 2005 at 02:54 UTC ( #507917=note: print w/ replies, xml ) Need Help??


in reply to Re: losing precision reading numbers from file
in thread losing precision reading numbers from file

Not all floats are rationals, for example sqrt(2). If you force it to a rational, the process itself reduces precision.


Comment on Re^2: losing precision reading numbers from file
Re^3: losing precision reading numbers from file
by spiritway (Vicar) on Nov 12, 2005 at 05:36 UTC
    Not all floats are rationals, for example sqrt(2). If you force it to a rational, the process itself reduces precision.

    What you say is mathematically true, but computationally false. The decimal representation of sqrt(2) requires an infinite number of digits, hence of memory, which would be very expensive. Almost all "floats" in computers are already approximations, and often they are bad ones. It is a simple matter to represent a rational number as a pair of ordered integers. 1/3 would be simply 1,3. In floating point, you'd get something like 0.3333333333333333, which is close, but inexact. This will come back and bite you if you don't take extraordinary precautions like keeping track of a delta and comparing equality as being within the range of the delta. This is awkward and often unnecessary.

    The OP's interest appeared to be in keeping track of dates, which can of course be represented as integers, even if only the integer number of seconds (or milliseconds, or whatever) between two events. Simple integer calculations could be performed to obtain more useful values of days, hours, minutes, etc.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-12-28 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls