Floating-Point to Fixed-Point Conversion in Python
Advertisement
This article describes Python scripts for converting between floating-point and fixed-point number representations using file operations.
Introduction
Floating-point and fixed-point are two common formats for storing numbers with decimal components. They differ in how the decimal point is represented.
- Fixed-point numbers have a predetermined number of digits after the decimal point.
- Floating-point numbers have a variable number of digits after the decimal point, offering more dynamic range.

The equations above illustrate the conversion between floating-point and fixed-point representations. The “Q” format plays a vital role in these conversions.
Q notation specifies the parameters of the binary fixed-point number format. For example, Q8.8 in a fixed-point format means that when the number is an unsigned integer, there are 8 bits for the integer part and 8 bits for the fractional part. In the Q2.8 format, 2 bits are allocated to the integer part and 8 bits to the fractional part for an unsigned fixed-point number. When dealing with signed integers, 1 bit is reserved for representing the sign.
Floating-Point to Fixed-Point Conversion
# Floating Point to Fixed point conversion
Q_format = 8
f = open("input.txt", "r") # Input file with floating point values
f1 = open("output.txt", "w") # Output file with fixed point values
i = 0
for line1 in f:
s1 = float(line1) * pow(2, Q_format)
s1 = round(s1)
f1.write(str(s1) + "\n")
print(line1, "\t", s1)
i += 1
f.close()
f1.close()
Explanation:
Q_format = 8: Sets the Q format, indicating the number of fractional bits.f = open("input.txt", "r"): Opens the input file (“input.txt”) in read mode ("r"). This file should contain floating-point numbers, one per line.f1 = open("output.txt", "w"): Opens the output file (“output.txt”) in write mode ("w"). The converted fixed-point values will be written to this file.- The
forloop iterates through each line (line1) in the input filef. s1 = float(line1) * pow(2, Q_format):float(line1): Converts the line (which is initially a string) to a floating-point number.pow(2, Q_format): Calculates 2 raised to the power ofQ_format(in this case, 28 = 256).- The multiplication effectively scales the floating-point number to its fixed-point representation. This step maps the floating-point value into the integer range that represents the fixed-point number.
s1 = round(s1): Rounds the result (s1) to the nearest integer. This is essential because fixed-point numbers are typically represented as integers.f1.write(str(s1) + "\n"):str(s1): Converts the rounded integers1back to a string."\n": Adds a newline character to ensure each fixed-point value is written on a new line in the output file.f1.write(...): Writes the string to the output filef1.
print(line1, "\t", s1): Prints the original floating-point value (line1) and the converted fixed-point value (s1) to the console, separated by a tab. This helps you monitor the conversion process.i += 1: Increments a counter (i) (though it’s not directly used in the logic).f.close(): Closes the input filef. It’s crucial to close files when you’re finished with them to release system resources.f1.close(): Closes the output filef1.
Fixed-Point to Floating-Point Conversion
# Fixed Point to Floating point conversion
Q_format1 = 8
fp = open("input_Fx.txt", "r") # Input file with fixed point values
fp1 = open("output_Fl.txt", "w") # Output file with floating point values
i = 0
for line in fp:
s2 = float(int(line) / pow(2, Q_format1))
fp1.write(str(s2) + "\n")
print(line, "\t", s2)
i += 1
fp.close()
fp1.close()
Explanation:
Q_format1 = 8: Defines the Q format, which is the number of fractional bits.fp = open("input_Fx.txt", "r"): Opens the file “input_Fx.txt” in read mode. This file contains fixed-point values (integers), one per line.fp1 = open("output_Fl.txt", "w"): Opens the file “output_Fl.txt” in write mode. The resulting floating-point values will be stored here.- The
forloop iterates through each line (line) of the input filefp. s2 = float(int(line) / pow(2, Q_format1)):int(line): Converts the line (a string) to an integer. This assumes that the fixed-point value is represented as an integer.pow(2, Q_format1): Calculates 2 raised to the power ofQ_format1(28 = 256).... / ...: Divides the integer representation of the fixed-point number by 2Q_format1. This effectively scales the integer back to its floating-point equivalent.float(...): Converts the result of the division to a floating-point number.
fp1.write(str(s2) + "\n"):str(s2): Converts the floating-point values2to a string."\n": Adds a newline character to ensure each value is on a separate line in the output file.fp1.write(...): Writes the string to the output filefp1.
print(line, "\t", s2): Prints the original fixed-point value (line) and the converted floating-point value (s2) to the console, separated by a tab for easy viewing.i += 1: Increments a counter (i). (Not used in the core logic).fp.close(): Closes the input filefp.fp1.close(): Closes the output filefp1.
Download Input Files
- Click Input text file >> to download.
- Click Input_Fx text file >> to download.
Advertisement
RF