Programming beauty-2.6-precise expression of Floating Point

Source: Internet
Author: User

1. Brief Introduction

To put it simply, convert the finite-cycle decimal places and the infinite-loop decimal places into the fractional form. For example:
0.9 = 9/10
0.333 (3) = 1/3, with parentheses representing the cyclic section.

2. Ideas

The method in the book is case-based discussion.

First, it is a finite repeating decimal point, such as 123.456. The integer part 123 is needless to say. It is mainly the decimal part 0.456, which is directly converted to 456/1000, and then the least major factor is eliminated.

The second is infinite cyclic decimal places, such as 123.4 (56), and the integer part 123 does not need to be solved, mainly the circular decimal part 0.4 (56 ), first, split the fractional part into two parts: Non-cyclic and loop. Note that the non-cyclic part, such as before the cyclic part (this is obvious), is 0.4 + 0.0 (56 ), the non-cyclic part 0.4 is directly converted to 4/10, and the processing of 0.0 (56) needs to be reduced by the recursive formula: 0.0 (56) * 100 = 5.6 (56) = 5.6 + 0.0 (56), 0.0 (56) = 5.6/99 = 56/990. The process of deriving a circular section is to multiply the circular section by the number of digits of the previous circular section, that is, for (56), multiply by 100, for (456) Multiply by 1000, after multiply, the circular section on both sides of the equation can be decomposed by means of formulas, and a circular Section is proposed and calculated.

This is basically the case. For the solution of the most common factor, the next section, that is, section 2.7, will discuss this issue. In general, the division of the moving phase is enough.

3. Code

Omitted.

4. Reference

The beauty of programming, section 2.6, precise expression of floating point numbers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.