| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
SoftFloatPy is a Python binding of Berkeley SoftFloat.
Berkeley SoftFloat is a software implementation of binary floating-point functionalities that conforms to the IEEE Standard for Floating-Point Arithmetic (IEEE 754 and the succeeding standards).
As of December 2025, the latest release is Berkeley SoftFloat 3e, and SoftFloatPy supports the following features in it.
The following features that are not in the IEEE 754 standard are excluded from SoftFloatPy support.
SoftFloatPy requires Python 3.11 or later. The build configuration provided in the SoftFloatPy repository assumes platforms with 64-bit integer arithmetic support.
The GitHub page is https://github.com/arithy/softfloatpy.
Links related to Berkeley SoftFloat:
You can install the release version by the following command.
$ python -m pip install softfloatpyYou can install the development version by the following commands.
$ git clone --recursive https://github.com/arithy/softfloatpy.git # NOTICE: `--recursive` option is required
$ cd softfloatpy # the repository root directory
$ make req
$ make clean
$ make dist
$ python -m pip install --no-index --find-links=./dist softfloatpyTo use SoftFloatPy in a Python script, import softfloatpy module. An example is shown below.
import softfloatpy as sfTo use the SoftFloatPy functions, you need to create objects of the classes shown below.
For the floating-point classes, there are two options to create its object.
f = sf.Float16.from_bytes(b'\x3c\x00') # The byte order is big-endian.f = sf.Float16.from_float(1.0)For the fixed-bit integer classes, there are two options to create its object.
i = sf.Int32.from_bytes(b'\x00\x00\x00\x01') # The byte order is big-endian.i = sf.Int32.from_int(1)Once creating the objects, you can use the functions described in the SoftFloat documentation.
a = sf.Float16.from_float(2.0)
b = sf.Float16.from_float(-3.0)
c = sf.f16_mul(a, b)In addition, you can use Python operators for arithmetic operations such as +, -, *, //, and %. For more details, see the SoftFloatPy API documentation.
a = sf.Float16.from_float(2.0)
b = sf.Float16.from_float(-3.0)
c = a * bYou can retrieve the value retained in an object as a Python built-in type.
For the floating-point classes, there are two built-in types to retrieve the value.
print(f.to_bytes())
# -> b'\x3c\x00' (The byte order is big-endian.)print(f.to_float())
# -> 1.0For the fixed-bit integer classes, there are two built-in types to retrieve the value.
print(i.to_bytes())
# -> b'\x00\x00\x00\x01' (The byte order is big-endian.)print(i.to_int())
# -> 1The other option is to create a string representation of the object value using str() or formatted strings.
print(f'value: {f}')
# -> value: 1.0You can set and get the default rounding mode using the functions below.
The rounding mode is a thread-local property.
You can set, get, and test the floating point exceptions using the functions below.
The floating-point exceptions are thread-local properties.
| Back | FazBrowse Home | New Git URL |