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

Re: limit of for loop!!

by GrandFather (Saint)
on Jan 01, 2012 at 21:41 UTC ( [id://945810]=note: print w/replies, xml ) Need Help??


in reply to limit of for loop!!

What are you actually trying to do? What precision (how many digits) do you really need? Moritz provided a good solution (work with integer values then scale as the last step) - what is wrong with that solution?

As an exercise try writing 1/3 as a decimal fraction in full. Let me get you started:

0.33333333333333333333333333333

Have you figured out that you can't exactly represent 1/3 as a finite decimal fraction? In the same way you can't exactly represent 1/10 (base 10) as a finite binary fraction. This seems like a problem, but for almost all real applications for arithmetic it is not an issue. You seldom need more than a few digits of precision for an answer (although for some calculations you may need more precision for intermediate answers).

So the key questions are: what are you trying to do and how much precision do you really need? There are many possible ways to solve your problem, but a solution depends very much on what you are trying to achieve.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: limit of for loop!!
by sagar123 (Novice) on Jan 02, 2012 at 07:31 UTC
    HI,

    I need some value upto 3 decimal places and want to use that in calculations. But actually the value becomes 22.430000001 like that. in summary I want that value remains 22.43 just not "nice value" to be 'printed'. yes 22.43 and 22.43000001 are almost same. but I need to extract some data from million number of values (which are three decimal place) from text file. So I need exact decimal place or it will be ignored to be extracted.

    As suggested I will divide 22430 by 1000, that will be a good option

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found