G1.1.2 A Fraction in Binary

EasyImplementIcarus · browser samples / server checksReview the topic

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

SignalWidthRole
question8input of each check
Full specification
ConstantWhat to write
FIVE_EIGHTHSThe first 8 fraction bits of 0.625 (0.101, then zeros).
THREE_QUARTERSThe first 6 fraction bits of 0.75 (0.11, then zeros).
QUARTERSThe value of the binary fraction 0.11, counted in quarters (2 + 1).
THREE_TENTHSThe first 8 fraction bits of 0.3, cut after the last bit.
SEVEN_TENTHSThe first 8 fraction bits of 0.7, cut after the last bit.
ONE_FIFTHThe first 8 fraction bits of 0.2, cut after the last bit.
SIX_TENTHSThe first 6 fraction bits of 0.6, cut after the last bit.
ONE_TENTHThe first 12 fraction bits of 0.1, cut after the last bit.
EXACT_BITSHow many fraction bits 0.40625 needs to be exact.
SIXTEENTHSThe value of the binary fraction 0.1011, counted in sixteenths.
How a pattern is written: 0.625 is 0.101 in binary
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

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Worked Example: A Fraction That Ends

Input
the first 8 fraction bits of 0.625 (0.101, then zeros)
Expected 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.

Run samples executes all 3 public checks. Check all tests runs 10 checks on the server.

Icarus · browser samples / server checks