[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master b0efc465: Library (type.c): gal_type_to_string
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master b0efc465: Library (type.c): gal_type_to_string uses 6 and 14 digits for floats |
Date: |
Sun, 30 Oct 2022 17:50:15 -0400 (EDT) |
branch: master
commit b0efc465a21a2b1b4a85f94484dccac396b3d543
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Library (type.c): gal_type_to_string uses 6 and 14 digits for floats
Until now, this function (which is used to print numbers as strings) was
using '%.6g' and '%.10g' for 32-bit and 64-bit floating point
numbers. However, the '%g' format will (in some situations) not print
statistically significant digits and generally, it is hard to predict if it
will print a fixed point or exponential notation.
With this commit, the printing formats for this function have been changed
to '%.6f' and '%.14f'. This will always print fixed point, and will print
the required number of digits after the decimal point. Making the result
more predictable and without any possible loss of precision.
---
lib/type.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/lib/type.c b/lib/type.c
index 8f1d5086..1ca0b49f 100644
--- a/lib/type.c
+++ b/lib/type.c
@@ -427,8 +427,13 @@ gal_type_to_string(void *ptr, uint8_t type, int
quote_if_str_has_space)
case GAL_TYPE_INT32: TO_STRING( int32_t, "%"PRId32 ); break;
case GAL_TYPE_UINT64: TO_STRING( uint64_t, "%"PRIu64 ); break;
case GAL_TYPE_INT64: TO_STRING( int64_t, "%"PRId64 ); break;
- case GAL_TYPE_FLOAT32: TO_STRING( float, "%.6g" ); break;
- case GAL_TYPE_FLOAT64: TO_STRING( double, "%.10g" ); break;
+
+ /* We aren't using '%g' for floating points because it can remove
+ statisically significant digits in some scenarios and its result is
+ generally not easily predictable (can be fixed-point or
+ exponential). The most conservative format is therefore '%f'. */
+ case GAL_TYPE_FLOAT32: TO_STRING( float, "%.6f" ); break;
+ case GAL_TYPE_FLOAT64: TO_STRING( double, "%.14f" ); break;
default:
error(EXIT_FAILURE, 0, "%s: type code %d not recognized",
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnuastro-commits] master b0efc465: Library (type.c): gal_type_to_string uses 6 and 14 digits for floats,
Mohammad Akhlaghi <=