Convert decimal fractions to binary by repeated multiplication, and stop at the width the brief asks for.
Hardware context: A hex register dump of a motor controller and a table of fractional gain settings. Convert decimal fractions to binary fraction bits by repeated multiplication, cut at a given width.
A sensor reports gain settings such as 0.3 and 0.7. The chip stores each gain in a few fraction bits. Someone has to write the bit patterns into the configuration table, and most of these fractions never end in binary.
This is an Implement problem with no logic to write. Convert each fraction by hand and write its fraction bits into the answer module. The first three constants are worked examples and are already correct.
Each pattern holds only the bits to the right of the binary point. The leftmost bit of the pattern has the weight one half. Where a fraction does not end, cut it after the last bit you are asked for. Do not round.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
question | 8 | input of each check |
Full specification
| Constant | What to write |
|---|---|
| FIVE_EIGHTHS | The first 8 fraction bits of 0.625 (0.101, then zeros). |
| THREE_QUARTERS | The first 6 fraction bits of 0.75 (0.11, then zeros). |
| QUARTERS | The value of the binary fraction 0.11, counted in quarters (2 + 1). |
| THREE_TENTHS | The first 8 fraction bits of 0.3, cut after the last bit. |
| SEVEN_TENTHS | The first 8 fraction bits of 0.7, cut after the last bit. |
| ONE_FIFTH | The first 8 fraction bits of 0.2, cut after the last bit. |
| SIX_TENTHS | The first 6 fraction bits of 0.6, cut after the last bit. |
| ONE_TENTH | The first 12 fraction bits of 0.1, cut after the last bit. |
| EXACT_BITS | How many fraction bits 0.40625 needs to be exact. |
| SIXTEENTHS | The value of the binary fraction 0.1011, counted in sixteenths. |
FIVE_EIGHTHS = 8'b10100000 // one half + one eighth, then zeros up to 8 bits
The seven patterns are binary. QUARTERS, EXACT_BITS and SIXTEENTHS are plain decimal numbers. Write each answer as one literal: the check refuses operators and function calls.
Keep the module name and the constant names.
Examples
Example 1
Worked Example: A Fraction That Ends- Input
the first 8 fraction bits of 0.625 (0.101, then zeros)
- Output
five_eighths = 'b10100000
0.625 times 2 is 1.25, so the first bit is 1. 0.25 times 2 is 0.5, so the next bit is 0. 0.5 times 2 is 1.0, so the third bit is 1 and nothing is left. The other five bits are 0.
Example 2
Worked Example: Six Bits- Input
the first 6 fraction bits of 0.75 (0.11, then zeros)
- Output
three_quarters = 'b110000
0.75 times 2 is 1.5, so the first bit is 1. 0.5 times 2 is 1.0, so the second bit is 1 and nothing is left. The pattern has six bits, so four zeros follow.
Example 3
Worked Example: Reading a Pattern Back- Input
the value of the binary fraction 0.11, counted in quarters (2 + 1)
- Output
quarters = 3
The pattern 0.11 has two fraction bits, so it counts quarters. The bits weigh 2 quarters and 1 quarter: 3 quarters, which is 0.75.
Constraints and Contract
- Keep the module name answer and the names of the constants.
- A pattern has exactly the number of bits its question asks for.
Hints
Hint 1
Multiply the fraction by 2. The whole part of the product is the next bit. Keep only the part after the point and repeat.
Hint 2
The first bit you find sits next to the binary point, so it is the leftmost bit of the pattern.
Hint 3
Watch for a fraction you have already seen: from there the bits repeat. Stop when you have the number of bits asked for, and leave the rest of the fraction out.
What the grader checks: The check reads ten constants and compares each with a value computed by exact integer arithmetic. Before that, a source rule refuses any answer that is not one literal.
10 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 steps and the final capstone