Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a special case in write_float, added tests #308

Merged
merged 1 commit into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion sandy/core/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,33 @@ def write_list(C1, C2, L1, L2, N2, B):


def write_float(x):
"""
Converts a floating-point number to a formatted string representation.

Parameters
----------
x (float): The floating-point number to be converted.

Returns
-------
str: The formatted string representation of the floating-point number.

Examples
--------
>>> assert sandy.write_float(2) == ' 2.00000000'
>>> assert sandy.write_float(2e1) == ' 20.0000000'
>>> assert sandy.write_float(2e2) == ' 200.000000'
>>> assert sandy.write_float(2e3) == ' 2000.00000'
>>> assert sandy.write_float(2e4) == ' 20000.0000'
>>> assert sandy.write_float(2e5) == ' 200000.000'
>>> assert sandy.write_float(2e6) == ' 2000000.00'
>>> assert sandy.write_float(2e7) == ' 20000000.0'
>>> assert sandy.write_float(2e8) == ' 200000000'
>>> assert sandy.write_float(0) == ' 0.00000000'
>>> assert sandy.write_float(2e-3) == ' 2.000000-3'
>>> assert sandy.write_float(2e-10) == ' 2.00000-10'
>>> assert sandy.write_float(1-1e-8) == ' 1.000000+0'
"""
if abs(x) >= 1e0 and abs(x) < 1e1:
y = f"{x:11.8f}"
elif abs(x) >= 1E1 and abs(x) < 1E2:
Expand All @@ -316,7 +343,7 @@ def write_float(x):
elif x == 0:
y = f"{x:11.8f}"
elif abs(x) < 1E0 and abs(x) >= 1e-9:
y = f"{x:13.6e}".replace("e-0", "-")
y = f"{x:13.6e}".replace("e-0", "-").replace("e+0", "+")
else:
y = f"{x:12.5e}".replace("e", "")
return y
Expand Down
Loading