开发者

Memory and irrationality and floating numbers

Some numbers can not be stored in "memory bits" as their binary representation would make them go endlessly.

In binary 11.111011101110... this is not the exact number. I can't remember exactly what it is right now.

But we can think of it "like an irrational number" in our base 10 that goes on infinitely after decimal place.

Some rational numbers face this problem when converted to base 2. So how do we...

1) represent them?

2) calculate them with computer programs?

开发者_如何学编程3) perform operations on them?

Edit: To remove Confusion and say thanks:

Ok I said in above post --> to view it or think it like a irrational or a number going long and long in decimal place. please read the word "like".

Now i have a number 0.2 this simple number cannot be represented in binary form. so how to perform Operations over it.

Other Questions are still unsolved .

well just to mention for my belief in people here:

Well go for "library" or approximately storage is not an answer for geeks of stackoverflow as their are certain applications where it is important to have the numbers as they are.


You should look into the floating point numbers standard, and also read "Everthing every programmer should know about floating point numbers"

There is specific issues regarding rounding of floating point numbers and their precision when dealing with them, as like you say, we cannot infinitely represent them in our current machines, so these things have been solved to create the best precision possible, however there is certain caveats one has to keep in mind when dealing with them.


All integer numbers are represented precisely. You are talking about fractional and irrational numbers, right?

Irrational numbers cant be represented by binary digits as like by decimal ones. So they can be represented only with specific precision. The problem with fractional numbers is that not every decimal number can be precisely represented as binary. for example 0.9. That is because of floating point number nature.

The easiest way to solve this problem is to use fixed point numbers - it is basic integer numbers with some numbers fixed to represent fractional part. This method usualy used in financial software/ Where every dollar is represented by 100 cents ) This method have one drawback - it can represent only limited precision of numbers.

Another solution is like for decimal periodic numbers. Represent them as two numbers dividend and divisor. so decimal periodic 0.(3) can be represented precisely as 1 and 3.


They're not irrational numbers, they are periodic numbers. 1/3 is a periodic number in decimal. 1/10 is a periodic number in binary. Both are rational (independent of the base). In fact, whether a number is rational or not never has anything to do with the base.

All rational numbers can be represented as a fraction where the numerator and the denominator are integers. Yes, including all periodic ones. Some system use fractions internally to provide more accuracy. I believe this is what Perl6 will be using instead of floating point numbers.

You can end up with very large numbers internally, and you will still have precision loss when dealing with irrational numbers. For example, you could only approximate pi, perhaps using

 314159265358979/100000000000000

or

3141592653589793/1000000000000000


There is no general way. For eaxmple, the number:

sqrt(2) * sqrt(2)

can be found with symbolic or numeric approaches.

Regarding rationals: one can represent them as a pair of (big) integers and perform arithmetic operations on them.


Numbers with a finite decimal representation that do not have one in binary are repeating rather than irrational. How you deal with them depends on what your needs are. If you are dealing with money you usually use a binary coded decimal or a scaled representation, which guarantee that additions and subtractions are exact up to a certain number of decimal places. In other fields you might use a symbolic representation so as to delay performing actual computations until needed. If you think about it sqrt(2) is a finite, exact representation as long as you do not actually evaluate it.


If you want to represent rational numbers you can do it in the same way that you do it on paper, store two numbers, the numerator and the denominator (of course you are best off finding a library to do this for you as there are a lot of nuances to get right).

The rationals that do not have finite base 2 representations are the rationals which do not have powers of 2 in the denominator, 2 being the only prime factor of the base. Likewise for other bases you need to look at the prime factors of the base, so for base 10, the product of powers of 2 and powers of 5 have finite representations.

Irrational numbers are different beasts all together and I don't think relevant to your question, the general technique here is either to approximate them to some finite precision e.g. M_PI or to represent them symbolically and either never calculate them or calculate them at the end to a finite precision.


We don't. We use their approximate values. There are algorithms that can compute them with any precision needed.

And by the way, not every number that is infinitely long is irrational. For example, the number 1/3 is infinitely long in base 10: 0.333333333... and in base 2: 0.0101010101..., but it is rational.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜