Octal Fractions

Interview

Time limit1sMemory limit128 MB

Summary
Read octal fractions from 0 to 1 and print each one's exact decimal value, with trailing zeros stripped.
Level

Medium4 of 10

Topics
Math, String, Implementation, Simulation
Solved
No attempts yet

Problem

A fraction written in octal (base 8) can always be written exactly in decimal (base 10). For example, the octal number 0.75 equals 7/8+5/64=0.9531257/8 + 5/64 = 0.953125 in decimal. Every octal fraction with nn digits after the point can be written with at most 3n3n digits after the decimal point.

Write a program that converts octal fractions strictly between 0 and 1 into the equivalent decimal fractions.

Input

The input is a list of octal numbers to convert, one per line. Each number has the form 0.d1d2d3…dk0.d_1 d_2 d_3 \dots d_k, where every did_i is an octal digit (00 through 77). There is no limit on the number of digits kk. Input continues until end of file.

Output

For each input line, print one line of the form

X [8] = 0.Y [10]

where XX is the input octal number copied verbatim and 0.Y0.Y is its decimal value. On the decimal side, strip every trailing zero so the last digit is never 00.

Examples3

  1. Example 1

    Input
    0.75
    0.0001
    0.01234567
    
    Expected output
    0.75 [8] = 0.953125 [10]
    0.0001 [8] = 0.000244140625 [10]
    0.01234567 [8] = 0.020408093929290771484375 [10]
    
  2. Example 2

    Input
    0.1
    0.7
    0.4
    
    Expected output
    0.1 [8] = 0.125 [10]
    0.7 [8] = 0.875 [10]
    0.4 [8] = 0.5 [10]
    
  3. Example 3

    Input
    0.2
    0.5
    0.6
    0.3
    
    Expected output
    0.2 [8] = 0.25 [10]
    0.5 [8] = 0.625 [10]
    0.6 [8] = 0.75 [10]
    0.3 [8] = 0.375 [10]